Summary | 3.1 | 3.2 | 3.3 | 3.4 | 3.5 | 3.6 | 3.7 | 3.10 | Relevancy |
Homeworks and Popcorn Hacks for Lessons
Homeworks and Hacks for Lesson 3.2
Lesson 3.2 Popcorn Hacks and Homework
Completed Quizziz (100%!!)
Popcorn Hack #1
Javascript Version
%%js
const fruitKeys = { 1: "bananas", 2: "apples", 3: "pears" };
const bestDictionaryEver = { "bananas": 1, "apples": 2, "pears": 3 };
function accessFruitByNumber(number) {
const fruit = fruitKeys[number];
if (fruit) {
console.log(`The count of ${fruit} is ${bestDictionaryEver[fruit]}`);
} else {
console.log(`Key ${number} does not correspond to a valid fruit.`);
}
}
// Example usage
accessFruitByNumber(3); // Access value using number 3, which corresponds to 'pears'
<IPython.core.display.Javascript object>
Python Version
fruit_keys = {1: "bananas", 2: "apples", 3: "pears"}
bestDictionaryEver = {"bananas": 1, "apples": 2, "pears": 3}
def access_fruit_by_number(number):
fruit = fruit_keys.get(number)
if fruit:
print(f"The count of {fruit} is {bestDictionaryEver[fruit]}")
else:
print(f"Key {number} does not correspond to a valid fruit.")
access_fruit_by_number(3)
The count of pears is 3
Popcorn Hack #2
Javascript Version
%%js
function calculator(num1, operator, num2) {
switch(operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
return "Invalid operator";
}
}
let num1 = 10;
let num2 = 5;
let operator = '+';
console.log(`${num1} ${operator} ${num2} = ${calculator(num1, operator, num2)}`);
<IPython.core.display.Javascript object>
Python Version
def calculator(num1, operator, num2):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
return num1 / num2
else:
return "Invalid operator"
Popcorn Hack #3
Javascript Version
%%js
function repeatStrings(stringList, n) {
return stringList.map(string => string.repeat(n));
}
const strings = ["soccer", "football", "baseball"];
const n = 3;
const result = repeatStrings(strings, n);
console.log(result);
<IPython.core.display.Javascript object>
Python Version
def repeat_strings(string_list, n):
return [string * n for string in string_list]
strings = ["soccer", "football", "baseball"]
n = 3
result = repeat_strings(strings, n)
print(result)
['soccersoccersoccer', 'footballfootballfootball', 'baseballbaseballbaseball']
Popcorn Hack #4
Javascript Version
%%js
function compareSets(set1, set2) {
for (let value of set2) {
if (set1.has(value)) {
return true;
}
}
return false;
}
const set1 = new Set([1, 2, 3, 4, 5]);
const set2 = new Set([6, 7, 3]);
const result = compareSets(set1, set2);
console.log(result);
<IPython.core.display.Javascript object>
Python Version
def compare_sets(set1, set2):
return any(value in set1 for value in set2)
set1 = {1, 2, 3, 4, 5}
set2 = {6, 7, 3}
result = compare_sets(set1, set2)
print(result)
True
Homework for 3.2
Python Homework: Profile Information
name = "Mirabelle Anderson"
age = 16
city = "San Diego"
favorite_color = "Pink"
profile = {
"name": name,
"age": age,
"city": city,
"favorite_color": favorite_color
}
hobbies = ["flag football", "soccer", "coding" , "snowboarding"]
print("Hobbies List:", hobbies)
profile["hobbies"] = hobbies
print("Updated Profile:", profile)
chosen_hobby = "soccer"
has_hobby = True
print(f'Is "{chosen_hobby}" available today? {has_hobby}')
total_hobbies = len(hobbies)
print(f"I have {total_hobbies} hobbies.")
favorite_hobbies = ("soccer", "snowboarding")
print("Favorite Hobbies Tuple:", favorite_hobbies)
skills = {"Leadership", "Communication", "Teamwork"}
print("Skills Set:", skills)
new_skill = None
print("New Skill:", new_skill)
hobby_cost = 5.0
skill_cost = 10.0
total_cost = (total_hobbies * hobby_cost) + (len(skills) * skill_cost)
print(f"Total cost to develop hobbies and skills: ${total_cost:.2f}")
Hobbies List: ['flag football', 'soccer', 'coding', 'snowboarding']
Updated Profile: {'name': 'Mirabelle Anderson', 'age': 16, 'city': 'San Diego', 'favorite_color': 'Pink', 'hobbies': ['flag football', 'soccer', 'coding', 'snowboarding']}
Is "soccer" available today? True
I have 4 hobbies.
Favorite Hobbies Tuple: ('soccer', 'snowboarding')
Skills Set: {'Communication', 'Leadership', 'Teamwork'}
New Skill: None
Total cost to develop hobbies and skills: $50.00
BONUS: Javascript Version
%%js
let name = "Mirabelle Anderson";
let age = 16;
let city = "San Diego";
let favorite_color = "Pink";
let profile = {
name: name,
age: age,
city: city,
favorite_color: favorite_color
};
let hobbies = ["flag football", "soccer", "coding", "snowboarding"];
console.log("Hobbies List:", hobbies);
profile.hobbies = hobbies;
console.log("Updated Profile:", profile);
let chosen_hobby = "soccer";
let has_hobby = true;
console.log(`Is "${chosen_hobby}" available today? ${has_hobby}`);
let total_hobbies = hobbies.length;
console.log(`I have ${total_hobbies} hobbies.`);
let favorite_hobbies = ["soccer", "snowboarding"];
console.log("Favorite Hobbies Array:", favorite_hobbies);
let skills = new Set(["Leadership", "Communication", "Teamwork"]);
console.log("Skills Set:", skills);
let new_skill = null;
console.log("New Skill:", new_skill);
let hobby_cost = 5.0;
let skill_cost = 10.0;
let total_cost = (total_hobbies * hobby_cost) + (skills.size * skill_cost);
console.log(`Total cost to develop hobbies and skills: $${total_cost.toFixed(2)}`);
<IPython.core.display.Javascript object>