Exemplo n.º 1
0
def factRep(n):
	i = 12
	while factorial(i) > n and i > 0:
		i-=1

	result = []
	for j in range(i, -1, -1):
		fact = factorial(j)
		coef = n / fact
		result.append(coef)
		n = n % fact

	return result
Exemplo n.º 2
0
	def test_factorial(self):
		sa = self.assertEqual
		sa(120, recursion.factorial(5))
		sa(2, recursion.factorial(2))

		l = recursion.allFactorials(5)
		sa(1, l[0])
		sa(1, l[1])
		sa(2, l[2])
		sa(6, l[3])
		sa(24, l[4])
		sa(120, l[5])

		sa(1, recursion.itFactorial(0))
		sa(1, recursion.itFactorial(1))
		sa(120, recursion.itFactorial(5))
Exemplo n.º 3
0
def iteratorPerm(input_list):
	max_num_perm = factorial(len(input_list))
	i = [-1]
	def closure():
		i[0] += 1
		if i[0] >= max_num_perm:
			return None
		else:
			fr = factRep(i[0])
			return factToPerm(input_list, fr)
	return closure
Exemplo n.º 4
0
	def test_factorial_of_3_is_6(self):
		self.assertEqual(6, factorial(3))
Exemplo n.º 5
0
	def test_factorial_of_2_is_2(self):
		self.assertEqual(2, factorial(2))
Exemplo n.º 6
0
def test_factorial():
    a = 5
    out = recursion.factorial(a)
    assert out == 120
Exemplo n.º 7
0
def test_fatorial():
    assert factorial(0) == 1
    assert factorial(1) == 1
    assert factorial(8) == 40320
    assert factorial(5) == 120
    assert factorial(9) == 362880
Exemplo n.º 8
0
import recursion

number_list = [1,2,3,4,5]
print "When multiplying all the elements in the list", number_list, "the answer is", recursion.multiply_list(number_list)

n = 5
print "The factorial of", n, "is", recursion.factorial(n)


short_list = [0,1,2,3,4,5,6,7,8,9,10]
print "In the list", short_list, "there are/is", recursion.count_list(short_list), "element(s)."

list_to_add = [0,1,2,3,4,5]
print "The sum of the list", list_to_add, "is", recursion.sum_list(list_to_add) 

list_to_reverse = [9,8,7,6,5,4,3,2,1]
print "The reverse of", list_to_reverse, "is", recursion.reverse(list_to_reverse)

x = 10
print "The", x, "th Fibonacci number is", recursion.fibonacci(x)

list_to_search = [6,7,8]
element = 8
print "I will attempt to find the element", element, "in the list", list_to_search, "."
print "If found, I will return the element; otherwise I will print 'None'."
print "Result:", recursion.find(list_to_search, element)


def isPalindrome(word):
    if recursion.palindrome(word):
        print "The word", word, "is a palindrome!"
 def test_factorial_with_floating_point_numbers(self):
     # factorial should raise a ValueError for non-integer n
     with self.assertRaises(ValueError, msg='function undefined for float'):
         factorial(2.0)
         factorial(3.14159)
 def test_factorial_with_small_integers(self):
     # factorial should return the product n*(n-1)*...*2*1 for n >= 0
     assert factorial(0) == 1  # base case
     assert factorial(1) == 1  # base case
     assert factorial(2) == 2*1
     assert factorial(3) == 3*2*1
     assert factorial(4) == 4*3*2*1
     assert factorial(5) == 5*4*3*2*1
     assert factorial(6) == 6*5*4*3*2*1
     assert factorial(7) == 7*6*5*4*3*2*1
     assert factorial(8) == 8*7*6*5*4*3*2*1
     assert factorial(9) == 9*8*7*6*5*4*3*2*1
     assert factorial(10) == 10*9*8*7*6*5*4*3*2*1
 def test_factorial_with_negative_integers(self):
     # factorial should raise a ValueError for n < 0
     with self.assertRaises(ValueError, msg='function undefined for n < 0'):
         factorial(-1)
         factorial(-5)
Exemplo n.º 12
0
#!/usr/bin/python

import recursion
# import recursion as rek
# from recursion import *
# from recursion import factorial
# from recursion import fibonacci as fib

print recursion.factorial(6)
print recursion.fibonacci(5)
Exemplo n.º 13
0
 def test_permutation_by_length_with_small_integers(self):
     assert len(permutation(5)) == factorial(5)
     assert len(permutation(6)) == factorial(6)
     assert len(permutation(7)) == factorial(7)
Exemplo n.º 14
0
	def test_factorial_of_12_is_6(self):
		self.assertEqual(479001600, factorial(12))
Exemplo n.º 15
0
 def test_factorial_with_large_integers(self):
     assert factorial(15) == 1307674368000
     assert factorial(20) == 2432902008176640000
     assert factorial(25) == 15511210043330985984000000
     assert factorial(30) == 265252859812191058636308480000000
Exemplo n.º 16
0
	def test_factorial_of_0_is_1(self):
		self.assertEqual(1, factorial(0))
Exemplo n.º 17
0
from recursion import sum_array, factorial, reverse, fibonacci
from sorting import bubble_sort, merge_sort, quick_sort

assert sum_array([1,2,3]) == 6
assert factorial(5) == 120
assert reverse('hello world') == 'dlrow olleh'
assert fibonacci(3) = 1
assert bubble_sort([1,3,5,2,4]) == [1, 2, 3, 4, 5]
assert merge_sort([3,0,6,33,9]) == [0, 3, 6, 9, 33]
assert quick_sort([3,1,7,54,2]) == [1, 2, 3, 7, 54]