3.3.3 Popcorn Hacks

num = int(input())
total_sum = 0
for i in range (1, num + 1):
	total_sum += i

print(total_sum)

3.3.4 Popcorn hacks

function calculateExpression() {
    const result = (10 + 6) * 4 / 2 - 0;
    return result;
}
console.log(calculateExpression());

3.3.3 Homework Hacks

def computeMeanandMedian(nums):
    mean = sum(nums) / len(nums)
    
    sorted_nums = sorted(nums)
    nums_len = len(nums)
    
    if n % 2 == 0: 
        median = (sorted_nums[nums_len // (2 - 1)] + sorted_nums[nums_len // 2]) / 2
    else: 
        median = sorted_nums[nums_len // 2]
    
    # Print the results
    print(f"Mean: {mean}")
    print(f"Median: {median}")
def collatzConjecture(num):
	collatz = [num]

	while num != 1:
		if num % 2 == 0:
			num = num // 2

		else:
			num = (3 * num) + 1

		collatz.append(num)

	print(collatz)

collatzConjecture(5)

3.3.4 Homework Hacks

%%js
function gcdAndLcm(a, b) {
	// Helper function to compute the GCD using the Euclidean algorithm
	function gcd(x, y) {
	  while (y !== 0) {
		let temp = y;
		y = x % y;
		x = temp;
	  }
	  return x;
	}
  
	// Compute GCD
	const gcdResult = gcd(a, b);
  
	// Compute LCM using the relation: LCM(a, b) = (a * b) / GCD(a, b)
	const lcmResult = (a * b) / gcdResult;
  
	// Return the results as an object
	return {
	  gcd: gcdResult,
	  lcm: lcmResult
	};
  }
  
  // Example usage
  const result = gcdAndLcm(12, 18);
  console.log(result); // { gcd: 6, lcm: 36 }
  
%%js
function primeFactors(n) {
	const factors = [];
  
	// Check for number of 2s that divide n
	while (n % 2 === 0) {
	  factors.push(2);
	  n /= 2;
	}
  
	// Check for odd factors from 3 up to the square root of n
	for (let i = 3; i <= Math.sqrt(n); i += 2) {
	  while (n % i === 0) {
		factors.push(i);
		n /= i;
	  }
	}
  
	// If n is a prime number greater than 2
	if (n > 2) {
	  factors.push(n);
	}
  
	return factors;
  }
  
  // Example usage
  console.log(primeFactors(28)); // Output: [2, 2, 7]
  console.log(primeFactors(29)); // Output: [29]
  

Extra Hacks

# Hack 1
num_list = [1, 2, 3, 4]
total_sum = 0
for num in num_list:
	total_sum += num

print(total_sum)
# Hack 2
price = int(input())
bags = int(input())
total_cost = price * bags
print(total_cost)
%%js
function makeSandwich() {
	let ingredients = [];
  
	for (let i = 1; i <= 3; i++) {
	  let ingredient = prompt(`Add ingredient ${i} to your sandwich:`);
	  ingredients.push(ingredient);
	}
  
	let sandwichName = prompt("Give your sandwich a name:");
  
	console.log(`Your sandwich "${sandwichName}" is ready with the following ingredients:`);
  }
  
  makeSandwich();
  

%%js
function startGame() {
	alert("Welcome to the Food Adventure!");
  
	// Start the first storyline
	let choice1 = prompt("You walk into a new restaurant and the chef offers you two specials. Do you pick (1) the mysterious dish or (2) the chef's signature dish? Type 1 or 2:");
  
	if (choice1 == "1") {
	  // Storyline 1: The Mysterious Dish
	  let choice2 = prompt("You chose the mysterious dish. It arrives, and it's an exotic seafood platter you've never seen before. Do you (1) try it or (2) send it back?");
	  
	  if (choice2 == "1") {
		let choice3 = prompt("You take a bite and it's surprisingly delicious! The chef offers you a challenge: finish the whole platter to get a free meal. Do you (1) accept the challenge or (2) politely decline?");
		if (choice3 == "1") {
		  alert("You bravely finish the whole platter! The chef is impressed and gives you a free meal voucher. You win!");
		} else if (choice3 == "2") {
		  alert("You politely decline and enjoy the rest of your meal. You leave full and satisfied. The end!");
		} else {
		  alert("Invalid choice, try again.");
		}
	  } else if (choice2 == "2") {
		alert("You send the dish back and order something familiar. The meal is fine, but nothing special. Game over.");
	  } else {
		alert("Invalid choice, try again.");
	  }
  
	} else if (choice1 == "2") {
	  // Storyline 2: The Chef's Signature Dish
	  let choice4 = prompt("You chose the chef's signature dish, a perfectly grilled steak. It arrives with a fancy sauce you've never tasted before. Do you (1) pour the sauce on the steak or (2) keep the steak plain?");
	  
	  if (choice4 == "1") {
		alert("You pour the sauce and it's amazing! The chef compliments your adventurous spirit. You win!");
	  } else if (choice4 == "2") {
		alert("You keep it plain and enjoy a classic steak. It's good, but you wonder what the sauce tasted like. The end.");
	  } else {
		alert("Invalid choice, try again.");
	  }
  
	} else {
	  alert("Invalid choice, try again.");
	}
  }
  
  // Start the game
  startGame();