Homework and Popcorn Hacks for Base64 and Images Lesson
.
Popcorn Hack 1
- png
- jpg/jpeg
- gif
Popcorn Hack 2 - Decode This!
- 1011
- 010
- 1110
Popcorn Hack 1
What will be the result of this expression?
True or False and False result:True
Popcorn Hack 2
What will be the result of this expression?
not True and False result:False
Popcorn Hack 3
What will be the result of this expression?
True or False and not False result:True
Homework Hack Binary Converter
def decimal_to_binary(n):
"""Convert a decimal number to a binary string."""
if n == 0:
return "0"
is_negative = n < 0
n = abs(n)
binary = ""
while n > 0:
binary = str(n % 2) + binary
n //= 2
return "-" + binary if is_negative else binary
def binary_to_decimal(binary_str):
"""Convert a binary string to a decimal number."""
is_negative = binary_str.startswith("-")
if is_negative:
binary_str = binary_str[1:]
decimal = 0
for digit in binary_str:
decimal = decimal * 2 + int(digit)
return -decimal if is_negative else decimal
# Test cases
test_decimals = [10, -10, 0, 255, -255]
test_binaries = ["1010", "-1010", "0", "11111111", "-11111111"]
print("Decimal to Binary:")
for dec in test_decimals:
print(f"{dec} → {decimal_to_binary(dec)}")
print("\nBinary to Decimal:")
for bin_str in test_binaries:
print(f"{bin_str} → {binary_to_decimal(bin_str)}")
Decimal to Binary:
10 → 1010
-10 → -1010
0 → 0
255 → 11111111
-255 → -11111111
Binary to Decimal:
1010 → 10
-1010 → -10
0 → 0
11111111 → 255
-11111111 → -255
Homework Hack 2: Difficulty Level Checker
import time
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
while difficulty not in ["easy", "medium", "hard"]:
print("Please enter a valid difficulty level.")
time.sleep(0.5)
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
print("Difficulty set to:", difficulty)
Difficulty set to: easy