Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. โปรแกรม Python นับจำนวนคำและตัวอักษรในสตริง

    นับจำนวนคำและตัวอักษรในสตริง โดยนับตัวอักษรทุกตัว และนับคำจากจำนวนช่องว่างตัวอย่างโค้ดmy_string = Hi there, how are you Will ? print(The string is:, my_string) chars = 0 words = 1 # เริ่มที่ 1 เพราะคำแรกไม่มี space นำหน้า for ch in my_string: chars += 1 if ch == : words += 1 pri

  2. โปรแกรม Python Depth First Search แบบ Post-order Traversal

    Post-order Traversal เยี่ยมชม Left → Right → Root ใช้ Recursion สำหรับ General Tree (ไม่ใช่ Binary Tree เท่านั้น)ตัวอย่างโค้ดclass TreeNode: def __init__(self, key): self.key = key self.children = [] def add_child(self, node): self.children.append(node)

  3. สร้าง Mirror Copy ของ Binary Tree และแสดงด้วย BFS ใน Python

    Mirror Tree คือต้นไม้ที่ลูกซ้ายและลูกขวาของทุกโหนดถูกสลับกัน สร้างโดย Recursion แล้วแสดงด้วย BFSตัวอย่างโค้ดfrom collections import deque class Node: def __init__(self, key): self.key = key self.left = None self.right = None def mirror_copy(self): new_no

  4. เปรียบเทียบความยาวสตริงสองสตริงไม่ใช้ Built-in Function (เวอร์ชัน 2)

    อีกตัวอย่างการเปรียบเทียบความยาวสตริงสองสตริงโดยนับตัวอักษรด้วยลูป for เองตัวอย่างโค้ดs1 = Malala s2 = Male print(First string:, s1) print(Second string:, s2) c1 = c2 = 0 for _ in s1: c1 += 1 for _ in s2: c2 += 1 if c1 < c2: print(Longer:, s2) elif c1 == c2: print(Equal length) el

  5. นับตัวอักษรพิมพ์เล็กในสตริง Python (เวอร์ชัน 2)

    นับจำนวนตัวอักษรพิมพ์เล็กในสตริงด้วยเมธอด islower() - เวอร์ชันอีกแบบหนึ่งตัวอย่างโค้ดmy_string = Hi there how are you print(The string is:, my_string) count = sum(1 for ch in my_string if ch.islower()) print(Number of lowercase characters:, count)ผลลัพธ์The string is: Hi there how are you Number of

  6. สร้างสตริงใหม่จาก 2 ตัวแรกและ 2 ตัวสุดท้ายของสตริงเดิมใน Python

    เมื่อต้องการสร้างสตริงใหม่จากตัวอักษร 2 ตัวแรกและ 2 ตัวสุดท้ายของสตริงเดิม ใช้ String Slicingตัวอย่างโค้ดmy_string = Hi there how are you print(Original:, my_string) # วิธี 1: slicing โดยตรง new_string = my_string[:2] + my_string[-2:] print(New string (slicing):, new_string) # วิธี 2: นับความยาวก่

  7. พิมพ์ Permutation ทั้งหมดของสตริงแบบ Lexicographic ด้วย Recursion ใน Python

    สร้าง Permutation ทั้งหมดของสตริงในลำดับ Lexicographic (พจนานุกรม) โดยใช้อัลกอริทึม Next Permutation แบบเรียกซ้ำตัวอย่างโค้ดdef next_permutation(seq): # หา index i ที่ seq[i] seq[i] จากขวา j = len(seq) - 1 while seq[j] <= seq[i]: j -= 1 # สลับและกลับลำดับส่วนท้าย se

  8. แสดงโหนดของ Tree ด้วย BFS Traversal ใน Python

    Breadth-First Search (BFS) เรียงโหนดตามระดับ (Level Order) ใช้ Queue เก็บโหนดรอประมวลผลตัวอย่างโค้ดfrom collections import deque class TreeNode: def __init__(self, data): self.key = data self.left = None self.right = None def bfs(self): q = deque([self])

  9. โปรแกรม Python หาผลรวมของโหนดทั้งหมดใน Binary Tree

    เมื่อต้องการหาผลรวมค่าของโหนดทั้งหมดใน Binary Tree ใช้ Recursion ผ่านทุกโหนด รวมค่า key ของโหนดปัจจุบันกับผลรวมจากลูกซ้ายและลูกขวาตัวอย่างโค้ดclass TreeNode: def __init__(self, data): self.key = data self.left = None self.right = None def sum_nodes(self):

  10. หาโหนดที่ถึงได้ (Reachable Nodes) จากโหนดกำหนดด้วย BFS ใน Graph

    เมื่อต้องการหาโหนดทั้งหมดที่ถึงได้จากโหนดเริ่มต้นแต่ละตัวในลิสต์ ใช้ BFS จากแต่ละโหนดนั้นตัวอย่างโค้ดfrom collections import deque def add_edge(adj, u, v): adj[u].append(v) adj[v].append(u) def bfs_reachable(adj, start): visited = [False] * len(adj) q = deque([start]) visited[s

  11. หา Connected Components ใน Undirected Graph ด้วย BFS ใน Python

    ใช้ BFS หา Connected Components ทั้งหมดในกราฟไม่มีทิศทาง โดยเริ่ม BFS จากโหนดที่ยังไม่ได้เยี่ยมชมแต่ละตัวตัวอย่างโค้ดfrom collections import deque def bfs_components(adj, V): visited = [False] * V components = [] for start in range(V): if not visited[start]: com

  12. ตรวจสอบว่า Undirected Graph มี Cycle หรือไม่ด้วย BFS ใน Python

    ใช้ BFS ตรวจจับ Cycle ในกราฟไม่มีทิศทาง โดยติดตาม parent ของแต่ละโหนด ถ้าเจอเพื่อนบ้านที่เยี่ยมชมแล้วแต่ไม่ใช่ parent → มี Cycleตัวอย่างโค้ดfrom collections import deque def has_cycle_bfs(adj, V): visited = [False] * V for start in range(V): if not visited[start]: parent

  13. โปรแกรม Python ลบตัวอักษรที่ Index ที่กำหนดออกจากสตริง

    เมื่อต้องการลบตัวอักษรที่ตำแหน่ง index เฉพาะจากสตริง ให้วนลูปสร้างสตริงใหม่โดยข้าม index นั้นตัวอย่างโค้ดmy_string = Hi there how are you print(The string is:, my_string) index_to_remove = 2 new_string = for i in range(len(my_string)): if i != index_to_remove: new_string += my_string[

  14. โปรแกรม Python สลับตัวอักษรแรกและตัวอักษรสุดท้ายของสตริง

    เมื่อต้องการสร้างสตริงใหม่โดยสลับตัวอักษรแรกและตัวสุดท้าย ใช้ Slicing ของ Pythonตัวอย่างโค้ดdef swap_first_last(s): if len(s) <= 1: return s return s[-1] + s[1:-1] + s[0] my_string = Hi there how are you print(Original:, my_string) print(Modified:, swap_first_last(my_string))ผลลัพธ์

  15. โปรแกรม Python คำนวณความยาวสตริงโดยไม่ใช้ Library Function

    เมื่อต้องการหาความยาวสตริงโดยไม่ใช้ len() ให้วนลูปนับตัวอักษรด้วยตัวแปรนับตัวอย่างโค้ดmy_string = Hi Will print(The string is:, my_string) count = 0 for ch in my_string: count += 1 print(The length of the string is:, count)ผลลัพธ์The string is: Hi Will The length of the string is: 7คำอธิบายเริ

  16. หา Connected Components ทั้งหมดใน Undirected Graph ด้วย DFS ใน Python

    เมื่อต้องการหา Connected Components ทั้งหมดในกราฟไม่มีทิศทาง ใช้ Depth First Search (DFS) จากแต่ละโหนดที่ยังไม่ได้เยี่ยมชมตัวอย่างโค้ดclass Graph: def __init__(self, V): self.V = V self.adj = [[] for _ in range(V)] def add_edge(self, u, v): self.adj[u].append(v)

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:267/451  20-คอมพิวเตอร์/Page Goto:1 261 262 263 264 265 266 267 268 269 270 271 272 273