iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0
Python

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

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

  • 分享至 

  • xImage
  •  
  • Even indices
    😈 找索引值為偶數的項目
a = input().split()
for i in range(len(a)):
    if i%2 == 0:
        print(a[i])
  • Even elements
    😈 找偶數的項目
a = input().split()
for i in range(len(a)):
    if int(a[i])%2 == 0:
        print(a[i])
  • Greater than previous
    😈 比前者大的印出來
    👉 我是用一個變數去儲存然後相比
a = input().split()
pre = int(a[0])
for i in range(1, len(a)):
    if int(a[i])>pre:
        print(a[i])
    pre = int(a[i])
  • Neighbors of the same sign
    😈 相同符號印出來
    👉 如果符號一樣相乘等於1
a = input().split()
pre = int(a[0])
# print(a)
for i in range(1, len(a)):
    if pre*int(a[i])>0:
        print(pre, int(a[i]))
        break
    else:
        pre = int(a[i])
    
  • Greater than neighbours
    😈 比左右大
    👉 就...比大小
a = [int(s) for s in input().split()]
s = 0
for i in range(1, len(a) - 1):
  if a[i - 1] < a[i] > a[i + 1]:
    s += 1
print(s)
  • The largest element
    😈 找最大的
a = [int(s) for s in input().split()]
print(max(a))
print(a.index(max(a)))
  • The number of distinct elements
    😈 找出有幾個同的元素
a = [s for s in input().split()]
c = []
s = 0
for i in a:
    if i not in c:
        c.append(i)
        s += 1
print(s)
  • Swap neighbours
    😈 兩兩交換
a = [i for i in input().split()]

if len(a) <= 1:
    print(a[0])
else:
    if len(a)%2 == 0:
        for i in range(0, len(a), 2):
            print(a[i+1])
            print(a[i])
    else:
        for i in range(0, len(a)-1, 2):
            print(a[i+1])
            print(a[i])
        print(a[-1])
  • Swap min and max
    😈 交換最大和最小
a = [int(i) for i in input().split()]
x, n = a.index(max(a)), a.index(min(a))
a[x], a[n] = min(a), max(a)
print(' '.join([str(i) for i in a]))
  • The number of pairs of equal
    😈 計算有多少對相同的值
    👉 用雙迴圈
a = [int(i) for i in input().split()]
c = 0
for i in range(len(a)+1):
    for j in range(i+1, len(a)):
        if a[i] == a[j]:
            c+=1
print(c)
  • Unique elements
    😈 印出只出現過一次的數
a = [int(i) for i in input().split()]
b = []
for i in range(len(a)):
    if a.count(a[i]) == 1:
        b.append(a[i])
print(' '.join([str(i) for i in b]))

💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥 精神集中囉 💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥

  • Queens
    😈 下棋♟️
n = 8
x = []
y = []
for i in range(n):
    new_x, new_y = [int(s) for s in input().split()]
    x.append(new_x)
    y.append(new_y)

correct = True
for i in range(n):
    for j in range(i + 1, n):
        if x[i] == x[j] or y[i] == y[j] or abs(x[i] - x[j]) == abs(y[i] - y[j]):
            correct = False
if correct:
    print('NO')
else:
    print('YES')
  • The bowling alley
    😈 保齡球遊戲
n, k = map(int,input().split())
a = []
for i in range(k):
    row_array=[int(i) for i in input().split()]
    a.append(row_array)
    
pin=[1]*n
for i in range(k):
    for j in range(a[i][0]-1,a[i][1]):
        pin[j]=0
for i in range(n):
    if (pin[i]==0):
        print(".",sep="",end="")
    elif (pin[i]==1):
        print("I",sep="",end="")

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

尚未有邦友留言

立即登入留言