Permalink
Browse files

Changed "[ " to \[ (syntax update)

  • Loading branch information...
1 parent e3708c0 commit 4de246644181443b62c3ff6529c34880f8d35450 @dandavison dandavison committed May 2, 2011
Showing with 191 additions and 189 deletions.
  1. +1 −2 python_code/complete/no001
  2. +6 −5 python_code/complete/no008
  3. +11 −11 python_code/complete/no011
  4. +1 −1 python_code/complete/no013
  5. +1 −1 python_code/complete/no014
  6. +1 −1 python_code/complete/no016
  7. +2 −2 python_code/complete/no017
  8. +1 −1 python_code/complete/no018
  9. +3 −2 python_code/complete/no020
  10. +4 −4 python_code/complete/no021
  11. +2 −2 python_code/complete/no022
  12. +4 −3 python_code/complete/no023
  13. +1 −1 python_code/complete/no024
  14. +5 −5 python_code/complete/no027
  15. +2 −2 python_code/complete/no030
  16. +4 −4 python_code/complete/no031
  17. +3 −3 python_code/complete/no032
  18. +7 −7 python_code/complete/no033
  19. +4 −4 python_code/complete/no036
  20. +4 −4 python_code/complete/no038
  21. +3 −3 python_code/complete/no039
  22. +1 −1 python_code/complete/no040
  23. +2 −2 python_code/complete/no042
  24. +1 −1 python_code/complete/no043
  25. +1 −1 python_code/complete/no046
  26. +3 −3 python_code/complete/no049
  27. +7 −7 python_code/complete/no050
  28. +4 −4 python_code/complete/no052
  29. +1 −1 python_code/complete/no053
  30. +42 −42 python_code/complete/no054
  31. +3 −3 python_code/complete/no056
  32. +4 −4 python_code/complete/no057
  33. +1 −1 python_code/complete/no058
  34. +9 −9 python_code/complete/no059
  35. +18 −18 python_code/complete/no061
  36. +1 −1 python_code/complete/no063
  37. +2 −2 python_code/complete/no064
  38. +1 −1 python_code/complete/no065
  39. +1 −1 python_code/complete/no066
  40. +2 −2 python_code/complete/no067
  41. +7 −7 python_code/complete/no089
  42. +4 −4 python_code/complete/no102
  43. +1 −1 python_code/complete/no190
  44. +5 −5 python_code/complete/no199
@@ -6,9 +6,8 @@ from python_code.decorators import euler_timer
@euler_timer(1)
def main():
- multiples = [ i for i in range(1, 1000) if i % 3 == 0 or i % 5 == 0 ]
+ multiples = [i for i in range(1, 1000) if i % 3 == 0 or i % 5 == 0]
print sum(multiples)
if __name__ == "__main__":
main()
-
View
@@ -3,23 +3,24 @@
# Find the greatest product of five consecutive
# digits in the 1000-digit number.
+import operator
+
from python_code.decorators import euler_timer
from python_code.functions import get_data
-import operator
def product_consec_digits(number, consecutive):
"""
Returns the largest product of "consecutive"
consecutive digits from number
"""
- digits = [ int(dig) for dig in str(number)]
+ digits = [int(dig) for dig in str(number)]
max_start = len(digits) - consecutive
- return [ reduce(operator.mul, digits[i:i + consecutive])
- for i in range(max_start + 1) ]
+ return [reduce(operator.mul, digits[i:i + consecutive])
+ for i in range(max_start + 1)]
@euler_timer(8)
def main():
- n = int("".join([ line.strip() for line in get_data(8).split("\n") ]))
+ n = int("".join([line.strip() for line in get_data(8).split("\n")]))
print max(product_consec_digits(n, 5))
View
@@ -34,32 +34,32 @@ from python_code.functions import get_data
import operator
def make_path(point, step, length):
- return [ (point[0] + i*step[0], point[1] + i*step[1]) for i in range(length) ]
+ return [(point[0] + i*step[0], point[1] + i*step[1]) for i in range(length)]
def convert(path, data):
# Assumes path is made of points (x,y) where data[x][y] exists
- return reduce(operator.mul, [ data[x][y] for x, y in path ])
+ return reduce(operator.mul, [data[x][y] for x, y in path])
@euler_timer(11)
def main():
DATA = get_data(11)
- DATA = [ [ int(entry) for entry in row.split() ] for row in DATA.split("\n") if row ]
+ DATA = [[int(entry) for entry in row.split()] for row in DATA.split("\n") if row]
# UP/DOWN goes from DATA[x][y] to DATA[x+3][y] where 0 <= x, x+3, y <= 19
- vert = max([ convert(make_path((x, y), (1, 0), 4), DATA) for x
- in range(0, 16 + 1) for y in range(19 + 1) ])
+ vert = max([convert(make_path((x, y), (1, 0), 4), DATA) for x
+ in range(0, 16 + 1) for y in range(19 + 1)])
# LEFT/RIGHT goes from DATA[x][y] to DATA[x][y+3] where 0 <= x, y, y+3 <= 19
- horiz = max([ convert(make_path((x, y), (0, 1), 4), DATA) for x
- in range(0, 19 + 1) for y in range(16 + 1) ])
+ horiz = max([convert(make_path((x, y), (0, 1), 4), DATA) for x
+ in range(0, 19 + 1) for y in range(16 + 1)])
# DIAGONAL L->R goes from DATA[x][y] to DATA[x+3][y+3] via +[1,1]
- diag_l_r = max([ convert(make_path((x, y), (1, 1), 4), DATA) for x
- in range(0, 16 + 1) for y in range(16 + 1) ])
+ diag_l_r = max([convert(make_path((x, y), (1, 1), 4), DATA) for x
+ in range(0, 16 + 1) for y in range(16 + 1)])
# DIAGONAL R->L goes from DATA[x][y] to DATA[x-3][y+3] via +[-1,1]
- diag_r_l = max([ convert(make_path((x, y), (-1, 1), 4), DATA) for x
- in range(3, 19 + 1) for y in range(16 + 1) ])
+ diag_r_l = max([convert(make_path((x, y), (-1, 1), 4), DATA) for x
+ in range(3, 19 + 1) for y in range(16 + 1)])
print max(vert, horiz, diag_l_r, diag_r_l)
@@ -8,7 +8,7 @@ from python_code.functions import get_data
@euler_timer(13)
def main():
number = get_data(13)
- numbers = [ int(line) for line in number.split("\n") if line ]
+ numbers = [int(line) for line in number.split("\n") if line]
total = sum(numbers)
first_digits = int(str(total)[:10])
print first_digits
@@ -25,7 +25,7 @@ def max_collatz_length_up_to_n(n, hash_ = {1: 1}):
if length(i, hash_) > max_lng:
max_lng = length(i, hash_)
max_lng_at = i
- return [ max_lng_at , max_lng ]
+ return [max_lng_at , max_lng]
@euler_timer(14)
def main():
@@ -6,7 +6,7 @@ from python_code.decorators import euler_timer
@euler_timer(16)
def main():
- print sum([ int(dig) for dig in str(2**1000) ])
+ print sum([int(dig) for dig in str(2**1000)])
if __name__ == "__main__":
main()
@@ -24,11 +24,11 @@ def words(n):
return "one thousand"
result = []
- digs = [ int(dig) for dig in str(n).zfill(3) ]
+ digs = [int(dig) for dig in str(n).zfill(3)]
if digs[0] != 0:
if n != 100*digs[0]:
- result.extend([ ones[digs[0]] , "hundred", "and"])
+ result.extend([ones[digs[0]] , "hundred", "and"])
else:
return "%s hundred" % ones[digs[0]]
@@ -32,7 +32,7 @@ from python_code.functions import max_sum
def main():
triangle = get_data(18).strip()
TRIANGLE_MAT = [[int(elt) for elt in line.split()]
- for line in triangle.split("\n") if line ]
+ for line in triangle.split("\n") if line]
print max_sum(TRIANGLE_MAT)
@@ -2,12 +2,13 @@
# Find the sum of the digits in the number 100!
-from python_code.decorators import euler_timer
from math import factorial
+from python_code.decorators import euler_timer
+
@euler_timer(20)
def main():
- print sum([ int(digit) for digit in str(factorial(100)) ])
+ print sum([int(digit) for digit in str(factorial(100))])
if __name__ == "__main__":
main()
@@ -9,7 +9,7 @@ def main():
factors = all_factors(n - 1)
# sum of proper divisors
- first_pass = [ sum(factors[i]) - i for i in range(1, n) ]
+ first_pass = [sum(factors[i]) - i for i in range(1, n)]
max_out = max(first_pass)
factors = all_factors(max_out, factors)
@@ -21,9 +21,9 @@ def main():
# i == sum(factors[sum(factors[i]) - i]) - (sum(factors[i]) - i) <==>
# sum(factors[sum(factors[i]) - i]) == sum(factors[i])
# Similarly s_1 != i <==> sum(factors[i]) != 2*i
- result = [ i for i in range(2, n) if
- sum(factors[sum(factors[i]) - i]) == sum(factors[i]) and
- sum(factors[i]) != 2*i ]
+ result = [i for i in range(2, n) if
+ sum(factors[sum(factors[i]) - i]) == sum(factors[i]) and
+ sum(factors[i]) != 2*i]
print "%s.\nThe full list of such amicable numbers is %s." % (
sum(result), ", ".join([str(elt) for elt in result]))
@@ -14,13 +14,13 @@ from python_code.decorators import euler_timer
from python_code.functions import get_data
def name_score(name):
- return sum([ (ord(letter.upper()) - ord('A') + 1) for letter in name ])
+ return sum([(ord(letter.upper()) - ord('A') + 1) for letter in name])
@euler_timer(22)
def main():
# The name file is a comma separated file with quotes
names = sorted(get_data(22).strip('"').split('","'))
- print sum([ (i + 1)*name_score(name) for i, name in enumerate(names) ])
+ print sum([(i + 1)*name_score(name) for i, name in enumerate(names)])
if __name__ == "__main__":
main()
@@ -8,21 +8,22 @@
# Find the sum of all the positive integers which cannot be written as
# the sum of two abundant numbers.
+import operator
+
from python_code.decorators import euler_timer
from python_code.functions import all_factors
from python_code.functions import apply_to_list
-import operator
def abundant_numbers(n):
factor_hash = all_factors(n)
# sum of proper divisors
- return [ i for i in range(2, n + 1) if i < sum(factor_hash[i]) - i ]
+ return [i for i in range(2, n + 1) if i < sum(factor_hash[i]) - i]
@euler_timer(23)
def main():
abundants = abundant_numbers(28123)
sums = sorted(set(apply_to_list(operator.add, abundants)))
- bad_ones = [ i for i in range(28123 + 1) if i not in sums ]
+ bad_ones = [i for i in range(28123 + 1) if i not in sums]
print sum(bad_ones)
if __name__ == "__main__":
@@ -28,7 +28,7 @@ def lex(list_, perm):
def main():
list_ = range(10)
perm = 10**6 - 1 # Our indexing begins at 0
- print "".join( [ str(dig) for dig in lex(list_, perm) ] )
+ print "".join([str(dig) for dig in lex(list_, perm)])
if __name__ == "__main__":
main()
View
@@ -34,12 +34,12 @@ def polynomial_consecutive_primes(a, b, primes):
@euler_timer(27)
def main():
PRIMES = sieve(86238)
- b_choices = [ prime for prime in PRIMES if prime < 1000 ]
+ b_choices = [prime for prime in PRIMES if prime < 1000]
- candidates = [ (a, b, polynomial_consecutive_primes(a, b, PRIMES))
- for a in range(-999, 999 + 1)
- for b in b_choices ]
- quantities = [ entry[2] for entry in candidates ]
+ candidates = [(a, b, polynomial_consecutive_primes(a, b, PRIMES))
+ for a in range(-999, 999 + 1)
+ for b in b_choices]
+ quantities = [entry[2] for entry in candidates]
winner = candidates[quantities.index(max(quantities))]
prod = winner[0]*winner[1]
a = winner[0]
@@ -10,7 +10,7 @@
from python_code.decorators import euler_timer
def sum_of_digits_powers(n, power):
- return sum([ int(dig)**power for dig in str(n) ])
+ return sum([int(dig)**power for dig in str(n)])
@euler_timer(30)
def main():
@@ -20,7 +20,7 @@ def main():
valid.append(i)
print "%s.\nThe numbers satisfying this property are: %s." % (sum(valid),
- ", ".join([ str(num) for num in valid ]))
+ ", ".join([str(num) for num in valid]))
if __name__ == "__main__":
main()
@@ -5,24 +5,24 @@
# How many different ways can 200p be made using any number of coins?
# This will be the coeficient of x^200 in
-# (1 + x + x^2 + ... )(1 + x^2 + x^4 + ... )(1 + x^5 + x^10 + ... )*...
-# ...*(1 + x^10 + x^20 + ... )(1 + x^20 + x^40 + ... )(1 + x^50 + x^100 + ... )
+# (1 + x + x^2 + ...)(1 + x^2 + x^4 + ...)(1 + x^5 + x^10 + ...)*...
+# ...*(1 + x^10 + x^20 + ...)(1 + x^20 + x^40 + ...)(1 + x^50 + x^100 + ...)
from python_code.decorators import euler_timer
def polynomial_add(left, right):
max_len = max(len(left), len(right))
to_add_left = [0]*(max_len - len(left)) + left[:]
to_add_right = [0]*(max_len - len(right)) + right[:]
- return [ to_add_left[i] + to_add_right[i] for i in range(max_len) ]
+ return [to_add_left[i] + to_add_right[i] for i in range(max_len)]
# represent ax^n + bx^(n-1) + ... + c as [c,...b,a]
# 1 + 2x + x^2 + 2x^3 = (1+2x)*(1+x^2) =
# [1,2]*[1,0,1] = [1,0,1,0] + [2,0,2] = [1,2,1,2]
def polynomial_mult(f, g):
result = []
for ind in range(len(f)):
- to_add = [ f[-ind - 1]*coeff for coeff in g ] + [0] * ind
+ to_add = [f[-ind - 1]*coeff for coeff in g] + [0] * ind
result = polynomial_add(result, to_add)
return result
@@ -9,14 +9,14 @@ from python_code.decorators import euler_timer
def all_orderings(list_):
if len(list_) == 1:
- return [ list_ ]
+ return [list_]
result = []
for elt in list_:
sublist = list_[:]
sublist.remove(elt)
- result.extend([ [elt] + ordering
- for ordering in all_orderings(sublist) ])
+ result.extend([[elt] + ordering
+ for ordering in all_orderings(sublist)])
return result
View
@@ -18,8 +18,8 @@ import operator
def canceled_pair(numer, denom):
shared = set(str(numer)).intersection(set(str(denom)))
- result_n = [ dig for dig in str(numer) ]
- result_d = [ dig for dig in str(denom) ]
+ result_n = [dig for dig in str(numer)]
+ result_d = [dig for dig in str(denom)]
for dig in shared:
result_n.remove(dig) # Only removes first instance
result_d.remove(dig) # Only removes first instance
@@ -39,11 +39,11 @@ def equals_canceled_pair(numer, denom):
@euler_timer(33)
def main():
- pairs = [ (numer, denom) for numer in range(10,99)
- for denom in range(numer + 1, 100)
- if equals_canceled_pair(numer, denom) ]
- num = reduce(operator.mul, [ pair[0] for pair in pairs ])
- denom = reduce(operator.mul, [ pair[1] for pair in pairs ])
+ pairs = [(numer, denom) for numer in range(10,99)
+ for denom in range(numer + 1, 100)
+ if equals_canceled_pair(numer, denom)]
+ num = reduce(operator.mul, [pair[0] for pair in pairs])
+ denom = reduce(operator.mul, [pair[1] for pair in pairs])
print denom/(gcd(num,denom))
if __name__ == "__main__":
@@ -7,19 +7,19 @@ from python_code.decorators import euler_timer
from python_code.functions import is_palindrome
def binary_incrementer(str_):
- digs = [ int(dig) for dig in str_ ]
+ digs = [int(dig) for dig in str_]
digs[-1] += 1
for i in range(len(digs) - 1, -1, -1):
if digs[i] > 1:
temp = digs[i]
digs[i] = temp % 2
if i == 0:
- digs = [ temp/2 ] + digs
+ digs = [temp/2] + digs
else:
digs[i - 1] += temp/2 # int division intended
else:
break
- return "".join([ str(dig) for dig in digs ])
+ return "".join([str(dig) for dig in digs])
def all_base10_base2_palindromes(n):
result = []
@@ -37,7 +37,7 @@ def all_base10_base2_palindromes(n):
def main():
ans = all_base10_base2_palindromes(10**6)
print "%s.\nThe full list of palindromes is: %s" % (sum(ans),
- ", ".join([ str(number) for number in ans ]))
+ ", ".join([str(number) for number in ans]))
if __name__ == "__main__":
main()
@@ -6,7 +6,7 @@
from python_code.decorators import euler_timer
def is_pandigital_9(str_):
- for dig in [ str(elt) for elt in range(1, 10) ]:
+ for dig in [str(elt) for elt in range(1, 10)]:
if str_.count(dig) != 1:
return False
return True
@@ -16,12 +16,12 @@ def all_pandigitals_1_to_n(n):
multiplier = 1
result = []
- curr = "".join([ str(multiplier*elt) for elt in to_mult ])
+ curr = "".join([str(multiplier*elt) for elt in to_mult])
while len(curr) < 10:
if is_pandigital_9(curr):
result.append(curr)
multiplier += 1
- curr = "".join([ str(multiplier*elt) for elt in to_mult ])
+ curr = "".join([str(multiplier*elt) for elt in to_mult])
return result
@@ -30,7 +30,7 @@ def main():
result = []
for n in range(2, 10):
result.extend(all_pandigitals_1_to_n(n))
- print max([ int(elt) for elt in result ])
+ print max([int(elt) for elt in result])
if __name__ == "__main__":
main()
Oops, something went wrong.

0 comments on commit 4de2466

Please sign in to comment.