Following is a list of programming exercises of increasing difficulty. Select one or more exercises according to the available time to complete. Exercise 0 should be completed prior to commencement of other exercises. Each exercise typically takes on a form whereby it transforms a given input.
Any programming language or environment may be used, with the provision that such an environment is capable of completing Exercise 0.
Exercises specify that a result should be "printed." To print a result is to display it on the standard output stream of the program process. Further, exercises specify that some data is "input." This data is provided either as a command line argument, or as standard input, to the program process.
Produce an executable script or program, such that given a string as input, print the string "hello" followed by that given string.
Example:
> exercise0 Fred
hello Fred
> exercise0 Mary
hello Mary
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Print the largest palindrome made from the product of two 4-digit numbers.
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
Given an integer between 1 and 50 on the input (n), print the smallest positive number that is evenly divisible by all of the numbers from 1 to n.
Example:
> exercise2 7
420
> exercise2 15
360360
> exercise2 10
2520
Encode a URL such that special characters are replaced by a given string of characters. The encoding rules are as follows:
'+' -> "%2B"
' ' -> "%20"
'#' -> "%23"
'%' -> "%25"
All other characters remain unchanged.
Example:
> exercise3 http://google.com/
http://google.com/
> exercise3 http://google.com/?a b
http://google.com/?a%20b
> exercise3 http://google.com/?a b&x=#y
http://google.com/?a%20b&x=%23y
> exercise3 http://google.com/?a b&x=#y&%+f=g%
http://google.com/?a%20b&x=%23y&%25%2Bf=g%25
The sum of the squares of the first ten natural numbers is, 1² + 2² + ... + 10² = 385
The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)² = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Given an integer between 1 and 200 on the input (n), print the difference between the sum of the squares of the first n natural numbers and the square of the sum.
Example:
> exercise4 10
2640
> exercise4 100
25164150
> exercise4 120
52124380
Given 1000 consecutive arbitrary digits (a digit is [0-9]) as input, print the greatest product of any 5 consecutive digits of that input.
Example:
> exercise5 73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450
40824
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
Given an integer between 1 and 1000 as input (n), print the value of the first triangle number to have over n divisors.
Example:
> exercise6 5
28
> exercise6 12
120
> exercise6 16
300
The decimal number, 585 = 1001001001₂ (binary), is palindromic in both bases.
Given an integer between 50 and 5000000 as input (n), print the sum of all positive numbers, less than n, which are palindromic in base 10 and base 2.
Note that the palindromic number, in either base, may not include leading zeros.
Example:
> exercise7 100
157
> exercise7 1000
1772
> exercise7 100000
286602
> exercise7 2000000
6544940
Given an odd number (3 or greater) of integers as input, call the first integer the maximum possible weight (M) and the remaining pairs of integers a list of value and associated weight of some items.
Select any items (pair of value and weight) and print the sum of their values such that the following hold:
- the sum of the individual values of the items is equal to, or greater than, any other possible selection of items.
- the sum of the individual weights of the items does not exceed the maximum possible weight (M).
Example (M=100):
> exercise8 3 3 2 4 1 5 3
7
> exercise8 4 3 2 4 1 5 3
9
> exercise8 5 3 2 4 1 5 3
9
> exercise8 100 40 100 35 50 18 45 4 20 10 10 2 5
55
> exercise8 100 50 92 93 79 89 38 39 5 84 95 52 49 41 94 51 19 65 99 88 6 88 73 89 63 3 50 92 93 79 89 38 39 5 84 95 52 49 41 94 51
310
> exercise8 100 50 92 93 79 89 38 39 5 84 95 52 49 41 94 51 19 65 99 88 6 88 73 89 63 3 50 92 93 79 89 38 39 5 84 95 52 49 41
2
624C
73
Given a string representing a numeric value of money on the input, print the verbatim transcription of that amount in English words.
For example, the input "1.11" will result in a return value of "one dollar and eleven cents". Pluralisation should be grammatically correct. For example, "1.01" produces "one dollar and one cent".
Invalid characters should be ignored, meaning that every input string has an output string. The empty string produces "zero dollars and zero cents".
The names of large numbers are given: [ "thousand" , "million" , "billion" , "trillion" , "quadrillion" , "quintillion" , "sextillion" , "septillion" , "octillion" , "nonillion" , "decillion" , "undecillion" , "duodecillion" , "tredecillion" , "quattuordecillion" , "quindecillion" , "sexdecillion" , "septendecillion" , "octodecillion" , "novemdecillion" , "vigintillion" ]
Example:
> exercise9 0
zero dollars and zero cents
> exercise9 1
one dollar and zero cents
> exercise9 0.1
zero dollars and ten cents
> exercise9 1.
one dollar and zero cents
> exercise9 0.
zero dollars and zero cents
> exercise9 0.0
zero dollars and zero cents
> exercise9 .34
zero dollars and thirty-four cents
> exercise9 0.3456789
zero dollars and thirty-four cents
> exercise9 1.0
one dollar and zero cents
> exercise9 1.01
one dollar and one cent
> exercise9 a1a
one dollar and zero cents
> exercise9 a1a.a0.7b
one dollar and seven cents
> exercise9 100
one hundred dollars and zero cents
> exercise9 100.0
one hundred dollars and zero cents
> exercise9 100.00
one hundred dollars and zero cents
> exercise9 100.00000
one hundred dollars and zero cents
> exercise9 1000456.13
one million four hundred and fifty-six dollars and thirteen cents
> exercise9 1001456.13
one million one thousand four hundred and fifty-six dollars and thirteen cents
> exercise9 16000000456.13
sixteen billion four hundred and fifty-six dollars and thirteen cents
> exercise9 100.45
one hundred dollars and forty-five cents
> exercise9 100.07
one hundred dollars and seven cents
> exercise9 9abc9def9ghi.jkl9mno
nine hundred and ninety-nine dollars and ninety cents
> exercise9 12345.67
twelve thousand three hundred and forty-five dollars and sixty-seven cents
> exercise9 456789123456789012345678901234567890123456789012345678901234567890.12
four hundred and fifty-six vigintillion seven hundred and eighty-nine novemdecillion one hundred and twenty-three octodecillion four hundred and fifty-six septendecillion seven hundred and eighty-nine sexdecillion twelve quindecillion three hundred and forty-five quattuordecillion six hundred and seventy-eight tredecillion nine hundred and one duodecillion two hundred and thirty-four undecillion five hundred and sixty-seven decillion eight hundred and ninety nonillion one hundred and twenty-three octillion four hundred and fifty-six septillion seven hundred and eighty-nine sextillion twelve quintillion three hundred and forty-five quadrillion six hundred and seventy-eight trillion nine hundred and one billion two hundred and thirty-four million five hundred and sixty-seven thousand eight hundred and ninety dollars and twelve cents
Some of the exercises are inspired by Project Euler.