寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

paizaラーニング: Pythonによる「データセット選択メニュー 」問題集 商品の検索

paizaラーニングのレベルアップ問題集「データセット選択メニュー 」からの出典です。
paiza.jp
Pythonによる「データセット選択メニュー 」問題集 商品の検索 と、それらの提出コードの解答例です。

僕が作成、提出したコードは、以下のとおりです。

・STEP: 1 数値の出現率

paiza.jp
数列から各数値の出現回数を求めます。

'''
Pythonによる「データセット選択メニュー 」問題集 商品の検索 
STEP: 1 数値の出現率 
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step1
提出コードの解答例
https://neguse-atama.hatenablog.com
'''
# coding: utf-8
n = int(input())
a = list(map(int, input().split()))
lis = [0]*10
for i in a:
    if i == 0:
        lis[i] += 1
    elif i == 1:
        lis[i] += 1
    elif i == 2:
        lis[i] += 1
    elif i == 3:
        lis[i] += 1
    elif i == 4:
        lis[i] += 1
    elif i == 5:
        lis[i] += 1
    elif i == 6:
        lis[i] += 1
    elif i == 7:
        lis[i] += 1
    elif i == 8:
        lis[i] += 1
    elif i == 9:
        lis[i] += 1
print(*lis)
・STEP: 2 英小文字の出現率

paiza.jp
文字列から各文字の出現回数を求めます。

'''
Pythonによる「データセット選択メニュー 」問題集 商品の検索 
STEP: 2 英小文字の出現率 
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step2
提出コードの解答例
https://neguse-atama.hatenablog.com
'''
# coding: utf-8
n = int(input())
s = input()
lis = [0]*26
a = ord("a")
for i in s:
    num = ord(i)
    lis[num-a] += 1
print(*lis)
・STEP: 3 文字列の出現率

paiza.jp
文字列の出現回数を求めます。

'''
Pythonによる「データセット選択メニュー 」問題集 商品の検索 
STEP: 3 文字列の出現率 
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step3
提出コードの解答例
https://neguse-atama.hatenablog.com
'''
# coding: utf-8
n = int(input())
lis = []
ans = []
for i in range(n):
    s = input()
    lis.append(s)
    if s not in ans:
        ans.append(s)
ans.sort()
for i in ans:
    num = lis.count(i)
    print(i, num)
・STEP: 4 価格の算出

paiza.jp
各商品に対してその価格を求めます。

'''
Pythonによる「データセット選択メニュー 」問題集 商品の検索 
STEP: 4 価格の算出 
https://paiza.jp/works/mondai/data_structure/data_structure__dict_step4
提出コードの解答例
https://neguse-atama.hatenablog.com
'''
# coding: utf-8
n, m = map(int, input().split())
foo = []
bar = []
for i in range(n):
    a, b = input().split()
    foo.append(a)
    bar.append(b)
for i in range(m):
    s = input()
    if s in foo:
        num = foo.index(s)
        print(bar[num])
    else:
        print(-1)
・FINAL問題 商品の検索

paiza.jp
文字列の探索をします。

'''
Pythonによる「データセット選択メニュー 」問題集 商品の検索 
FINAL問題 商品の検索 
https://paiza.jp/works/mondai/data_structure/data_structure__dict_boss
提出コードの解答例
https://neguse-atama.hatenablog.com
'''
# coding: utf-8
n, q = map(int, input().split())
lis = []
for i in range(n):
    s = input()
    lis.append(s)
for i in range(q):
    t = input()
    if t in lis:
        num = lis.index(t)
        print(num+1)
    else:
        print(-1)

paizaラーニングのレベルアップ問題集については、ユーザー同士で解答を教え合ったり、コードを公開したりするのは自由としています。
また授業や研修、教材などにも利用できるそうです。