0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[デイリーコーディング] 文字列内の各アルファベットを10個後のものに変換する

Posted at

#今日の問題: 文字列内の各アルファベットを10個後のものに変換せよ

ord()とchr()を使って解決していきます。

回答(Python3)
from icecream import ic # デバッグ用にicecreamをインポートしています。

def move_ten(st):
    result = ""
    for i in st:
        ordI = ord(i)
        if ordI >= 113:
            result += chr(ordI - 16)
        else:
            result += chr(ordI + 10)
    return result

ic(move_ten("testcase")) # "docdmkco"
ic(move_ten("codewars")) # "mynogkbc"
ic(move_ten("exampletesthere")) # "ohkwzvodocdrobo"
ic(move_ten("returnofthespacecamel")) # "bodebxypdroczkmomkwov"
ic(move_ten("bringonthebootcamp")) # "lbsxqyxdrolyydmkwz"
ic(move_ten("weneedanofficedog")) # "goxoonkxyppsmonyq"
ic(move_ten("qeneedanofficedog")) # "goxoonkxyppsmonyq"
結果
ic| move_ten("testcase"): 'docdmkco'
ic| move_ten("codewars"): 'mynogkbc'
ic| move_ten("exampletesthere"): 'ohkwzvodocdrobo'
ic| move_ten("returnofthespacecamel"): 'bodebxypdroczkmomkwov'
ic| move_ten("bringonthebootcamp"): 'lbsxqyxdrolyydmkwz'
ic| move_ten("weneedanofficedog"): 'goxoonkxyppsmonyq'
ic| move_ten("qeneedanofficedog"): 'aoxoonkxyppsmonyq'

ベターな回答:

BetterCode
def move_ten(st):
    return ''.join(chr(ord(i)+10) if i<'q' 
        else chr(ord(i)-16) for i in st)

参照: Move 10 (Codewars)

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?