Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for The Best Number Ever by siebenschlaefer
import collections
import functools
import itertools
def checkio():
"""153 is the best number ever."""
N = 153
assert N == compute_smallest_nontrivial_narcisstic_number()
assert N == sum_numbers_up_to(17)
assert N == sum_faculties_up_to(5)
assert is_palindrome(to_binary(N))
assert is_palindrome(to_hexadecimal(N))
assert is_balanced(to_binary(N))
assert divisible_by(N, sum(digits(N)))
assert N == sum_of_ascii_codes(str(N))
return N
def compute_smallest_nontrivial_narcisstic_number():
for n in itertools.count(2):
if n == sum(int(d) ** 3 for d in str(n)):
return n
def sum_numbers_up_to(n):
return sum(range(n + 1))
def sum_faculties_up_to(n):
return sum(faculty(i) for i in range(1, n + 1))
def is_palindrome(text):
return text == "".join(reversed(text))
def to_binary(n):
return format(n, "b")
def to_hexadecimal(n):
return format(n, "x")
def is_balanced(seq):
counter = collections.Counter(seq)
return len(set(counter.values())) == 1
def divisible_by(a, b):
return a % b == 0
def sum_of_ascii_codes(n):
return sum(ord(c) for c in str(n))
def faculty(n):
if n == 0:
return 1
return functools.reduce(lambda a, b: a * b, range(1, n + 1))
def digits(n):
return [int(d) for d in str(n)]
def mirror(n):
return int("".join(reversed(str(n))))
def count(sequence, *, value=None, func=None):
if value is not None:
return sum(1 for elem in sequence if elem == value)
if func is not None:
return sum(1 for elem in sequence if func(elem))
raise ValueError("specify 'value' or 'func'")
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(checkio(), (int, float, complex))
July 7, 2015
Comments: