October 11th, 2020
Written by Brahm Capoor
def in_common(l1, l2):
num_in_common = 0
total_elem = len(l1) + len(l2)
for i in range(len(l1)):
if l1[i] in l2:
num_in_common += 1
return num_in_common / total_elem
def calc_score(netflix_history1, netflix_history2, fav_books1, fav_books2):
netflix_score = in_common(netflix_history1, netflix_history2)
book_score = in_common(fav_books1, fav_books2)
return netflix_score + book_score
def new_friend(name_list, compatability_scores):
best_score = -1
best_friend = None
for i in range(len(compatability_scores)):
compatability_score = compatability_scores[i]
if compatability_score > best_score: # we could make this >= as well
best_score = compatability_score
best_friend = name_list[i]
def enumerate(lst):
enum_lst = []
for i in range(len(lst)):
enum_lst.append([i, lst[i]])
return enum_lst
def matrix_constant_multiply(c, m):
prod_matrix = []
for row in m:
new_row = []
prod_matrix.append(new_row)
for elem in row:
new_row.append(elem * c)
return prod_matrix
def matrix_add(m1, m2):
sum_matrix = []
for row_idx in range(len(m1)):
new_sum_row = []
sum_matrix.append(new_sum_row)
row = m1[row_indx]
for col_idx in range(len(row)):
sum_elem = m1[row_idx][col_idx] + m2[row_idx][col_idx]
new_sum_row.append(sum_elem)
return sum_matrix
def make_times_table(m, n):
times_table = []
for i in range(m):
row_num = i + 1
new_row = []
times_table.append(new_row)
for j in range(n):
col_num = j + 1
new_row.append(row_num * col_num)
return times_table
import sys
import tkinter
import random
WIDTH = 800
HEIGHT = 600
RADIUS_MIN = 30
RADIUS_MAX = 100
N_CIRCLES_MAX = 10
def make_canvas():
"""
(provided)
Creates and returns a drawing canvas
of the given int size with a blue border,
ready for drawing.
"""
top = tkinter.Tk()
top.minsize(width=WIDTH + 10, height=HEIGHT + 10)
canvas = tkinter.Canvas(top, width=WIDTH, height=HEIGHT)
canvas.pack()
canvas.xview_scroll(6, 'units') # hack so (0, 0) works correctly
canvas.yview_scroll(6, 'units')
return canvas
def make_one_circle(canvas):
radius = random.randint(RADIUS_MIN, RADIUS_MAX)
x0 = random.randint(0, WIDTH - 2 * radius)
y0 = random.randint(0, HEIGHT - 2 * radius)
x1 = x0 + 2 * radius
y1 = y0 + 2 * radius
canvas.create_oval(x0, y0, x1, y1)
def make_all_circles():
canvas = make_canvas()
n_circles = random.randrange(0, N_CIRCLES_MAX) + 1
for i in range(n_circles):
make_one_circle(canvas)
def main():
make_all_circles()
tkinter.mainloop()
if __name__ == "__main__":
main()
CANVAS_HEIGHT = 100
CANVAS_WIDTH = 500
DX = 4
DY = 0
DELAY = 0.01
def get_messages():
messages = []
message = input("Type a message here or press enter to finish: ")
while message != "":
messages.append(message)
message = input("Type a message here or press enter to finish: ")
return messages
def main():
messages = get_messages()
canvas = make_canvas(CANVAS_WIDTH, CANVAS_HEIGHT, "News Ticker")
current_message_index = 0
current_label = canvas.create_text(
CANVAS_WIDTH,
CANVAS_HEIGHT/2,
anchor='w',
font=('Calibri', '24'),
fill='Red',
text=messages[0],
)
while True:
canvas.move(current_label, -DX, DY)
canvas.update()
time.sleep(0.01)
if is_past_left_edge(canvas, current_label):
current_message_index += 1
if current_message_index == len(messages):
current_message_index = 0
current_label = canvas.create_text(
CANVAS_WIDTH,
CANVAS_HEIGHT/2,
anchor='w',
font=('Calibri', '24'),
fill='Red',
text=messages[current_message_index],
)
canvas.mainloop()
def is_past_left_edge(canvas, label):
right_x = get_right_x(canvas, label)
return right_x <= 0