Notes

Popcorn hacks and homework hacks

Popcorn hack one

  • #1 is binary and is 42

  • #2 is not binary because of the numbers

  • #3 is binary

Popcorn hack three

  • #1 101+110 is 1011

  • #2 1101 - 1011 is 010

  • #3 111 + 1001 is 110

Popcorn hack 3

  • Popcorn Hack 1: Problem: True or False and False

True

  • Popcorn Hack 2: Problem:

not True and False

False

  • Popcorn Hack 3: Problem:

True or False and not False

True

homework hack 1

def decimal_to_binary(decimal_num):
    """Converts a decimal number to a binary string."""
    if decimal_num >= 0:
        return bin(decimal_num)[2:]
    else:
        return '-' + bin(abs(decimal_num))[2:]

def binary_to_decimal(binary_str):
    """Converts a binary string to a decimal number."""
    if binary_str.startswith('-'):
        return -int(binary_str[1:], 2)
    else:
        return int(binary_str, 2)

# --- Testing the functions ---

# Decimal to Binary
print("Decimal to Binary Tests:")
print(decimal_to_binary(10))   # Output: 1010
print(decimal_to_binary(0))    # Output: 0
print(decimal_to_binary(-7))   # Output: -111

# Binary to Decimal
print("\nBinary to Decimal Tests:")
print(binary_to_decimal("1010"))   # Output: 10
print(binary_to_decimal("0"))      # Output: 0
print(binary_to_decimal("-111"))   # Output: -7

Homework Hack 2

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.")
    difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
    time.sleep(0.5)

print("Difficulty set to:", difficulty)
Difficulty set to: easy