CB popcorn hacks

4.as a popcorn hack (binary challenge), describe an overflow in 8 binary digits

  • Add 1 to 11111111 (255) because this exceeds the maximum value so it causes overflow

5.As a popcorn hack (coding challenge), create multiple new circuits and gates

  • NAND: def NAND_gate(A, B): return not (A and B)

11.as a hack (binary challenge), make the rgb standard colors


import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Function to convert binary to decimal
def binary_to_decimal(binary):
    return int(binary, 2)

def plot_colors(rgb_triplets):
    # Create a figure with one subplot per RGB triplet
    fig, axs = plt.subplots(1, len(rgb_triplets), figsize=(2 * len(rgb_triplets), 2))
    
    # Ensure axs is always a list
    axs = axs if len(rgb_triplets) > 1 else [axs]

    for ax, (red_binary, green_binary, blue_binary) in zip(axs, rgb_triplets):
        # Convert to binary strings to decimal
        red_decimal = binary_to_decimal(red_binary)
        green_decimal = binary_to_decimal(green_binary)
        blue_decimal = binary_to_decimal(blue_binary)

        # Normalize number to [0, 1] range, as it is expected by matplotlib 
        red, green, blue = red_decimal/255, green_decimal/255, blue_decimal/255

        # Define a rectangle patch with the binary RGB triplet color and a black border
        rect = patches.Rectangle((0, 0), 1, 1, facecolor=(red, green, blue), edgecolor='black', linewidth=2)
        
        # Add the rectangle to the plot which shows the color 
        ax.add_patch(rect)

        # Remove axis information, we just want to see the color
        ax.axis('off')

        # Print the binary and decimal values
        print("binary:", red_binary, green_binary, blue_binary)    
        print("decimal", red_decimal, green_decimal, blue_decimal)
        print("proportion", red, green, blue)

    # Show the colors
    plt.show()

50.as a popcorn hack (coding challenge), scale list of size by factor of 10 and measure the times

import time

def scaled_list(lst, factor):
    return lst * 10

# O(n) Algorithm that accesses each element in the list twice, 2 * n times 
def algorithm_2n(lst):
    for i in lst:
        pass
    for i in lst:
        pass

# O(n^2) Algorithm that accesses each element in the list n times, n * n times
def algorithm_nSquared(lst):
    for i in lst:
        for j in lst:
            pass

# O(1) Algorithm that accesses only the first 10 elements in the list, 10 * 1 is constant 
def algorithm_10times(lst):
    for i in lst[:10]:
        pass

# Create a large list
n = 10000
lst = list(range(n))

# Measure the time taken by algorithm1
start = time.time()
algorithm_2n(lst)
end = time.time()
print(f"Algorithm 2 * N took {(end - start)*1000:.2f} milliseconds")

# Measure the time taken by algorithm2
start = time.time()
algorithm_nSquared(lst)
end = time.time()
print(f"Algorithm N^2 took {(end - start)*1000:.2f} milliseconds")

# Measure the time taken by algorithm3
start = time.time()
algorithm_10times(lst)
end = time.time()
print(f"Algorithm 10 times took {(end - start)*1000:.2f} milliseconds")
Algorithm 2 * N took 0.44 milliseconds
Algorithm N^2 took 1665.42 milliseconds
Algorithm 10 times took 0.07 milliseconds

56.as a popcorn hack (coding challenge) measure the time to calulate fibonacci sequence at small and large numbers

import time

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

def version_I(idList):
    start = time.time()
    topScore = 0
    for idNum in idList:
        predictedScore = fibonacci(idNum)
        if predictedScore > topScore: 
            topScore = predictedScore
    end = time.time()
    print(f"Version I took {end - start + len(idList) * 60:.2f} simulated seconds")

def version_II(idList):
    start = time.time()
    topID = idList[0]
    for idNum in idList:
        if fibonacci(idNum) > fibonacci(topID):
            topID = idNum
    end = time.time() 
    print(f"Version II took {end - start + len(idList) * 2 * 60 + 60:.2f} simulated seconds")

idList = [10, 20, 30, 35]
version_I(idList)
version_II(idList)
Version I took 245.11 simulated seconds
Version II took 546.92 simulated seconds

64.as a popcorn hack (coding challenge), fix the multiply function to work with negative numbers

def multiply(x, y):
    count = 0
    result = 0
    while count < abs(y):
        result += x
        count += 1
    return result if y > 0 else result * -1

print(multiply(2, 5))
print(multiply(2, -5))
print(multiply(-2, 5))
print(multiply(-2, -5))
10
-10
-10
10

65.as a popcorn hack (binary challenge), create string and concatenation options for A, B, C

animal = "jackrabbit"[0:4]  # Substring("jackrabbit", 1, 4)
animal += "a"  # Concat(animal, "a")
animal = "antelope"[4:8] + animal  # Concat(Substring("antelope", 5, 4), animal)
print(animal)  # Outputs: lopejacka

animal = "antelope"[4:9]
animal += "a"
animal = "jackrabbit"[0:4] + animal
print(animal)


animal = "antelope"[4:9]
animal = "a" + animal
animal = "jackrabbit"[0:4] + animal
print(animal)


lopejacka
jacklopea
jackalope