Homeworks and Popcorn Hacks for Lesson 3.3/3.5
All homeworks for Lessons
3.3 and 3.5 Popcorn Hacks
Popcorn Hack 1
def calculate_operations(a, b):
result = {
"addition": a + b,
"subtraction": a - b,
"multiplication": a * b,
"division": a / b if b != 0 else "undefined (division by zero)",
"mods": a % b if b != 0 else "undefined (mods by zero)"
}
return result
a = 10
b = 3
operations_result = calculate_operations(a, b)
for operation, value in operations_result.items():
print(f"{operation}: {value}")
addition: 13
subtraction: 7
multiplication: 30
division: 3.3333333333333335
mods: 1
Popcorn Hack 2
def fibonacci(n):
if n <= 0:
return "Input should be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
prev, curr = 0, 1
for _ in range(3, n+1):
prev, curr = curr, prev + curr
return curr
n = 7
print(f"The {n}th Fibonacci number is: {fibonacci(n)}")
The 7th Fibonacci number is: 8
Popcorn Hack 3
%%js
public class ContrapositiveExample {
public static void main(String[] args) {
int age = 18;
boolean isAdult = checkIfAdult(age);
if (isAdult) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
public static boolean checkIfAdult(int age) {
return age >= 18;
}
}
<IPython.core.display.Javascript object>
Popcorn Hack 4
def is_eligible_to_drive(age):
if age >= 18:
return True
else:
return False
age = 20
if is_eligible_to_drive(age):
print("You can drive.")
else:
print("You cannot drive.")
You can drive.
Homework Hack 1
def fibonacci_iterative(n):
if n <= 0:
return "Input should be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
prev, curr = 0, 1
for _ in range(3, n + 1):
prev, curr = curr, prev + curr
return curr
print(fibonacci_iterative(10))
34
Uses iterative: best in terms of simplicity and performance Uses recursive: Clear in readability but bad for larger values Memoization: Combines readability and efficiency/simplicity and is good for large inputs.
Homework Hack 2
def AND_gate(a, b):
return a & b
def OR_gate(a, b):
return a | b
def NOT_gate(a):
return int(not a)
def NAND_gate(a, b):
return int(not (a & b))
def NOR_gate(a, b):
return int(not (a | b))
def XOR_gate(a, b):
return a ^ b
def generate_truth_table():
print("Truth Table for Logic Gates:")
print("A B | AND OR NAND NOR XOR")
for a in [0, 1]:
for b in [0, 1]:
print(f"{a} {b} | {AND_gate(a, b)} {OR_gate(a, b)} {NAND_gate(a, b)} {NOR_gate(a, b)} {XOR_gate(a, b)}")
print("\nTruth Table for NOT Gate:")
print("A | NOT")
for a in [0, 1]:
print(f"{a} | {NOT_gate(a)}")
generate_truth_table()
Truth Table for Logic Gates:
A B | AND OR NAND NOR XOR
0 0 | 0 0 1 1 0
0 1 | 0 1 1 0 1
1 0 | 0 1 1 0 1
1 1 | 1 1 0 0 0
Truth Table for NOT Gate:
A | NOT
0 | 1
1 | 0
%%js
public class LogicGateSimulator {
public static int AND_gate(int a, int b) {
return a & b;
}
public static int OR_gate(int a, int b) {
return a | b;
}
public static int NOT_gate(int a) {
return (a == 1) ? 0 : 1;
}
public static int NAND_gate(int a, int b) {
return NOT_gate(AND_gate(a, b));
}
public static int NOR_gate(int a, int b) {
return NOT_gate(OR_gate(a, b));
}
public static int XOR_gate(int a, int b) {
return a ^ b;
}
public static void generateTruthTable() {
System.out.println("Truth Table for Logic Gates:");
System.out.println("A B | AND OR NAND NOR XOR");
for (int a = 0; a <= 1; a++) {
for (int b = 0; b <= 1; b++) {
System.out.printf("%d %d | %d %d %d %d %d\n",
a, b, AND_gate(a, b), OR_gate(a, b), NAND_gate(a, b), NOR_gate(a, b), XOR_gate(a, b));
}
}
System.out.println("\nTruth Table for NOT Gate:");
System.out.println("A | NOT");
for (int a = 0; a <= 1; a++) {
System.out.printf("%d | %d\n", a, NOT_gate(a));
}
}
public static void main(String[] args) {
generateTruthTable();
}
}
<IPython.core.display.Javascript object>