[MITx: 6.00.1x] Introduction to Computer Science and Programming Using Python (Final Exam)

สำหรับ Blog อันนี้เขียนหลังสอบ Final Exam ครับ โดยการสอบครั้งนี้ น่าจะเป็น One Week Miracle มากๆ ครับ เพราะจากการที่ผมไปเน้นตัว MCSD เป็นหลักครับ ทำเหลือเวลาเตรียมตัวในการสอบครั้งนี้น้อยพอสมควรครับ จริงๆ อยากรีบให้มันจบไปด้วย ฮ่าๆ รู้สึกว่า Project ใหม่ ตัว Equity Rebalance เข้ามาบีบแล้ว

เข้ามาที่ตัวข้อสอบเลยดีกว่าครับ สำหรับข้อสอบโครงสร้างคล้ายๆกับ Mid-Term ครับ คือ

  • 80% เป็นการเขียน Code ตามโจทย์ที่ให้มาครับ โดยมีโอกาศให้ Submit Code เข้า Grader ได้ 10 ครั้ง
  • 20% เป็นพวก Choice แต่ผมว่าไม่โหดเหมือน Mid-Term นะ

ตัวที่ยากสำหรับผมน่าเป็น Final-Exam Problem 4: ที่ให้หาค่ามากที่สุดใน Tuple ครับ โดยสิ่งที่โจทย์ให้มา ดังนี้

  • Code บางส่วนให้ โดยให้เรา Implement Business Logic
    def max_val(t): 
        """ t, tuple or list
            Each element of t is either an int, a tuple, or a list
            No tuple or list is empty
            Returns the maximum int in t or (recursively) in an element of t """ 
        # Your code here
  • ตัวอย่างการใช้งาน
    max_val((5, (1,2), [[1],[2]])) returns 5.
    max_val((5, (1,2), [[1],[9]])) returns 9.

ที่ผมว่ามันโหด และยาก เพราะ ว่าตัว Final-Exam เค้าถึงว่า ผู้เรียนได้ผ่านบทเรียนเรื่อง Testing & Debugging ของ Week ที่ 4 แล้วครับ นั่นแสดงว่า เราต้องคิด Test Case เองครับ แต่นั่นทำให้ผมรู้ว่าตัว tuple มีลูกเล่นเยอะครับ เช่น

  • Tuple
  • Tuple ที่มีไส้ในเป็น List
  • Tuple ที่มีไส้ในเป็น List ซ้อน List และมันไม่ได้แค่ 2 ชั้นครับ ที่เจอใน Random Test Case น่าจะ 8 ชั้น

และ Code ที่ผมได้เขียนตัว Method หลักๆ ผมยังใช้ Loop ครับ ในโจทย์ข้อนี้เน้นแนวคิดในการเล่นข้อมูลใน Tuple ที่มีความหลากหลายตาม Test Case ซึ่งผมพบว่าสิ่งที่ tuple กับ List มีเหมือนกัน คือ มัน Iterable ได้ ผมจึงทำ Helper flatten (เอา Recusive มาช่วย) เพื่อทำให้ข้อมูลมันแบนลง โดยใช้ Python Package Collection มาช่วยครับ ซึ่งถ้าใน JavaScript เรียกว่าทำ flat ส่วนใน C# ถ้าใช้ Linq เป็น Extension SelectMany ส่วนนี่เป็น Code ที่ผมเขียนครับ

import collections
def max_val(inputlist):
  """ t, tuple or list
        Each element of t is either an int, a tuple, or a list
        No tuple or list is empty
        Returns the maximum int in t or (recursively) in an element of t """
  def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el
            
  MaxInList = []
  for element in inputlist:
    if type(element) is int:
      #print(element, " int")
      MaxInList.append(element)
    elif type(element) is list or type(element) is tuple:
      #print(element, " list")
      flattened = flatten(element)
      #print(flattened, " flattened list")
      MaxInList.append(max(flattened))
    else:
      raise TypeError()
  
  #print(MaxInList)
  return max(MaxInList)
  #return max([sublist[-1] for sublist in inputlist])

#Test Case
print(max_val((5, (1,2), [[1],[2]])))
print(max_val((5, (1,2), [[1],[9]])))
print(max_val((9, [3, 8, 2])))
print(max_val(([1, 2], [3, 4], [5, 6])))
print(max_val([[[[[[6]]]]]]))
print(max_val(([[2, 1, 5], [4, 2], 6], ([2, 1], (7, 7, 5), 6))))

จริงๆ ผมว่าตัว def max_val(inputlist) Code หลักที่ใช้ for มันควรทำแบบ Recursive มากกว่าครับ Code น่าสวยกว่าและดูแลรักษาได้ง่ายมากกว่าครับ สำหรับข้อนี้เองผมใช้เวลาไปเชั่วโมงนิดๆ แต่ข้ออื่นๆ ที่เขียน Code ผมใช้เวลาประมาณ 15-25 นาทีครับ ส่วนอีกปัญหาที่ผมพบ คือ ภาษาอังกฤษ ถึงแม้ว่าเราตีความคำถามได้ รู้ว่าต้องตอบอะไร แต่ดันไปงง Choice ครับ

Blog ตอนต่อไปเป็นตอนสุดท้ายที่พูดภาพรวม ทุกๆอย่างในการเรียนผ่าน Edx ครับ


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.