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)
|
22
ttng1234/Q2/hand_draft.md
Normal file
22
ttng1234/Q2/hand_draft.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# need to setup (minimum, my list)
|
||||
- setup editor (vscode, windows)
|
||||
- https://code.visualstudio.com/download
|
||||
- setup python (windows)
|
||||
- https://www.python.org/ftp/python/3.11.5/python-3.11.5-embed-amd64.zip
|
||||
|
||||
# string
|
||||
|
||||
string = '123'
|
||||
string = 'abc'
|
||||
|
||||
|
||||
# integer
|
||||
|
||||
integer = 1
|
||||
integer = 1234567
|
||||
|
||||
|
||||
# boolean
|
||||
|
||||
boolean = True
|
||||
boolean = False
|
6
ttng1234/Q2/helloworld.py
Normal file
6
ttng1234/Q2/helloworld.py
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os,sys
|
||||
|
||||
|
||||
print('helloworld')
|
125
ttng1234/Q2/notes.md
Normal file
125
ttng1234/Q2/notes.md
Normal file
@@ -0,0 +1,125 @@
|
||||
### Q2. 25%, Checking mathematical series number
|
||||
|
||||
Fibonacci Sequence
|
||||
|
||||
Write a Python function named `check_mathematical_series` that takes a positive integer n as input and checks whether the given number belongs to any of the following important mathematical series: `Fibonacci sequence`, `Triangular sequence`, `Square sequence`.
|
||||
|
||||
The function should determine the series type and return a string indicating the result.
|
||||
|
||||
The possible return values are:
|
||||
|
||||
- "Fibonacci" if the number belongs to the Fibonacci sequence.
|
||||
- "Triangular" if the number belongs to the Triangular sequence.
|
||||
- "Square" if the number belongs to the Square sequence.
|
||||
- "None" if the number does not belong to any of the above sequences.
|
||||
|
||||
For this question, consider the following definitions for the series:
|
||||
|
||||
1. Fibonacci sequence: A series of numbers in which each number (after the first two) is the sum of the two preceding ones. The sequence starts with 0 and 1. For example, the Fibonacci sequence begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
|
||||
|
||||
```python
|
||||
def fibonacci_sequence(n):
|
||||
# Check if the input is valid
|
||||
if n <= 0:
|
||||
return []
|
||||
elif n == 1:
|
||||
return [0]
|
||||
elif n == 2:
|
||||
return [0, 1]
|
||||
|
||||
# Initialize the sequence with the first two numbers
|
||||
sequence = [0, 1]
|
||||
|
||||
# Generate the Fibonacci sequence
|
||||
for i in range(2, n):
|
||||
next_num = sequence[-1] + sequence[-2]
|
||||
sequence.append(next_num)
|
||||
|
||||
return sequence
|
||||
```
|
||||
|
||||
2. Triangular sequence: A sequence of numbers in which each term represents the
|
||||
total number of dots required to form a triangle with that many dots on each side.
|
||||
The nth term of the triangular sequence is given by the formula (n * (n + 1)) / 2.
|
||||
For example, the triangular sequence begins as follows: 1, 3, 6, 10, 15, …
|
||||
|
||||
|
||||
```python
|
||||
def triangular_sequence(n):
|
||||
# Check if the input is valid
|
||||
if n <= 0:
|
||||
return []
|
||||
|
||||
# Initialize the sequence with the first number
|
||||
sequence = [1]
|
||||
|
||||
# Generate the Triangular sequence
|
||||
for i in range(2, n+1):
|
||||
next_num = sequence[-1] + i
|
||||
sequence.append(next_num)
|
||||
|
||||
return sequence
|
||||
```
|
||||
|
||||
|
||||
Square number sequence: A sequence of numbers that are the squares of
|
||||
consecutive integers. For example, the square number sequence begins as follows:
|
||||
1, 4, 9, 16, 25, 36, ...
|
||||
Write the check_mathematical_series function and test it with the following values
|
||||
of n:
|
||||
- 21
|
||||
- 9
|
||||
- 17
|
||||
- 10
|
||||
|
||||
```python
|
||||
def square_number_sequence(n):
|
||||
# Check if the input is valid
|
||||
if n <= 0:
|
||||
return []
|
||||
|
||||
# Initialize the sequence with the first number
|
||||
sequence = [1]
|
||||
|
||||
# Generate the Triangular sequence
|
||||
for i in range(2, n+1):
|
||||
next_num = sequence[-1] + i
|
||||
sequence.append(next_num)
|
||||
|
||||
return sequence
|
||||
```
|
||||
|
||||
Print the result for each value of n, indicating the series types to which it belongs.
|
||||
|
||||
Using commas (,) if it belongs to more than 1 series.
|
||||
|
||||
"None" if it doesn't belong to any of the series.
|
||||
|
||||
Your function should have the following signature:
|
||||
|
||||
```python
|
||||
def check_mathematical_series(n: int) -> str:
|
||||
```
|
||||
|
||||
# Your code here
|
||||
|
||||
Example output:
|
||||
|
||||
```python
|
||||
n = 21: Fibonacci, Triangular
|
||||
n = 9: Square
|
||||
n = 17: None
|
||||
n = 10: Triangular
|
||||
```
|
||||
|
||||
Note: You can assume that the given n will be a positive integer
|
||||
|
||||
|
||||
Q:
|
||||
有冇話係邊到學python最好?
|
||||
我睇完data camp都唔識 🙃🙃
|
||||
覺得自己好蠢。
|
||||
|
||||
A:
|
||||
你方唔方便比份 notes 我望一望?
|
||||
同埋你係咪理科生?
|
9
ttng1234/Q2/step1.py
Normal file
9
ttng1234/Q2/step1.py
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
test_list = [ 21,9,17,10 ]
|
||||
|
||||
def check_mathematical_series(n: int) -> str:
|
||||
print(n)
|
||||
|
||||
for n in test_list:
|
||||
check_mathematical_series(n)
|
54
ttng1234/Q2/step2.py
Normal file
54
ttng1234/Q2/step2.py
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/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 check_mathematical_series(n: int) -> str:
|
||||
print(fibonacci_sequence(n))
|
||||
|
||||
for n in test_list:
|
||||
21, 9, 17, 10
|
||||
check_mathematical_series(n)
|
26
ttng1234/Q2/step3.py
Normal file
26
ttng1234/Q2/step3.py
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
test_list = [ 21,9,17,10 ]
|
||||
|
||||
def triangular_sequence(test_number):
|
||||
# test_number represent the number that sequence should stop at.
|
||||
|
||||
# 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 check_mathematical_series(n: int) -> str:
|
||||
print(triangular_sequence(n))
|
||||
|
||||
for n in test_list:
|
||||
check_mathematical_series(n)
|
24
ttng1234/Q2/step4.py
Normal file
24
ttng1234/Q2/step4.py
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
test_list = [ 21,9,17,10 ]
|
||||
|
||||
def square_number_sequence(test_number):
|
||||
|
||||
# Initialize the sequence with the first number
|
||||
sequence = [1]
|
||||
|
||||
# Generate the Square sequence
|
||||
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:
|
||||
print(square_number_sequence(n))
|
||||
|
||||
for n in test_list:
|
||||
check_mathematical_series(n)
|
Reference in New Issue
Block a user