Python Popcorn and Homework Hacks

#Popcorn Hack 1

num_list = [3, 7, 11, 17, 19, 22, 25]

sum_list = sum(num_list)
print("sum_list = " + str(sum_list))
sum_list = 104
#Popcorn Hack 2

popcorn_price = int(input())
popcorn_bags = int(input())
popcorn_cost = popcorn_price * popcorn_bags

print("Popcorn cost is $" + str(popcorn_cost))
Popcorn cost is $45
# Popcorn Hack 3

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

print("total_sum = " + str(total_sum))
total_sum = 595
# Homework Hack 1

def compute_mean_median(numbers):
    mean = sum(numbers) / len(numbers)
    sorted_numbers = sorted(numbers)
    n = len(sorted_numbers)
    
    if n % 2 == 0: 
        median = (sorted_numbers[n // 2 - 1] + sorted_numbers[n // 2]) / 2
    else:
        median = sorted_numbers[n // 2]
    
    print("Arithmetic Mean: " + str(mean))
    print("Median: " + str(median))

numbers = [5, 2, 9, 1, 5, 6]
compute_mean_median(numbers)

Arithmetic Mean: 4.666666666666667
Median: 5.0
# Homework Hack 2

def collatz_sequence(a):
    sequence = [a]
    while a != 1:
        if a % 2 == 0:
            a = a // 2 
        else:
            a = 3 * a + 1 
        sequence.append(a)
    
    print("Collatz sequence:", sequence)

# Example usage:
collatz_sequence(6)
Collatz sequence: [6, 3, 10, 5, 16, 8, 4, 2, 1]

Javascript Popcorn and Homework Hacks

%%javascript

/* Popcorn Hack 1 */

function calculate() {
    let result = ((10 + 6) * 4) / 2 - 0;
    return result;
}

console.log(calculate());
<IPython.core.display.Javascript object>
%%javascript

/* Popcorn Hack 2 */

function sandwich() {
    let ingredients = []; 
    let addMore = true;
    while (addMore) {
        let ingredient = prompt("Enter an ingredient for your sandwich (or type 'done' to finish):");
        if (ingredient.toLowerCase() === 'done') {
            addMore = false;  // Stop the loop if the user is done
        } else {
            ingredients.push(ingredient); 
        }
    }

    let sandwichName = prompt("Give your sandwich a name:");
    console.log(`Your sandwich '${sandwichName}' includes: ${ingredients.join(", ")}`);
}

// Example usage:
sandwich();

<IPython.core.display.Javascript object>
%%javascript

/* Homework Hack 1 */

function gcd(a, b) {
    while (b !== 0) {
        let temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

function lcm(a, b) {
    // LCM formula: (a * b) / GCD(a, b)
    return (a * b) / gcd(a, b);
}

function computeGCDandLCM(a, b) {
    let gcdValue = gcd(a, b);
    let lcmValue = lcm(a, b);
    
    return {
        GCD: gcdValue,
        LCM: lcmValue
    };
}

// Example usage:
let result = computeGCDandLCM(12, 18);
console.log(result);  // Output: { GCD: 6, LCM: 36 }
<IPython.core.display.Javascript object>
%%javascript

/* Homework Hack 2 */

function primeFactors(n) {
    let factors = [];  // Array to store prime factors
    let divisor = 2;

    // Divide n by 2 until it's odd
    while (n % divisor === 0) {
        factors.push(divisor);
        n = n / divisor;
    }

    // Check for odd factors starting from 3
    divisor = 3;
    while (n > 1) {
        while (n % divisor === 0) {
            factors.push(divisor);
            n = n / divisor;
        }
        divisor += 2;  // Only check odd numbers
    }

    // If the input is a prime number, return the array with just n
    return factors.length > 0 ? factors : [n];
}

// Example usage:
let result = primeFactors(28);
console.log(result);  // Output: [2, 2, 7]
<IPython.core.display.Javascript object>