import sys
# Maximum float
max_float = sys.float_info.max
print(f"Max float: {max_float}")
# Attempt to overflow float
overflow_float = max_float * 300
print(f"Overflow float to infinity: {overflow_float}")
Max float: 1.7976931348623157e+308
Overflow float to infinity: inf
def OR_gate(A, B):
return A or B
# AND gate
def AND_gate(A, B):
return A and B
# Theoritical circuit representing a Car starting
# A and B could be security checks, such as key being inserted or a fob being present
# C and D could be operational checks, such as a start button being pressed and safety belt being fastened
# The enclosing AND gate ensures car only operates when both security and operational checks are met
def circuit(A, B, C, D):
return AND_gate(OR_gate(A, B), AND_gate(C, D))
# Print truth table for circuit
print('A', 'B', 'C', 'D', "-->", 'Output')
# nesting of loops for both the True, False combination of A, B, C, D
# this algorithm is 2 ^ 4 = 16, thus producing all 16 combinations
# each combination terminates with the output of the circuit
for A in [False, True]:
for B in [False, True]:
for C in [False, True]:
for D in [False, True]:
print(A, B, C, D, "-->", circuit(A, B, C, D))
A B C D --> Output
False False False False --> False
False False False True --> False
False False True False --> False
False False True True --> False
False True False False --> False
False True False True --> False
False True True False --> False
False True True True --> True
True False False False --> False
True False False True --> False
True False True False --> False
True False True True --> True
True True False False --> False
True True False True --> False
True True True False --> False
True True True True --> True
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()
# Test the function with a list of RGB triplets
rgb_triplet = [('11001111', '11111000', '11110111')] # College Board example
plot_colors(rgb_triplet)
rgb_primary = [('00000111', '00000000', '00000000'),
('11111111', '11001111', '00000000'),
('00000000', '01111000', '11100000')]
plot_colors(rgb_primary)
binary: 11001111 11111000 11110111
decimal 207 248 247
proportion 0.8117647058823529 0.9725490196078431 0.9686274509803922
binary: 00000111 00000000 00000000
decimal 7 0 0
proportion 0.027450980392156862 0.0 0.0
binary: 11111111 11001111 00000000
decimal 255 207 0
proportion 1.0 0.8117647058823529 0.0
binary: 00000000 01111000 11100000
decimal 0 120 224
proportion 0.0 0.47058823529411764 0.8784313725490196
import time
# 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 3.44 milliseconds
Algorithm N^2 took 2683.56 milliseconds
Algorithm 10 times took 0.10 milliseconds
animal = "jackrabbit"[0:4] # Substring("jackrabbit", 1, 4)
animal += "a" # Concat(animal, "a")
animal = animal + "antelope"[4:8] # Concat(Substring("antelope", 5, 4), animal)
print(animal) # Outputs: lopejacka
jackalope