Solutions will be posted under Other Content on Blackboard as we get closer to the exam.
Given the string s = 'Columbus'
, evaluate the following:
s[0]
s[0:1]
s[1:]
s[-1]
s[3: :-1]
s[2: :2]
Given the list myList = [3, 2, -1, [4, 7], 5]
, evaluate the following:
myList[0]
myList[0:1]
myList[-1]
myList[3]
myList[:2]
myList[2: :2]
Evaluate the following:
'a' in 'backache'
[1, 2, 3] + [[11, 13, 12][1]] + [22, 33, 44, 55][1:]
[3 for x in range(6)]
[2*y + 1 for y in [1, 3, 5, 7]]
[x for x in range(3, 10) if x % 2 == 0]
[len(w) for w in ['Go', 'Terriers']]
[chr(ord(c) + 1) for c in 'hello']
Write a function count_ones(s)
that takes in a string s
of
'0'
s and '1'
s and that uses recursion to count and return
the number of '1'
s in the input.
Write a function swap_bits(s)
that takes in a string s
of
'0'
s and '1'
s and returns a string in which each bit in s
has been swapped/replaced with the other bit. For example,
swap_bits('101011')
should return '010100'
.
Write a function num_divisors(n)
that returns the number of integers
from 1 to n
(inclusive) that divide n
evenly. For example,
num_divisors(42)
should return 8
, because 1, 2, 3, 6, 7, 14, 21,
and 42 are all divisors of 42.
Use the above num_divisors(n)
function in order to write a function
most_divisors(lst)
that takes in a list of integers lst
and
returns the integer from that list with the most divisors. For
instance, most_divisors([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
should return 12
.
Write a function common_chars(s1, s2)
that takes in two
arbitrary strings s1
and s2
and returns a new string
containing only the characters that s1
and s2
have “in common”
– i.e., characters from positions in which both s1
and s2
have the same character. The characters in the returned string
should be in the same order with respect to each other as they
were in the original string. For example, common_chars('pretty',
'pleased')
should return 'pe'
because the only positions at
which the strings have the same character are position 0 ('p'
)
and position 2 ('e'
).
Write a function longest_string(lst)
that takes in a list of strings
lst
as input and returns the longest string from that list.
For example, longest_string(['short', 'longer', 'sesquipedalian'])
should return 'sesquipedalian'
.
Write a function larger_than(n, vals)
that takes in a single
integer n
and a list of numbers vals
and that returns a list
containing the numbers from vals
that are larger than n
. (We
encourage you to implement this function in two different ways:
once using recursion, and once using a list comprehension.)
What is printed by the following working Python program?
def dog(x): print('in dog, x is', x) y = cat(x - 1) + cat(x + 2) print('in dog, y is', y) return y def cat(y): print('in cat, y is', y) x = rat(y * 2) + 3 print('in cat, x is', x) return x def rat(x): print('in rat, x is', x) return 2 * x y = dog(3) print('at this level, y is', y)
What is printed by the following working Python program?
def mystery(x): print('x is', x) if x < 1: return 2 else: p = 6 - mystery(x - 1) print('p is', p) return p y = mystery(3) print('y is', y)
Last updated on October 3, 2024.