Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # While loop
- # The power of two - ข้อนี้ที่ค้างไว้ตั้งแต่ครั้งที่แล้วค่ะ หากำลังสอง ที่มากที่สุดที่มากกว่าเท่ากับเลขที่กำหนดค่ะ (ข้อนี้ห้ามใช้ เครื่องหมายยกกำลังค่ะ (**) )
- n = int(input())
- result, i = 1, 0
- while result * 2 <= n:
- result *= 2
- i += 1
- print(i, result)
- # Lists
- # The Largest Element - print ค่าที่มากที่สุด และ index ของค่าที่มากที่สุดใน list
- a = [int(s) for s in input().split()]
- print(max(a), a.index(max(a)))
- # Functions and recursions
- # Uppercase - เขียนฟังก์ชัน capitalize เพื่อ return ชื่อแบบ proper case คือมีตัวพิมพ์ใหญ่หนึ่งตัว ตามด้วยตัวพิมพ์เล็ก (ข้อนี้ไม่ต้องทำตามวิธีที่โจทย์แนะนำก็ได้ แต่ต้องสร้าง function)
- def capitalize(word: str) -> str:
- return word.capitalize()
- for i in input().split(): print(capitalize(i), end=" ")
- #OR
- def capitalize(word: str) -> str:
- return word.title()
- print(capitalize(input()))
- # Two dimensional lists
- # Snowflake - พิมพ์รูป snowflake ด้วย . และ * โดยใช้ array (list) 2 มิติ
- # Nested loop
- n = int(input())
- a = [['.'] * n for i in range(n)]
- for i in range(n):
- for j in range(n):
- if i == j:
- a[i][j] = '*'
- elif n - 1 -i == j:
- a[i][j] = '*'
- elif i == n//2 or j == n//2 :
- a[i][j] = '*'
- print(a[i][j], end=" ")
- print()
- # Non-nested Loop
- n = int(input())
- a = [['.'] * n for i in range(n)]
- for i in range(n):
- a[i][i] = '*'
- a[i][n - 1 - i] = '*'
- a[n//2][i] = '*'
- a[i][n//2] = '*'
- for row in a:
- print(" ".join(row))
- # Cannot print within the same loop,
- # because the array needed to update afterward.
- # Sets
- # The number of distinct numbers - หาตัวเลขทีทั้งหมดที่มี ไม่นับตัวซ้ำ (ลองเขียนด้วย 1 บรรทัด)
- print(len(set(input().split())))
- # Dictionary
- # Election in the USA - รวมผลโหวตประธานาธิบดีเรียงตามลำดับตัวอักษร
- n = int(input())
- v = {}
- for i in range(n):
- name, count = input().split()
- v[name] = v[name] + int(count) if name in v.keys() else int(count)
- for name in sorted(v): print(name, v[name])
- # English - Latin dictionary - ทำ dictionary แปลงภาษา English - Latin ใช้การตัด string
- n = int(input())
- dictionary = {}
- for line in range(n):
- eng, latin = input().split(" - ")
- for lat in latin.split(", "):
- if lat not in dictionary:
- dictionary[lat] = [eng.strip()]
- else:
- dictionary[lat].append(eng.strip())
- print(len(dictionary))
- for lat, eng in sorted(dictionary.items()):
- print(lat, "-", ", ".join(eng))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement