P4-M 4/28 Binary Lesson HACKS
Learn the basics of binary including truth tables, boolean expressions, binary conversions, and binary searches.
Lesson Note Taker
Fill in the blanks below during the binary presentation. You can visit our website here!^ Due to last minute deployment issues, may need to run a local server
- git clone https://github.com/TheoH32/Runtime_Terror.git
- run:
- bundle install
- bundle exec jekyll serve
Binary
- Binary is a base 2 number system.
- 0 represents OFF and 1 represents ON.
- A Bit is the minimum unit of binary information stored in a computer system.
Boolean Expressions
- A Boolean Expression is a logical statement that is either TRUE or FALSE and compares data.
Truth Tables
- The logical operations shown in truth tables are AND, OR, NOT, and XOR.
# AND
5 > 3 and 5 == 3 + 2
5 < 3 and 5 == 5
5 == 5 or 5 != 5
5 < 3 or 5 != 5
age = 18
citizen = True
if age >= 18 and citizen:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Binary Conversions
Binary to Decimal
- We can count in binary by using powers of two.
- In binary, we read from right to left.
- 0111 has a value of 7.
Binary Search
- For a binary search, the list must be sorted.
- In a binary search, computers start at the middle(front,middle,end)/
- The number of steps required in a binary search follows the equation: log2(n)+1.
- Binary searches also work with a list of strings. We can sort them alphabetically.
- Binary searches can be represented in tree diagrams.
Hacks
You will NOT be awarded any points for sections that are INCOMPLETE
Note Taker
- Fill in all of the blanks above.
Lesson Quiz
- Complete the lesson quiz
- SCREENSHOT SCORE and paste it here (or paste screenshot with submission)
Binary Game
- Complete the Binary game and reach a minimum score of 10!
- SCREENSHOT SCORE and paste it here (or with submission)
print("put number")
x = int(input())
print(x)
y=""
print("binary")
while x>0:
y=str(x%2)+y
x//=2
print(y)
s = input("put the number")
print(s)
s = s[::-1]
ten = 0
for i in range(len(s)):
if i == 0:
x = 1 if s[int(i)] == '1' else 0
ten += x
else:
if s[i] == '1':
ten = ten + (2 ** i)
print(ten)
def hexa(decimal):
hex_digits = "0123456789ABCDEF"
hex_string = ""
while decimal > 0:
remainder = decimal % 16
hex_string += hex_digits[remainder]
decimal //= 16
return hex_string[::-1]
number = int(input("put a number"))
print(number)
hexa(number)
def hex_to_binary(hex_string):
hex_digits = "0123456789ABCDEF"
binary_string = ""
for hex_digit in hex_string:
decimal_value = hex_digits.index(hex_digit)
binary_value = bin(decimal_value)[2:].zfill(4)
binary_string += binary_value
return binary_string
number = input("put a number")
print(number)
hex_to_binary(number)
def binary_search(array=list(), target=int(), left=int(), right=int()):
if left > right:
return None
mid = (left + right) // 2
if target == array[mid]:
return mid
elif target > array[mid]:
return binary_search(array=array, target=target, left=mid + 1, right=right)
else:
return binary_search(array=array, target=target, left=left, right=mid - 1)
array=[1,2,4,15,25,30,31]
result = binary_search(array=array, target=25, left=0, right=len(array) - 1)
if result:
print(result)
else:
print("NO")
list2 = ["Donut", "Cake", "Soda", "Banana", "Fruit"]
list2.sort()
print(list2)
def decimal_to_octal(decimal):
result = ''
while decimal > 0:
remainder = decimal % 8
result = str(remainder) + result
decimal //= 8
return result or '0'
s = input("put the number")
print(s)
s = s[::-1]
ten = 0
for i in range(len(s)):
if i == 0:
x = 1 if s[int(i)] == '1' else 0
ten += x
else:
if s[i] == '1':
ten = ten + (2 ** i)
decimal_to_octal(ten)
def change(num, first = False):
ret = ''
while num:
ret += chr(num % 2 + 48)
num //= 2
while len(ret) < 3:
ret += '0'
idx = 3
if first:
while idx > 1 and ret[idx - 1] == '0':
idx -= 1
return ret[:idx][::-1]
N = input()
isFirst = True
for i in range(len(N)):
print(change(int(N[i]), isFirst),end='')
isFirst = False
Hacks Scoring
Hack | Comments | Grade |
---|---|---|
Note Taker | fill in the blanks above | 0.1 |
Lesson Quiz | under 100% = 0.1 only | 0.2 |
Binary Game | must score at least 10 points | 0.2 |
Binary Conversions Practice | if incorrect= 0.2 awarded | 0.2 |
Binary Search Questions | if incorrect= 0.2 awarded | 0.2 |
Extra Credit | MUST SHOW WORK | 0.1 |
Total | expected= 0.9/1 | 1/1 |