Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. หาคู่ผสมทั้งหมด (All Pair Combinations) ของ 2 ทัพเพิลใน Python

    เมื่อต้องการสร้างคู่สั่ง (ordered pairs) ทั้งหมดจากช่วงตัวเลข 1 ถึง N สามารถใช้ itertools.product กับพารามิเตอร์ repeat=Nตัวอย่างโค้ดfrom itertools import product N = 2 print(The value of N is:, N) result = [ele for ele in product(range(1, N + 1), repeat=N)] print(All tuple combinations up to, N,

  2. ลบทัพเพิลที่มีความยาว K ออกจากลิสต์ใน Python

    เมื่อต้องการกรองลิสต์ของทัพเพิล ให้เหลือเฉพาะทัพเพิลที่ความยาวไม่เท่ากับ K สามารถใช้ List Comprehension กับเงื่อนไข len(ele) != Kตัวอย่างโค้ดmy_list = [(32, 51), (22, 13), (94, 65, 77), (70,), (80, 61, 13, 17)] print(The list is:) print(my_list) K = 1 print(The value of K is:, K) filtered = [ele f

  3. โปรแกรม Python เรียงลำดับทัพเพิลตามลิสต์ภายนอก

    เมื่อต้องการเรียงลำดับลิสต์ของทัพเพิลตามลำดับที่กำหนดในลิสต์อื่น สามารถแปลงเป็นดิกชันนารีแล้วสร้างลิสต์ใหม่ตามลำดับที่ต้องการตัวอย่างโค้ดmy_list = [(Mark, 34), (Will, 91), (Rob, 23)] print(The list of tuples is:) print(my_list) ordered_list = [Will, Mark, Rob] print(The ordered list is:) print(orde

  4. ทำให้ทัพเพิลของลิสต์แบน (Flatten Tuple of List to Tuple) ใน Python

    เมื่อต้องการทำให้ทัพเพิลที่มีโครงสร้างซ้อนซับซ้อนแบนเป็นทัพเพิลเดียวที่มีทัพเพิลย่อยเฉพาะระดับเดียว สามารถใช้ฟังก์ชันแบบเรียกซ้ำตัวอย่างโค้ดdef flatten_tuple(t): if isinstance(t, tuple) and len(t) == 2 and not isinstance(t[0], tuple): return (t,) result = [] for sub in t:

  5. แปลงทัพเพิลแบบซ้อน (Nested Tuple) เป็นดิกชันนารีที่มีคีย์กำหนดเองใน Python

    เมื่อต้องการแปลงทัพเพิลที่มีทัพเพิลย่อยอยู่ภายใน เป็นลิสต์ของดิกชันนารีที่มีคีย์เฉพาะ สามารถใช้ List Comprehensionตัวอย่างโค้ดmy_tuple = ((6, Will, 13), (2, Mark, 15), (9, Rob, 12)) print(The tuple is:) print(my_tuple) my_result = [{key: sub[0], value: sub[1], id: sub[2]} for sub in my_tuple] print

  6. โปรแกรม Python พิมพ์ตัวเลขในช่วงโดยไม่ใช้ลูป (Recursion)

    เมื่อต้องการพิมพ์ตัวเลข 1 ถึง n โดยไม่ใช้ลูป สามารถใช้การเรียกซ้ำ (recursion) โดยลดค่าลงทีละ 1 จนถึง 0 แล้วพิมพ์ขณะกลับมาจากการเรียกซ้ำตัวอย่างโค้ด 0: print_numbers(n - 1) print(n) upper = 6 print(The upper limit is:, upper) print(The numbers are:) print_numbers(upper)ผลลัพธ์The upp

  7. โปรแกรม Python คำนวณค่าคงที่ออยเลอร์ (e) ด้วยอนุกรมเทย์เลอร์

    เมื่อต้องการคำนวณค่าคงที่ออยเลอร์ e ใช้สูตรอนุกรม: e = 1 + 1/1! + 1/2! + ... + 1/n!ตัวอย่างโค้ดdef factorial(n): result = 1 for i in range(2, n + 1): result *= i return result def compute_e(n): s = 1.0 # เริ่มจาก 1 (พจน์แรก) for i in range(1, n + 1): s += 1.0 /

  8. โปรแกรม Python หาสามตัวเลขพีทาโกรัส (Pythagorean Triplets) ในช่วงที่กำหนด

    เมื่อต้องการหาสามตัวเลขพีทาโกรัส (a, b, c) ที่ทำให้ a² + b² = c² ภายในขอบเขตที่กำหนด สามารถใช้สูตรยูคลิด (Euclids formula)ตัวอย่างโค้ดdef pythagorean_triplets(limit): c, m = 0, 2 while c limit: break print(a, b, c) m += 1 upper_limit = 15 print(The upper

  9. โปรแกรม Python หาจำนวนครั้งที่ตัวเลขปรากฏในลิสต์

    เมื่อต้องการนับความถี่ของตัวเลขในลิสต์ สามารถกำหนดฟังก์ชันที่วนลูปผ่านทุกองค์ประกอบและเพิ่มตัวนับเมื่อพบค่าตรงกันตัวอย่างโค้ดdef count_occurrences(lst, target): count = 0 for elem in lst: if elem == target: count += 1 return count my_list = [66, 26, 48, 140, 66, 20,

  10. โปรแกรม Python ล้างบิตเซตขวามากที่สุด (Rightmost Set Bit) ของตัวเลข

    เมื่อต้องการล้างบิต 1 ขวามากที่สุดของตัวเลข สามารถใช้การดำเนินการ AND ระหว่างตัวเลขกับตัวเลขลบ 1ตัวอย่างโค้ดdef clear_rightmost_set_bit(n): return n & (n - 1) n_val = 6 print(The value of n is:, n_val) print(The number after unsetting the rightmost set bit is:) print(clear_rightmost_set_bit(n_v

  11. โปรแกรม Python สร้างรหัสเกรย์ (Gray Codes) ด้วยการเรียกซ้ำ (Recursion)

    เมื่อต้องการสร้างรหัสเกรย์แบบเรียกซ้ำ สามารถสร้างรายการว่าง เริ่มจาก [0, 1] แล้วขยายทีละขั้นโดยการคัดลอกย้อนกลับและเติมคำนำหน้า 0 และ 1ตัวอย่างโค้ดdef generate_gray_list(n): if n <= 0: return gray_list = [0, 1] i = 2 while i < (1 << n): for j in range(i - 1, -1, -1):

  12. โปรแกรม Python แปลงรหัสเกรย์ (Gray Code) เป็นรหัสไบนารี

    เมื่อต้องการแปลงรหัสเกรย์กลับเป็นรหัสไบนารี สามารถใช้วิธีการตรวจสอบบิตแต่ละตำแหน่งและสลับค่าตามกฎของ Gray Codeตัวอย่างโค้ดdef flip_bit(bit): return 1 if bit == 0 else 0 def gray_to_binary(gray): binary = binary += gray[0] for i in range(1, len(gray)): if gray[i] == 0:

  13. โปรแกรม Python แปลงรหัสไบนารีเป็นรหัสเกรย์ (Gray Code)

    เมื่อต้องการแปลงรหัสไบนารีเป็นรหัสเกรย์ สามารถกำหนดฟังก์ชันที่ดำเนินการ XOR ระหว่างตัวเลขกับตัวเลขที่ขยับบิตไปทางขวา 1 ตำแหน่งตัวอย่างโค้ด 1) return bin(n)[2:] gray_val = input(Enter the binary number: ) binary_val = binary_to_gray(gray_val) print(Gray codeword is:, binary_val)ผลลัพธ์Enter the

  14. โปรแกรม Python สำหรับพิมพ์สามเหลี่ยมปาสคาล (Pascal's Triangle) สำหรับจำนวนแถวที่ผู้ใช้ระบุ

    เมื่อต้องการพิมพ์สามเหลี่ยมปาสคาลสำหรับจำนวนแถวเฉพาะที่ผู้ใช้ป้อนเข้ามา สามารถใช้ลูป for แบบง่ายๆ ได้ตัวอย่างโค้ดfrom math import factorial rows = int(input(Enter the number of rows: )) for i in range(rows): for j in range(rows - i + 1): print(end= ) for j in range(i + 1): pr

  15. ตรวจสอบว่าเป็น Perfect Number หรือไม่ใน Python

    Perfect Number คือเลขที่เท่ากับผลรวมตัวหารบวกทั้งหมด (ไม่รวมตัวมันเอง) เช่น 6: 1+2+3 = 6 ตัวอย่างโค้ด n = 6 my_sum = 0 for i in range(1, n): if(n % i == 0): my_sum = my_sum + i if (my_sum == n): print(The number is a perfect number) else: print(The number is not a perfect number)

  16. ตรวจสอบว่าเป็น Strong Number หรือไม่ใน Python

    Strong Number คือเลขที่ผลรวมแฟกทอเรียลของทุกหลักเท่ากับตัวเลขนั้น เช่น 145: 1!+4!+5! = 1+24+120 = 145 ตัวอย่างโค้ด my_sum = 0 my_num = 296 print(The number is) print(my_num) temp = my_num while(my_num): i = 1 fact = 1 remainder = my_num % 10 while(i <= remainder): fact = fact

  17. คำนวณสมการพหุนาม (Polynomial) จากรายการสัมประสิทธิ์ใน Python

    เมื่อมีสัมประสิทธิ์พหุนามอยู่ใน List เรียงจากกำลังสูงสุดไปต่ำสุด สามารถคำนวณค่าพหุนามที่ x ใดๆ ได้ด้วยลูปซ้อน ตัวอย่างโค้ด my_polynomial = [2, 5, 3, 0] num = 2 poly_len = len(my_polynomial) my_result = 0 for i in range(poly_len): my_sum = my_polynomial[i] for j in range(poly_len - i - 1):

  18. ตรวจสอบว่าเลขสองจำนวนเป็น Amicable Numbers (จำนวนเพื่อน) ใน Python

    Amicable Numbers คือคู่เลขที่ผลรวมตัวหารของแต่ละจำนวน (ไม่รวมตัวมันเอง) เท่ากับอีกจำนวนหนึ่ง ตัวอย่างโค้ด import math def divided_sum_val(my_val): res = 0 for i in range(2, int(math.sqrt(my_val)) + 1): if (my_val % i == 0): if (i == int(my_val / i)): res

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