\chapter*{Extra Extension Exercises from Part 1}
\addcontentsline{toc}{chapter}{Extra Extension Exercises from Part 1}

These are the extra extension exercises from the first part of the
paper, as posted on Blackboard. Some of these are quite challenging
and others are reasonably straightforward. Either way, you will gain
many skills trying to nut out how to solve these problems.

\section*{Lab 3}
\begin{enumerate}
\item Convert a date read from the user, given in DD/MM/YYYY format
  into written format. For example, here's a run of how the program
  might work: 

\begin{pythonOutput}
Enter a date in DD/MM/YY format: 16/7/2003
Output: 16th July, 2003
\end{pythonOutput}

There are two problems to solve here: converting a numeric month into
the name for the month, and getting the correct suffix to the day (st,
nd, rd, th). Both problems can be solved with judicious use of lists.  

\item A palindrome is a word that looks the same forwards and
  backwards. E.G. "mum" is a palindrome, but "mummy" is not. Write a
  program that gets input from the user and prints "1" if the input is
  a palindrome and "0" if it isn't. Hint: You can solve this problem
  using strings and lists and their respective methods.
\end{enumerate}

\section*{Lab 4}
\begin{enumerate}
\item Write a function that prints the 4th largest number in a list of numbers
For example:
\begin{pythonInteractive}
>>> fourth_largest([1,2,3,4,5,6])
4
>>> fourth_largest([6,5,4,3,2,1])
4 
\end{pythonInteractive}

\item Write a function that prints out the absolute values for all
  elements in a list of numbers. E.g.:
\begin{pythonInteractive}
>>> abs_list([-1,2,-3,4])
[1,2,3,4]
\end{pythonInteractive}
Hint: check out the builtin function map.

\item Write a function that prints out a list with a number x being added to each element.
 E.g.:
\begin{pythonInteractive}
>>> add_list([1,2,3,4], 5)
[6,7,8,9]
\end{pythonInteractive}
Hint 1: again check out the builtin function map. Hint 2: check out
dir(int) and dir(float) to see if there are any functions to help. 

\item In question 6, lab 4, you were asked to write a function info
  that worked on a list of numbers. Extend that solution so that you
  read in the list of numbers from the user. There should be no fixed
  limit to the size of the list. E.g.: 
\begin{pythonOutput}
Enter your list: 1 2 3 4 5 6 7 8 9 10 11
Min: 1, Max: 11, Sum: 66, Average: 6
\end{pythonOutput}
Hint: you'll need to convert all elements of a list to numbers.
\end{enumerate}

\section*{Lab 5 and 6}
\begin{enumerate}
\item Write a function that takes 3 parameters and prints them out in
  order. For example:
\begin{pythonInteractive}
>>> print_in_order(5, 6, 4)
In order: 4, 5, 6
\end{pythonInteractive}

\item Write a function that takes 3 parameters and prints the middle one. For example:
\begin{pythonInteractive}
>>> print_middle(5, 6, 4)
The middle value is 5
\end{pythonInteractive}

\item This really is quite an advanced question. Type in and call the
  following function. Then try to come up with a sensible description
  of what is going on. 

\begin{minipage}{\textwidth}
\begin{python}
def adams():
   answer = 42
   guess = input("What is the answer to the ultimate question? ")
   if guess < answer:
       print "No silly, that's too small."
       adams()
   elif guess > answer:
       print "You dope, that's too big."
       adams()
   else:
       print "Well done. Wonder what the question is?"
\end{python}
\end{minipage}
\end{enumerate}

\section*{Lab 7 and 8}
\begin{enumerate}
\item 
Given a list of numbers, find all numbers in the list divisible by a given number.
For example:
\begin{pythonInteractive}
>>> is_divisible([2, 3, 4], 2)
2 4
\end{pythonInteractive}

\item Given two lists of numbers, find all numbers in the first list
  divisible by all numbers in the second list. For example:
\begin{pythonInteractive}
>>> is_divisible([2,3,4,5,6], [2,3])
6
\end{pythonInteractive}

\item Write a function that will sort a list of numbers with the odd
  numbers coming first and the even numbers coming second. You can use
  the list.sort function, but you will have to provide it with a
  compare function. For example:
\begin{pythonInteractive}
>>> sort_odd_even([1,2,3,4,5,6,7,8])
1 3 5 7 2 4 6 8
\end{pythonInteractive}
\end{enumerate}

\section*{Lab 10}
\begin{enumerate}
\item Write a function that finds all the prime numbers less than a
  given number. E.g.: 
\begin{pythonInteractive}
>>> prime_numbers(10)
2 3 5 7
\end{pythonInteractive}

\item Write a spell-checking program that reads in a text file, scans
  through every word in the file and checks that it is spelled
  correctly. To do this you will probably also want to read in the
  system dictionary: \pythoninline{/usr/share/dict/words}.
\end{enumerate}

\section*{Lab 11}
\begin{enumerate}
\item You have seen Newton's algorithm for calculating the square
  root. Modify that algorithm so that it calculates the cube root. You
  might like to make use of the following identity: given that
  \pythoninline{c} is
  the cube root of \pythoninline{n}, we have: \pythoninline{n/(c**2) =
    c}.

\item Generalise the cube root algorithm to calculate the n-th root
  where n is any positive integer. 

\item Write a function to add together the digits of a number (this is
  simpler than the extension exercise printed in the book where you
  add the squared digits), so: 
\begin{pythonInteractive}
>>> add_digits(123456789)
45
\end{pythonInteractive}
Use this function to reduce a number to a single digit -- keep adding
the digits until the result is a single digit 
\begin{pythonInteractive}
>>> reduce_number_to_single_digit(123456789)
9
>>> reduce_number_to_single_digit(784)
1
\end{pythonInteractive}
\end{enumerate}
