Skip to content

Instantly share code, notes, and snippets.

@cxrayana
cxrayana / HW3-P6.py
Created June 30, 2023 03:43
CS582-R342D927
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:
@cxrayana
cxrayana / HW3-P5.py
Created June 30, 2023 03:39
CS582-R342D927
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)
@cxrayana
cxrayana / HW3-P4.py
Created June 30, 2023 03:36
CS582-R342D927
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]
@cxrayana
cxrayana / HW3-P3.py
Last active June 30, 2023 03:31
CS582-R342D927-HW3
#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)
@cxrayana
cxrayana / HW3-P2.py
Last active June 30, 2023 03:31
CS5823-R342D927-HW3
#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
@cxrayana
cxrayana / HW3-P1.py
Last active June 30, 2023 03:31
CS582-R342D927-HW3
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
@cxrayana
cxrayana / HW2-1.4.py
Last active June 22, 2023 06:57
CS582-R342D927-HW2
#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
@cxrayana
cxrayana / HW2-1.3.py
Created June 22, 2023 05:34
CS582-R342D927
#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)
@cxrayana
cxrayana / HW2-1.2.py
Last active June 22, 2023 05:32
CS582-R342D927
# 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))
@cxrayana
cxrayana / HW2-1.1.py
Created June 22, 2023 04:43
CS582-R342D927
# 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: