iT邦幫忙

2024 iThome 鐵人賽

DAY 24
0
Python

一起來用 Snakify 練練手系列 第 24

【一起來用 Snakify 練練手】Day24 Lesson 10 練習題參考解答

  • 分享至 

  • xImage
  •  

從這裡開始 你可以試著用 def() 去定義
set 是很好用的東西
這一單元的題目不難

  • The number of distinct numbers
print(len(set(input().split())))
  • The number of equal numbers
print(len(set(input().split()) & set(input().split())))
  • The intersection of sets
print(*sorted(set(input().split()) & set(input().split()), key=int))
  • Has the number been encountered before
numbers = [int(s) for s in input().split()]
occur_before = set()
for num in numbers:
    if num in occur_before:
        print('YES')
    else:
        print('NO')
        occur_before.add(num)
  • Cubes
def print_set(some_set):
    print(len(some_set))
    print(*[str(item) for item in sorted(some_set)])

N, M = [int(s) for s in input().split()]
A_colors, B_colors = set(), set()
for i in range(N):
    A_colors.add(int(input()))
for i in range(M):
    B_colors.add(int(input()))
    
print_set(A_colors & B_colors)
print_set(A_colors - B_colors)
print_set(B_colors - A_colors)
  • The number of distinct words in some text
words = set()
for _ in range(int(input())):
    words.update(input().split())
print(len(words))
  • Guess the number
n = int(input())
all_nums = set(range(1, n + 1))
possible_nums = all_nums
while True:
    guess = input()
    if guess == 'HELP':
        break
    guess = {int(x) for x in guess.split()}
    answer = input()
    if answer == 'YES':
        possible_nums &= guess
    else:
        possible_nums &= all_nums - guess

print(' '.join([str(x) for x in sorted(possible_nums)]))
  • Polyglots
students = [{input() for j in range(int(input()))} for i in range(int(input()))]
known_by_everyone, known_by_someone = set.intersection(*students), set.union(*students)
print(len(known_by_everyone), *sorted(known_by_everyone), sep='\n')
print(len(known_by_someone), *sorted(known_by_someone), sep='\n')

你➕1了沒 台灣首發晶片設計教材要開始募資啦
趕快點擊去了解 👉 https://wlinny.com


上一篇
【一起來用 Snakify 練練手】Day23 Lesson 10 概念
下一篇
【一起來用 Snakify 練練手】Day25 Lesson 11 概念
系列文
一起來用 Snakify 練練手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言