diff --git a/classes/solutions/classes_ex02.py b/classes/solutions/classes_ex02.py index 5ab650f..0180723 100755 --- a/classes/solutions/classes_ex02.py +++ b/classes/solutions/classes_ex02.py @@ -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 diff --git a/classes/solutions/classes_ex03.py b/classes/solutions/classes_ex03.py index 5e2aecc..0228d34 100755 --- a/classes/solutions/classes_ex03.py +++ b/classes/solutions/classes_ex03.py @@ -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() diff --git a/classes/solutions/classes_ex04.py b/classes/solutions/classes_ex04.py index a112373..6244187 100755 --- a/classes/solutions/classes_ex04.py +++ b/classes/solutions/classes_ex04.py @@ -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. diff --git a/collections/solutions/collections_ex01.py b/collections/solutions/collections_ex01.py old mode 100644 new mode 100755 diff --git a/collections/solutions/collections_ex02.py b/collections/solutions/collections_ex02.py old mode 100644 new mode 100755 diff --git a/collections/solutions/collections_ex06.py b/collections/solutions/collections_ex06.py index b676bab..3080c8b 100755 --- a/collections/solutions/collections_ex06.py +++ b/collections/solutions/collections_ex06.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 """ A Solution For collections_ex06 Rewrite collections_ex02 to count the frequency of each diff --git a/functions/solutions/functions_ex08.py b/functions/solutions/functions_ex08.py index 1178993..2de4cb7 100755 --- a/functions/solutions/functions_ex08.py +++ b/functions/solutions/functions_ex08.py @@ -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)) diff --git a/functions/solutions/functions_ex09.py b/functions/solutions/functions_ex09.py index d3a74b0..e9439bc 100755 --- a/functions/solutions/functions_ex09.py +++ b/functions/solutions/functions_ex09.py @@ -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()) diff --git a/modules/solutions/modules_ex01_test.py b/modules/solutions/modules_ex01_test.py index 03a2443..c93e3c3 100755 --- a/modules/solutions/modules_ex01_test.py +++ b/modules/solutions/modules_ex01_test.py @@ -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() diff --git a/modules/solutions/modules_ex02_test.py b/modules/solutions/modules_ex02_test.py index 2c820ad..330d81a 100755 --- a/modules/solutions/modules_ex02_test.py +++ b/modules/solutions/modules_ex02_test.py @@ -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() diff --git a/modules/solutions/modules_ex03.py b/modules/solutions/modules_ex03.py index 013e07f..97452c8 100755 --- a/modules/solutions/modules_ex03.py +++ b/modules/solutions/modules_ex03.py @@ -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) diff --git a/modules/solutions/modules_ex04.py b/modules/solutions/modules_ex04.py index 0bfd34d..33312f4 100755 --- a/modules/solutions/modules_ex04.py +++ b/modules/solutions/modules_ex04.py @@ -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))