## Decreasing-Increasing Array

### May 29, 2020

Today’s task is somebody’s homework:

Given an array of integers, determine if it is in decreasing-increasing order, with some initial segment of the array in decreasing order and the remainder of the array in increasing order. If the array is in decreasing-increasing order, return the pivot element (the minimum element in the array); otherwise, return an indication the array is not in decreasing-increasing order. Array elements may be duplicated. For example, the array (10 10 10 8 8 6 4 4 3 12 13 22 31 40 59 68) is in decreasing-increasing order, with pivot element 3, but the array (1 2 4 8 12 32 64) is not in decreasing-increasing order.

The student who asked the question suggests a solution using binary search that takes O(log *n*) time, but can’t get it to work.

Your task is to write a program to determine if an array is in decreasing-increasing order as described above. 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.

## Prime Power Triples

### May 22, 2020

Today’s exercise is Project Euler Problem 87:

The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way:

28 = 2^{2}+ 2^{3}+ 2^{4}33 = 3^{2}+ 2^{3}+ 2^{4}49 = 5^{2}+ 2^{3}+ 2^{4}47 = 2^{2}+ 3^{3}+ 2^{4}How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power?

Your task is to solve Project Euler 87; in the spirit of Project Euler, show only your code but not the solution. 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.

## MinStack

### May 19, 2020

[ My apologies that this exercise is so long delayed. We have been rewriting all of our business practices in the light of the pandemic, and work has been madness. The biggest projects are now complete, and we have retreated to merely super-busy, so maybe I will have time to write some more exercises. I hope everyone is well; I am. ]

Today’s task is to implement a minstack data structure that provides the normal stack operations `push`

, `pop`

and `top`

and also the operation `least`

which returns the smallest item in the stack without altering the stack in any way. All four operations must operate in O(1) time and O(*n*) space, where *n* is the size of the stack.

Your task is to implement a minstack as described above. 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.

## Nth Item In A Linked List

### April 10, 2020

[ I continue working at home. It’s no fun, and less productive than working in my office, but I’m coping. There remains much emergency work to be done related to moving all of our students online. I hope everyone is doing well during this time. ]

A student recently asked for help on a beginning-programmer forum to write a Scheme program that finds the nth item in a linked list, mimicking the `list-ref`

built-in function. He posted his code, which was awful; I won’t repost it here. Instead of engaging him, I sent a private email suggesting that he consult either his professor or his teaching assistant, as his posted code showed several misconceptions about Scheme. He wrote back, saying he was sure there was only one thing wrong with his code and I could easily point it out. I didn’t respond, as there was far more than one thing wrong with his code.

Your task is to write a program to find the nth item in a linked list; your program must be recursive, as was required by the original assignment. 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.

## Homework

### April 3, 2020

[ I continue working from home. It is interesting that my commute has gone from a daily 75-minute round-trip by car to a 75-foot round-trip by foot, so I think I should have more energy in the evening, but the opposite is true; I am exhausted by the end of the day, just from sitting in front of my laptop. ]

Today’s exercise is somebody’s homework:

Given positive integers C and N, find N numbers that sum up to C and the difference between the highest and the lowest of these number should not be more than one. For example: with C = 26 and N = 7, the desired output is [4 4 4 4 4 3 3].

Your task is to write a program to solve the student’s homework. 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.

## 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.

## Homework

### March 20, 2020

Today’s exercise is somebody’s homework:

Write a program that displays the digits from 1 to

nthen back down to 1; for instance, ifn= 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.

## 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.

## 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.

## 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.