[8 pts]
Suppose the file pops.txt
contains a series of lines,
each containing three tab-separated values:
a city name, a state name, the city's current population, and the
city's population three years ago. Here are five example lines from
the file.
Chicago Illinois 9899902 9840929
Los Angeles California 18238998 17877006
New York New York 23362099 23076664
San Francisco California 8370967 8153696
Washington DC 9331587 9051961
Complete the below program to display the name of each city with a current population exceeding 5,000,000. (The order in which the names appear does not matter.)
infile = open('pops.txt')
for line in infile:
[12 pts]
Without using loops or any built-in functions or methods except
len
, write a recursive function named multiply_list
that
takes two parameters, a number and a list of numbers, and returns a list
of everything in the parameter list multiplied by the integer.
For example, if nums
is [2, 3, 5, 7, 11],
then multiply_list(7, nums)
should return the list
[14, 21, 35, 49, 77].
infile = open('pops.txt')
for line in infile:
cols = line.rstrip().split('\t')
if int(cols[2]) >= 5000000:
print(cols[0])
def multiply_list(mult, nums):
if len(nums) == 1:
return [mult * nums[0]]
else:
first = mult * nums[0]
rest = multiply_list(mult, nums[1:])
return [first] + rest