List Rotation

March 27, 2020

[ I’ve been busy at work this week with virus stuff. I’m working at home, which is less productive than at the office. And we are making accommodations regarding the virus for our students, which requires a lot of urgent work. So, a quick little exercise today. ]

Given a length-n list like (a b c d e), the rotations of the list are the n lists (a b c d e), (b c d e a), (c d e a b), (d e a b c), and (e a b c d), in any order.

Your task is to write a program that takes a list and returns its rotations. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Homework

March 20, 2020

Today’s exercise is somebody’s homework:

Write a program that displays the digits from 1 to n then back down to 1; for instance, if n = 5, the program should display 123454321. You are permitted to use only a single for loop.

The questioner did not specify what should happen when n reaches 10, so we will specify 0 < n < 10.

Your task is to write the requested program; if you like, think of other ways to write that program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

CSV To HTML

March 17, 2020

Today’s exercise is another in my continuing escapades in “stealth programming” using awk. I frequently write programs that produce CSV files as output. Most of the time the output file is loaded into Excel by the user. Sometimes the CSV file must be printed as well as loaded into Excel, and I wrote a program to do that in a previous exercise. I recently had a request to produce the output in HTML format, so I wrote that program yesterday.

Your task is to write a that converts a CSV file to HTML output; use whatever conventions make sense to you. When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Perfect Shuffle

March 13, 2020

A perfect shuffle, also known as a faro shuffle, splits a deck of cards into equal halves (there must be an even number of cards), then perfectly interleaves them. Eventually a series of perfect shuffles returns a deck to its original order. For instance, with a deck of 8 cards named (1 2 3 4 5 6 7 8), the first shuffle rearranges the cards to (1 5 2 6 3 7 4 8), the second shuffle rearranges the cards to (1 3 5 7 2 4 6 8), and the third shuffle restores the original order (1 2 3 4 5 6 7 8).

Your task is to write a program that performs a perfect shuffle and use it to determine how many perfect shuffles are required to return an n-card deck to its original order; how many perfect shuffles are required for a standard 52-card deck? When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

2SUM

March 10, 2020

We’ve done this in a previous exercise, but it’s a common problem both as an interview question and in programming classes, and I’ve seen in it several times in the last week, so now is a good time to do it again:

Given a list of integers and a target integer, find all the pairs of integers in the list that sum to the target integer, or report that there are no such pairs.

Your task is to write a program to find pairs of integers that sum to a target; you should write three programs, with time complexities of O(n²), O(n log n), and O(n). When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

This problem has been going around the internet the last few days. I’ve seen it on several message boards, though I don’t know the original source:

Given an array of integers sorted in non-decreasing order, return an array of the squares of each number, also sorted in non-decreasing order. For instance, given (-4 -1 0 3 10), the desired output is (0 1 9 16 100).

Your task is to write a program to compute the sorted squares of a sorted array. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Two Easy Tasks

March 3, 2020

Many of the tasks I publish on this blog come from beginning-programmer forums. Here is a task taken from some sort of entrance examination:

Jason rings every multiple of 13 less than 500. He then crosses every multiple of 17 less than 500. How many numbers get both ringed and crossed?

The test is a multiple-choice examination with possible selections 10, 0, 1 and 4. The solution sheet shows the correct answer is 4. The questioner who posted this question was asking how to calculate the answer given on the solution sheet.

And here is another simple task:

Given positive integer n < 1018, find the sum of the integers from 1 to n, mod 109 + 7. Assume you are using a language that provides 64-bit arithmetic, so no intermediate results can be larger than 264.

Your task is to write a program to solve these two tasks. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2