Skip to content

Instantly share code, notes, and snippets.

UPPER_LETTER_OFFSET = ord("A") - 1
names = sorted(open("euler_022_data.txt").read().replace('"', "").split(","))
result = 0
for (i, name) in enumerate(names, 1):
name_score = 0
for c in name:
name_score += ord(c) - UPPER_LETTER_OFFSET
result += name_score * i
print result
def gnome_sort(a):
i = 0
while i < len(a)-1:
if a[i] > a[i+1] and i >= 0:
(a[i], a[i+1]) = (a[i+1], a[i])
i -= 1
else:
i += 1
a = [4, 2, 42, 356, 5, 100, 1]
from official.ref.is_palindromic_1 import is_palindromic
result = 0
for i in range(1, 1000000):
if is_palindromic(str(i)) and is_palindromic(bin(i)[2:]):
result += i
print result
def print_halves(n):
print n
while n > 0:
n = n / 2
print n
print_halves(57)
def binary_length(n):
i = 0
while n > 0:
pond = input("Diameter of the pond? ")
water_lily = input("Initial diameter of the water lily? ")
growth = input("Daily growth? ")
day = 0
while water_lily < pond:
day += 1
water_lily *= growth
print "On day %s: %s" % (day, water_lily)
print"The pond is smothered!"
@fabian57
fabian57 / amicable_numbers.py
Last active August 29, 2015 14:10
C'est bon
def sum_proper_divisors(n):
acc = 0
for i in xrange(1, n):
if n % i == 0:
acc += i
return acc
def amicable_nums(n):
m = sum_proper_divisors(n)
return sum_proper_divisors(m) == n and m != n
def max_length(elements):
acc = ""
for element in elements:
if len(element) > len(acc):
acc = element
return len(acc)
def print_boxed_text(elements):
max_length_of_elements = max_length(elements)
print "#" * (max_length_of_elements + 4)
for element in elements:
for i in range(1 , 21):
if i % 15 == 0:
print "FizzBuzz"
elif i % 3 == 0:
print "Fizz"
elif i % 5 == 0:
print "Buzz"
else:
print i
@fabian57
fabian57 / gist:e73360edc38b5ce01fd7
Last active August 29, 2015 14:08
Les deux programmes (sans fonctions et avec fonctions(avec tout plein de nombres magiques pour le deuxième))
#Program without functions
mileage = input("Mileage? ")
duration = input("Duration (in days)? ")
category = raw_input("Categorie (A or B)? ")
COST_PER_DAY_UNLIMITED_A = 70
COST_PER_DAY_PACKAGE_A = 50
COST_PER_DAY_UNLIMITED_B = 140