From 151377cb5049184f0551a4a8a74222e1fdc2aa07 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Wed, 15 Jul 2020 19:55:12 -0700 Subject: [PATCH 1/3] Add Count Magical exercise (created by Ke Hao Chen) --- .../chapter11/practice/count_magical.py | 8 ++++++++ .../chapter11/solutions/count_magical.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 2_intermediate/chapter11/practice/count_magical.py create mode 100644 2_intermediate/chapter11/solutions/count_magical.py diff --git a/2_intermediate/chapter11/practice/count_magical.py b/2_intermediate/chapter11/practice/count_magical.py new file mode 100644 index 00000000..eadbf427 --- /dev/null +++ b/2_intermediate/chapter11/practice/count_magical.py @@ -0,0 +1,8 @@ +# Write a function called count_magical +# that returns the number of even numbers +# in a given list. In the function, +# if the number of evens is greater than +# half of the length of the list, print "Magical" +# Else, print "Not Magical" + +# write code here diff --git a/2_intermediate/chapter11/solutions/count_magical.py b/2_intermediate/chapter11/solutions/count_magical.py new file mode 100644 index 00000000..77f23849 --- /dev/null +++ b/2_intermediate/chapter11/solutions/count_magical.py @@ -0,0 +1,19 @@ +# Write a function called count_magical +# that returns the number of even numbers +# in a given list. In the function, +# if the number of evens is greater than +# half of the length of the list, print "Magical" +# Else, print "Not Magical" + +def count_magical(my_list): + number_of_evens = 0 + for n in my_list: + if n % 2 == 0: + number_of_evens += 1 + + if number_of_evens > len(my_list) / 2: + print("Magical") + else: + print("Not Magical") + + return number_of_evens From 8330f3f900ac0bb18319dbb41bc9fa35037bb75d Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Wed, 15 Jul 2020 20:00:27 -0700 Subject: [PATCH 2/3] Add main function to test count_magical --- .../chapter11/practice/count_magical.py | 5 +++++ .../chapter11/solutions/count_magical.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/2_intermediate/chapter11/practice/count_magical.py b/2_intermediate/chapter11/practice/count_magical.py index eadbf427..114698b2 100644 --- a/2_intermediate/chapter11/practice/count_magical.py +++ b/2_intermediate/chapter11/practice/count_magical.py @@ -4,5 +4,10 @@ # if the number of evens is greater than # half of the length of the list, print "Magical" # Else, print "Not Magical" +# +# Write a function called main which tests +# the count_magical function on at least +# 3 different lists of integers. Use the main +# function to test count_magical by calling main(). # write code here diff --git a/2_intermediate/chapter11/solutions/count_magical.py b/2_intermediate/chapter11/solutions/count_magical.py index 77f23849..02562be2 100644 --- a/2_intermediate/chapter11/solutions/count_magical.py +++ b/2_intermediate/chapter11/solutions/count_magical.py @@ -4,6 +4,11 @@ # if the number of evens is greater than # half of the length of the list, print "Magical" # Else, print "Not Magical" +# +# Write a function called main which tests +# the count_magical function on at least +# 3 different lists of integers. Use the main +# function to test count_magical by calling main(). def count_magical(my_list): number_of_evens = 0 @@ -17,3 +22,16 @@ def count_magical(my_list): print("Not Magical") return number_of_evens + + +def main(): + list_1 = [1, 2, 3, 4, 5, 6] # not magical, 3 evens + list_2 = [0, 35, 1, 35, 2, 4] # not magical, 3 evens + list_3 = [10, 20, 12, 3, -9] # magical, 3 evens + + print("Number of evens in list 1:", count_magical(list_1)) + print("Number of evens in list 2:", count_magical(list_2)) + print("Number of evens in list 3:", count_magical(list_3)) + + +main() From 52c8b982106c13ee779d2a4cc77b590c21d93276 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Thu, 16 Jul 2020 03:01:59 +0000 Subject: [PATCH 3/3] Fix code style issues with Black --- 2_intermediate/chapter11/solutions/count_magical.py | 1 + 1 file changed, 1 insertion(+) diff --git a/2_intermediate/chapter11/solutions/count_magical.py b/2_intermediate/chapter11/solutions/count_magical.py index 02562be2..b9989882 100644 --- a/2_intermediate/chapter11/solutions/count_magical.py +++ b/2_intermediate/chapter11/solutions/count_magical.py @@ -10,6 +10,7 @@ # 3 different lists of integers. Use the main # function to test count_magical by calling main(). + def count_magical(my_list): number_of_evens = 0 for n in my_list: