added fifth week
This commit is contained in:
parent
22fe9fc96f
commit
9d3c30c2db
59
ex_17.py
Normal file
59
ex_17.py
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
|
||||||
|
|
||||||
|
class Person(object):
|
||||||
|
def __init__(self, name, age, birthplace):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
self.birthplace = birthplace
|
||||||
|
|
||||||
|
def about_myself(self):
|
||||||
|
print("I am {name}, {age} years old and was born in {birthplace}".format(**self.__dict__))
|
||||||
|
|
||||||
|
class Student(Person):
|
||||||
|
def __init__(self, name, age, birthplace, semester, studium):
|
||||||
|
Person.__init__(self, name, age, birthplace)
|
||||||
|
self.semester = semester
|
||||||
|
self.studium = studium
|
||||||
|
def about_myself(self):
|
||||||
|
Person.about_myself(self)
|
||||||
|
print("Also I am a student and I study {studium} in semester {semester}".format(**self.__dict__))
|
||||||
|
|
||||||
|
class Musician(Person):
|
||||||
|
def __init__(self, name, age, birthplace, instrument):
|
||||||
|
Person.__init__(self, name, age, birthplace)
|
||||||
|
self.instrument = instrument
|
||||||
|
def about_myself(self):
|
||||||
|
Person.about_myself(self)
|
||||||
|
print("Also I am a musician and I play {instrument}".format(**self.__dict__))
|
||||||
|
|
||||||
|
class FootballPlayer(Person):
|
||||||
|
def __init__(self, name, age, birthplace, football_team):
|
||||||
|
Person.__init__(self, name, age, birthplace)
|
||||||
|
self.football_team = football_team
|
||||||
|
def about_myself(self):
|
||||||
|
Person.about_myself(self)
|
||||||
|
print("Also I am a football player and I play in the {football_team}".format(**self.__dict__))
|
||||||
|
|
||||||
|
class StudenWhoHappensToPlayFootball(Student, FootballPlayer):
|
||||||
|
def __init__(self, name, age, birthplace, semester, studium, football_team):
|
||||||
|
Student.__init__(self, name, age, birthplace, semester, studium,)
|
||||||
|
FootballPlayer.__init__(self, name, age, birthplace, football_team)
|
||||||
|
|
||||||
|
def about_myself(self):
|
||||||
|
Student.about_myself(self)
|
||||||
|
print("Also I am a football player and I play in the {football_team}".format(**self.__dict__))
|
||||||
|
|
||||||
|
|
||||||
|
if( __name__ == "__main__"):
|
||||||
|
p = Person("Emil", 12, "Dresden")
|
||||||
|
s = Student("Thomas", 22, "Duggendorf", 3, "Bachelor of Dye")
|
||||||
|
m = Musician("Gandalf", 3612, "Mittelerde", "Picolo")
|
||||||
|
f = FootballPlayer("Özil", 30, "Anatolien", "FC BAYERN")
|
||||||
|
sf = StudenWhoHappensToPlayFootball("Peter", 54, "Pentling", 10, "Philosopy", "FC Hinterdupfingen")
|
||||||
|
|
||||||
|
l = [p, s, m, f, sf]
|
||||||
|
for i in l:
|
||||||
|
i.about_myself()
|
||||||
|
|
||||||
|
|
||||||
|
|
50
ex_18.py
Normal file
50
ex_18.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
from cmath import exp
|
||||||
|
|
||||||
|
class PolarComplex(object):
|
||||||
|
def __init__(self, r, phi):
|
||||||
|
self._r = r
|
||||||
|
self._phi = phi
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "{}({}, {})".format(PolarComplex.__name__, self._r, self._phi)
|
||||||
|
def __abs__(self):
|
||||||
|
return abs(self._r)
|
||||||
|
def __mul__(self, other):
|
||||||
|
if(isinstance(other, (int, float))):
|
||||||
|
return PolarComplex(self._r * other, self._phi)
|
||||||
|
if(isinstance(other, PolarComplex)):
|
||||||
|
return PolarComplex(self._r * other._r, self._phi + other._phi)
|
||||||
|
raise TypeError("*: cannot multiply {} and {}".format(type(other), type(self)))
|
||||||
|
def __truediv__(self, other):
|
||||||
|
if(isinstance(other, (int, float))):
|
||||||
|
return PolarComplex(self._r / other, self._phi)
|
||||||
|
if(isinstance(other, PolarComplex)):
|
||||||
|
return PolarComplex(self._r / other._r, self._phi - other._phi)
|
||||||
|
raise TypeError("/: cannot divide {} and {}".format(type(other), type(self)))
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self._r * exp(1j * self._phi))
|
||||||
|
|
||||||
|
def __rmul__(self):
|
||||||
|
if(isinstance(other, (int, float))):
|
||||||
|
return PolarComplex(self._r * other, self._phi)
|
||||||
|
if(isinstance(other, PolarComplex)):
|
||||||
|
return PolarComplex(self._r * other._r, self._phi + other._phi)
|
||||||
|
raise TypeError("*: cannot multiply {} and {}".format(type(other), type(self)))
|
||||||
|
|
||||||
|
def __rtruediv__(self, other):
|
||||||
|
if(isinstance(other, (int, float))):
|
||||||
|
return PolarComplex(other / self._r, -self._phi)
|
||||||
|
if(isinstance(other, PolarComplex)):
|
||||||
|
return PolarComplex(other._r / self._r, other._phi - self._phi)
|
||||||
|
raise TypeError("/: cannot divide {} and {}".format(type(other), type(self)))
|
||||||
|
|
||||||
|
if( __name__ == "__main__"):
|
||||||
|
c = PolarComplex(3, 0.3)
|
||||||
|
print(repr(c))
|
||||||
|
print(str(c))
|
||||||
|
print(repr(c / 3))
|
||||||
|
print(repr(c * 3))
|
||||||
|
z = PolarComplex(2, 0.2)
|
||||||
|
print(repr(c * z))
|
||||||
|
print(repr(c / z))
|
44
ex_19.py
Normal file
44
ex_19.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
class ext_int(int):
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
return int.__new__(cls, *args, **kwargs)
|
||||||
|
|
||||||
|
def is_prime(self):
|
||||||
|
if(abs(self) in (1, 0)):
|
||||||
|
return False
|
||||||
|
for i in range(2, abs(self)//2 + 1):
|
||||||
|
if(not self % i):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def is_positive(self):
|
||||||
|
return self > 0
|
||||||
|
def is_even(self):
|
||||||
|
return not self % 2
|
||||||
|
def is_odd(self):
|
||||||
|
return not self.is_even()
|
||||||
|
def get_inverse(self):
|
||||||
|
return 1 / self
|
||||||
|
|
||||||
|
class shitty_list(list):
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
return list.__new__(cls, *args, **kwargs)
|
||||||
|
|
||||||
|
def divide(self):
|
||||||
|
return (self[:len(self)//2], self[len(self)//2:])
|
||||||
|
|
||||||
|
|
||||||
|
if( __name__ == "__main__"):
|
||||||
|
print(ext_int(3).is_prime())
|
||||||
|
print(ext_int(4).is_prime())
|
||||||
|
print(ext_int(2).is_positive())
|
||||||
|
print(ext_int(-2).is_positive())
|
||||||
|
print(ext_int(2).is_even())
|
||||||
|
print(ext_int(3).is_even())
|
||||||
|
print(ext_int(2).is_odd())
|
||||||
|
print(ext_int(3).is_odd())
|
||||||
|
print(ext_int(2).get_inverse())
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(shitty_list("abcdefg").divide())
|
||||||
|
|
58
ex_20.py
Normal file
58
ex_20.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
|
||||||
|
class FibonacciIterator(object):
|
||||||
|
def __init__(self, max_n):
|
||||||
|
self._precalculated = {0: 1, 1: 1, 2: 2, 3: 3, 4: 5}
|
||||||
|
self._biggest = 4
|
||||||
|
self._max_n = max_n
|
||||||
|
self._i = 0
|
||||||
|
|
||||||
|
def get_biggest_pair(self):
|
||||||
|
return ((self._biggest - 1, self._precalculated[self._biggest - 1])
|
||||||
|
, (self._biggest, self._precalculated[self._biggest]))
|
||||||
|
def fibonacci_element(self, n):
|
||||||
|
if(not isinstance(n, int)):
|
||||||
|
raise TypeError("n must be integer")
|
||||||
|
if(0 <= n <= self._biggest):
|
||||||
|
return self._precalculated[n]
|
||||||
|
|
||||||
|
(n_start_minus_one, b), (n_start, a) = self.get_biggest_pair()
|
||||||
|
|
||||||
|
for i in range(n_start, n):
|
||||||
|
swp = a
|
||||||
|
a += b
|
||||||
|
b = swp
|
||||||
|
self._precalculated[n] = a
|
||||||
|
|
||||||
|
self._biggest = n
|
||||||
|
return a
|
||||||
|
|
||||||
|
def __contains__(self, n):
|
||||||
|
if(n > self._max_n):
|
||||||
|
raise IndexError("{} > max_n({})".format(n, self._max_n))
|
||||||
|
return n <= self._biggest
|
||||||
|
|
||||||
|
def __getitem__(self, n):
|
||||||
|
if(n > self._max_n):
|
||||||
|
raise IndexError("{} > max_n({})".format(n, self._max_n))
|
||||||
|
return self.fibonacci_element(n)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
self._i = -1
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if(self._i > self._max_n):
|
||||||
|
raise StopIteration()
|
||||||
|
self._i += 1
|
||||||
|
return self.fibonacci_element(self._i)
|
||||||
|
|
||||||
|
if( __name__ == "__main__"):
|
||||||
|
i = FibonacciIterator(20)
|
||||||
|
|
||||||
|
for j in i:
|
||||||
|
print(j)
|
||||||
|
|
||||||
|
print(15 in i)
|
||||||
|
print(i[14])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user