Skip to the content.

Base 2 Math

popcorn/homework hacks

POPCORN HACKS

Hack 1

Example 1: 101010 = YES BINARY

Example 2: 12301 = NO BINARY

Example 3: 11001 = YES BINARY

Hack 2

Example 1 (Adding): 101 + 110 = 1011

Example 2 (Subtracting): 1101 - 1011 = 010

Example 3 (Adding): 111 + 1001 = 1110

Hack 3

Part 1

What will be the result of this expression? —> True or False and False

Result: True

Part 2

What will be the result of this expression? —> not True and False

Result: False

Part 3

What will be the result of this expression? —> True or False and not False

Result: True


HOMEWORK HACKS

Hack 1

def decimal_to_binary(decimal_number):
    """Converts a decimal number to its binary representation."""
    if decimal_number == 0:
        return '0'
    is_negative = decimal_number < 0
    decimal_number = abs(decimal_number)
    binary = ''
    while decimal_number > 0:
        binary = str(decimal_number % 2) + binary
        decimal_number = decimal_number // 2
    if is_negative:
        binary = '-' + binary
    return binary

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

# Testing
print("Decimal to Binary:")
print(f"10 -> {decimal_to_binary(10)}")     # Should print 1010
print(f"-10 -> {decimal_to_binary(-10)}")   # Should print -1010
print(f"0 -> {decimal_to_binary(0)}")       # Should print 0

print("\nBinary to Decimal:")
print(f"1010 -> {binary_to_decimal('1010')}")    # Should print 10
print(f"-1010 -> {binary_to_decimal('-1010')}")  # Should print -10
print(f"0 -> {binary_to_decimal('0')}")          # Should print 0
Decimal to Binary:
10 -> 1010
-10 -> -1010
0 -> 0

Binary to Decimal:
1010 -> 10
-1010 -> -10
0 -> 0

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