#! /usr/bin/python ## Note: load into python with ## import f0830 ## or, to avoid module prefix, ## from f0830 import * # This is a comment # define a list comprising 10 digits digits = range(10) # define a function, num_to_word, which takes a single digit, num, # and returns the corresponding English word # ---------------------------------------------------------------- def num_to_word(num): if num > 9: return False numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"] return numbers[num] # define a function, word_to_num, which is the inverse of num-to-word # Note that we define a sub-function within the function itself. # The sub-function, find-index, is recursive. # ------------------------------------------------------------------- def word_to_num(word): numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"] def find_index(lst,n, word): if not lst: return False if lst[0] == word: return n return find_index(lst[1:], n+1, word) return find_index(numbers,0,word) # test out the functions x = map(num_to_word,digits) xlist = list(x) y = xlist[:] y.sort() z = map(word_to_num,y) zlist = list(z) print ("end of f0830") print ('name: ' + __name__)