3.5 Hacks
Python/Javascript Popcorn and HW hacks
3.5.3 Popcorn Hacks
def findVowel(char):
if char.lower() in "aeiou":
print("the character is a vowel")
else:
print("not a vowel")
letter = str(input())
findVowel(letter)
3.5.4 Popcorn Hacks
function isEvenOrOdd(number) {
if (number % 2 === 0) {
return "The number is even.";
} else {
return "The number is odd.";
}
}
3.5.3 Homework Hacks
from itertools import product
def truth_table():
print(" A | B | C | A AND (B OR NOT C)")
print("----------------------------------------")
for A, B, C in product([True, False], repeat=3):
result = A and (B or not C)
print(f"{A:5} | {B:5} | {C:5} | {result}")
truth_table()
A | B | C | A AND (B OR NOT C)
----------------------------------------
1 | 1 | 1 | True
1 | 1 | 0 | True
1 | 0 | 1 | False
1 | 0 | 0 | True
0 | 1 | 1 | False
0 | 1 | 0 | False
0 | 0 | 1 | False
0 | 0 | 0 | False
import random
def de_morgan_game():
expressions = [
("Not (A and B)", "Not A or Not B"),
("Not (A or B)", "Not A and Not B"),
("Not (A and B and C)", "Not A or Not B or Not C"),
("Not (A or B or C)", "Not A and Not B and Not C"),
]
score = 0
rounds = 5
print("Welcome to the De Morgan's Law Game!")
print("You will see a logical expression and its De Morgan's Law simplification.")
print("Respond with 'yes' if they are equivalent and 'no' if they are not.\n")
for _ in range(rounds):
original, simplified = random.choice(expressions)
print(f"Original: {original}")
print(f"Simplified: {simplified}")
user_input = input("Are they equivalent? (yes/no): ").strip().lower()
correct_answer = "yes" if original == simplified else "no"
if user_input == correct_answer:
print("Correct!\n")
score += 1
else:
print("Incorrect.\n")
print(f"Game over! Your score: {score}/{rounds}")
de_morgan_game()
3.5.4 Homework Hacks
function isValidPassword(password) {
const hasUpperCase = /[A-Z]/.test(password); // Check for at least one uppercase letter
const hasLowerCase = /[a-z]/.test(password); // Check for at least one lowercase letter
const hasNumber = /\d/.test(password); // Check for at least one number
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); // Check for at least one special character
const noSpaces = !/\s/.test(password); // Ensure no spaces
const isLongEnough = password.length >= 10; // Ensure password is at least 10 characters long
const noMoreThanThreeInARow = !/(.)\1\1/.test(password); // Ensure no more than 3 of the same letter in a row
if (!(hasUpperCase && hasLowerCase && hasNumber && hasSpecialChar && noSpaces && isLongEnough && noMoreThanThreeInARow)) {
return "Invalid password. Please meet all the requirements.";
}
return "Password is valid!";
}
function personalityQuiz() {
let score = 0;
let answer1 = prompt("1. How do you usually spend your weekends?\n(a) Socializing\n(b) Reading\n(c) Outdoor activities\n(d) Working");
if (answer1 === 'a') score += 3;
else if (answer1 === 'b') score += 1;
else if (answer1 === 'c') score += 2;
else if (answer1 === 'd') score += 4;
let answer2 = prompt("2. What do you value most?\n(a) Freedom\n(b) Knowledge\n(c) Adventure\n(d) Stability");
if (answer2 === 'a') score += 2;
else if (answer2 === 'b') score += 1;
else if (answer2 === 'c') score += 3;
else if (answer2 === 'd') score += 4;
let answer3 = prompt("3. What's your favorite type of movie?\n(a) Action\n(b) Drama\n(c) Comedy\n(d) Documentary");
if (answer3 === 'a') score += 3;
else if (answer3 === 'b') score += 2;
else if (answer3 === 'c') score += 1;
else if (answer3 === 'd') score += 4;
let answer4 = prompt("4. What type of environment do you thrive in?\n(a) Busy city life\n(b) Quiet, peaceful spaces\n(c) Wild nature\n(d) Organized, structured places");
if (answer4 === 'a') score += 3;
else if (answer4 === 'b') score += 1;
else if (answer4 === 'c') score += 2;
else if (answer4 === 'd') score += 4;
let answer5 = prompt("5. How do you handle challenges?\n(a) By planning ahead\n(b) By improvising\n(c) By staying calm and adapting\n(d) By pushing through forcefully");
if (answer5 === 'a') score += 4;
else if (answer5 === 'b') score += 3;
else if (answer5 === 'c') score += 1;
else if (answer5 === 'd') score += 2;
let answer6 = prompt("6. What’s your ideal vacation?\n(a) Relaxing at a resort\n(b) Exploring historical sites\n(c) Adventuring in the wilderness\n(d) Trying new experiences in a foreign city");
if (answer6 === 'a') score += 1;
else if (answer6 === 'b') score += 2;
else if (answer6 === 'c') score += 3;
else if (answer6 === 'd') score += 4;
let answer7 = prompt("7. What best describes your work style?\n(a) Creative and free-form\n(b) Analytical and data-driven\n(c) Action-oriented\n(d) Structured and methodical");
if (answer7 === 'a') score += 2;
else if (answer7 === 'b') score += 1;
else if (answer7 === 'c') score += 3;
else if (answer7 === 'd') score += 4;
let answer8 = prompt("8. How do you make decisions?\n(a) Based on logic and facts\n(b) Based on emotions and feelings\n(c) By trusting my gut instinct\n(d) By seeking advice and analyzing options");
if (answer8 === 'a') score += 1;
else if (answer8 === 'b') score += 2;
else if (answer8 === 'c') score += 3;
else if (answer8 === 'd') score += 4;
let answer9 = prompt("9. What kind of books do you prefer?\n(a) Fiction\n(b) Non-fiction\n(c) Self-help\n(d) Adventure novels");
if (answer9 === 'a') score += 2;
else if (answer9 === 'b') score += 4;
else if (answer9 === 'c') score += 1;
else if (answer9 === 'd') score += 3;
let answer10 = prompt("10. What’s your biggest motivator?\n(a) Success\n(b) Freedom\n(c) Stability\n(d) Adventure");
if (answer10 === 'a') score += 4;
else if (answer10 === 'b') score += 2;
else if (answer10 === 'c') score += 1;
else if (answer10 === 'd') score += 3;
if (score <= 15) {
return "You are thoughtful, introverted, and prefer peace and quiet.";
} else if (score <= 25) {
return "You are creative, curious, and enjoy exploring new ideas.";
} else if (score <= 35) {
return "You are adventurous, active, and thrive in dynamic environments.";
} else {
return "You are ambitious, driven, and seek success and stability.";
}
}
Extra Hacks
def check_number(num):
if num >= 0:
print("Number is nonnegative")
else:
print("number is negative")
n = int(input())
check_number(n)
function suggestOutfit(temperature) {
if (temperature >= 86) {
return "It's hot! Wear shorts and a t-shirt.";
} else if (temperature >= 68 && temperature < 86) {
return "Nice weather! Wear jeans and a light shirt.";
} else if (temperature >= 50 && temperature < 68) {
return "It's a bit chilly. Wear a jacket.";
} else {
return "It's cold! Wear a coat and warm clothing.";
}
}
let temp = 75;
console.log(suggestOutfit(temp));
function findLargestAndSort(numbers) {
numbers.sort(function(a, b) {
return a - b;
});
let largest = numbers[numbers.length - 1];
return {
largest: largest,
sortedNumbers: numbers
};
}