From 2cde209722ab0c75e912d3527471b751ea26c037 Mon Sep 17 00:00:00 2001 From: Yohaan Date: Fri, 30 Oct 2020 03:03:06 +0000 Subject: [PATCH] day 7 filter() --- day7_filter_prime_numbers.py | 21 +++++++++++++++++++++ main.py | 32 ++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 day7_filter_prime_numbers.py diff --git a/day7_filter_prime_numbers.py b/day7_filter_prime_numbers.py new file mode 100644 index 0000000..d5009d9 --- /dev/null +++ b/day7_filter_prime_numbers.py @@ -0,0 +1,21 @@ +# Required concepts: https://www.w3schools.com/python/python_intro.asp map, filter, lambda + +print('Question: prime numbers in list,') + +def check_prime(num): + # note: you can end the for loop at √num + for i in range(2,num): + if num == 1: + return False + if num%i == 0: + # you found a factor of num! + return False + # you didn't find any multiple, you're prime + return True + +li = [2,3,4,5,6,7,8,9] +print(li) + +primes = filter(check_prime,li) + +print(list(primes)) \ No newline at end of file diff --git a/main.py b/main.py index 3f2b8a4..1196f57 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,20 @@ -# Required concepts: https://www.w3schools.com/python/python_intro.asp while loop and maths -print('Question: reverse number without using strings') - -def getReverse(num): - res = 0 - while num > 0: - # take last digit and put in the front of res - digit = num%10 - num //=10 - res = res*10 + digit - return res -print(getReverse(1234)) +# Required concepts: https://www.w3schools.com/python/python_intro.asp map, filter, lambda + +print('Question: prime numbers in list,') + +def check_prime(num): + for i in range(2,num): + if num == 1: + return False + if num%i == 0: + # you found a factor of num! + return False + # you didn't find any multiple, you're prime + return True + +li = [2,3,4,5,6,7,8,9] +print(li) + +primes = filter(check_prime,li) + +print(list(primes)) \ No newline at end of file