Solution to problem on slide 4 :
# shifting the grid K times is the same as shifting the last K items in from of the list
# essentially, nums = nums[-k:] + nums[:-k]
flat = [i for row in grid for i in row] #flatten the array into (n*m) shape instead of (n, m) shape
k = k % len(flat) # make sure steps is not looping around the array more than 1 time
shift = lambda lst, x: lst[-x:] + lst[:-x] # shift flattened array k steps
# reshape array to (n,m) shape
reshape = lambda lst, c: [lst[i:i+c] for i in range(0, len(lst), c)]
return reshape(shift(flat, k), len(grid[0]))
Solution to problem on slide 5-6(HW):
# simple brute force solution
vals = (input("INPUTS: ")).split(" ")
print(vals)
vals = [int(i, base=16) for i in vals]
print(vals)
len_8 = lambda x: '0' * (8 - len(x)) + x
vals = [list(len_8(bin(i)[2:])) for i in vals]
print(vals)
for i in range(10):
inp = (input("INPUTS: ")).split(" ")
s_r, s_c = inp[:2]
s_r, s_c = int(s_r), int(s_c)
val = [list(i) for i in inp[2:]]
print(val)
matchs = 0
for r in range(8 - s_r):
for c in range(8 - s_c):
match = True
for rr in range(s_r):
for cc in range(s_c):
if vals[r + rr][c+cc] != val[rr][cc]:
match = False
break
if not match:
break
if match:
matchs += 1
print(matchs)
Let me know if you have any questions about the slides or code in this post or need help understanding a problem or its solution.