Yash Parikh - Lesson Day 9 - Binary and Logic Gates
Popcorn Hack 1
- Yes, it is binary
- No, it is not binary
- Yes, it is binary
Popcorn Hack 2
Example 1 (Adding): Binary Numbers: 101 + 110
The sum is 1011
Example 2 (Subtracting): Binary Numbers: 1101 - 1011
The difference is 10
Example 3 (Adding): Binary Numbers: 111 + 1001
The sum is 10000
Popcorn Hack 3
What will be the result of this expression?
True or False and False
The result is True
What will be the result of this expression?
not True and False
The result is False
What will be the result of this expression?
True or False and not False
The result is True
Homework Hack 1
Your task is to write a program that converts between decimal and binary numbers.
Instructions:
Create a function to convert a decimal number to binary. Create a function to convert a binary number (as a string) back to decimal. Test your functions with different numbers, both positive and negative. Use the following functions as a starting point:
A function that takes a decimal number and returns its binary equivalent. A function that takes a binary string and returns its decimal equivalent. Example Input/Output:
Decimal to Binary: 10 → 1010 Binary to Decimal: 1010 → 10
"""
Your task is to write a program that converts between decimal and binary numbers.
Instructions:
Create a function to convert a decimal number to binary.
Create a function to convert a binary number (as a string) back to decimal.
Test your functions with different numbers, both positive and negative.
Use the following functions as a starting point:
A function that takes a decimal number and returns its binary equivalent.
A function that takes a binary string and returns its decimal equivalent.
Example Input/Output:
Decimal to Binary: 10 → 1010
Binary to Decimal: 1010 → 10
"""
def decimal_to_binary(n):
"""
Convert a decimal number to binary.
:param n: Decimal number (int)
:return: Binary equivalent (str)
"""
if n < 0:
raise ValueError("Negative numbers are not supported.")
return bin(n).replace("0b", "")
def binary_to_decimal(b):
"""
Convert a binary string to decimal.
:param b: Binary string (str)
:return: Decimal equivalent (int)
"""
if not all(bit in '01' for bit in b):
raise ValueError("Invalid binary string.")
return int(b, 2)
# Test cases
if __name__ == "__main__":
# Decimal to Binary
decimal_number = 10
binary_result = decimal_to_binary(decimal_number)
print(f"Decimal to Binary: {decimal_number} → {binary_result}")
# Binary to Decimal
binary_string = "1010"
decimal_result = binary_to_decimal(binary_string)
print(f"Binary to Decimal: {binary_string} → {decimal_result}")
# Additional test cases
Decimal to Binary: 10 → 1010
Binary to Decimal: 1010 → 10
Homework Hack 2
"""
This program asks the user to enter a difficulty level (“easy”, “medium”, or “hard”).
If the user types something invalid, it keeps asking until they enter a valid choice.
Once a valid difficulty is entered, it prints a confirmation message.
Please Help Fix this Code!
import time
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
while difficulty != "easy" or difficulty != "medium" or difficulty != "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)
Please enter a valid difficulty level.
"""
def get_difficulty():
"""
Ask the user to enter a difficulty level and validate the input.
:return: Valid difficulty level (str)
"""
valid_difficulties = ["easy", "medium", "hard"]
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
while difficulty not in valid_difficulties:
print("Please enter a valid difficulty level.")
difficulty = input("Enter difficulty (easy, medium, hard): ").lower().strip()
time.sleep(0.5)
return difficulty
# Test the function
if __name__ == "__main__":
difficulty = get_difficulty()
print("Difficulty set to:", difficulty)
Difficulty set to: easy