Skip to content

Commit

Permalink
sol(cs50-python): pset4
Browse files Browse the repository at this point in the history
  • Loading branch information
jfvillablanca committed Dec 4, 2023
1 parent f33d07e commit 6855134
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions cs50-python/pset4/adieu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/python/2022/psets/4/adieu/
12 changes: 12 additions & 0 deletions cs50-python/pset4/adieu/adieu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import inflect

p = inflect.engine()

names = []
while True:
try:
names.append(input("Name: "))
except EOFError:
break

print(f"\nAdieu, adieu, to {p.join(names)}")
1 change: 1 addition & 0 deletions cs50-python/pset4/bitcoin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/python/2022/psets/4/bitcoin/
22 changes: 22 additions & 0 deletions cs50-python/pset4/bitcoin/bitcoin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys

import requests

if len(sys.argv) != 2:
sys.exit("Missing command-line argument")

try:
bitcoin = float(sys.argv[1])
except ValueError:
sys.exit("Command-line argument is not a number")


try:
response = requests.get(
"https://api.coindesk.com/v1/bpi/currentprice.json", timeout=1
)
except (requests.RequestException, requests.Timeout) as error:
sys.exit(f"Request error:\n{error}")

bitcoin_rate = response.json()["bpi"]["USD"]["rate_float"]
print(f"${bitcoin_rate * bitcoin:,.4f}")
1 change: 1 addition & 0 deletions cs50-python/pset4/emojize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/python/2022/psets/4/emojize/
4 changes: 4 additions & 0 deletions cs50-python/pset4/emojize/emojize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from emoji import emojize

emoji_input = input("Input: ")
print(f"Output: {emojize(emoji_input, language='alias')}")
1 change: 1 addition & 0 deletions cs50-python/pset4/figlet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/python/2022/psets/4/figlet/
34 changes: 34 additions & 0 deletions cs50-python/pset4/figlet/figlet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys
from random import choice

from pyfiglet import Figlet


def main():
figlet = Figlet()

if len(sys.argv) != 1 and len(sys.argv) != 3:
print("Invalid usage")
sys.exit(1)
if len(sys.argv) == 3:
is_invalid_font_flag = sys.argv[1] != "-f" and sys.argv[1] != "--font"
is_invalid_font_name = sys.argv[2] not in figlet.getFonts()
if is_invalid_font_flag or is_invalid_font_name:
print("Invalid usage")
sys.exit(1)

font = get_font_choice(figlet)
my_string = input("Input: ")
figlet.setFont(font=font)

print(figlet.renderText(my_string))


def get_font_choice(figlet):
if len(sys.argv) == 3:
return sys.argv[2]
return choice(figlet.getFonts())


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions cs50-python/pset4/game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/python/2022/psets/4/game/
24 changes: 24 additions & 0 deletions cs50-python/pset4/game/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import random

while True:
try:
level = int(input("Level: "))
if level > 0:
break
except ValueError:
pass

answer = random.randint(1, level)

while True:
try:
guess = int(input("Guess: "))
if guess < answer:
print("Too small!")
elif guess > answer:
print("Too large!")
else:
print("Just right!")
break
except ValueError:
pass
1 change: 1 addition & 0 deletions cs50-python/pset4/professor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/python/2022/psets/4/professor/
49 changes: 49 additions & 0 deletions cs50-python/pset4/professor/professor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import random


def main():
score = 0
level = get_level()
problems = [generate_integer(level) for _ in range(10)]

for problem in problems:
tries = 0
while True:
print(f"{problem[0]} + {problem[1]} = ", end="")
try:
answer = int(input())
if problem[0] + problem[1] != answer:
raise ValueError
score += 1
break
except ValueError:
print("EEE")
tries += 1
if tries == 3:
print(f"{problem[0]} + {problem[1]} = {problem[0] + problem[1]}")
break

print(f"Score: {score}")


def get_level():
while True:
try:
level = int(input("Level: "))
if level in range(1, 4):
return level
except ValueError:
pass


def generate_integer(level):
if level == 1:
return random.randint(0, 9), random.randint(0, 9)
elif level == 2:
return random.randint(10, 99), random.randint(10, 99)
else:
return random.randint(100, 999), random.randint(100, 999)


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@
certifi
cachelib
werkzeug

# requirements for cs50-python/pset4/emojize
emoji

# requirements for cs50-python/pset4/figlet
pyfiglet

# requirements for cs50-python/pset4/adieu
inflect
pydantic
]);
}
];
Expand Down

0 comments on commit 6855134

Please sign in to comment.