Skip to content

Instantly share code, notes, and snippets.

@acbalingit
Created December 10, 2011 01:00
Show Gist options
  • Save acbalingit/1454098 to your computer and use it in GitHub Desktop.
Save acbalingit/1454098 to your computer and use it in GitHub Desktop.
def cat_n_times(s, n):
print n*str(s)
def is_divisible_by_3(n):
if n%3 == 0: print "This number is divisible by 3."
else: print "This number is not divisible by 3."
def is_divisible_by_5(n):
if n%5 == 0: print "This number is divisible by 5."
else: print "This number is not divisible by 5."
def is_divisible_by_n(x, n):
if x%n == 0: print "Yes, {0} is divisible by {1}".format(x, n)
else: print "No, {0} is not divisible by {1}".format(x, n)
def sum_of_squares_of_digits(n):
"""
>>> sum_of_squares_of_digits(1)
1
>>> sum_of_squares_of_digits(9)
81
>>> sum_of_squares_of_digits(11)
2
>>> sum_of_squares_of_digits(121)
6
>>> sum_of_squares_of_digits(987)
194
"""
return sum([int(i)**2 for i in str(n)])
def dot_product(u, v):
"""
>>> dot_product([1, 1], [1, 1])
2
>>> dot_product([1, 2], [1, 4])
9
>>> dot_product([1, 2, 1], [1, 4, 3])
12
>>> dot_product([2, 0, -1, 1], [1, 5, 2, 0])
0
"""
return sum([u[i]*v[i] for i in range(len(u))])
if __name__ == "__main__":
import doctest
print doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment