Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Largest Perimeter Triangle (สามเหลี่ยมเส้นรอบรูปมากที่สุด) ใน Python

    โจทย์: ให้ Array ของความยาวด้านบวก หาเส้นรอบรูปมากที่สุดของสามเหลี่ยมที่มีพื้นที่ไม่เป็นศูนย์ จาก 3 ด้าน หากไม่สามารถสร้างได้ คืน 0 Input/Output Input: [3, 6, 2, 3] Output: 8 (สามเหลี่ยมด้าน 3, 3, 2) หลักการ: Triangle Inequality Theorem อีกด้านหนึ่ง a อัลกอริทึม เรียง Array จากน้อยไปมาก Pop

  2. Binary Prefix Divisible By 5 ใน Python

    โจทย์: ให้ Array A ของ 0 และ 1 ให้ N[i] คือค่าบाइनนารีของ Subarray A[0] ถึง A[i] หา List ของ Boolean ว่า N[i] หาร 5 ลงตัวหรือไม่ Input/Output Input: [0, 1, 1, 1, 1, 1] Output: [True, False, False, False, True, False] อัลกอริทึม แปลง Array ทั้งหมดเป็นตัวเลข Binary หนึ่งตัว วนลูปจาก MSB ไป LSB (ขว

  3. โปรแกรมหา Longest Common Directory Path ใน Python

    โจทย์: หา Directory Path ที่ยาวที่สุดและซ้ำกัน (Common prefix) จากลิสต์ของ File Paths Input/Output paths = [home/tutorialspoint/python, home/tutorialspoint/c, home/tutorialspoint/javascript, home/tutorialspoint/react, home/tutorialspoint/django] Output: home/tutorialspoint วิธีแก้: ใช้ os.path Mod

  4. โปรแกรมตรวจสอบความเท่ากันของสามเหลี่ยม (Congruency) ใน Python

    โปรแกรมนี้ตรวจสอบความเท่ากัน (Congruency) และคล้ายกัน (Similarity) ของสามเหลี่ยม 2 รูป ตามเกณฑ์มาตรฐาน: SSS (Side-Side-Side), SAS (Side-Angle-Side), AAA (Angle-Angle-Angle) หลักเกณฑ์ SSS: ด้านทั้ง 3 คู่สัมพันธ์กัน SAS: ด้าน 2 คู่สัมพันธ์ และมุมระหว่างด้านเท่ากัน AAA: มุมทั้ง 3 เท่ากัน (คล้ายกัน ไม

  5. การดึงสมาชิกจาก Sublist จนถึง Index N ใน Python

    โจทย์: ให้ลิสต์ของ Sublist ต้องการดึงสมาชิกตัวแรก (หรือ Index ใดๆ) จาก Sublist แรก ถึง N Input/Output random_list = [[Python, Java], [C, Pascal], [Javascript, PHP], [C#, C++], [React, Angular]] N = 3 Output: [Python, C, Javascript] # สมาชิกตัวแรกของ 3 sublist แรก วิธีที่ 1: ใช้ For Loop random_li

  6. การจัดกลุ่ม Anagrams (คำอ่านคล้ายกัน) ใน Python

    Anagrams คือ คำที่ประกอบด้วยตัวอักษรชุดเดียวกัน แต่เรียงลำดับต่างกัน (เช่น cat และ tac, listen และ silent) Input/Output Input: [cat, dog, fired, god, pat, tap, fried, tac] Output: [[cat, tac], [dog, god], [fried, fired], [pat, tap]] ขั้นตอนการแก้ปัญหา สร้างฟังก์ชันเช็ค Anagram: เรียงตัวอักษรทั้ง

  7. การจัดกลุ่มสมาชิก Index เดียวกันจากหลายลิสต์ (Transpose) ใน Python

    โจทย์: ให้ลิสต์ของลิสต์ (Matrix) ทุก sublist ยาวเท่ากัน ต้องการจัดกลุ่มสมาชิก Index เดียวกันจากทุก sublist (Matrix Transpose) Input/Output Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]] วิธีที่ 1: ใช้ Nested Loop (Manual) lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9

  8. การจัดกลุ่ม Tuple ในลิสต์ตามค่าตัวแรก (First Value) ใน Python

    โจทย์: ให้ลิสต์ของ Tuple จัดกลุ่ม Tuple ที่มีค่าตัวแรก (Index 0) เหมือนกัน โดยรวมค่าต่อจากตัวแรกเข้าด้วยกัน Input/Output Input: [(1, 2, 3), (1, 4, 5), (3, 4, 1), (3, 4, 2)] Output: [(1, 2, 3, 4, 5), (3, 4, 1, 4, 2)] วิธีแก้: ใช้ Dictionary + Tuple Unpacking tuples = [(1, 2, 3), (1, 4, 5), (3, 4,

  9. การรวมค่า Tuple List ตาม Key เดียวกันใน Python (Grouped Summation)

    โจทย์: มี 2 ลิสต์ของ Tuple (key, value) ต้องการรวม value ของ key เดียวกันจากทั้ง 2 ลิสต์ Input/Output list_one = [(a, 2), (b, 3), (c, 5)] list_two = [(c, 7), (a, 4), (b, 2)] Output: [(a, 6), (b, 5), (c, 12)] วิธีที่ 1: ใช้ Dictionary + Loop list_one = [(a, 2), (b, 3), (c, 5)] list_two = [(c, 7), (

  10. การจัดกลุ่ม Substring ที่คล้ายกันในลิสต์ Python

    โจทย์: ให้ลิสต์สตริงที่มีรูปแบบ prefix-suffix จัดกลุ่มตาม prefix (ส่วนหน้าของ -) Input/Output Input: [tutorials-python, tutorials-c, tutorials-java, tutorials-javascript, python-1, python-2, javascript-1] Output: [[tutorials-python, tutorials-c, tutorials-java, tutorials-javascript], [python-1, py

  11. ไลบรารี Holidays ใน Python สำหรับตรวจสอบวันหยุด

    ไลบรารี holidays ช่วยตรวจสอบวันหยุดราชการ/สาธารณะของประเทศต่างๆ ได้อย่างง่ายดาย การติดตั้ง pip install holidays การใช้งานพื้นฐาน 1. ดูวันหยุดทั้งปีของประเทศ (ตัวอย่าง: ประเทศอินเดีย ปี 2020) import holidays india_holidays = holidays.India(years=2020) for date, occasion in india_holidays.items():

  12. การดึงชื่อฟังก์ชัน (Function Name) ใน Python

    การได้ชื่อฟังก์ชันขณะรันไทม์ (Runtime) ทำได้ต่างกันใน Python 2 และ Python 3 Python 2 (Deprecated) ใช้ attribute func_name def testing_function(): This is a simple function for testing return None print(Function name is (Python2) {}.format(testing_function.func_name)) Function name is (Pyth

  13. การเข้าถึงสมาชิกตัวสุดท้ายของลิสต์ใน Python

    มีหลายวิธีในการดึงสมาชิกตัวสุดท้ายของลิสต์ใน Python วิธีที่ 1: ใช้ Index แบบคำนวณ (len - 1) numbers = [1, 2, 3, 4, 5] print(fLast element: {numbers[len(numbers) - 1]}) Last element: 5 วิธีที่ 2: ใช้ Negative Index (แนะนำ) Python รองรับ Negative Index: -1 = ตัวสุดท้าย, -2 = ตัวก่อนสุดท้าย, ฯลฯ numb

  14. การเรียงลำดับลิสต์สตริง (Sort List of Strings) ใน Python

    บทความนี้ครอบคลุมการเรียงลำดับลิสต์สตริง ด้วย list.sort() และ sorted() รวมถึงการเรียงตามเกณฑ์ต่างๆ (ความยาว, ค่าตัวเลข, ฯลฯ) 1. ใช้ list.sort() (In-place sorting) sort() เรียงลิสต์เดิมโดยตรง ไม่คืนค่าลิสต์ใหม่ เรียงขึ้น (Ascending) strings = [Python, C, Java, Javascript, React, Django, Spring] str

  15. รูปแบบการเพิ่มสมาชิกสลับกันแบบเพิ่มขึ้น (Increasing Alternate Pattern) ในลิสต์ Python

    โจทย์: จากลิสต์ที่กำหนด ให้สร้างลิสต์ใหม่โดยแทรกสัญลักษณ์ # จำนวนเพิ่มขึ้นทีละ 1 หลังสมาชิกแต่ละตัว Input/Output Input: [a, b, c] Output: [a, #, b, ##, c, ###] วิธีแก้: ใช้ Loop และ String Multiplication alphabets = [a, b, c] result = [] for i in range(len(alphabets)): result.append(alphabet

  16. การวนลูป Cyclic จาก Index ที่กำหนดในลิสต์ Python

    โจทย์: วนลูปแสดงสมาชิกในลิสต์แบบ Cyclic (วนรอบ) เริ่มจาก Index ที่กำหนด Input/Output alphabets = [a, b, c, d, e, f, g, h] start_index = 5 Output: f g h a b c d e วิธีแก้: ใช้ Modulo Operator (%) ใช้ index % length เพื่อให้ index วนกลับมาที่ 0 เมื่อถึงปลายลิสต์ alphabets = [a, b, c, d, e, f, g, h]

  17. การหา Index ของสมาชิกใน Sublist (Nested List) ด้วย Python

    โจทย์: หาดัชนีของ sublist ที่มีสมาชิกตามที่ต้องการ Input/Output nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] # ค้นหา 7, 5, 3 Output: Index of 7: 2 Index of 5: 1 Index of 3: 0 วิธีแก้: Nested Loop พร้อม Flag nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] def find_index(element): is_found =

  18. การหา Index ของตัวเลขที่มากกว่า K ในลิสต์ Python

    โจทย์: หาดัชนี (Index) ของสมาชิกในลิสต์ที่มีค่ามากกว่า K Input/Output numbers = [3, 4, 5, 23, 12, 10, 16] K = 10 Output: 3 4 6 (index ของ 23, 12, 16) วิธีที่ 1: ใช้ Loop ธรรมดา K: print(i, end= ) ผลลัพธ์ 3 4 6 วิธีที่ 2: ใช้ enumerate() และ List Comprehension (แนะนำ) enumerate() ให้ (i

  19. การคัดลอก Nested List (ลิสต์ซ้อน) ใน Python

    การคัดลอก Nested List (ลิสต์ที่มีลิสต์อยู่ภายใน) ต้องระวังเรื่อง Shallow Copy vs Deep Copy บทความนี้เปรียบเทียบ 3 วิธี วิธีที่ 1: ใช้ Loop แบบ Manual (Deep Copy) nested_list = [[1, 2], [3, 4], [5, 6, 7]] copy = [] for sub_list in nested_list: temp = [] for element in sub_list: temp

  20. เริ่มต้นใช้งาน psycopg2 กับ PostgreSQL ใน Python

    บทความนี้แนะนำการใช้ PostgreSQL กับ Python ผ่านไลบรารี psycopg2 การเตรียมความพร้อม ติดตั้ง PostgreSQL และสร้างฐานข้อมูลผ่าน pgAdmin ติดตั้ง psycopg2: pip install psycopg2 ขั้นตอนการเชื่อมต่อและใช้งาน Import psycopg2 เก็บข้อมูลเชื่อมต่อ (database, user, password) ในตัวแปร สร้าง connection ด้วย p

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:149/451  20-คอมพิวเตอร์/Page Goto:1 143 144 145 146 147 148 149 150 151 152 153 154 155