def check_number(number):
if number < 0:
print(f"{number} is negative.")
else:
print(f"{number} is non-negative.")
# Example usage
check_number(-5) # Output: -5 is negative.
check_number(10) # Output: 10 is non-negative.
check_number(0) # Output: 0 is non-negative.
-5 is negative.
10 is non-negative.
0 is non-negative.
Hack 2 Python:
def check_vowel(char):
if char.lower() in 'aeiou':
print(f"{char} is a vowel.")
else:
print(f"{char} is not a vowel.")
# Example usage
check_vowel('a') # Output: a is a vowel.
check_vowel('b') # Output: b is not a vowel.
check_vowel('E') # Output: E is a vowel.
check_vowel('y') # Output: y is not a vowel.
a is a vowel.
b is not a vowel.
E is a vowel.
y is not a vowel.
3.5.4 Hack 1 Javascript:
%%javascript
function checkEvenOrOdd(number) {
// Check if the number is an integer
if (Number.isInteger(number)) {
// Use the modulus operator to check if the number is even or odd
if (number % 2 === 0) {
console.log(`${number} is even.`);
} else {
console.log(`${number} is odd.`);
}
} else {
console.log("Please enter a valid integer.");
}
}
// Example usage
checkEvenOrOdd(4); // Output: 4 is even.
checkEvenOrOdd(7); // Output: 7 is odd.
checkEvenOrOdd(0); // Output: 0 is even.
checkEvenOrOdd(3.5); // Output: Please enter a valid integer.
<IPython.core.display.Javascript object>
3.5.3 Homework 1 Python:
def truth_table():
print("A\tB\tC\tA AND (B OR NOT C)")
print("-" * 40)
for A in [True, False]:
for B in [True, False]:
for C in [True, False]:
result = A and (B or not C)
print(f"{A}\t{B}\t{C}\t{result}")
# Call the function to generate the truth table
truth_table()
A B C A AND (B OR NOT C)
----------------------------------------
True True True True
True True False True
True False True False
True False False True
False True True False
False True False False
False False True False
False False False False
Homework 2:
def game_using_de_morgan():
print("Welcome to the De Morgan's Law Game!")
print("Answer 'yes' or 'no' to the following questions:")
# Get user input
A = input("Is it sunny? (yes/no): ").strip().lower() == 'yes'
B = input("Is it a holiday? (yes/no): ").strip().lower() == 'yes'
# Use De Morgan's Law to evaluate the answers
# We can represent the logic of 'not (A and B)' as 'not A or not B'
not_A = not A
not_B = not B
simplified_logic = not_A or not_B # This uses De Morgan's Law
if simplified_logic:
print("It's either not sunny or it's not a holiday!")
else:
print("It's both sunny and a holiday!")
# Call the game function
game_using_de_morgan()
Welcome to the De Morgan's Law Game!
Answer 'yes' or 'no' to the following questions:
It's either not sunny or it's not a holiday!
3.5.4 Homework 1 Javascript:
%%js
3.5.4 Homework 1 Javascript:
function isPasswordValid(password) {
const minLength = 10;
const specialCharacters = /[!@#$%^&*(),.?":{}|<>]/;
// Conditions
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasNumber = /[0-9]/.test(password);
const hasSpecialChar = specialCharacters.test(password);
const noSpaces = !/\s/.test(password);
const maxRepeatedChars = !/(.)\1{2,}/.test(password); // No more than 3 of the same letter in a row
// De Morgan's Law simplification
const isValid = password.length >= minLength &&
(hasUppercase && hasLowercase) &&
hasNumber &&
hasSpecialChar &&
noSpaces &&
maxRepeatedChars;
return isValid;
}
// Example usage
const password = "StrongPassword123!";
if (isPasswordValid(password)) {
console.log("Your password is valid.");
} else {
console.log("Your password does not meet the required criteria.");
}
<IPython.core.display.Javascript object>
Homework 2 Javascript:
%%js
function personalityQuiz() {
let score = 0;
const questions = [
{
question: "What's your favorite season?",
options: ["1. Winter", "2. Spring", "3. Summer", "4. Fall"],
answerKey: [1, 2, 3, 4] // Corresponds to score allocation
},
{
question: "How do you prefer to spend your free time?",
options: ["1. Reading", "2. Socializing", "3. Traveling", "4. Playing video games"],
answerKey: [1, 2, 3, 4]
},
{
question: "What type of movies do you like?",
options: ["1. Action", "2. Comedy", "3. Horror", "4. Documentary"],
answerKey: [3, 2, 4, 1]
},
// Add more questions here
];
// Loop through each question and collect user's answer
for (let i = 0; i < questions.length; i++) {
const q = questions[i];
console.log(q.question);
for (const option of q.options) {
console.log(option);
}
let answer = parseInt(prompt("Choose your answer (1-4): "));
if (answer >= 1 && answer <= 4) {
score += q.answerKey[answer - 1]; // Adds corresponding score based on user's answer
} else {
console.log("Invalid answer! Please enter a number between 1 and 4
<IPython.core.display.Javascript object>