mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-08-03 03:56:40 -04:00
updates
This commit is contained in:
parent
cf40c0610f
commit
dad3793c4b
44 changed files with 25 additions and 2028 deletions
25
src/further_examples/others/printing_numbers.py
Normal file
25
src/further_examples/others/printing_numbers.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2014
|
||||
# steinkirch at gmail
|
||||
# for hacker school application
|
||||
'''
|
||||
Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Crackle instead of the number. If it's divisible by 5, print Pop. If it's divisible by both 3 and 5, print CracklePop.
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def CracklePop(n):
|
||||
for i in range(1, n+1):
|
||||
if i%3 == 0 and i%5 == 0: print('CracklePop!!!')
|
||||
elif i%3 == 0: print("Crackle!!!")
|
||||
elif i%5 == 0: print("Pop!!!")
|
||||
else: print(i)
|
||||
|
||||
|
||||
def main():
|
||||
CracklePop(100)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
'''
|
||||
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
|
||||
|
||||
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
|
||||
|
||||
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
|
||||
|
||||
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
|
||||
'''
|
||||
|
||||
def find_sum_proper_divisors(n):
|
||||
sum_proper_div = 0
|
||||
for i in range(1, n):
|
||||
if n%i == 0:
|
||||
sum_proper_div += i
|
||||
return sum_proper_div
|
||||
|
||||
|
||||
def amicable_numbers(N):
|
||||
sum_div_list = [find_sum_proper_divisors(i) for i in range(1, N+1)]
|
||||
sum_amicable_numbers = 0
|
||||
set_div = set()
|
||||
for a in range(1, N):
|
||||
da = sum_div_list[a-1]
|
||||
if da < N:
|
||||
b = da
|
||||
db = sum_div_list[b-1]
|
||||
if a != b and db == a and a not in set_div and b not in set_div:
|
||||
sum_amicable_numbers += a + b
|
||||
set_div.add(a)
|
||||
set_div.add(b)
|
||||
return sum_amicable_numbers
|
||||
|
||||
|
||||
def main():
|
||||
print(amicable_numbers(10000))
|
||||
print('Tests Passed!')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
|
||||
# very dumb version, need to be optimized
|
||||
|
||||
'''
|
||||
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
|
||||
|
||||
The product 7254 is unusual, as the identity, 39 x186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
|
||||
|
||||
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
|
||||
|
||||
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
|
||||
'''
|
||||
|
||||
def is_pandigital(n):
|
||||
size = len(n)
|
||||
pan = set([x for x in range(1,size + 1)])
|
||||
for i in n:
|
||||
if int(i) in pan:
|
||||
pan = pan - {int(i)}
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
def find_multiples(n):
|
||||
multiples = set([1, n])
|
||||
for i in range(2, n):
|
||||
if n%i == 0:
|
||||
multiples.add(i)
|
||||
multiples.add(n//i)
|
||||
return multiples
|
||||
|
||||
|
||||
def find_pandigital():
|
||||
sum_pan = 0
|
||||
|
||||
for i in range(10, 987654322):
|
||||
if i//10 == 0:
|
||||
continue
|
||||
digits = set(str(i))
|
||||
if len(digits) != len(str(i)):
|
||||
continue
|
||||
multiples = find_multiples(i)
|
||||
mult_set = set()
|
||||
for j in multiples:
|
||||
for c in str(j):
|
||||
if c in digits or int(c) == 0:
|
||||
continue
|
||||
for c in str(j):
|
||||
mult_set.add(c)
|
||||
k = i//j
|
||||
for c in str(k):
|
||||
if c in digits or int(c) == 0:
|
||||
continue
|
||||
for c in str(k):
|
||||
mult_set.add(c)
|
||||
if len(mult_set ) == 9:
|
||||
sum_pan += j + k + i
|
||||
return sum_pan
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
assert(is_pandigital('15234') == True )
|
||||
assert(is_pandigital('15233') == False )
|
||||
|
||||
print(find_pandigital())
|
||||
|
||||
print('Tests Passed!')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -1,73 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
'''
|
||||
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
|
||||
|
||||
The product 7254 is unusual, as the identity, 39 x186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
|
||||
|
||||
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
|
||||
|
||||
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
|
||||
'''
|
||||
|
||||
def is_pandigital(n):
|
||||
size = len(n)
|
||||
pan = set([x for x in range(1,size + 1)])
|
||||
for i in n:
|
||||
if int(i) in pan:
|
||||
pan = pan - {int(i)}
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
def find_multiples(n):
|
||||
multiples = set([1, n])
|
||||
for i in range(2, n):
|
||||
if n%i == 0:
|
||||
multiples.add(i)
|
||||
multiples.add(n//i)
|
||||
return multiples
|
||||
|
||||
|
||||
def find_pandigital():
|
||||
sum_pan = 0
|
||||
|
||||
for i in range(10, 987654322):
|
||||
if i//10 == 0:
|
||||
continue
|
||||
digits = set(str(i))
|
||||
if len(digits) != len(str(i)):
|
||||
continue
|
||||
multiples = find_multiples(i)
|
||||
mult_set = set()
|
||||
for j in multiples:
|
||||
for c in str(j):
|
||||
if c in digits or int(c) == 0:
|
||||
continue
|
||||
for c in str(j):
|
||||
mult_set.add(c)
|
||||
k = i//j
|
||||
for c in str(k):
|
||||
if c in digits or int(c) == 0:
|
||||
continue
|
||||
for c in str(k):
|
||||
mult_set.add(c)
|
||||
if len(mult_set ) == 9:
|
||||
sum_pan += j + k + i
|
||||
return sum_pan
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
assert(is_pandigital('15234') == True )
|
||||
assert(is_pandigital('15233') == False )
|
||||
|
||||
print(find_pandigital())
|
||||
|
||||
print('Tests Passed!')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
def path_sum_two_ways(m1):
|
||||
paths = []
|
||||
|
||||
row, col = 0, 0
|
||||
p = len(m1)
|
||||
|
||||
while row < len(m1):
|
||||
print(m1[0:p])
|
||||
while p:
|
||||
aux = sum([x for x in m1[0:p]])
|
||||
paths.append(aux)
|
||||
p -= 1
|
||||
row += 1
|
||||
|
||||
return max(paths)
|
||||
|
||||
|
||||
def main():
|
||||
m1 = [[131, 673, 234, 103, 18], [201, 96, 342, 965, 150], [630, 803, 746, 422, 111], [537, 699, 497, 121, 956], [805, 732, 524, 37, 331]]
|
||||
print(path_sum_two_ways(m1))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue