Advertisement
Guest User

Python Walkthrough2 [OR2DE]

a guest
Apr 8th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. # While loop
  2. # The power of two - ข้อนี้ที่ค้างไว้ตั้งแต่ครั้งที่แล้วค่ะ หากำลังสอง ที่มากที่สุดที่มากกว่าเท่ากับเลขที่กำหนดค่ะ (ข้อนี้ห้ามใช้ เครื่องหมายยกกำลังค่ะ (**) )
  3.  
  4. n = int(input())
  5. result, i = 1, 0
  6.  
  7. while result * 2 <= n:
  8.     result *= 2
  9.     i += 1
  10. print(i, result)
  11.  
  12.  
  13. # Lists
  14. # The Largest Element - print ค่าที่มากที่สุด และ index ของค่าที่มากที่สุดใน list
  15.  
  16. a = [int(s) for s in input().split()]
  17. print(max(a), a.index(max(a)))
  18.  
  19.  
  20. # Functions and recursions
  21. # Uppercase - เขียนฟังก์ชัน capitalize เพื่อ return ชื่อแบบ proper case คือมีตัวพิมพ์ใหญ่หนึ่งตัว ตามด้วยตัวพิมพ์เล็ก (ข้อนี้ไม่ต้องทำตามวิธีที่โจทย์แนะนำก็ได้ แต่ต้องสร้าง function)
  22.  
  23. def capitalize(word: str) -> str:
  24.     return word.capitalize()
  25.  
  26.  
  27. for i in input().split(): print(capitalize(i), end=" ")
  28.  
  29. #OR
  30.  
  31. def capitalize(word: str) -> str:
  32.     return word.title()
  33.  
  34. print(capitalize(input()))
  35.  
  36.  
  37. # Two dimensional lists
  38. # Snowflake - พิมพ์รูป snowflake ด้วย . และ * โดยใช้ array (list) 2 มิติ
  39.  
  40. # Nested loop
  41. n = int(input())
  42. a = [['.'] * n for i in range(n)]
  43. for i in range(n):
  44.     for j in range(n):
  45.         if i == j:
  46.                 a[i][j] = '*'
  47.         elif n - 1 -i == j:
  48.                 a[i][j] = '*'
  49.         elif i == n//2 or j == n//2 :
  50.                 a[i][j] = '*'
  51.         print(a[i][j], end=" ")
  52.     print()
  53.  
  54. # Non-nested Loop
  55. n = int(input())
  56. a = [['.'] * n for i in range(n)]
  57. for i in range(n):
  58.     a[i][i] = '*'
  59.     a[i][n - 1 - i] = '*'
  60.     a[n//2][i] = '*'
  61.     a[i][n//2] = '*'
  62. for row in a:
  63.     print(" ".join(row))
  64. # Cannot print within the same loop,
  65. # because the array needed to update afterward.
  66.  
  67. # Sets
  68. # The number of distinct numbers - หาตัวเลขทีทั้งหมดที่มี ไม่นับตัวซ้ำ (ลองเขียนด้วย 1 บรรทัด)
  69.  
  70. print(len(set(input().split())))
  71.  
  72.  
  73. # Dictionary
  74. # Election in the USA - รวมผลโหวตประธานาธิบดีเรียงตามลำดับตัวอักษร
  75.  
  76. n = int(input())
  77. v = {}
  78. for i in range(n):
  79.     name, count = input().split()
  80.     v[name] = v[name] + int(count) if name in v.keys() else int(count)
  81. for name in sorted(v): print(name, v[name])
  82.  
  83.  
  84. # English - Latin dictionary - ทำ dictionary แปลงภาษา English - Latin ใช้การตัด string
  85. n = int(input())
  86. dictionary = {}
  87. for line in range(n):
  88.     eng, latin = input().split(" - ")
  89.     for lat in latin.split(", "):
  90.             if lat not in dictionary:
  91.                     dictionary[lat] = [eng.strip()]
  92.             else:
  93.                     dictionary[lat].append(eng.strip())
  94. print(len(dictionary))
  95. for lat, eng in sorted(dictionary.items()):
  96.     print(lat, "-", ", ".join(eng))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement