Skip to the content.

Binary Lesson

popcorn hack #1

  • 1.) yes
  • 2.) no
  • 3.) yes

popcorn hack #2

  • 1.) 1011
  • 2.) 010
  • 3.) 1110

popcorn hack #1

  • True

popcorn hack #2

  • false

popcorn hack #3

  • True

Homework Hack #1!

# Function to convert decimal to binary
def decimal_to_binary(decimal_number):
    if decimal_number >= 0:
        return bin(decimal_number)[2:]  # Remove '0b' prefix
    else:
        return '-' + bin(abs(decimal_number))[2:]

# Function to convert binary to decimal
def binary_to_decimal(binary_string):
    if binary_string.startswith('-'):
        return -int(binary_string[1:], 2)
    else:
        return int(binary_string, 2)

# Test the functions
# Positive number
print("Decimal 10 to Binary:", decimal_to_binary(10))   # Output: 1010
print("Binary 1010 to Decimal:", binary_to_decimal('1010'))  # Output: 10

# Negative number
print("Decimal -10 to Binary:", decimal_to_binary(-10))   # Output: -1010
print("Binary -1010 to Decimal:", binary_to_decimal('-1010'))  # Output: -10

# More tests
print("Decimal 0 to Binary:", decimal_to_binary(0))  # Output: 0
print("Binary 0 to Decimal:", binary_to_decimal('0'))  # Output: 0

Homework Hack #2!

import time

difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()

while difficulty != "easy" and difficulty != "medium" and difficulty != "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)