Peaks

February 15, 2019

Given an array of integers, a peak is a subarray of minimal length in which the integer values increase and then decrease. For instance, in the array [5,5,4,5,4] there is a peak [4,5,4] and in the array [6,5,4,4,4,4,4,5,6,7,7,7,7,7,6] there is a peak [6,7,7,7,7,7,6]. Note that [4,5,6,7,7,7,7,7,6] shows a pattern of increasing and decreasing values, but it is not minimal because the first two items can be removed and the remaining subarray remains a peak. The array [5,5,5,5,4,5,4,5,6,7,8,8,8,8,8,9,9,8] has two peaks, [4,5,4] and [8,9,9,8].

Your task is to write a program that finds all of the peaks in an 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.

Advertisements

Pages: 1 2

Consecutive Sums

February 12, 2019

Given the positive integer 15, there are three ways to take consecutive positive integers that sum to 15: 1+2+3+4+5, 4+5+6 and 7+8.

Your task is to write a program that, given a positive integer, finds all the ways that consecutive positive integers can sum to the target integer. 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

String Data Type

February 5, 2019

Different programming languages handle strings differently. In C, for instance, strings are null-terminated arrays of characters, so the \0 character is not permitted in strings; other langauges permit any character in a string. Some languages are limited to ascii, others admit unicode. Some languages allow strings to be mutated, while others make strings immutable. Some languages number the characters in a string starting from 0, others from 1. If you’re porting a program from one language to another, dealing with strings can be a headache. (You can probably guess accurately at the motivation for today’s exercise.)

Today’s exercise calls for you to write a portable string data type that can be easily moved language to another. You are free to define strings as you wish — not everyone will have the same needs. Whatever you decide, you should provide conversions to and from the strings in your native language, as well as some basic operations on strings: determine their length, find the character at a particular position, modify a character if you decide strings should be mutable, extract a substring, concatenate two strings, perhaps others. Make your data type as simple or as elaborate as you wish.

Your task is to write a portable string data type that can be easily moved from one language to another. 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

Alternating Lists

February 1, 2019

Today’s exercise is a simple task in list manipulation:

Write a program that takes one or more lists and returns a single list containing the elements of the input lists, taken alternately from the lists. If the lists are of different lengths, continue taking items from the lists that remain.

For instance, given the inputs (1 2 3 4 5), (a b c) and (w x y z), the desired output is (1 a w 2 b x 3 c y 4 z 5).

Your task is to write a program to take items alternately from multiple lists. 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