CS50 Introduction To Python Programming


The CS50's Introduction to Programming with Python is a course offered by Harvard University, focusing on teaching Python programming.
This course, led by Harvard Professor David J. Malan, is designed for students with or without prior programming experience who want to learn Python specifically.
It covers topics such as functions, variables, conditionals, loops, exceptions, libraries, unit tests, file I/O, regular expressions, and object-oriented programming.
The course provides hands-on practice opportunities with exercises inspired by real-world programming problems. It can be taken independently or as a precursor to the more general CS50x course,
which covers computer science and programming with C, Python, SQL, and JavaScript.
The CS50's Introduction to Programming with Python course is available online through platforms like Harvard Online, edX, and YouTube, offering a comprehensive introduction to Python programming


Problem Set 3 Solutions

Seasons of Love


In a file called seasons.py, implement a program that prompts the user for their date of birth in YYYY-MM-DD format and then sings prints how old they are in minutes, rounded to the nearest integer, using English words instead of numerals, just like the song from Rent, without any and between words.


Source Code

        

seasons

from datetime import date import inflect import sys import operator p = inflect.engine() def main(): try: dob = input("Date of Birth: ") # date.fromisoformat(dob) will check whether dob is a valid date or not difference = operator.sub(date.today(), date.fromisoformat(dob)) print(convert(difference.days)) except ValueError: sys.exit("Invalid date") def convert(time): minutes = time * 24 * 60 return f"{(p.number_to_words(minutes, andword='')).capitalize()} minutes" if __name__ == "__main__": main()

test_seasons

from seasons import convert def test_date(): assert convert(10477) == "Fifteen million, eighty-six thousand, eight hundred eighty minutes" assert convert(365) == "Five hundred twenty-five thousand, six hundred minutes"

Cookie Jar


Source Code

            

jar

class Jar: def __init__(self, capacity=12): self.capacity = capacity self.size = 0 def __str__(self): return "🍪" * self.size def deposit(self, n): if self.size + n > self.capacity: raise ValueError("Deposit error") self.size = self.size + n def withdraw(self, n): if n > self.size: raise ValueError("Withdraw error") self.size = self.size - n @property def capacity(self): return self._capacity @capacity.setter def capacity(self, capacity): if capacity < 1: raise ValueError("@capacity.setter error") self._capacity = capacity @property def size(self): return self._size @size.setter def size(self, size): if size > self.capacity: raise ValueError("@size.setter error") self._size = size def main(): jar = Jar() print(jar) jar.deposit(1) print(jar) jar.deposit(3) print(jar) jar.withdraw(2) print(jar) if __name__ == "__main__": main()

test_jar

import pytest from jar import Jar def test_init(): jar = Jar() def test_str(): jar = Jar() assert str(jar) == "" jar.deposit(1) assert str(jar) == "🍪" jar.deposit(11) assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪" def test_deposit(): jar = Jar() jar.deposit(1) assert jar.size == 1 with pytest.raises(ValueError): jar.deposit(20) def test_withdraw(): jar = Jar() jar.deposit(4) jar.withdraw(1) assert jar.size == 3 with pytest.raises(ValueError): jar.withdraw(100)

CS50 Shirtificate


Source Code

from fpdf import FPDF

            class PDF(FPDF):
                def header(self):
                    self.image("./shirtificate.png", 10, 70, 190)
                    self.set_font("helvetica", "", 48)
                    self.cell(0, 57, "CS50 Shirtificate", align="C")
                    self.ln(20)
            
            
            def main():
                name = input("Name: ")
                shirt(name)
            
            
            def shirt(s):
                pdf = PDF()
                pdf.add_page(orientation="portrait", format="a4")
                pdf.set_font("helvetica", size=24)
                pdf.set_text_color(255,255,255)
                pdf.cell(0, 213, f"{s} took CS50", align="C")
                pdf.output("shirtificate.pdf")
            
            
            if __name__ == "__main__":
                main()