update,
This commit is contained in:
33
max015/CDS2003_assignment02/src/P01.py
Normal file
33
max015/CDS2003_assignment02/src/P01.py
Normal file
@@ -0,0 +1,33 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to calculate the sum of a list of numbers.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def list_sum_Recur(num_list):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(999999)
|
||||
|
||||
def list_sum_Recur(num_list):
|
||||
# implementation here
|
||||
if len(num_list) == 1:
|
||||
return num_list[0]
|
||||
else:
|
||||
return num_list[0] + list_sum_Recur(num_list[1:])
|
||||
|
||||
# test here
|
||||
def test_list_sum_Recur():
|
||||
print(list_sum_Recur([1, 2, 3, 4, 5]))
|
||||
print( (1+5)*5 / 2)
|
||||
|
||||
print(list_sum_Recur(list(range(1,9999+1))))
|
||||
print( (1+9999)*9999 / 2)
|
||||
|
||||
test_list_sum_Recur()
|
BIN
max015/CDS2003_assignment02/src/P01_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P01_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
73
max015/CDS2003_assignment02/src/P02.py
Normal file
73
max015/CDS2003_assignment02/src/P02.py
Normal file
@@ -0,0 +1,73 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Background
|
||||
The greatest common divisor (GCD) of integers a and b, at least one of which is nonzero, is the greatest positive integer d such that d is a divisor of both a and b; that is, there are integers e and f such that a = de and b = df, and d is the largest such integer. The GCD of a and b is generally denoted gcd(a, b).[8]
|
||||
|
||||
When one of a and b is zero, the GCD is the absolute value of the nonzero integer: gcd(a, 0) = gcd(0, a) = |a|.
|
||||
This case is important as the terminating step of the Euclidean algorithm.
|
||||
|
||||
The above definition is unsuitable for defining gcd(0, 0), since there is no greatest integer n such that 0 × n = 0.
|
||||
However, zero is its own greatest divisor if greatest is understood in the context of the divisibility relation,
|
||||
so gcd(0, 0) is commonly defined as 0.
|
||||
This preserves the usual identities for GCD, and in particular Bézout's identity,
|
||||
namely that gcd(a, b) generates the same ideal as {a, b}.[9][10][11]
|
||||
This convention is followed by many computer algebra systems.[12]
|
||||
Nonetheless, some authors leave gcd(0, 0) undefined.[13]
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to find the greatest common division of two positive integers.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def gcd_Recur(a,b):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
lteration = 0
|
||||
def gcd_Recur(a,b):
|
||||
# implementation here
|
||||
global lteration
|
||||
|
||||
# https://proofwiki.org/wiki/GCD_for_Negative_Integers
|
||||
if a < 0 or b < 0:
|
||||
print("Conservative result: tested number more reasonable if greater than 1")
|
||||
|
||||
if a == 0 and b == 0:
|
||||
return 0
|
||||
|
||||
if b == 0:
|
||||
return a
|
||||
else:
|
||||
lteration+=1
|
||||
return gcd_Recur(abs(b), abs(a % b))
|
||||
|
||||
# test here
|
||||
def test_gcd_Recur():
|
||||
global lteration
|
||||
input_list = [
|
||||
# from wiki: gcd(54,24) = 6
|
||||
(54,24),
|
||||
# from wiki: gcd(8, 12) = 4
|
||||
(8,12),
|
||||
|
||||
# for no gcd
|
||||
(5,7),
|
||||
|
||||
(-12,3), (-12,0),(12,-1),
|
||||
|
||||
# https://en.wikipedia.org/wiki/Greatest_common_divisor
|
||||
# from wiki: gcd(0, 0) is commonly defined as 0
|
||||
(0,0),
|
||||
|
||||
# https://en.wikipedia.org/wiki/Greatest_common_divisor
|
||||
# from wiki: When one of a and b is zero, the GCD is the absolute value of the nonzero integer: gcd(a, 0) = gcd(0, a) = |a|.
|
||||
(0,6), (1,0),
|
||||
(0,88),(99,0)]
|
||||
for a,b in input_list:
|
||||
lteration = 0
|
||||
print("gcd of ", a, " and ", b, " is: ", gcd_Recur(a,b))
|
||||
|
||||
test_gcd_Recur()
|
BIN
max015/CDS2003_assignment02/src/P02_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P02_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
47
max015/CDS2003_assignment02/src/P03.py
Normal file
47
max015/CDS2003_assignment02/src/P03.py
Normal file
@@ -0,0 +1,47 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Background
|
||||
In mathematics, the harmonic series is the infinite series formed by summing all positive unit fractions.
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to calculate the harmonic series upto `n` terms.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def gcd_Recur(a,b):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(999999)
|
||||
|
||||
lteration = 0
|
||||
def harmonic_series_Recur(n):
|
||||
# implementation here
|
||||
global lteration
|
||||
|
||||
# https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)
|
||||
if lteration == 0 and n < 1:
|
||||
return "Error: n should equal or lager than 1"
|
||||
if lteration == 0 and n == 0:
|
||||
return 1
|
||||
if n == 1:
|
||||
return 1
|
||||
else:
|
||||
lteration +=1
|
||||
return 1/n + harmonic_series_Recur(n-1)
|
||||
|
||||
# test here
|
||||
def test_harmonic_series_Recur():
|
||||
global lteration
|
||||
n_list = [-10,-1,0,1,5, 99, 9999]
|
||||
|
||||
for n in n_list:
|
||||
print("result of ", n, ' is : ', harmonic_series_Recur(n))
|
||||
lteration = 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_harmonic_series_Recur()
|
BIN
max015/CDS2003_assignment02/src/P03_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P03_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
42
max015/CDS2003_assignment02/src/P04.py
Normal file
42
max015/CDS2003_assignment02/src/P04.py
Normal file
@@ -0,0 +1,42 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to calculate the value of 𝑥 to the power of `n`.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def power_Recur(x,n):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(10000)
|
||||
|
||||
def power_Recur(x,n):
|
||||
# implementation here
|
||||
if n == 0:
|
||||
return 1
|
||||
else:
|
||||
return x * power_Recur(x, n-1)
|
||||
|
||||
# test here
|
||||
def test_power_Recur():
|
||||
input_list = [
|
||||
(0, 0),
|
||||
(0, 3),
|
||||
(3, 0),
|
||||
(2, 3),
|
||||
(-2, 3),
|
||||
(-2, 4),
|
||||
(10, 10),
|
||||
(10, 99),
|
||||
]
|
||||
for x, n in input_list:
|
||||
result = power_Recur(x, n)
|
||||
print("x:",x,"n:",n, " result : ", result, ",len of answer", len(str(result)))
|
||||
|
||||
|
||||
test_power_Recur()
|
BIN
max015/CDS2003_assignment02/src/P04_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P04_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
30
max015/CDS2003_assignment02/src/P05.py
Normal file
30
max015/CDS2003_assignment02/src/P05.py
Normal file
@@ -0,0 +1,30 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to accept a decimal integer and display its binary equivalent.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def Dec2Binary_Recur(num):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
def Dec2Binary_Recur(num):
|
||||
# implementation here
|
||||
if num > 1:
|
||||
return Dec2Binary_Recur(num // 2) + str(num % 2)
|
||||
else:
|
||||
return str(num % 2)
|
||||
|
||||
# test here
|
||||
def test_Dec2Binary_Recur():
|
||||
num_list = [ 1,32,64,128,255]
|
||||
for num in num_list:
|
||||
temp = Dec2Binary_Recur(num)
|
||||
print('result of DEC', str(num).zfill(4), ' is :',(8 - len(temp))*'0'+temp)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_Dec2Binary_Recur()
|
BIN
max015/CDS2003_assignment02/src/P05_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P05_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
43
max015/CDS2003_assignment02/src/P06.py
Normal file
43
max015/CDS2003_assignment02/src/P06.py
Normal file
@@ -0,0 +1,43 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to take in a string and returns a reversed copy of the string.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def reverse_Recur(a,b):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
def reverse_Recur(a, b):
|
||||
# implementation here
|
||||
if len(a) == 0:
|
||||
return b
|
||||
else:
|
||||
return reverse_Recur(a[1:], a[0] + b)
|
||||
|
||||
# test here
|
||||
test = ''''''
|
||||
print("test string:")
|
||||
print(test)
|
||||
print("reversed:")
|
||||
print(reverse_Recur(test, ""))
|
||||
print()
|
||||
print()
|
||||
|
||||
test = '''helloworld'''
|
||||
print("test string:")
|
||||
print(test)
|
||||
print("reversed:")
|
||||
print(reverse_Recur(test, ""))
|
||||
print()
|
||||
print()
|
||||
|
||||
test = '''The exponent is usually shown as a superscript to the right of the base. In that case, bn is called "b raised to the nth power", "b (raised) to the power of n", "the nth power of b", "b to the nth power",[2] or most briefly as "b to the n(th)".'''
|
||||
print("test string:")
|
||||
print(test)
|
||||
print("reversed:")
|
||||
print(reverse_Recur(test, ""))
|
BIN
max015/CDS2003_assignment02/src/P06_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P06_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
43
max015/CDS2003_assignment02/src/P07.py
Normal file
43
max015/CDS2003_assignment02/src/P07.py
Normal file
@@ -0,0 +1,43 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to check whether a number is Prime or not.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def isPrime_Recur(a,b):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(999999)
|
||||
|
||||
|
||||
def isPrime_Recur(a,b):
|
||||
# implementation here
|
||||
|
||||
# https://t5k.org/notes/faq/negative_primes.html
|
||||
if a < 0:
|
||||
return "tested number should be greater than 1"
|
||||
|
||||
# https://corp.tutorocean.com/is-1-a-prime-number/
|
||||
if a == 0 or a == 1:
|
||||
return "Conservative result: tested number is neither prime nor composite"
|
||||
|
||||
if b == 1:
|
||||
return True
|
||||
|
||||
if a % b == 0:
|
||||
return False
|
||||
else:
|
||||
return isPrime_Recur(a,b-1)
|
||||
|
||||
# test here
|
||||
if __name__ == '__main__':
|
||||
num_list = [-99, -3, -1, 0, 1, 3, 7, 9, 9999, 10007]
|
||||
for num in num_list:
|
||||
print('is ', num, ' Prime?', isPrime_Recur(num, num-1))
|
||||
|
BIN
max015/CDS2003_assignment02/src/P07_result.png
(Stored with Git LFS)
Normal file
BIN
max015/CDS2003_assignment02/src/P07_result.png
(Stored with Git LFS)
Normal file
Binary file not shown.
20
max015/CDS2003_assignment02/src/moodle.md
Normal file
20
max015/CDS2003_assignment02/src/moodle.md
Normal file
@@ -0,0 +1,20 @@
|
||||
P01
|
||||
https://leetcode.com/playground/gZTt9LMe
|
||||
|
||||
P02
|
||||
https://leetcode.com/playground/CsnfvkZe
|
||||
|
||||
P03
|
||||
https://leetcode.com/playground/Ga3kggri
|
||||
|
||||
P04
|
||||
https://leetcode.com/playground/R6Xh2hnB
|
||||
|
||||
P05
|
||||
https://leetcode.com/playground/8DPfAMSb
|
||||
|
||||
P06
|
||||
https://leetcode.com/playground/CsnfvkZe
|
||||
|
||||
P07
|
||||
https://leetcode.com/playground/H9BecSM9
|
BIN
max015/CDS2003_assignment02/src/result.docx
Normal file
BIN
max015/CDS2003_assignment02/src/result.docx
Normal file
Binary file not shown.
49
max015/CDS2003_assignment02/src/result.md
Normal file
49
max015/CDS2003_assignment02/src/result.md
Normal file
@@ -0,0 +1,49 @@
|
||||
assignment02_Q01_4086741
|
||||
Q01:
|
||||
Please use recursion to define and test a function to calculate the sum of a list of numbers.
|
||||
|
||||
A01:
|
||||
|
||||
---
|
||||
|
||||
assignment02_Q02_4086741
|
||||
Q02:
|
||||
|
||||
A02:
|
||||
|
||||
---
|
||||
|
||||
assignment02_Q03_4086741
|
||||
Q03:
|
||||
|
||||
A03:
|
||||
|
||||
---
|
||||
|
||||
assignment02_Q04_4086741
|
||||
Q04:
|
||||
|
||||
A04:
|
||||
|
||||
---
|
||||
|
||||
assignment02_Q05_4086741
|
||||
Q05:
|
||||
|
||||
A05:
|
||||
|
||||
---
|
||||
|
||||
assignment02_Q06_4086741
|
||||
Q06:
|
||||
|
||||
A06:
|
||||
|
||||
---
|
||||
|
||||
assignment02_Q07_4086741
|
||||
Q07:
|
||||
|
||||
A07:
|
||||
|
||||
---
|
26
max015/CDS2003_assignment02/src/warmup/Q1.py
Normal file
26
max015/CDS2003_assignment02/src/warmup/Q1.py
Normal file
@@ -0,0 +1,26 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Count down a nonnegative number to zero
|
||||
n,n— 1, n — 2, ...,0
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
'''
|
||||
|
||||
|
||||
def countdown(n):
|
||||
"""
|
||||
Count down a nonnegative number to zero
|
||||
"""
|
||||
if n == 0:
|
||||
print(n)
|
||||
else:
|
||||
print(n)
|
||||
countdown(n-1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
countdown(5)
|
22
max015/CDS2003_assignment02/src/warmup/Q2.py
Normal file
22
max015/CDS2003_assignment02/src/warmup/Q2.py
Normal file
@@ -0,0 +1,22 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Calculate the factorial Of a nonnegative integer where
|
||||
n! = n * (n - 1) * (n - 2) * ... * 2 * 1 and 0 != 1
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
'''
|
||||
|
||||
def factorial(n):
|
||||
"""
|
||||
Calculate the factorial Of a nonnegative integer where
|
||||
n! = n * (n - 1) * (n - 2) * ... * 2 * 1 and 0 != 1
|
||||
"""
|
||||
if n == 0 or n == 1:
|
||||
return 1
|
||||
else:
|
||||
return n * factorial(n-1)
|
Reference in New Issue
Block a user