Note Template
def DecimalToBinary(num):
strs = ""
while num:
# if (num & 1) = 1
if (num & 1):
strs += "1"
# if (num & 1) = 0
else:
strs += "0"
# right shift by 1
num >>= 1
return strs
# function to reverse the string
def reverse(strs):
print(strs[::-1])
# Driver Code
num = 7
print("Binary of num 7 is:", end=" ")
reverse(DecimalToBinary(num))
def min_max(numbers): #The f before the string indicates that this is an f-string,
return (min(numbers), max(numbers)) #which allows you to embed expressions inside string literals.
#The {min_value} and {max_value} inside the string are placeholders for the values of the min_value and max_value
numbers = [1, 2, 3, 4, 5]
min_value, max_value = min_max(numbers)
print(f"Min: {min_value}, Max: {max_value}")
Homework/Hacks: For the hw, you have two options, easy or hard. The easy hack is for a 2.7 + extra work for the full 3. The easy hack is simply creating your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do additional work to get a 3. For the hard hack, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. You can delete the existing code if you want. The only contraint is that you must use a procedure. Doing this will get you a 3.
def charToBinary(x):
# Convert the character to its ASCII code
ascii_code = ord(x)
# Convert the ASCII code to binary
binary = bin(ascii_code)
# Return the binary representation as a string
return binary
APSCP = "APSCP"
for char in APSCP:
binary = charToBinary(char)
print(binary)
binary_list = []
for char in APSCP:
binary = charToBinary(char)
binary_list.append(binary)
# Concatenate the binary strings into a single string
binary_string = ''.join(binary_list)
print(binary_string)