CS5001 / CS5003:
Intensive Foundations of Computer Science
def mystery_c(s1, s2):
"""
TODO: Explain what the function does
:param s1: a string
:param s2: a string
:return: None
Note: For the doctest, assume file.txt contains the following three lines:
the cat in the hat
green eggs and ham
fox in socks
>>> mystery_c('file.txt', 'ae')
>>> with open('file.txt') as f:
... for line in f:
... print(line[:-1])
TODO: Doctest output (note, the doctest output is just going to be the
contents of the file after you run the test)
"""
with open(s1, "r") as f:
lines = f.readlines()
with open(s1, "w") as f:
for line in lines:
f.write(''.join([c.upper() for c in line if c not in s2]))
def checksum(s):
"""
Returns the sum of all the ASCII values of the characters in the string.
:param s: A string
:return: The sum of the ASCII values of the string
>>> checksum("hello")
532
"""
sum = 0
for c in s:
sum += ord(c)
return sum
def hamming_distance(s1, s2):
"""
Returns the Hamming distance for two strings, or None if the two strings
have different lengths.
:param s1: the first string
:param s2: the second string
:return: An integer representing the Hamming distance between s1 and s2,
or None if the strings have different lengths
>>> hamming_distance('GGACG', 'GGTCA')
2
"""
if len(s1) != len(s2):
return None
hd = 0
for c1, c2 in zip(s1, s2):
if c1 != c2:
hd += 1
return hd
def count_and_wrap(total, wrap_after):
"""
Prints total number of lines, starting from 0 and wrapping after
wrap_after.
:param total: an integer
:param wrap_at: an integer
:return: None
>>> count_and_wrap(9, 4)
0
1
2
3
4
0
1
2
3
"""
for i in range(total):
print(i % (wrap_after + 1))
def multiply(a, b):
"""
Multiplies a and b using recursion and only + and - operators
:param a: a positive integer
:param b: a positive integer
:return: a * b
"""
if b == 0:
return 0
return a + multiply(a, b - 1)
def multiply_efficient(a, b):
if a < b:
return multiply(b, a)
if b == 0:
return 0
return a + multiply_efficient(a, b - 1)
import timeit
print("Timing multiply(10, 900):")
print(timeit.timeit(lambda: multiply(10, 900), number=10000))
print()
print("Timing multiply(900, 10):")
print(timeit.timeit(lambda: multiply(900, 10), number=10000))
print()
print("Timing multiply_efficient(900, 10):")
print(timeit.timeit(lambda: multiply_efficient(900, 10), number = 10000))
print()
print("Timing multiply_efficient(10, 900):")
print(timeit.timeit(lambda: multiply_efficient(10, 900), number = 10000))
print()
import timeit
print("Timing multiply(10, 900):")
print(timeit.timeit(lambda: multiply(10, 900), number=10000))
print()
print("Timing multiply(900, 10):")
print(timeit.timeit(lambda: multiply(900, 10), number=10000))
print()
print("Timing multiply_efficient(900, 10):")
print(timeit.timeit(lambda: multiply_efficient(900, 10), number = 10000))
print()
print("Timing multiply_efficient(10, 900):")
print(timeit.timeit(lambda: multiply_efficient(10, 900), number = 10000))
print()
Timing multiply(10, 900):
2.596630092
Timing multiply(900, 10):
0.017811094999999888
Timing multiply_efficient(900, 10):
0.020884906000000036
Timing multiply_efficient(10, 900):
0.019478217000000075
class Ball:
"""
The Ball class defines a "ball" that can bounce around the screen
"""
>>> class Ball:
... """
... The Ball class defines a "ball" that can bounce around the screen
... """
...
>>> print(Ball)
<class '__main__.Ball'>
>>>
Notice that the full name of the type is '__main__.Ball'
>>> class Ball:
... """
... The Ball class defines a "ball" that can bounce around the screen
... """
...
>>> print(Ball)
<class '__main__.Ball'>
>>>
>>> my_ball = Ball()
>>> print(my_ball)
<__main__.Ball object at 0x109b799e8>
>>>
>>> lots_of_balls = [Ball() for x in range(1000)]
>>> len(lots_of_balls)
1000
>>> print(lots_of_balls[100])
<__main__.Ball object at 0x109dc6e10>
>>>
class Ball:
"""
The Ball class defines a "ball" that can
bounce around the screen
"""
def __init__(self, canvas, x, y):
self.canvas = canvas
self.x = x
self.y = y
self.draw()
def draw(self):
width = 30
height = 30
outline = 'black'
fill = 'black'
self.canvas.create_oval(self.x, self.y,
self.x + width,
self.y + height,
outline=outline,
fill=fill)
class Ball:
"""
The Ball class defines a "ball" that can
bounce around the screen
"""
def __init__(self, canvas, x, y):
self.canvas = canvas
self.x = x
self.y = y
self.draw()
def draw(self):
width = 30
height = 30
outline = 'blue'
fill = 'blue'
self.canvas.create_oval(self.x, self.y,
self.x + width,
self.y + height,
outline=outline,
fill=fill)
class Ball:
"""
The Ball class defines a "ball" that can
bounce around the screen
"""
def __init__(self, canvas, x, y):
self.canvas = canvas
self.x = x
self.y = y
self.draw()
def draw(self):
width = 30
height = 30
outline = 'blue'
fill = 'blue'
self.canvas.create_oval(self.x, self.y,
self.x + width,
self.y + height,
outline=outline,
fill=fill)
def animate(playground):
canvas = playground.get_canvas()
ball = Ball(canvas, 10, 10)
canvas.update() // redraw canvas
class Ball:
"""
The Ball class defines a "ball" that can
bounce around the screen
"""
def __init__(self, canvas, x, y):
self.canvas = canvas
self.x = x
self.y = y
self.draw()
def draw(self):
width = 30
height = 30
outline = 'blue'
fill = 'blue'
self.canvas.create_oval(self.x, self.y,
self.x + width,
self.y + height,
outline=outline,
fill=fill)
def animate(playground):
canvas = playground.get_canvas()
balls = []
for i in range(10)
balls.append(Ball(canvas, 30 * i, 30 * i))
canvas.update() // redraw canvas
class Ball:
"""
The Ball class defines a "ball" that can
bounce around the screen
"""
def __init__(self, canvas, x, y, width, height, fill):
self.canvas = canvas
self.x = x
self.y = y
self.width = width
self.height = height
self.fill = fill
self.draw()
def draw(self):
self.canvas.create_oval(self.x, self.y,
self.x + self.width,
self.y + self.height,
outline=self.fill,
fill=self.fill)
def animate(playground):
canvas = playground.get_canvas()
ball1 = Ball(canvas, 100, 100, 50, 30, "magenta")
ball2 = Ball(canvas, 40, 240, 10, 100, "aquamarine")
ball3 = Ball(canvas, 200, 200, 150, 10, "goldenrod1")
ball4 = Ball(canvas, 300, 300, 1000, 1000, "yellow")
canvas.update()
class Ball:
"""
The Ball class defines a "ball" that can
bounce around the screen
"""
def __init__(self, canvas, x, y,
width=30, height=30, fill="blue"):
self.canvas = canvas
self.x = x
self.y = y
self.width = width
self.height = height
self.fill = fill
self.draw()
def draw(self):
self.canvas.create_oval(self.x, self.y,
self.x + self.width,
self.y + self.height,
outline=self.fill,
fill=self.fill)
def animate(playground):
canvas = playground.get_canvas()
ball1 = Ball(canvas, 100, 100) # default size and color
ball2 = Ball(canvas, 40, 240, fill="aquamarine")
ball3 = Ball(canvas, 200, 200, 150, 10)
ball4 = Ball(canvas, 300, 300, 1000, 1000, "yellow")
canvas.update()
ball1 = Ball(canvas, 100, 100) # default size and color
ball2 = Ball(canvas, 40, 240, fill="aquamarine")
ball3 = Ball(canvas, 200, 200, 150, 10)
ball4 = Ball(canvas, 300, 300, 1000, 1000, "yellow")
ball5 = Ball(canvas, 300, 300, 1000, 1000, "yellow") // same as ball4
canvas.update()
print(ball1)
print(ball2)
print(ball3)
print(ball4)
print(f"ball4 == ball5 ? {ball4 == ball5}")
print(f"ball1 == ball5 ? {ball1 == ball5}")
ball4 == ball5 ? False
ball1 == ball5 ? False
<__main__.Ball object at 0x10484f1d0>
<__main__.Ball object at 0x10484f208>
<__main__.Ball object at 0x10484f240>
<__main__.Ball object at 0x10484f278>
def __str__(self):
"""
Creates a string that defines a Ball
:return: a string
"""
ret_str = ""
ret_str += (f"x=={self.x}, y=={self.y}, "
f"width=={self.width}, height=={self.height}, "
f"fill=={self.fill}")
return ret_str
def __eq__(self, other):
return (
self.canvas == other.canvas and
self.x == other.x and
self.y == other.y and
self.width == other.width and
self.height == other.height and
self.fill == other.fill
)