จงเขียนฟังชัน add
ที่รับลิสต์ของจำนวนจริง 2 ลิสต์ แต่ละลิสต์แทนเวกเตอร์มีขนาดเท่ากัน ฟังก์ชันนี้คำนวณและคืนผลบวกของสองเวกเตอร์ที่ได้รับ (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
def add(u, v):
def add(u, v):
return [u[i]+v[i] for i in range(len(u))]
func = Ex().check_function_def('add', missing_msg="ไม่พบฟังก์ชัน add", expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
for v in ["[1,2,3], [2,3,4]", "[2.2, 3.3], [4.4, 5.5]"]:
func.check_call("add("+v+")").\
has_equal_value(incorrect_msg = "ผิด เช่น " + v)
func.check_call("add("+v+")").\
has_equal_value(expr_code="type(add("+v+"))", incorrect_msg = "ฟังก์ชันนี้ต้องคืนลิสต์")
จงเขียนฟังชัน dot
ที่รับลิสต์ของจำนวนจริง 2 ลิสต์ แต่ละลิสต์แทนเวกเตอร์มีขนาดเท่ากัน ฟังก์ชันนี้คำนวณและคืน dot product ของสองเวกเตอร์ที่ได้รับ (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด) หมายเหตุ: ฟังก์ชัน sum(x) คืนผลรวมของทุกจำนวนในลิสต์ x
def dot(u,v):
def dot(u,v):
return sum([u[i]*v[i] for i in range(len(u))])
func = Ex().check_function_def('dot', missing_msg="ไม่พบฟังก์ชัน dot", expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
for v in ["[1,2,3], [2,3,4]", "[2.2, 3.3], [4.4, 5.5]"]:
func.check_call("dot("+v+")").\
has_equal_value(incorrect_msg = "ผิด เช่น " + v)
จงเขียนฟังชัน to_list_of_strings
ที่รับลิสต์หนึ่งตัว เพื่อสร้างและคืนลิสต์อีกตัวที่ภายในเก็บค่าที่นำข้อมูลจากลิสต์ที่รับมาแปลงเป็นสตริง เช่น รับ [1,2,3] จะคืน ['1', '2', '3'] (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
def to_list_of_strings(x):
def to_list_of_strings(x):
return [str(e) for e in x]
func = Ex().check_function_def('to_list_of_strings', missing_msg="ไม่พบฟังก์ชัน to\_list\_of\_strings", expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
for v in ["[1, 2, 5]", "[1.2, 3.4]"]:
func.check_call("f("+v+")").\
has_equal_value(incorrect_msg = "ผิด เช่น " + v)
func.check_call("f("+v+")").\
has_equal_value(expr_code="type(to_list_of_strings("+v+"))", incorrect_msg = "ฟังก์ชันนี้ต้องคืนลิสต์")
จงเขียนฟังก์ชัน get_positive(x)
ฟังก์ชันนี้รับลิสต์ของจำนวน เพื่อคืนลิสต์ใหม่ที่เก็บจำนวนใน x เฉพาะตัวที่เป็นจำนวนบวก (เก็บเรียงลำดับก่อนหลังเหมือนใน x) เช่น x = [0,1,0,-2,9,3-4] จะคืน [1,9,3] (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
# อย่าเปลี่ยนหัวฟังก์ชันข้างล่างนี้
def get_positive(x):
def get_positive(x):
return [e for e in x if e > 0]
func = Ex().check_function_def('get_positive', missing_msg="ไม่พบฟังก์ชัน get\_positive", expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
for v in [[0,-1,-2], [1,2,3], [0,-2,2,3,-1]]:
func.check_call("f("+str(v)+")").\
has_equal_value(incorrect_msg = "ผิด เช่น " + str(v))
func.check_call("f("+str(v)+")").\
has_equal_value(expr_code="type(get_positive("+str(v)+"))", incorrect_msg = "ฟังก์ชันนี้ต้องคืนลิสต์")
func.check_body().set_context(v).\
has_equal_value(name = "x", incorrect_msg = "ไม่จำเป็นต้องเปลี่ยน x ในฟังก์ชันนี้")
จงเขียนฟังก์ชัน get_peak_indexes(x)
ฟังก์ชันนี้รับลิสต์ของจำนวน เพื่อหาและคืนอินเด็กซ์ต่าง ๆ ของ x ที่มีข้อมูลที่มากกว่าตัวที่ติดกันทางซ้ายและขวา เช่น x = [0,1,9,3,4,99,3,4,6,0,9] จะคืน [2,5,8] (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
def get_peak_indexes(x):
def get_peak_indexes(x):
return [i for i in range(1,len(x)-1) if x[i-1] < x[i] > x[i+1]]
func = Ex().check_function_def('get_peak_indexes', missing_msg="ไม่พบฟังก์ชัน get\_peak\_indexes", expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
for v in ["[1,2,3,4,5]", "[9,0,9]", "[0,9,0]", "[0,9,0,9,0,9,0,9,9,9,8]"]:
func.check_call("f("+v+")").\
has_equal_value(incorrect_msg = "ผิด เช่น " + v)
func.check_call("f("+v+")").\
has_equal_value(expr_code="type(get_peak_indexes("+v+"))", incorrect_msg = "ฟังก์ชันนี้ต้องคืนลิสต์")
จงเขียนฟังก์ชัน sum_all(M)
รับ M เป็นลิสต์ซ้อนลิสต์ที่แทนเมทริกซ์ของจำนวน ฟังก์ชันนี้คืนผลรวมของทุกจำนวนในเมทริกซ์ M
(ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด) หมายเหตุ: ฟังก์ชัน sum(x) คืนผลรวมของทุกจำนวนในลิสต์ x
def sum_all(M):
def sum_all(M):
return sum([sum(r) for r in M])
q = "`"
fname = "sum_all"
func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q), expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
import random
M = [[random.randint(-10,10) for i in range(4)] for j in range(3)]
params = [[[1,2],[3,4],[2,2]], M]
for p in params:
fcall = fname + "(" + str(p) + ")"
func.check_call(fcall).has_equal_value(incorrect_msg=(q+fcall+q) + " ให้ผลผิด")
จงเขียนฟังก์ชัน sum_row(M)
รับ M เป็นลิสต์ซ้อนลิสต์ที่แทนเมทริกซ์ของจำนวน ฟังก์ชันนี้คืนลิสต์ที่ช่องที่ k เก็บผลรวมของทุกจำนวนในแถวแนวนอนที่ k ของเมทริกซ์ M (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด) หมายเหตุ: ฟังก์ชัน sum(x) คืนผลรวมของทุกจำนวนในลิสต์ x
def sum_row(M):
def sum_row(M):
return [sum(r) for r in M]
q = "`"
fname = "sum_row"
func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q), expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
import random
M = [[random.randint(-10,10) for i in range(4)] for j in range(3)]
params = [[[1,2],[3,4],[2,2]], M]
for p in params:
fcall = fname + "(" + str(p) + ")"
func.check_call(fcall).has_equal_value(incorrect_msg=(q+fcall+q) + " ให้ผลผิด")
จงเขียนฟังก์ชัน sum_col(M)
รับ M เป็นลิสต์ซ้อนลิสต์ที่แทนเมทริกซ์ของจำนวน ฟังก์ชันนี้คืนลิสต์ที่ช่องที่ k เก็บผลรวมของทุกจำนวนในแถวแนวตั้วที่ k ของเมทริกซ์ M (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด) หมายเหตุ: ฟังก์ชัน sum(x) คืนผลรวมของทุกจำนวนในลิสต์ x
def sum_col(M):
def sum_col(M):
return [sum([r[c] for r in M]) for c in range(len(M[0]))]
q = "`"
fname = "sum_col"
func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q), expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
import random
M = [[random.randint(-10,10) for i in range(4)] for j in range(3)]
params = [[[1,2],[3,4],[2,2]], M]
for p in params:
fcall = fname + "(" + str(p) + ")"
func.check_call(fcall).has_equal_value(incorrect_msg=(q+fcall+q) + " ให้ผลผิด")
จงเขียนฟังก์ชัน identity(n)
รับ n เป็นจำนวนเต็มบวก ฟังก์ชันนี้คืนลิสต์ซ้อนลิสต์ที่แทนเมทริกซ์เอกลักษณ์ (identity matrix) ขนาด nxn (เมทริกซ์เอกลักษณ์ คือ เมทริกซ์จัตุรัสที่ทุกช่องมีค่า 0 ยกเว้นแนวทแยงจากซ้ายบนลงมาขวาล่างมีค่าเป็น 1 (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
def identity( n ):
def identity( n ):
return [[0]*i + [1] + [0]*(n-i-1) for i in range(n)]
q = "`"
fname = "identity"
func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q), expand_msg="")
func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว')
func.check_body(expand_msg="").has_code(
"\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
params = [1, 3, 5, 6]
for p in params:
fcall = fname + "(" + str(p) + ")"
func.check_call(fcall).has_equal_value(incorrect_msg=(q+fcall+q) + " ให้ผลผิด")
เขียนคำสั่งอ่านชื่อย่อธนาคารและราคาหุ้นจากอินพุตในรูปแบบดังตัวอย่าง SCB=132.0, BBL=176.5, KBANK=172.0
เพื่อแสดงชื่อธนาคารกับราคาหุ้น (คั่นด้วยช่องว่าง) บรรทัดละธนาคาร เรียงตามชื่อธนาคาร ตามพจนานุกรม (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
___inp___ = ["KTB=18.9, BBL=176.5, SCB=132.0, KBANK=172.0, TMB=1.8"]
def input():
t = ___inp___.pop(0)
___inp___.append(t)
return t
banks = [e.split("=") for e in input().split(", ")]
banks.sort()
print("\n".join([" ".join(e) for e in banks]))
Ex().has_code("\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
Ex().has_equal_output(incorrect_msg = "ผลลัพธ์ไม่ถูกต้อง")
เขียนคำสั่งอ่านชื่อย่อธนาคารและราคาหุ้นจากอินพุตในรูปแบบดังตัวอย่าง SCB=132.0, BBL=176.5, KBANK=172.0
เพื่อมาประมวลผลและแสดงชื่อธนาคาร บรรทัดละชื่อ จากชื่อที่มีราคาหุ้นมากสุดมาน้อยสุด ให้ถือว่าไม่มีหุ้นราคาเท่ากันเลย (ต้องใช้ list comprehension โดยไม่เขียนคำสั่งข้ามหลายบรรทัด)
___inp___ = ["KTB=18.9, BBL=176.5, SCB=132.0, KBANK=172.0, TMB=1.8"]
def input():
t = ___inp___.pop(0)
___inp___.append(t)
return t
banks = [e.split("=") for e in input().split(", ")]
banks = [[float(p),n] for n,p in banks]
banks.sort()
print("\n".join([n for p,n in banks[::-1]]))
Ex().has_code("\[(.*)for(.*)\]", not_typed_msg = "ฝึกใช้ list comprehension นะ")
Ex().has_equal_output(incorrect_msg = "ผลลัพธ์ไม่ถูกต้อง")