- Write a function called
reversedList1(lst). It should create
a new list with the elements of the original list in reversed order. It should
iterate over list values.
>>> reversedList1(['a', 'b', 'c'])
['c', 'b', 'a']
- Write a function called
reversedList2(lst). It should create
a new list with the elements of the original list in reversed order. It should
iterate over list indices.
>>> reversedList2(['a', 'b', 'c'])
['c', 'b', 'a']
- Write a function called
purgeDuplicates(lst). It should
create a new list with duplicated elements of the original list purged.
>>> purgeDuplicates([1,2,3,4,1,3,5,7])
[1, 2, 3, 4, 5, 7]
- Write a function called
longWordsOnly(s, minLength).
It should create a list of words from the string s that contain
at least minLength characters.
>>> longWordsOnly('Only words of length five or longer are to be included.', 5)
['words', 'length', 'longer', 'included.']
- Write a function called
wordsFromFile(filename). It will
return a list of every uniquely appearing word in the file designated by
filename. afterBreak.txt
is the file used for the example below:
>>> wordsFromFile('afterBreak.txt')
['simple', 'text', 'is', 'are', 'file', 'demonstration', 'use', 'for', 'should', 'These', 'written', 'perfectly', 'This', 'couple', 'functions', 'read.', 'repeated', 'words', 'with', 'a', 'functions.', 'being', 'purposes.', 'of', 'work', 'well', 'It', 'this', 'Certain', 'repeatedly.']
- Write a function called
countsFromFile(filename). It will
return a dictionary mapping each word in filename to the number
of times it appears:
>>> countsFromFile('afterBreak.txt')
{'simple': 1, 'text': 2, 'is': 2, 'are': 1, 'file': 3, 'demonstration': 1, 'use': 1, 'for': 2, 'should': 1, 'These': 1, 'written': 1, 'perfectly': 1, 'This': 1, 'couple': 1, 'functions': 1, 'read.': 1, 'repeated': 1, 'words': 1, 'with': 2, 'a': 2, 'functions.': 1, 'being': 1, 'purposes.': 1, 'of': 1, 'work': 1, 'well': 1, 'It': 1, 'this': 1, 'Certain': 1, 'repeatedly.': 1}
- Write a function called
prefixes(words, filename, size) that
will output to a file, one per line, each prefix of each element of the list
words of length size. If the word is shorter than
size, the entire word will be included.
>>> prefixes(['This', 'is', 'a', 'test', 'of', 'the', 'function'], 'pre.txt', 3)
>>> open('pre.txt').read()
'Thi\nis\na\ntes\nof\nthe\nfun\n'
- Write a function called
toFileInverted(d, filename) that outputs the contents of a dictionary to the specified file. Each line will contain
a space-separated key-value pair, with the value first.
>>> toFileInverted({'Moe': 10000, 'Larry': 20000, 'Shemp': 30000}, 'stoogesPay.txt')
>>> open('stoogesPay.txt').read()
'10000 Moe\n20000 Larry\n30000 Shemp\n'
- Write a function called
series(error) that computes the sum
of the infinite series of 1/2n for all non-negative n (i.e., 1 + 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + ...).
>>> series(0.001)
1.9990234375
>>> series(0.00000001)
1.9999999925494194
- Write a function called
names(filename). It will receive
from user input a series of names. It will output to the specified file
each name with a count of the number of times the name appeared.
>>> names('stooges.txt')
Name:Moe
Name:Larry
Name:Joe
Name:Curly
Name:Joe
Name:Shemp
Name:Larry
Name:Joe
Name:
>>> open('stooges.txt').read()
'Larry 2\nShemp 1\nMoe 1\nJoe 3\nCurly 1\n'