7: String Processing

7-2: Escape Char. & String Methods

** ถ้าใช้งานบนมือถือหรือ tablet แนะนำให้ใช้ Chrome หรือ Safari เท่านั้น **

แบบฝึกหัด 7-2 ข้อที่ 1

จงเขียนฟังก์ชัน number_of_newlines(s) หาว่ามีรหัสขึ้นบรรทัดใหม่ในสตริง s กี่ตัว

def number_of_newlines(s): def number_of_newlines(s): c = 0 for e in s: if e == "\n": c += 1 return c q = "`" fname = "number_of_newlines" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'hello'", "'hel\\nlo'", "'\\nhel\\nlo\\n\\n'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
แบบฝึกหัด 7-2 ข้อที่ 2

จงเขียนฟังก์ชัน left_strip(s) คืนสตริงใหม่ที่เหมือน s แต่ไม่มีช่องว่างทางซ้าย เช่น left_strip(" ab ") ได้ "ab "

def left_strip(s): def left_strip(s): return ((s+".").strip())[:-1] q = "`" fname = "left_strip" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'ABC'", "' ABC '", "'ABC '", "' ABC'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
เติมสักหนึ่งอักขระที่ไม่ใช่ช่องว่างทางขวา แล้วก็ strip หลังจากนั้นค่อยตัดตัวที่เติมออก
แบบฝึกหัด 7-2 ข้อที่ 3

จงเขียนฟังก์ชัน right_strip(s) คืนสตริงใหม่ที่เหมือน s แต่ไม่มีช่องว่างทางขวา เช่น right_strip(" ab ") ได้ " ab"

def right_strip(s): def right_strip(s): return (("."+s).strip())[1:] q = "`" fname = "right_strip" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'ABC'", "' ABC '", "'ABC '", "' ABC'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = "`"+ (q+fcall+q) +"`" + " ให้ผลผิด")
เติมสักหนึ่งอักขระที่ไม่ใช่ช่องว่างทางซ้าย แล้วก็ strip หลังจากนั้นค่อยตัดตัวที่เติมออก
แบบฝึกหัด 7-2 ข้อที่ 4

จงเขียนฟังก์ชัน first_index_of(s,t) หาว่ามี t ปรากฎเริ่มต้นใน s ที่อินเด็กซ์ใด ถ้ามีหลายที่ให้คืนอินเด็กซ์น้อยสุด ถ้าไม่มีให้คืน -1 เช่น first_index_of("abracadabra", "bra") ได้ 1 ในขณะที่ first_index_of("abracadabra", "BRA") ได้ -1

def first_index_of(s,t): def first_index_of(s,t): return s.find(t) q = "`" fname = "first_index_of" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'a given text', 'give'", "'Mamma Mia!', 'Mia'", "'Mamma Mia!', 'mamma'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
นึกถึงเมท็อด find ของสตริง
แบบฝึกหัด 7-2 ข้อที่ 5

จงเขียนฟังก์ชัน first_index_of(s,t) หาว่ามี t ปรากฎเริ่มต้นใน s ที่อินเด็กซ์ใด ถ้ามีหลายที่ให้คืนอินเด็กซ์น้อยสุด ถ้าไม่มีให้คืน -1 โดยการหาในถือว่าตัวอังกฤษเล็กเหมือนใหญ่ เช่น first_index_of("abracadabra", "bra") ได้ 1 และ first_index_of("abracadabra", "BRA") ก็ได้ 1

def first_index_of(s,t): def first_index_of(s,t): return s.lower().find(t.lower()) q = "`" fname = "first_index_of" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'a given text', 'gave'", "'Mamma Mia!', 'mia'", "'Mamma Mia!', 'maMMa'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
นึกถึงเมท็อด find, upper, lower ของสตริง
แบบฝึกหัด 7-2 ข้อที่ 6

จงเขียนฟังก์ชัน last_index_of(s,t) หาว่ามี t ปรากฎเริ่มต้นใน s ที่อินเด็กซ์ใด ถ้ามีหลายที่ให้คืนอินเด็กซ์มากสุด ถ้าไม่มีให้คืน -1 โดยการหาในถือว่าตัวอังกฤษเล็กเหมือนใหญ่ เช่น last_index_of("abracadabra", "bra") ได้ 8 และ last_index_of("abracadabra", "BRA") ก็ได้ 8

def last_index_of(s,t): #0123456789 #-ABC---ABC ABC 7 #CBA---CBA- CBA 0 10 - 0 - 3 def last_index_of(s,t): k = s[::-1].lower().find(t[::-1].lower()) if k != -1: k = len(s) - len(t) - k return k q = "`" fname = "last_index_of" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'a given text', 'gave'", "'Mamma Mia!', 'mia'", "'Mamma Mia!', 'maMMa'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
นึกถึงการกลับลำดับและเมท็อด find, upper, lower ของสตริง
แบบฝึกหัด 7-2 ข้อที่ 7

จงเขียนฟังก์ชัน index_of_kth(s,t,k) คืนอินเดกซ์ของสตริง s ที่ปรากฏสตริง t เป็นครั้งที่ k ถ้าไม่พบคืน -1 เช่น index_of_kth("ABabAB", "AB", 1) ได้ 0, index_of_kth("ABabAB", "AB", 2) ได้ 4, index_of_kth("ABabAB", "AB", 3) ได้ -1

def index_of_kth(s,t,k): def index_of_kth(s,t,k): j = -1 for i in range(k): j = s.find(t,j+1) if j == -1: return -1 return j q = "`" fname = "index_of_kth" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สามตัว') t = "'ABCabc--ABC-AABC-', 'ABC', " for p in [t + '1', t + '2', t + '3', t + '4', t + '5']: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
ต้องใช้วงวน ภายในใช้เมท็อด string1.find(string2, start)
แบบฝึกหัด 7-2 ข้อที่ 8

จงเขียนฟังก์ชัน remove_duplicates(s) ที่รับสตริง s แล้วคืนสตริงใหม่ที่เหมือน s แต่ไม่มีตัวอักษรที่ซ้ำครั้งแรกเป็นต้นไป โดยตัวอังกฤษใหญ่เหมือนตัวอังกฤษเล็ก เช่น remove_duplicates("AbaaaaBbBbC") จะได้ "AbC"

def remove_duplicates(s): def remove_duplicates(s): t = "" T = "" for e in s: if e.upper() not in T: t += e T += e.upper() return t q = "`" fname = "remove_duplicates" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์ตัวเดียว') for p in ["'012345'", "'ABCDEabcde'", "'abcdeABCDE'", "'AabBcCDd'"]: fcall = fname + "(" + str(p) + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
แบบฝึกหัด 7-2 ข้อที่ 9

จงเขียนฟังก์ชัน contains(s, w) เพื่อตรวจว่ามี w ที่ปรากฎเป็น "คำ" ในสตริง s หรือไม่ ให้ถือว่าอักขระที่คั่นระหว่างคำมีดังนี้ " ' / \ , . : ; ( ) [ ] { } กับช่องว่าง และถือว่าตัวอังกฤษใหญ่เหมือนกับเล็ก ดังนั้น contains("That's all folks", "that") จะได้ผลเป็นจริง แต่ contains("That's all folks", "folk") ได้ผลเป็นเท็จ

def contains(s, w): def replace(s, symbols, c): t = "" for e in s: if e in symbols: t += " " else: t += e return t def contains(s, w): t = " " + replace(s.lower(), "\"\'/\\,.:;()[]{}", " ") + " " return t.find(" " + w.lower() + " ") != -1 q = "`" fname = "contains" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') t = 'We,are (the) "champion".' for p in ["we", "are", "the", "Champion", "he", "amp"]: fcall = fname + "('" + t + "', '" + p + "')" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")