List and String exercises

Quick intro to splitting and joining:

Suppose you have a string of words, where each word is separated from its neighbors with a space.  You want to create a list of those words (without the spaces).  Then you want to take that list of words, capitalize each word, and then create a string with all of those capitalized words, separated by commas.  Here's how using the split() and join() string methods.  The dialog shows a sequence of commands and their results in IDLE:

>>> A = 'this is easier than you think'

>>> a_list = A.split()

>>> print a_list
['this', 'is', 'easier', 'than', 'you', 'think']

>>> for i in range(len(a_list)):
	a_list[i] = a_list[i].capitalize()

>>> print a_list
['This', 'Is', 'Easier', 'Than', 'You', 'Think']

>>> B = ','.join(a_list)

>>> print B
This,Is,Easier,Than,You,Think

Quick intro to detecting datatypes:

Sometimes you want to know what type of data a particular variable is holding.  For instance, does the variable A hold a string, or an integer, or a list?  There's a function called type() that returns that answer, but in a weird form.  Luckily you don't have what that weird thing is, all you need to do is test whether two weird things are the same.  For instance, to detect whether A contains an integer, test:   type(A) == type(0).  Here you're just testing whether the type of data in A is the same as the type of the integer 0. You could have compared it not only to 0, but to the type of any integer.  So:

if type(A)==type(12):
   return 'An integer'
if type(A)==type(45.298):
   return 'A floating point number'
if type(A)==type(''):
   return 'A string'
if type(A)==type([]):
   return 'A list'

Exercises:

1. Create the function Esrever(L) which will be given a list of strings, and will reverse each string (rearranging the characters backwards) and will return a list each of them. Example:
	Esrever(['abcd','derF'])  # returns ['dcba', 'Fred']

2. Create the function Av(L) which will be given a list, and will return the average (mean) of all the numbers in the list.  The list can be any length, and can contain numbers and other types of data as well.  If there are no numbers, it should return 0.  Examples:

	Av([3.5, 'Fred', 4.5, 1])  # returns 3.0
	Av(range(1,5))  # returns 2.5
	Av(["Nope, nothin' here"]) # returns 0

3. Create the function SmallestPos(L) which will be given a non-empty list of numbers and will return the position of the smallest number.  If there are duplicates of the smallest number, it will return the position of the first one.  Examples:

	SmallestPos([3, 1, -3, 10, 2, 45.04])  # returns 2, because -3 is the smallest number
	SmallestPos([12, 3.8, 13, 3.8])  # returns 1

4. Create the function SortNums(L) which will be given a non-empty list of numbers, and will return a list of those numbers in non-decreasing order.  You cannot use any built-in sorting function already in Python, though, perhaps, you might use SmallestPos(). BTW, there are several ways that you can remove an element from a list; you might want to do some research on this.  Example:

	SortNums([7, -3.4, 12, 7, 1.3])  # returns [-3.4, 1.3, 7, 7, 12]

Challenge problem:  Create the function NumInWords(n) which will be given a number in the range 0 <= n < 1000, and will return a string with the number stated in English words.  Try to make your code as short as you can.  Examples:

NumInWords(0)  # returns 'zero'
NumInWords(13)  # returns 'thirteen'
NumInWords(27)  # returns 'twenty-seven'
NumInWords(427)  # returns 'four hundred and twenty-seven'