update,
This commit is contained in:
96
ttng1234/Q2/ans.py
Normal file
96
ttng1234/Q2/ans.py
Normal file
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
test_list = [ 21,9,17,10 ]
|
||||
|
||||
def fibonacci_sequence(test_number):
|
||||
# test_number represent the number that sequence should stop at.
|
||||
|
||||
# Initialize the sequence with the first two numbers
|
||||
sequence = [0, 1]
|
||||
|
||||
# keep_find setup to let while loop keep find the number sequence
|
||||
# unless the last generated number (next_num) is greater than the number
|
||||
# need to be tested.
|
||||
keep_find = True
|
||||
|
||||
# initialize to true to run the while loop at the very first execution
|
||||
while keep_find:
|
||||
# after the very first execution, the keep_find may not keep true anymore.
|
||||
|
||||
# 1st, 2nd, 3rd, 4th
|
||||
# 0, 1, 1, 2
|
||||
# at the first execution, the next_num is the 3rd number
|
||||
# at the first execution, the sequence[-1] is the 2rd number
|
||||
# at the first execution, the sequence[-2] is the 1st number
|
||||
|
||||
# at the second execution, the next_num is the 4th number
|
||||
# at the second execution, the sequence[-1] is the 3rd number
|
||||
# at the second execution, the sequence[-2] is the 2nd number
|
||||
next_num = sequence[-1] + sequence[-2]
|
||||
|
||||
# append the number to the sequence
|
||||
# append means extend the list to with the value given
|
||||
# [0,1].append(2) => [0,1,2]
|
||||
# at the first execution the number appended is at 3rd place
|
||||
# at the second execution the number appended is at 4th place
|
||||
sequence.append(next_num)
|
||||
|
||||
# test if:
|
||||
# the last number(next_num) calculated is already larger than the number need to be tested
|
||||
keep_find = (next_num < test_number)
|
||||
|
||||
# test the number if it exist in fibonacci sequence
|
||||
# return true if the next_num is equal to the test_number
|
||||
# it is because there are two possibilities of next_num
|
||||
# next_num > test_number => means the number is not in sequence
|
||||
# next_num = test_number => means the number is in sequence
|
||||
return next_num == test_number
|
||||
|
||||
def triangular_sequence(test_number):
|
||||
# Initialize the sequence with the first number
|
||||
sequence = [1]
|
||||
|
||||
# Generate the Triangular sequence
|
||||
keep_find = True
|
||||
i = 2
|
||||
while keep_find:
|
||||
next_num = sequence[-1] + i
|
||||
keep_find = next_num < test_number
|
||||
sequence.append(next_num)
|
||||
i = i + 1
|
||||
return next_num == test_number
|
||||
|
||||
def square_number_sequence(test_number):
|
||||
# Initialize the sequence with the first number
|
||||
sequence = [1]
|
||||
|
||||
keep_find = True
|
||||
i = 2
|
||||
while keep_find:
|
||||
next_num = i * i
|
||||
keep_find = next_num < test_number
|
||||
i = i + 1
|
||||
|
||||
return next_num == test_number
|
||||
|
||||
def check_mathematical_series(n: int) -> str:
|
||||
all_result = []
|
||||
|
||||
f_result = fibonacci_sequence(n)
|
||||
t_result = triangular_sequence(n)
|
||||
s_result = square_number_sequence(n)
|
||||
|
||||
if (f_result):
|
||||
all_result.append('Fibonacci')
|
||||
if (t_result):
|
||||
all_result.append('Triangular')
|
||||
if (s_result):
|
||||
all_result.append('Square')
|
||||
|
||||
if (all_result == []):
|
||||
print(f'n = {n}: None')
|
||||
else:
|
||||
print(f'n = {n}: '+', '.join(all_result))
|
||||
|
||||
for n in test_list:
|
||||
check_mathematical_series(n)
|
Reference in New Issue
Block a user