{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 2\n", "\n", "string, input, print, defining a good function\n", "basic string manipulation, python3 string utilities\n", "\n", "บาง function หรือ method ที่ต้องใช้ในการแก้ปัญหาโจทย์เหล่านี้อาจจะยังไม่ได้เรียนมา ให้ลองหาใน documentation ของ python \n", "\n", "https://docs.python.org/3/library/stdtypes.html#string-methods\n", "\n", "อาจจะต้องใช้ methods เหล่านี้\n", "~~~~\n", " 'capitalize',\n", " 'count',\n", " 'endswith',\n", " 'find',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isdecimal',\n", " 'isdigit',\n", " 'islower',\n", " 'isnumeric',\n", " 'isspace',\n", " 'isupper',\n", " 'lower',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rstrip',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'upper'\n", "~~~~" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 1 \n", "เมื่อปี 2007 ที่ผ่านมานักร้องสาว Rihanna จากเกาะ Barbados ได้ร้องเพลงสุดฮิตที่เกี่ยวข้องกับสภาพภูมิอากาศของเมืองไทยในขณะนี้เป็นยิ่งนัก \n", "ท่อนฮุคที่ดังติดหูคือ\n", "\n", "> You can stand under my umbrella\n", ">\n", "> You can stand under my umbrella, ella, ella, eh, eh, eh\n", ">\n", "> Under my umbrella, ella, ella, eh, eh, eh\n", ">\n", "> Under my umbrella, ella, ella, eh, eh, eh\n", ">\n", "> Under my umbrella, ella, ella, eh, eh, eh, eh, eh, eh\n", "\n", "จงเขียนโปรแกรม Python ที่ print เนื้อเพลง Umbrella ส่วนที่คัดลอกไว้ข้างบนนี้โดย reuse function ให้ได้มากที่สุด \n", "\n", "ข้อนี้ไม่ต้องเขียน Docstring " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 2\n", "เว็บไซต์เขียนด้วยภาษา HTML เต็มไปด้วย HTML tag เพื่อ mark ส่วนต่างๆ ของเว็บไซต์ของออกมาเป็นในลักษณะที่เราต้องการ\n", "~~~~\n", "\n", " \n", " \"Hello, World\"\n", " \n", "\n", "~~~~\n", "เพื่อให้ถูกต้องตาม Syntax ของภาษา HTML เมื่อเราเปิดแท็ก `` แล้วก็ต้องปิดแท็ก `` ดังนี้เป็นต้น\n", "ชื่อของ tag ที่ล้อมข้อความมีหน้าทีี่เปลี่ยนให้เป็นลักษณะใดลักษณะหนึ่งเช่น\n", "\n", "~~~~\n", "\n", " \n", " \"Hello, World\"\n", " \n", "\n", "~~~~\n", "\n", "tag b จะทำให้ข้อความที่ถูกล้อมไว้ออกมาเป็นตัวหนา\n", "\n", "นอกจากนั้นแล้วเรายังสามารถใช้สองแท็คพร้อมกันด้วย\n", "\n", "\n", "~~~~\n", "\n", " \n", " \"Hello, World\"\n", " \n", "\n", "~~~~\n", "\n", "tag b และ u จะทำให้ข้อความที่ถูกล้อมไว้ออกมาเป็นตัวหนาและขีดเส้นใต้\n", "\n", "จงเขียน function ที่ print ภาษา HTML ออกมาแบบดังตัวอย่างเช่น\n", "~~~~\n", ">>>> make_HTML('\"Hello, World\"', 'b') \n", "\n", " \n", " \"Hello, World\"\n", " \n", "\n", "\n", ">>>> make_HTML_two_tags('\"Hello, World\"', 'b', 'u') \n", "\n", " \n", " \"Hello, World\"\n", " \n", "\n", "\n", "~~~~\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def make_HTML(text, tag_name):\n", " \n", "\n", "def make_HTML_two_tags(text, tag_name1, tag_name2):\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 3\n", "จงเขียน function ที่เปรียบเทียบ String ว่าเหมือนกันหรือไม่ โดยไม่คำนึงถึงตัวเล็กตัวใหญ่ (case insensitive)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 4\n", "จงเขียน function ที่ตัดสินว่า string ที่ให้มานั้นเป็นพยัญชนะภาษาอังกฤษล้วนๆ หรือไม่" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_all_consonants(text):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 5\n", "จงเขียน function ที่นับจำนวนของคำที่ระบุให้ว่าปรากฏอยู่ใน String กี่ครั้ง" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def count_occurrences(text, word):\n", " '''\n", " \n", " >>>> count_occurrences('You see me I be work work work work work work ', 'work')\n", " 6\n", " >>>> count_occurrences('Before the table turn turn turn turn ', 'turn')\n", " 4\n", " '''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 6\n", "บางทีเวลาเราอ่าน String ออกมาจากแหล่งอื่น String มักจะไม่ค่อยอยู่ในสภาพที่ดี ที่พบบ่อยคือมักจะมีเว้นว่างหลายช่องข้างหน้าข้างหลังของข้อความเช่น \n", "\n", "`' ความกตัญญูเป็นเครื่องหมายของคนดี '`\n", "\n", "เรามักจะต้อง trim ข้อความให้ไม่มี whitespace ข้างหน้าและข้างหลังข้อความ \n", "\n", "จงเขียน function ที่ใช้ในการ trim string" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def trim_string(text):\n", " '''\n", " \n", " >>>> trim_string(' ความกตัญญูเป็นเครื่องหมายของคนดี ')\n", " ความกตัญญูเป็นเครื่องหมายของคนดี\n", " '''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 7\n", "จงเขียน function นับจำนวน bilabial consonants ใน phonemic transcription ภาษาอังกฤษที่ให้มา เช่น เสียง /bīp/ มีสองตัว" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def count_bilabial_consonants(phonemic_trans):\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 8\n", "character แต่ละตัวใน string ของ python3 นั้นที่จริงแล้วเป็นตัวเลขโดยเราสามารถแปลงตัวอักษรให้เป็นตัวเลขที่ใช้เก็บค่าแทนตัวอักษรโดยใช้ built-in function `ord` เช่น\n", "~~~\n", "ord('a') \n", ">>>> 97\n", "ord('b')\n", ">>>> 98\n", "ord('z')\n", ">>>> 122\n", "~~~\n", "\n", "ตัวเลขเหล่านี้เรียกว่า Unicode code point สามารถเปิดดูตัวอย่างได้ที่\n", "https://en.wikipedia.org/wiki/List_of_Unicode_characters\n", "\n", "และเราสามารถแปลง Unicode code point ให้เป็นตัวอักษรได้โดยใช้ function `chr`\n", "\n", "~~~\n", "chr(97)\n", ">>>> 'a'\n", "chr(3585)\n", ">>>> 'ก'\n", "~~~\n", "\n", "เราสามารถใช้ unicode code point ในการแปลงข้อความให้เป็นรหัสลับ (encryption) ได้ง่ายๆ โดยบวก unicode code point เดิมกับจำนวนคงที่เล็กๆ (offset) เช่น 1 หรือ 2 จากนั้นก็เปลี่ยน code point ใหม่นี้กลับเป็น string เช่น 'abc' อาจจะกลายเป็น 'bcd' ถ้าเราบวก code point ด้วยเลข 1\n", "\n", "จงเขียน function ที่ encrypt ข้อความโดยวิธีการดังกล่าว และเขียน function ที่ถอดรหัสกลับมาสู่ข้อความเดิม (ถ้าหากจะถอดรหัสได้อย่างถูกต้อง ผู้ใช้จะต้องรู้ว่า offset ที่ใช้ในการ encrypt นั้นคืออะไร)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def simple_encrypt(text, offset):\n", " '''\n", " \n", " simple_encrypt('ความกตัญญูเป็นเครื่องหมายของคนดี')\n", " >>>> ฅศำยขถาฎฎฺแผ่บแฅฤุ้ฮจฬยำรฃฮจฅบตึ\n", " '''\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def simple_decrypt(text, offset):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 9\n", "จงเขียน function ที่ตัดสินว่า string ภาษาอังกฤษที่ให้มานั้นมีทั้งตัวเล็ก และตัวใหญ่หรือไม่" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 10\n", "จงเขียน function ที่แปลง regular verb รูป infinitive ให้กลายเป็น past tense โดยการเติม -ed โดยต้องสามารถแปลงรูปสำหรับ verb เหล่านี้ให้ได้ถูกต้อง\n", "\n", "* rain\n", "* clamp\n", "* panic\n", "* traffic\n", "* invade\n", "* reverse" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 11\n", "จงเขียน function ที่ลบ HTML tag ที่ล้อมรอบข้อความอยู่ โดยให้ assume ว่ามี HTML tag เพียงชั้นเดียว " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def remove_tag(line):\n", " '''\n", " \n", " remove_tag('bold-faced terms here')\n", " >>>> 'bold-faced terms here'\n", " remove_tag('important message')\n", " >>>> 'important message'\n", " '''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ข้อ 12\n", "เวลาเราได้ข้อมูลภาษาเข้ามาบางทีเราต้องลบข้อมูลส่วนตัวที่อาจจะปะปนมากับข้อมูล เช่น ชื่อ ที่อยู่ เบอร์โทรศัพท์ กระบวนการนี้เรียกว่า data scrubbing คือถูเอาข้อมูลส่วนตัวออกไป ในทางปฏิบัติแล้วแทนที่จะลบข้อมูลออก เราจะแทนข้อมูลเดิมด้วยข้อมูลอื่นๆ ที่เราตั้งขึ้นมาเอง เช่น อาจจะเปลี่ยนชื่อที่พบในข้อมูลเป็นชื่อสมมติอื่น\n", "\n", "จงเขียน function ที่เปลี่ยนชื่อของคนที่ระบุ มาเป็นชื่อสมมติ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def scrub_name(text, name, hypothetical_name):\n", " '''\n", " \n", " scrub_name('The CEO of Facebook is Mark Zuckerberg who is now a billionaire. His employees like Mark.', 'Mark', 'David')\n", " >>>> 'The CEO of Facebook is David Zuckerberg who is now a billionaire. His employees like David.'\n", " '''" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }