Write a program sort.py that takes a filename on the command line. The program should sort the file's lines in alphabetical order and print the result to standard output. For example,
$ cat text one two three four five six seven eight $ py sort.py usage: sort.py <filename> $ py sort.py text five six one two seven eight three four $
Modify the 'grep' utility we wrote in the lecture so that it prints matching strings in color:
$ py grep.py horse war_and_peace the morning, carriages with six horses had been coming and going horse—that I’ll warrant,” said Shinshín, patting him on the out as a post horse, but still I must have a talk with you, Catiche, a ...
If a string has multiple matches in an output line, all of them should be colored:
shouts of thousands of voices. His horse and the horse of the hussar
Use the sty library that we learned about last week for producing colored terminal output.
Write a function has_dups(s) that takes a string
and returns True if the string has any duplicate letters. The string
may contain any Unicode characters such as 'λ
' or 'ř
'.
Write a program that reads text from standard input until it ends, and prints the number of unique words in the input.
Write a function same_values(l, m) that returns True if the lists l and m contain the same values, i.e. every element of l is in m and every element of m is in l. You may assume that all list elements are immutable.
Write a program that reads input as follows. The first line of standard input will contain an integer N. The next N lines will contain a list of words, one per line. The next line will contain an integer M. The next M lines will contain a second list of words, one per line. The program should print out all words that are contained in both lists. Write each word on a separate line. You may write the output words in any order.
Sample input:
3 donut cookie cake 4 cake bread jam donut
Possible output:
cake donut
Write a program that reads a string and print out the five most common characters in the string along with their occurrence counts, e.g.
e 23 t 20 o 18 n 15 i 14
Write the characters in decreasing order of frequency. The string may contain any Unicode characters.
Write a function combine(d, e) that takes two dictionaries. It should return a dictionary that maps x to z if d maps x to some y, and e maps y to z. For example, suppose that d is a dictionary that maps Czech words to English words, and e maps English words to Spanish words:
>>> d = { 'žába' : 'frog', 'kočka' : 'cat', 'kráva' : 'cow' } >>> e = { 'cow' : 'vaca', 'cat' : 'gato', 'dog' : 'perro' }
Then combine(d, e) will map Czech to Spanish:
>>> combine(d, e) {'kočka': 'gato', 'kráva': 'vaca'}
Notice that 'žába' is not a key in the resulting dictionary, since e doesn't map 'frog' to anything. Similarly, 'perro' is not a value in the resulting dictionary, since d doesn't map anything to 'dog'.
Write a function that takes a list of integers and returns True if there are any duplicate values in the list. Use sorting to accomplish this task. Do not modify the original list.
Write a function with the same behavior, using a set to accomplish the task.
Which implementation do you think will be faster on a large list in the best case? In the worst case?
As an experiment, generate a list of random 1,000,000 integers in the range from 1 to 1,000,000,000, then call both of the above functions on your list. How does the exection time compare?
Modify the experiment so that all the integers your generated list are unique. Now run both functions on your list. How does the execution time compare?
Write a program that creates two sets of 10,000 random numbers from 1 to 1,000,000, then computes and prints their intersection. How many values do you expect that the intersection will contain, approximately?
Write
a class Time
that represents a time of day with 1-second
resolution, e.g. 11:32:07.
Include an
initializer that takes three integers (hours, minutes,
seconds) and returns a Time
. Seconds should default to
0 if not provided.
The
'+' operator should add a number of seconds to a Time
,
yielding a new Time
object (wrapping past midnight if
necessary).
The '-' operator should subtract two Time
objects, yielding a (possibly negative) number of seconds.
A
Time
object's
string representation should look like this: "11:32:07".
Write a class Polynomial representing a polynomial of a single variable. The class should support these operations:
Polynomial(c) – make a Polynomial with the given coefficients. For example, Polynomial(3, 2, 1) represents the polnomial 3x2 + 2x + 1.
p.degree() - return the degree of this Polynomial
The '+' operator should add two Polynomials.
The '*' operator should multiply two Polynomials.
p.eval(x) – return the value of the Polynomial at the given value of x
A Polynomial should have a string representation such as "3x^2 + 2x + 1".
Write a function flatten
that takes a
list of lists and returns a list containing all the values from all
sublists:
>>> flatten([[5, 2], [6], [8, 3]]) [5, 2, 6, 8, 3]
Write a function deep_flatten
that
takes a list that may contain sublists nested to any level. The
function should return a list containing all the values from all the
sublists:
>>> deep_flatten([ [[5, 2], 6], [8, 3], [[[[10]]]] ]) [5, 2, 6, 8, 3, 10]