Problem Set Week 3: Python Basics
Problem Set 3: Python Fundamentals
This problem set will help you practice the Python basics covered in Week 3.
Exercise 1: Mars Weather Station đ
Youâre programming a weather station for the Mars colony! Write a program that:
- Asks for the current Mars temperature (in Celsius)
- Converts to Earth equivalents (Fahrenheit and Kelvin)
- Determines if itâs a ânice dayâ on Mars (above -60°C is considered nice!)
- Checks if COâ would be solid (dry ice forms below -78.5°C)
- Warns if spacesuits are mandatory (below -50°C)
- Celebrates if itâs warm enough for liquid water (above 0°C - extremely rare!)
Fun fact: Mars temperatures range from -143°C to 35°C. Test your program with -80°C (typical Mars winter)
Exercise 2: Crypto Investment Simulator đ°
Youâve discovered a time machine and want to simulate Bitcoin investments! Write a program that:
- Asks how much money you would have invested (in USD)
- Asks what year you would have bought (2010-2023)
- Uses these historical Bitcoin prices:
- 2010: $0.10
- 2011: $1
- 2013: $100
- 2015: $250
- 2017: $1,000
- 2019: $4,000
- 2021: $30,000
- 2023: $25,000
- Calculate how many Bitcoin you could have bought
- Show the value at each subsequent year
- Display your best and worst decisions
- Add dramatic messages like âYouâre a millionaire!â or âHODL! đđâ
Reality check: $100 invested in 2010 = 1000 BTC = $25 million in 2023!
Exercise 3: Secret Agent Code Generator đľď¸
Youâre a secret agent who needs prime numbers for encryption! Write a program that:
- Asks for your agent number (any positive integer)
- Checks if your agent number is âprimeâ (secure) or âcompositeâ (compromised)
- If compromised, finds who can decode it (smallest divisor = enemy agent number)
- Generates your âsafe communication channelsâ (all primes less than your number)
- Creates a âsecurity scoreâ (percentage of numbers below yours that are prime)
- Assigns code names based on primality:
- Prime: âShadow Wolfâ
- Even composite: âDouble Agentâ
- Odd composite: âRogue Operatorâ
Mission briefing: Agent 007 is prime and secure. Agent 008 is compromised by Agent 002!
Exercise 4: Debugging Practice
The following programs contain errors. Identify and fix them:
a) Area of a circle
import math
radius = input("Enter radius: ")
area = math.pi * radius ^ 2
print(f"Area = area")
b) Quadratic formula
# Solve ax^2 + bx + c = 0
a = 1, b = -5, c = 6
discriminant = b**2 - 4ac
x1 = -b + discriminant**0.5 / 2*a
x2 = -b - discriminant**0.5 / 2*a
print("Solutions:", x1, x2)
c) Fibonacci sequence
# Print first 10 Fibonacci numbers
a = 0
b = 1
count = 0
while count < 10
print(a)
a = b
b = a + b
count += 1
Exercise 5: Hogwarts Grade Calculator âĄ
Youâre calculating grades at Hogwarts School! Create a program that:
- Asks for scores in three subjects:
- Defense Against Dark Arts (0-100)
- Potions (0-100)
- Transfiguration (0-100)
- Asks for Quidditch performance (0-100, counts as homework)
- Asks for House participation points (0-100)
- Calculate weighted average (same weights as before)
- Assign magical grades:
- Outstanding (O): 90-100 âYouâre the chosen one!â
- Exceeds Expectations (E): 80-89 âHermione would be proud!â
- Acceptable (A): 70-79 âYou pass, muggle!â
- Poor (P): 60-69 âNeeds more Felix Felicisâ
- Dreadful (D): 50-59 âTroll in the dungeon!â
- Troll (T): Below 50 âWorse than Crabbe and Goyleâ
- Award or deduct house points based on performance
- Determine if student makes the Quidditch team (needs O or E)
Exercise 6: Dragonâs Treasure Hunt đ
A dragon has hidden treasure! Create an adventure game:
- The dragon thinks of a number between 1 and 100 (the treasure location)
- You have 7 attempts before the dragon wakes up!
- After each guess, the dragon growls hints:
- Too high: âThe treasure lies deeper in the caveâŚâ
- Too low: âClimb higher, adventurerâŚâ
- Very close (within 5): âThe dragonâs breathing gets heavierâŚâ
- Very far (>30 away): âYou feel lost in the darknessâŚâ
- Track your path through the cave (all guesses)
- Victory: Show your bravery rating (fewer attempts = braver)
- 1-2 attempts: âDragon Slayer!â
- 3-4 attempts: âTreasure Hunter!â
- 5-6 attempts: âLucky Adventurer!â
- 7 attempts: âNarrow Escape!â
- Defeat: The dragon wakes! Show where the treasure was
- Calculate your âexplorer efficiencyâ (how close your average guess was)
Exercise 7: Pyramid of Power đş
Build the ancient Pyramid of Power where each stone has magical properties!
- Ask the architect how many levels to build (1-15)
- Each stoneâs power equals the sum of the two stones above it
- The pyramid reveals magical patterns:
- Each row sum is a power of 2 (show this!)
- The diagonal shows counting numbers
- Row 5 contains the sacred sequence: 1, 4, 6, 4, 1
- Color code the output (using text):
- Powers of 2: Show with [brackets]
- Perfect squares: Show with asterisks
- Others: Normal display
- Calculate the âtotal pyramid powerâ (sum of all numbers)
- Reveal if itâs a âPerfect Pyramidâ (rows = 7 or 11, considered lucky)
Ancient wisdom: Row 7 contains 1, 6, 15, 20, 15, 6, 1 - all divisible by row number 7!
Exercise 8: Alien Message Decoder đ˝
Youâve intercepted an alien transmission! Analyze it:
- Ask for the intercepted message
- Decode its properties:
- Word count (âtransmission unitsâ)
- Vowel frequency (âharmonic resonanceâ)
- Consonant count (âdissonance factorâ)
- Numbers found (âcoordinate markersâ)
- Space gaps (âquantum separatorsâ)
- Identify the âpower wordâ (longest) and âkey wordâ (shortest)
- Reverse engineer the message (print it backwards)
- Create a transmission pyramid for analysis
- Determine the message type:
-
50% vowels: âPeaceful greetingâ
-
5 numbers: âStar coordinatesâ
- All caps: âDISTRESS SIGNAL!â
- Contains âhelpâ: âRescue requestâ
- Otherwise: âUnknown intentâ
-
- Calculate âtranslation confidenceâ (% of recognizable patterns)
Test transmission: âHELP 42 we come in peace from ZORG-7â
Submission Instructions
- Submit a single Python file
problem_set_3.py
with all solutions - Each exercise should be clearly marked with comments
- Include test cases showing your programs work correctly
- Due: Before Week 4 lecture