예제 #1
0
def test_fibonacci():
    """
    test the function fibonacci(n)
    """

    assert fibonacci(9) == 34, 'incorrect'
    assert fibonacci(6) == 8, 'incorrect'
예제 #2
0
def test_fibonacci():
    n = 5
    out = recursion.fibonacci(n)
    assert out == 8
    n = 6
    out = recursion.fibonacci(n)
    assert out == 13
예제 #3
0
def test_fibonacci():
    n = 5
    out = recursion.fibonacci(n)
    assert out == 5
    n = 7
    out = recursion.fibonacci(n)
    assert out == 13
    n = 1
    out = recursion.fibonacci(n)
    assert out == 1
    n = 2
    out = recursion.fibonacci(n)
    assert out == 1
예제 #4
0
def main():
    n = input('Print fibonacci numbers upto which nth term?')
    n = int(n)

    for i in range(0, n):
        print(fibonacci(i), end=' ')

    print('\n')
예제 #5
0
 def test_fibonacci_with_large_integers(self):
     # These could run for a long time, depending on your implementation...
     assert fibonacci(15) == 610
     assert fibonacci(20) == 6765
     assert fibonacci(25) == 75025
     # TODO: Improve the runtime of your fibonacci function for these cases
     # If you're not careful, you may need to be very patient for these...
     assert fibonacci(30) == 832040
     assert fibonacci(35) == 9227465
     assert fibonacci(40) == 102334155
     assert fibonacci(100) == 354224848179261915075
     assert fibonacci(200) == 280571172992510140037611932413038677189525
     assert fibonacci(300) == \
         222232244629420445529739893461909967206666939096499764990979600
예제 #6
0
파일: fib_upto.py 프로젝트: neugn/learnings
def main():
    n = input('Upto which number do you want to print all fibonacci numbers? ')
    n = int(n)

    x = 0
    while True:
        fib_series_term = fibonacci(x)
        if fib_series_term > n:
            break
        else:
            print(fib_series_term, end=' ')
            x = x + 1
    print('\n')
예제 #7
0
 def test_fibonacci_with_floating_point_numbers(self):
     # Should raise a ValueError for non-integer n
     with self.assertRaises(ValueError):
         fibonacci(2.0)
     with self.assertRaises(ValueError):
         fibonacci(3.14159)
     with self.asesertRaises(ValueError):
         fibonacci('5')
예제 #8
0
    def test_fibonacci_with_large_integers(self):
        # These could run for a long time, depending on your implementation...
        assert fibonacci(15) == 610
        assert fibonacci(20) == 6765
        assert fibonacci(25) == 75025

        # If you're not careful, you may need to be very patient for these...
        assert fibonacci(30) == 832040
        assert fibonacci(35) == 9227465
        assert fibonacci(40) == 102334155
예제 #9
0
def test_functions():
    """
    make sure all functions work correctly
    """

    assert factorial(4) == 24, 'incorrect'
    assert fibonacci(8) == 21, 'incorrect'
    assert reverse(
        'A sentence that has to be reversed by the reverse function'
    ) == 'noitcnuf esrever eht yb desrever eb ot sah taht ecnetnes A', 'incorrect'
    assert sum_array([[5, 5], [20], [2, 1]]) == 33, 'incorrect'
    assert bubble_sort([59, 69, 3, 58, 494, 4, 13,
                        30]) == [59, 3, 58, 69, 4, 13, 30, 494], 'incorrect'

    #lists to test merge_sort function

    x = [34, 5, 2, 6]
    y = [12, 34, 54, 1]
    assert merge_sort(x + y) == [1, 2, 5, 6, 12, 34, 34, 54], 'incorrect'
    assert quick_sort([5, 6, 3, 7, 11, 44]) == [3, 5, 6, 7, 11,
                                                44], 'incorrect'
예제 #10
0
 def test_fibonacci_with_floating_point_numbers(self):
     # fibonacci should raise a ValueError for non-integer n
     with self.assertRaises(ValueError, msg='function undefined for float'):
         fibonacci(2.0)
         fibonacci(3.14159)
예제 #11
0
 def test_fibonacci_with_large_integers(self):
     # these could run for a long time...
     assert fibonacci(15) == 610
     assert fibonacci(20) == 6765
     assert fibonacci(25) == 75025
예제 #12
0
 def test_fibonacci_with_negative_integers(self):
     # fibonacci should raise a ValueError for n < 0
     with self.assertRaises(ValueError, msg='function undefined for n < 0'):
         fibonacci(-1)
         fibonacci(-5)
예제 #13
0
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!"
    else:
        print "The word", word, "is not a palindrome."

isPalindrome("racecar")
예제 #14
0
 def test_fibonacci_with_small_integers(self):
     # fibonacci should return fibonacci(n-1) + fibonacci(n-2) for n >= 0
     assert fibonacci(0) == 0  # base case
     assert fibonacci(1) == 1  # base case
     assert fibonacci(2) == 1
     assert fibonacci(3) == 2
     assert fibonacci(4) == 3
     assert fibonacci(5) == 5
     assert fibonacci(6) == 8
     assert fibonacci(7) == 13
     assert fibonacci(8) == 21
     assert fibonacci(9) == 34
     assert fibonacci(10) == 55
예제 #15
0
    def test_when_n_is_zero(self):

        self.assertEqual(r.fibonacci(0), 0)
예제 #16
0
    def test_fibonacci_list(self):
        fib_list = [fibonacci(num) for num in range(14)]

        self.assertEqual(fib_list, self.fib_results)
예제 #17
0
from recursion import sum_array, factorial, reverse, fibonacci
from sorting import bubble_sort, merge_sort, quick_sort

print(sum_array([1, 2, 3]))
print(fibonacci(10))
print(factorial(5))
print(reverse("abcdef"))
print(bubble_sort([5, 4, 1, 2, 4, 9]))
print(merge_sort([1, 3, 2, 5, 4, 7, 6]))
print(quick_sort([3, 2, 4, 5, 7, 6]))
예제 #18
0
 def test_when_n_is_one(self):
     self.assertEqual(r.fibonacci(1), 1)
예제 #19
0
 def test_fibonacci_with_small_integers(self):
     # Known fibonacci numbers with small integer arguments
     assert fibonacci(0) == 0  # base case
     assert fibonacci(1) == 1  # base case
     assert fibonacci(2) == 1
     assert fibonacci(3) == 2
     assert fibonacci(4) == 3
     assert fibonacci(5) == 5
     assert fibonacci(6) == 8
     assert fibonacci(7) == 13
     assert fibonacci(8) == 21
     assert fibonacci(9) == 34
     assert fibonacci(10) == 55
예제 #20
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]
예제 #21
0
 def test_fibonacci_with_large_integers(self):
     # These could run for a long time, depending on your implementation...
     assert fibonacci(15) == 610
예제 #22
0
 def test_when_n_is_greater_than_one(self):
     self.assertEqual(r.fibonacci(5), 5)
     self.assertEqual(r.fibonacci(8), 21)
     self.assertEqual(r.fibonacci(12), 144)
예제 #23
0
def test_fibonacci():
    assert fibonacci(0) == 0
    assert fibonacci(1) == 1
    assert fibonacci(7) == 13
    assert fibonacci(8) == 21
    assert fibonacci(15) == 610
예제 #24
0
def test_fibonacci():
    result = [fibonacci(x) for x in range(10)]
    assert [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] == result
# We want to use the functions that we have declared in the recursion.py file
import recursion

# Writing data to a file
my_file = open("test.txt", "w")
my_file.write("This is some file content. Hello 403IT :D")
my_file.close()

my_file = open("test.txt", "r")
my_string = my_file.read()
print(my_string)

# Write a limited part of Fibonacci sequence into a file called "Fibonacci.txt"
fib_list = recursion.fibonacci(0, 1, 100)
print(fib_list)
my_file = open("Fibonacci.txt", "w")
for num in fib_list:
    my_file.write(str(num))
    my_file.write("\n")
my_file.close()
예제 #26
0
    def test_fibonacci(self):
        self.assertEqual(recursion.fibonacci(10), 34)
        self.assertEqual(recursion.fibonacci_slow(10), 34)

        self.assertEqual(recursion.fibonacci(2), 1)
        self.assertEqual(recursion.fibonacci_slow(2), 1)
예제 #27
0
파일: 5.1.py 프로젝트: hajdusia/python
#!/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)
예제 #28
0
 def test_fibonacci_with_negative_integers(self):
     # Should raise a ValueError for n < 0
     with self.assertRaises(ValueError):
         fibonacci(-1)
     with self.assertRaises(ValueError):
         fibonacci(-5)
예제 #29
0
 def test_fibonacci_with_medium_integers(self):
     assert fibonacci(11) == 89
     assert fibonacci(12) == 144
예제 #30
0
def test_fibonacci():
    assert fibonacci(0) == 0
    assert fibonacci(1) == 1
    assert fibonacci(4) == 3
    assert fibonacci(8) == 21
예제 #31
0
def time_1():
    return recursion.fibonacci(2000)