paizaラーニングのレベルアップ問題集「二重ループメニュー」からの出典です。
paiza.jp
Pythonによる「文字列処理メニュー」問題集と、それらの提出コードの解答例です。
僕が作成、提出したコードは、以下のとおりです。
・文字列の出力
''' Pythonによる「文字列処理メニュー 」問題集 文字列の出力 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 print("paiza")
・文字列の受け取り
''' Pythonによる「文字列処理メニュー 」問題集 文字列の受け取り https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() print(s)
・i 文字目の出力
''' Pythonによる「文字列処理メニュー 」問題集 i 文字目の出力 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() i = int(input()) print(s[i-1])
・文字列の条件判定
''' Pythonによる「文字列処理メニュー 」問題集 文字列の条件判定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() if s == "paiza": print("YES") else: print("NO")
・文字列の文字数
''' Pythonによる「文字列処理メニュー 」問題集 文字列の文字数 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() print(len(s))
・ 文字の検索
''' Pythonによる「文字列処理メニュー 」問題集 文字の検索 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() c = input() print(s.find(c)+1)
・ 文字列の連結
''' Pythonによる「文字列処理メニュー 」問題集 文字列の連結 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 n = int(input()) for i in range(n): s = input() if i == n-1: print(s) else: print(s, end="")
・部分文字列
''' Pythonによる「文字列処理メニュー 」問題集 部分文字列 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() i, j = map(int, input().split()) print(s[i-1:j])
・文字列の挿入
''' Pythonによる「文字列処理メニュー 」問題集 文字列の挿入 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() t = input() n = int(input()) print(s[:n] + t + s[n:])
・文字列の書き換え
''' Pythonによる「文字列処理メニュー 」問題集 文字列の書き換え https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() i, c = input().split() for n in range(len(s)): if n == int(i)-1: print(c, end="") else: print(s[n], end="")
・文字列から数値への変換
''' Pythonによる「文字列処理メニュー 」問題集 文字列から数値への変換 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = int(input()) print(s - 813)
・数値から文字列への変換
''' Pythonによる「文字列処理メニュー 」問題集 数値から文字列への変換 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 x = int(input()) y = int(input()) n = int(input()) foo = x + y bar = str(foo) print(bar[n-1])
・大文字から小文字への変換
''' Pythonによる「文字列処理メニュー 」問題集 大文字から小文字への変換 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() print(s.upper())
・小文字から大文字への変換
''' Pythonによる「文字列処理メニュー 」問題集 小文字から大文字への変換 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() print(s.swapcase())
・大文字小文字の反転
''' Pythonによる「文字列処理メニュー 」問題集 大文字小文字の反転 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() t = input() if t in s: print("YES") else: print("NO")
・文字列の検索
''' Pythonによる「文字列処理メニュー 」問題集 文字列の検索 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() for i in range(len(s)-1, -1, -1): print(s[i], end="")
・文字列の反転
''' Pythonによる「文字列処理メニュー 」問題集 文字列の反転 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 foo = input() bar = "".join(list(reversed(foo))) if foo == bar: print("YES") else: print("NO")
・回文判定
''' Pythonによる「文字列処理メニュー 」問題集 回文判定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = list(input().split(",")) for i in range(len(s)): print(s[i])
・文字列の分割
''' Pythonによる「文字列処理メニュー 」問題集 文字列の分割 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() print(s.lower())
・日時データの変換その1
''' Pythonによる「文字列処理メニュー 」問題集 日時データの変換その1 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = list(input().split("/")) for i in range(len(s)): if i == 3: print(s[3][:2]) print(s[3][-2:]) else: print(s[i])
・日時データの変換その2
''' Pythonによる「文字列処理メニュー 」問題集 日時データの変換その2 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 import re s = input() s = re.split("[/: ]", s) for i in range(len(s)): print(s[i])
・数値判定
''' Pythonによる「文字列処理メニュー 」問題集 数値判定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() if s.isnumeric(): print("YES") else: print("NO")
・重複の削除
''' Pythonによる「文字列処理メニュー 」問題集 重複の削除 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() lis = [] for i in range(len(s)): if not s[i] in lis: lis.append(s[i]) ans = "".join(lis) print(ans)
・パスワード作成
''' Pythonによる「文字列処理メニュー 」問題集 パスワード作成 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 n = int(input()) q = int(input()) dic = {} for i in range(q): num, s = input().split() dic[int(num)] = s c = input() lis = [] for i in range(1, n+1): if i in dic: lis.append(dic[i]) else: lis.append(c) print("".join(lis))
・表記の訂正
''' Pythonによる「文字列処理メニュー 」問題集 表記の訂正 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() n = s.count(".") if n == 0: print(int(s)) elif n == 1: a = s.find(".") foo = s[0:a] bar = s[a+1:] for i in range(len(bar)-1, -1, -1): if bar[i] != "0": b = i break foobar = bar[:b+1] print(str(int(foo)) + "." + foobar) else: a = s.find(".") foo = s[0:a] bar1 = s[a+1:] bar2 = list(bar1.split(".")) bar3 = "".join(bar2) for i in range(len(bar3)-1, -1, -1): if bar3[i] != "0": b = i break foobar = bar3[:b+1] print(str(int(foo)) + "." + foobar)
・数式の計算( 1 桁)
''' Pythonによる「文字列処理メニュー 」問題集 数式の計算( 1 桁) https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() ans = int(s[0]) for i in range(len(s)): if s[i] == "+": ans += int(s[i+1]) elif s[i] == "-": ans -= int(s[i+1]) print(ans)
・数式の計算
''' Pythonによる「文字列処理メニュー 」問題集 数式の計算 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() lis = [] tempo = 0 for i in range(len(s)): if s[i] == "+" or s[i] == "-": lis.append(s[tempo:i]) tempo = i elif i == len(s)-1: lis.append(s[tempo:i+1]) ans = 0 for i in range(len(lis)): ans += int(lis[i]) print(ans)
・巨大な数の足し算(繰り上がりなし)
''' Pythonによる「文字列処理メニュー 」問題集 巨大な数の足し算(繰り上がりなし) https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() t = input() tempo = [] ans = [] if len(s) > len(t): n = len(s) - len(t) for i in range(n): tempo.append("0") tempo.append(t) u = "".join(tempo) for i in range(len(s)): m = int(s[i]) + int(u[i]) ans.append(str(m)) print("".join(ans)) else: n = len(t) - len(s) for i in range(n): tempo.append("0") tempo.append(s) u = "".join(tempo) for i in range(len(t)): m = int(t[i]) + int(u[i]) ans.append(str(m)) print("".join(ans))
・巨大な数の足し算
''' Pythonによる「文字列処理メニュー 」問題集 巨大な数の足し算 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() t = input() tempo = [] ans = [] num = 0 if len(s) > len(t): n = len(s) - len(t) for i in range(n): tempo.append("0") tempo.append(t) u = "".join(tempo) for i in range(len(s)-1, -1, -1): m = int(s[i]) + int(u[i]) + num if m >= 10: m -= 10 ans.append(str(m)) num = 1 if i == 0: ans.append(str(num)) else: ans.append(str(m)) num = 0 print("".join(list(reversed(ans)))) else: n = len(t) - len(s) for i in range(n): tempo.append("0") tempo.append(s) u = "".join(tempo) for i in range(len(t)-1, -1, -1): m = int(t[i]) + int(u[i]) + num if m >= 10: m -= 10 ans.append(str(m)) num = 1 if i == 0: ans.append(str(num)) else: ans.append(str(m)) num = 0 print("".join(list(reversed(ans))))
・巨大な数のかけ算
''' Pythonによる「文字列処理メニュー 」問題集 巨大な数のかけ算 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com ''' # coding: utf-8 s = input() t = int(input()) ans = [] num = 0 if s == "0" or t == 0: print(0) else: for i in range(len(s)-1, -1, -1): m = int(s[i]) * t + num if m >= 10: n = int(m%10) ans.append(str(n)) num = int((m - n)/10) if i == 0: ans.append(str(num)) else: ans.append(str(m)) num = 0 print("".join(list(reversed(ans))))
paizaラーニングのレベルアップ問題集については、ユーザー同士で解答を教え合ったり、コードを公開したりするのは自由としています。
また授業や研修、教材などにも利用できるそうです。