Skip to content

Instantly share code, notes, and snippets.

@tedhagos
Created May 23, 2018 05:41
Show Gist options
  • Select an option

  • Save tedhagos/060090efd5d63e85e1ad81cdb6973fd9 to your computer and use it in GitHub Desktop.

Select an option

Save tedhagos/060090efd5d63e85e1ad81cdb6973fd9 to your computer and use it in GitHub Desktop.
Solutions for Exer 2 and Exer 3 - Python TOP Training
# ask the user for input
# find the divisors of that number
def imperative_solution(end):
numrange = range(1, num + 1)
divisors = []
for i in numrange:
if num % i == 0:
divisors.append(i)
print("divisors of {0}".format(num))
print("-----------------")
print(divisors)
def declarative_solution(end):
numrange = range(1, num + 1)
print("divisors of {0}".format(num))
print("-----------------")
print(list(filter(lambda x: num % x == 0, numrange)))
num = int(input("enter a number : "))
declarative_solution(num)
# imperative_solution(num)
# if we were to write the lambda in line no 20, this is
# how it will look like
# def fun foo(x):
# if num % x == 0:
# True
# project euler problem; 3 and 5 multiples under 1000
# This is the correct solution
def calc(end):
numlist = range(1, end)
# find all the multiples of 3 < 1000
list_3 = list(filter(lambda x: x % 3 == 0, numlist))
# find all the multiples of 5 < 1000
list_5 = list(filter(lambda x: x % 5 == 0, numlist))
# now, we need to combine the two lists above, but ensure
# that there are no duplicates
combined_list = list_3 + [i for i in list_5 if i not in list_3]
print(combined_list)
print(sum(combined_list))
calc(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment