Skip to content

Commit

Permalink
day 7 filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 30, 2020
1 parent 1ffccbc commit 2cde209
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
21 changes: 21 additions & 0 deletions day7_filter_prime_numbers.py
Original file line number Diff line number Diff line change
@@ -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))
32 changes: 20 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 2cde209

Please sign in to comment.