{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Lesson 3.3.3 (Popcorn) - Python\n", "- Hack: Prompt the user to enter a number n, initialize total_sum to 0, use a loop to iterate from 1 to n, add each number to total_sum, and print" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Python Program to calculate total sum from 1 to n\n", "n = int(input(\"Enter a number n: \")) # Prompt user for input\n", "total_sum = 0 # Initialize total_sum to 0\n", "\n", "# Loop to add numbers from 1 to n\n", "for i in range(1, n + 1):\n", " total_sum += i # Add each number to total_sum\n", "\n", "# Print the total sum\n", "print(\"Total sum from 1 to\", n, \"is:\", total_sum)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lesson 3.3.4 (Popcorn) - JavaScript\n", "- Hack: Create a function that uses 4 of the 5 basic arithmetic operations and returns the number 32" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "html" } }, "outputs": [], "source": [ "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.3 Python Hacks!!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hack #1 (main) - python:\n", "- Define a List of Integers, Compute the Sum, and Print It" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define a list of integers\n", "integers = [3, 5, 7, 2, 8]\n", "\n", "# Compute the sum of all integers\n", "sum_of_integers = sum(integers)\n", "\n", "# Print the sum\n", "print(f\"The sum of the integers is: {sum_of_integers}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hack #2 (main) - python: \n", "- Prompt User to Enter Price per Bag and Number of Bags, Calculate Total Cost" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Prompt user for price per bag and number of bags\n", "price_per_bag = float(input(\"Enter the price per bag of popcorn: \"))\n", "num_bags = int(input(\"Enter the number of bags you want to buy: \"))\n", "\n", "# Calculate the total cost\n", "total_cost = price_per_bag * num_bags\n", "\n", "# Print the total cost\n", "print(f\"The total cost is: ${total_cost:.2f}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hack #3 (Main) - python: \n", "- Calculate Sum of Numbers from 1 to n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Prompt the user to enter a number n\n", "n = int(input(\"Enter a number: \"))\n", "\n", "# Initialize total_sum\n", "total_sum = 0\n", "\n", "# Use a loop to iterate from 1 to n\n", "for i in range(1, n + 1):\n", " total_sum += i\n", "\n", "# Print the total_sum\n", "print(f\"The sum of numbers from 1 to {n} is: {total_sum}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework Hack #1 - python: \n", "- Compute Arithmetic Mean and Median\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Function to compute mean and median of a list of integers\n", "def mean_and_median(integers):\n", " mean = sum(integers) / len(integers)\n", " sorted_integers = sorted(integers)\n", " \n", " # Compute median\n", " if len(sorted_integers) % 2 == 0:\n", " median = (sorted_integers[len(integers)//2 - 1] + sorted_integers[len(integers)//2]) / 2\n", " else:\n", " median = sorted_integers[len(integers)//2]\n", " \n", " # Print results\n", " print(f\"Mean: {mean}\")\n", " print(f\"Median: {median}\")\n", "\n", "# Example usage\n", "integers = [7, 2, 5, 9, 4]\n", "mean_and_median(integers)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework Hack #2: Python\n", "- Collatz Sequence Function\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Function to generate the Collatz sequence\n", "def collatz(a):\n", " sequence = [a]\n", " \n", " while a != 1:\n", " if a % 2 == 0:\n", " a = a // 2\n", " else:\n", " a = a * 3 + 1\n", " sequence.append(a)\n", " \n", " return sequence\n", "\n", "# Example usage\n", "a = int(input(\"Enter a positive integer: \"))\n", "collatz_sequence = collatz(a)\n", "print(f\"Collatz sequence starting at {a}: {collatz_sequence}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.3 Java Hacks!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Popcorn Hack 1 Easy - Java\n", "- Create a function that uses 4 of the 5 basic arithmetic operations and returns the number 32 as the answer." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%% js \n", "function basicOperations() {\n", " let sum = 20 + 12;\n", " let difference = 40 - 8;\n", " let product = 8 * 4;\n", " let quotient = 64 / 2;\n", "\n", " // Since we need to return 32, we will use addition.\n", " return sum;\n", "}\n", "\n", "console.log(`The result is: ${basicOperations()}`);\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Popcorn Hack 2: Medium Java\n", "- Make a function that lets you make a sandwich. Ask for different ingredients and at the end give the sandwich a name." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js\n", "function makeSandwich() {\n", " let breadType = prompt(\"What type of bread do you want?\");\n", " let mainIngredient = prompt(\"What is the main ingredient?\");\n", " let condiment = prompt(\"What kind of condiment would you like?\");\n", " let sandwichName = prompt(\"What would you like to name your sandwich?\");\n", "\n", " let sandwich = `Your ${sandwichName} is made with ${breadType} bread, filled with ${mainIngredient}, and topped with ${condiment}.`;\n", " \n", " return sandwich;\n", "}\n", "\n", "console.log(makeSandwich());\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Popcorn Hack 3: Hard Java\n", "- Create a “choose-your-own-path” type of game. There should be 2-3 different storylines the user can choose from." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js\n", "function chooseYourPath() {\n", " let choice1 = prompt(\"You see a fight at school. Do you (a) break it up or (b) mind your own business?\");\n", "\n", " if (choice1 === \"a\") {\n", " let choice2 = prompt(\"You decide to break it up. Do you (a) call a teacher or (b) jump in yourself?\");\n", " if (choice2 === \"a\") {\n", " return \"You call a teacher, and they resolve the situation. You're safe, but everyone thanks you!\";\n", " } else if (choice2 === \"b\") {\n", " return \"You jump into the fight and get hurt. Bad idea!\";\n", " } else {\n", " return \"Invalid choice!\";\n", " }\n", " } else if (choice1 === \"b\") {\n", " let choice2 = prompt(\"You mind your own business. Do you (a) walk away or (b) watch from a distance?\");\n", " if (choice2 === \"a\") {\n", " return \"You walk away and avoid the chaos. Smart move!\";\n", " } else if (choice2 === \"b\") {\n", " return \"You watch from a distance and see a teacher arrive to break it up.\";\n", " } else {\n", " return \"Invalid choice!\";\n", " }\n", " } else {\n", " return \"Invalid choice!\";\n", " }\n", "}\n", "\n", "console.log(chooseYourPath());\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework Hack 1: (java) Compute GCD and LCM\n", "- Write a function that takes two variables and returns both the Greatest Common Divisor (GCD) and Least Common Multiple (LCM)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js\n", "function gcdAndLcm(a, b) {\n", " // Helper function to compute the GCD\n", " function gcd(x, y) {\n", " while (y !== 0) {\n", " let temp = y;\n", " y = x % y;\n", " x = temp;\n", " }\n", " return x;\n", " }\n", "\n", " // Compute GCD\n", " let gcdResult = gcd(a, b);\n", "\n", " // Compute LCM\n", " let lcmResult = (a * b) / gcdResult;\n", "\n", " // Return the results as an object\n", " return { GCD: gcdResult, LCM: lcmResult };\n", "}\n", "\n", "console.log(gcdAndLcm(24, 36));\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework Hack 2: (java) Prime Factors\n", "- Write a function that takes a positive integer and returns an array of its prime factors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js \n", "function primeFactors(n) {\n", " let factors = [];\n", " \n", " // Start dividing by 2\n", " while (n % 2 === 0) {\n", " factors.push(2);\n", " n = n / 2;\n", " }\n", "\n", " // Try odd numbers from 3 onwards\n", " for (let i = 3; i <= Math.sqrt(n); i += 2) {\n", " while (n % i === 0) {\n", " factors.push(i);\n", " n = n / i;\n", " }\n", " }\n", "\n", " // If n is still a prime number greater than 2\n", " if (n > 2) {\n", " factors.push(n);\n", " }\n", "\n", " return factors;\n", "}\n", "\n", "console.log(`Prime factors of 84 are: ${primeFactors(84)}`);\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }