Odilov Abdullajon 650-20
Mavzu: Dasturiy ta'minotni testlash usullari va yo'llarini tadqiq qilish
Fibonachchi ketma-ketligi sonlar qatoridir:
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Har bir keyingi raqam oldidagi ikkita sonni qo'shish orqali topiladi.
Kutilayotgan natija: 1 1 2 3 5 8 13 21 34
Amaliy qism
2 ta test muvaffaqiyatli bajarildi.
import unittest
def fibonacci(n):
if n < 0:
raise ValueError("Musbat son kiritishingiz kerak")
elif n == 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
sequence = [0, 1]
while len(sequence) < n:
next_value = sequence[-1] + sequence[-2]
sequence.append(next_value)
return sequence
class TestFibonacci(unittest.TestCase):
def test_fibonacci_sequence(self):
self.assertEqual(fibonacci(0), [])
self.assertEqual(fibonacci(1), [0])
self.assertEqual(fibonacci(2), [0, 1])
self.assertEqual(fibonacci(5), [0, 1, 1, 2, 3])
self.assertEqual(fibonacci(10), [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
def test_fibonacci_negative_input(self):
with self.assertRaises(ValueError):
fibonacci(-1)
if __name__ == '__main__':
unittest.main()
Dostları ilə paylaş: |