cs107-lecture-examples

Example codes used during Harvard CS107 lectures
git clone https://git.0xfab.ch/cs107-lecture-examples.git
Log | Files | Refs | README | LICENSE

03.py (467B)


      1 #!/usr/bin/env python3
      2 def Complex(r, i):
      3     """
      4     Construct a complex number
      5     Arguments:
      6         r: real part
      7         i: imaginary part
      8     """
      9     def implementation(method):
     10         if method.lower() == 'real':
     11             return r
     12         elif method.lower() == 'imag':
     13             return i
     14         elif method.lower() == 'string':
     15             return f"{r:e} + i{i:e}"
     16 
     17     return implementation
     18 
     19 
     20 z = Complex(1, 2)
     21 print(z('real'), z('imag'), z('string'))