Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes during review for China #22

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions classes/solutions/classes_ex02.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
""" A Solution For classes_ex02
Create a class called Family.
• The Family does not extend Person but rather should be composed
Expand Down
155 changes: 78 additions & 77 deletions classes/solutions/classes_ex03.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
""" A Solution For classes_ex03
Implement the necessary special methods so that the <, ==, and >
operators can be used with Family objects.
• The criteria for the methods should be the number of children.
• The following code could be used to test the methods.
myFamily = Family(mom, dad, kid1, kid2)
smiths = Family(mom, dad, kid1)
if (myFamily > smiths):
print("we have more kids than smiths")
if (myFamily == smiths):
print("families have same # of kids")
if (myFamily < smiths):
print("we have fewer kids than smiths")
"""


class Person:
default = "Unknown"

def __init__(self, name=default, age=1, gender=default):
self.name = name
self.age = age
self.gender = gender

def __str__(self):
return "{} {} {}".format(self.name, self.age, self.gender)


class Family:
def __init__(self, parent1, parent2, *children):
self.parent1 = parent1
self.parent2 = parent2
self.kids = list(children)

def add(self, child):
self.kids.append(child)

def __str__(self):
family = str(self.parent1) + " " + str(self.parent2)
for kid in self.kids:
family += "\n\t" + str(kid)
return family

def __gt__(self, other):
return len(self.kids) > len(other.kids)

def __eq__(self, other):
return len(self.kids) == len(other.kids)

def __lt__(self, other):
return len(self.kids) < len(other.kids)


def main():
mom = Person("Sally", 76, "F")
dad = Person("Arthur", 62, "M")
joel = Person("Joel", 34, "M")
judy = Person("Judy", 35, "F")
fam1 = Family(mom, dad, joel, judy)
fam2 = Family(mom, dad, joel)

if fam1 > fam2:
print("fam1 has more kids than fam2")

fam2.add(judy)

if fam1 == fam2:
print("families have == number of kids")

fam2.add(Person("Aiden", 13, "M"))

if fam1 < fam2:
print("fam1 has fewer kids than fam2")


if __name__ == "__main__":
main()
#!/usr/bin/env python3
""" A Solution For classes_ex03
Implement the necessary special methods so that the <, ==, and >
operators can be used with Family objects.
• The criteria for the methods should be the number of children.
• The following code could be used to test the methods.
myFamily = Family(mom, dad, kid1, kid2)
smiths = Family(mom, dad, kid1)
if (myFamily > smiths):
print("we have more kids than smiths")
if (myFamily == smiths):
print("families have same # of kids")
if (myFamily < smiths):
print("we have fewer kids than smiths")
"""


class Person:
default = "Unknown"

def __init__(self, name=default, age=1, gender=default):
self.name = name
self.age = age
self.gender = gender

def __str__(self):
return "{} {} {}".format(self.name, self.age, self.gender)


class Family:
def __init__(self, parent1, parent2, *children):
self.parent1 = parent1
self.parent2 = parent2
self.kids = list(children)

def add(self, child):
self.kids.append(child)

def __str__(self):
family = str(self.parent1) + " " + str(self.parent2)
for kid in self.kids:
family += "\n\t" + str(kid)
return family

def __gt__(self, other):
return len(self.kids) > len(other.kids)

def __eq__(self, other):
return len(self.kids) == len(other.kids)

def __lt__(self, other):
return len(self.kids) < len(other.kids)


def main():
mom = Person("Sally", 76, "F")
dad = Person("Arthur", 62, "M")
joel = Person("Joel", 34, "M")
judy = Person("Judy", 35, "F")
fam1 = Family(mom, dad, joel, judy)
fam2 = Family(mom, dad, joel)

if fam1 > fam2:
print("fam1 has more kids than fam2")

fam2.add(judy)

if fam1 == fam2:
print("families have == number of kids")

fam2.add(Person("Aiden", 13, "M"))

if fam1 < fam2:
print("fam1 has fewer kids than fam2")


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions classes/solutions/classes_ex04.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
""" A Solution For classes_ex04
Implement the following class hierarchy.
• Define a Worker class with a name, a salary, and number of years worked.
Expand Down
Empty file modified collections/solutions/collections_ex01.py
100644 → 100755
Empty file.
Empty file modified collections/solutions/collections_ex02.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions collections/solutions/collections_ex06.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
""" A Solution For collections_ex06

Rewrite collections_ex02 to count the frequency of each
Expand Down
38 changes: 19 additions & 19 deletions functions/solutions/functions_ex08.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/usr/bin/env python3
""" A Solution For functions_ex08
Write a function that returns a nested function.
• When the nested function is executed it should return the sum of two
integers.
• The two parameters should be passed to the outer function and used by
the inner function.
"""
def deliver():
def addthem(a, b):
return a + b
return addthem
f = deliver()
print(f(3, 4))
print(f(10, 20))
#!/usr/bin/env python3
""" A Solution For functions_ex08
Write a function that returns a nested function.
• When the nested function is executed it should return the sum of two
integers.
• The two parameters should be passed to the outer function and used by
the inner function.
"""


def deliver():
def addthem(a, b):
return a + b
return addthem


f = deliver()
print(f(3, 4))
print(f(10, 20))
44 changes: 22 additions & 22 deletions functions/solutions/functions_ex09.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#!/usr/bin/env python3
""" A Solution For functions_ex09
Write a function that returns a nested function.
• When the nested function is executed it should return the sum of two
integers.
• The two parameters should be passed to the outer function and used by
the inner function.
Re-write the above solution such that the outer function
receives no parameters, and the nested function is defined as taking
the two parameters.
"""
def deliver(a, b):
def addthem():
return a + b
return addthem
f = deliver(10, 20)
print(f())
#!/usr/bin/env python3
""" A Solution For functions_ex09
Write a function that returns a nested function.
• When the nested function is executed it should return the sum of two
integers.
• The two parameters should be passed to the outer function and used by
the inner function.

Re-write the above solution such that the outer function
receives no parameters, and the nested function is defined as taking
the two parameters.
"""


def deliver(a, b):
def addthem():
return a + b
return addthem


f = deliver(10, 20)
print(f())
18 changes: 9 additions & 9 deletions modules/solutions/modules_ex01_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
""" A Solution For modules_ex01 Part 2
Now, write a Python program in a separate file that imports
the module and calls the functions.
"""
from modules_ex01_functions import *
one()
two()
#!/usr/bin/env python3
""" A Solution For modules_ex01 Part 2
Now, write a Python program in a separate file that imports
the module and calls the functions.
"""
from modules_ex01_functions import *

one()
two()
32 changes: 16 additions & 16 deletions modules/solutions/modules_ex02_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env python3
""" A Solution For modules_ex02 Part 2
• In a separate file, create an application that imports
the module from this exercise that contains the function
and the module from the previous exercise.
• The application should be able to successfully call all of
the functions from both of the imported modules.
"""
import modules_ex01_functions
import modules_ex02_functions
modules_ex01_functions.one()
modules_ex02_functions.one()
modules_ex01_functions.two()
modules_ex02_functions.two()
#!/usr/bin/env python3
""" A Solution For modules_ex02 Part 2
• In a separate file, create an application that imports
the module from this exercise that contains the function
and the module from the previous exercise.
• The application should be able to successfully call all of
the functions from both of the imported modules.
"""
import modules_ex01_functions
import modules_ex02_functions

modules_ex01_functions.one()
modules_ex02_functions.one()

modules_ex01_functions.two()
modules_ex02_functions.two()
20 changes: 10 additions & 10 deletions modules/solutions/modules_ex03.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python3
""" A Solution For modules_ex03
Write a program that sorts its command line arguments.
"""
from sys import argv
argv.pop(0)
argv.sort()
for arg in argv:
print(arg)
#!/usr/bin/env python3
""" A Solution For modules_ex03
Write a program that sorts its command line arguments.
"""
from sys import argv

argv.pop(0)
argv.sort()
for arg in argv:
print(arg)
28 changes: 14 additions & 14 deletions modules/solutions/modules_ex04.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env python3
""" A Solution For modules_ex04
Write a program that sums the command line arguments.
• The program should print both the sum of the arguments
and the average value.
"""
from sys import argv
total = 0
for item in argv[1:]:
total += float(item)
print("Total:", total)
print("Average:", total / (len(argv) - 1))
#!/usr/bin/env python3
""" A Solution For modules_ex04
Write a program that sums the command line arguments.
• The program should print both the sum of the arguments
and the average value.
"""
from sys import argv

total = 0
for item in argv[1:]:
total += float(item)

print("Total:", total)
print("Average:", total / (len(argv) - 1))