This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binarySearch(target, sortedLyst): | |
left = 0 | |
right = len(sortedLyst) - 1 | |
while left <= right: | |
midpoint = (left + right) // 2 | |
if target == sortedLyst[midpoint]: | |
return midpoint | |
elif target < sortedLyst[midpoint]: | |
right = midpoint - 1 | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sequentialSearch(target, lyst): | |
position = 0 | |
while position < len(lyst): | |
if target == lyst[position]: | |
return position | |
position += 1 | |
return -1 | |
lyst = [4,5,12,8] | |
value = sequentialSearch(5,lyst) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def indexOfMin(lyst): | |
minIndex = 0 | |
currentIndex = 1 | |
while currentIndex < len(lyst): | |
if lyst[currentIndex] < lyst[minIndex]: | |
minIndex = currentIndex | |
currentIndex += 1 | |
return minIndex | |
lyst = [4,5,12,8] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Prints the number of calls of a recursive Fibonacci function with problem sizes that double. | |
from counter import Counter | |
def fib(n, counter): | |
counter.increment() | |
if n < 3: | |
return 1 | |
else: | |
return fib(n - 1, counter) + fib(n - 2, counter) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Prints the number of iterations for problem sizes that double, using a nested loop. | |
problemSize = 1000 | |
print("%12s%15s" % ("Problem Size", "Iterations")) | |
for count in range(5): | |
number = 0 | |
work = 1 | |
for j in range(problemSize): | |
for k in range(problemSize): | |
number += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
problemSize = 10000000 | |
print("%12s%16s" % ("Problem Size", "Seconds")) | |
for count in range(5): | |
start = time.time() | |
work = 1 | |
for x in range(problemSize): | |
work += 1 | |
work -= 1 | |
elapsed = time.time() - start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1.4 Use a one-time pad to encrypt and decrypt images. | |
import os | |
from PIL import Image | |
def generate_key(image_data_size): | |
key = bytearray(os.urandom(image_data_size)) | |
return key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#3 Write a solver for The Towers of Hanoi that works for any number of towers. | |
class TowersOfHanoi: | |
def __init__(self, num_towers, num_disks): | |
self.num_towers = num_towers | |
self.num_disks = num_disks | |
self.towers = [[] for _ in range(num_towers)] | |
for disk in range(num_disks, 0, -1): | |
self.towers[0].append(disk) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You saw how the simple int type in Python can be used to represent a bit string. Write an ergonomic wrapper around int that can be used generically as asequence of bits (make it iterable and implement __getitem__()). Reimplement CompressedGene, using the wrapper. | |
class BitSequence: | |
def __init__(self, value, length): | |
self.value = int(value) | |
self.length = length | |
def __iter__(self): | |
return iter(self.get_bit(i) for i in range(self.length)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write yet another function that solves for element n of the Fibonacci sequence,using a technique of your own design. Write unit tests that evaluate its correctness and performance relative to the other versions in this chapter. | |
def fibonacci(n): | |
if n <= 0: | |
return None | |
memo = [None] * (n + 1) | |
return fibonacci_helper(n, memo) | |
def fibonacci_helper(n, memo): | |
if n == 1 or n == 2: |