Description
/ Đại Học Chính Qui (Bacherlor program (Full-time study))
/ Khoa Khoa học và Kỹ thuật Máy tính (Faculty of Computer Science and Engineering ) / Khoa Học Máy Tính
/ Nguyên lý ngôn ngữ lập trình (CO3005)_Nguyễn Hứa Phùng (DH_HK211) / 5-FP / FP Programming
Đã bắt đầu vào
Tình trạng Đã hoàn thành
Hoàn thành vào
Thời gian thực
hiện 2 giờ 10 phút
Điểm 7,00/7,00
Điểm 10,00 của 10,00 (100%)
1
Use recursive approach to write a function lstSquare(n:Int) that returns a list of the squares of the numbers from 1 to n?
For example:
Test Result
lstSquare(3) [1,4,9]
Answer: (penalty regime: 0 %)
1
2 ▼ def lstSquare(size):
3 ▼ if (size > 0):
4 return lstSquare(size-1)+[size*size] 5 ▼ else:
6 return []
7
8
9
Test Expected Got
lstSquare(3) [1,4,9] [1,4,9]
lstSquare(1) [1] [1]
lstSquare(5) [1,4,9,16,25] [1,4,9,16,25]
lstSquare(4) [1,4,9,16] [1,4,9,16]
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
2
Use list comprehension approach to write a function lstSquare(n:Int) that returns a list of the squares of the numbers from 1 to n?
For example:
Test Result
lstSquare(3) [1,4,9]
Answer: (penalty regime: 0 %)
1 ▼ def square(n): 2 return n*n
3
4
5 ▼ def lstSquare(size):
6 result = []
7 result = [square(number) for number in range(1, size+1)]
8 return result9
Test Expected Got
lstSquare(3) [1,4,9] [1,4,9]
lstSquare(1) [1] [1]
lstSquare(5) [1,4,9,16,25] [1,4,9,16,25]
lstSquare(4) [1,4,9,16] [1,4,9,16]
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
3
Use high-order function approach to write function lstSquare(n:Int) to return a list of i square for i from 1 to n?
For example:
Test Result
lstSquare(3) [1,4,9]
Answer: (penalty regime: 0 %)
1 ▼ def lstSquare(size):
2 return list(map(lambda x: x * x, list(range(1, size+1))))
Test Expected Got
lstSquare(3) [1,4,9] [1,4,9]
lstSquare(1) [1] [1]
lstSquare(5) [1,4,9,16,25] [1,4,9,16,25]
lstSquare(4) [1,4,9,16] [1,4,9,16]
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
4
high-order function approach to write function dist(lst,n) that returns the list of pairs of
an element of lst and n.
For example:
Test Result
dist([1,2,3],4) [(1, 4),(2, 4),(3, 4)]
Answer: (penalty regime: 0 %)
1 ▼ def dist(lst, n):
2 return list(map(lambda x: (x, n), lst))
Test Expected Got
dist([1,2,3],4) [(1, 4),(2, 4),(3, 4)] [(1, 4),(2, 4),(3, 4)]
dist([],4) [] []
dist([1,2,3],’a’) [(1, ‘a’),(2, ‘a’),(3, ‘a’)] [(1, ‘a’),(2, ‘a’),(3, ‘a’)]
dist([3,4,1,5],6) [(3, 6),(4, 6),(1, 6),(5, 6)] [(3, 6),(4, 6),(1, 6),(5, 6)]
dist([1],’a’) [(1, ‘a’)] [(1, ‘a’)]
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
5
list comprehension approach to write function dist(lst,n) that returns the list of pairs of
an element of lst and n.
For example:
Test Result
dist([1,2,3],4) [(1, 4),(2, 4),(3, 4)]
Answer: (penalty regime: 0 %)
1 ▼ def ulti(a, b):
2 return (a, b)
3
4
5 ▼ def dist(lst, n):
6 return list([ulti(number, n) for number in lst])
Test Expected Got
dist([1,2,3],4) [(1, 4),(2, 4),(3, 4)] [(1, 4),(2, 4),(3, 4)]
dist([],4) [] []
dist([1,2,3],’a’) [(1, ‘a’),(2, ‘a’),(3, ‘a’)] [(1, ‘a’),(2, ‘a’),(3, ‘a’)]
dist([3,4,1,5],6) [(3, 6),(4, 6),(1, 6),(5, 6)] [(3, 6),(4, 6),(1, 6),(5, 6)]
dist([1],’a’) [(1, ‘a’)] [(1, ‘a’)]
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
6
recursive approach to write function dist(lst,n) that returns the list of pairs of an element
of lst and n.
For example:
Test Result
dist([1,2,3],4) [(1, 4),(2, 4),(3, 4)]
Answer: (penalty regime: 0 %)
1 ▼ def dist(lst, n):
2 ▼ if len(lst):
3 return [(lst[0], n)]+dist(lst[1:], n) 4 ▼ else:
5 return []
Test Expected Got
dist([1,2,3],4) [(1, 4),(2, 4),(3, 4)] [(1, 4),(2, 4),(3, 4)]
dist([],4) [] []
dist([1,2,3],’a’) [(1, ‘a’),(2, ‘a’),(3, ‘a’)] [(1, ‘a’),(2, ‘a’),(3, ‘a’)]
dist([3,4,1,5],6) [(3, 6),(4, 6),(1, 6),(5, 6)] [(3, 6),(4, 6),(1, 6),(5, 6)]
dist([1],’a’) [(1, ‘a’)] [(1, ‘a’)]
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
7
Scala has function compose to compose two functions but Python does not have this function. Write function compose that can takes at least two functions as its parameters and returns the composition of these parameter functions. For example compose(f,g,h)(x) is defined as f(g(h(x))).
For example:
Test Result
f = compose(increase,square) print(f(3)) #increase(square(3)) = 10 10
Answer: (penalty regime: 0 %)
1 ▼ def compose (*functions):
2 if len(functions) < 2: raise TypeError
3
4 ▼ def inner(arg):
5 ▼ for f in reversed(functions):
6 arg = f(arg)
7 return arg
8 return inner
Test Expected Got
f = compose(increase,square) print(f(3)) #increase(square(3)) = 10 10 10
f = compose(increase,square,double) print(f(3)) 37 37
f =
compose(increase,square,double,decrease) print(f(3)) 17 17
try:
f = compose(increase)
except TypeError:
print(“compose() missing 1 required positional argument”) compose() missing 1 required positional argument compose() missing 1 required positional argument
try:
f = compose() except TypeError:
print(“compose() missing 1 required positional argument”) compose() missing 1 required positional argument compose() missing 1 required positional argument
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
◄ FP Quiz
Chuyển tới…
Địa chỉ: Nhà A1- 268 Lý Thường Kiệt, Phường 14, Quận 10, Tp.HCM.
Email: elearning@hcmut.edu.vn
Phát triển dựa trên hệ thống Moodle
Reviews
There are no reviews yet.