LoginSignup
7
4

More than 1 year has passed since last update.

ワクチンを2倍にしてワクワクチンチンにする処理

Posted at

はじめに

2回接種するということなので

リテラルでワクチンを2倍にする

def double_vaccine_literal(injection: str) -> str:
    if injection == "ワクチン":
        return "ワクワクチンチン"
    else:
        return injection

出力

>>> double_vaccine_literal("ワク")
'ワク'
>>> double_vaccine_literal("ワクチン")
'ワクワクチンチン'

内部処理的には2倍してない

演算子でワクチンを2倍にする

def double_vaccine(injection: str) -> str:
    if injection == "ワクチン":
        return injection[0:2] * 2 + injection[2:4] * 2
    else:
        return injection

出力

>>> double_vaccine("ワク")
'ワク'
>>> double_vaccine("ワクチン")
'ワクチンワクチン'

n倍にも対応できる

全てのワクチンを2倍にする

def double_all_vaccines(injections: str) -> str:
    vaccine = "ワクチン"
    return injections.replace(vaccine, vaccine[0:2] * 2 + vaccine[2:4] * 2)

出力

>>> double_all_vaccines("ワク")
'ワク'
>>> double_all_vaccines("ワクチン")
'ワクワクチンチン'
>>> double_all_vaccines("ワクチンワクチン")
'ワクワクチンチンワクワクチンチン'

全てのワクチンって何なんだ

ワンライナーで2倍にする

>>> "".join(map(lambda s: s * 2, ["ワクチン"[i : i + 2] for i in [0, 2]]))
'ワクワクチンチン'

"".joinがなんか嫌

まとめ

  • ワクチンを2倍にしてみた
  • "ワクチン"[0:2]みたいに分離するのダサい
  • もっとスマートな書き方教えて下さい
  • 他の言語でも見てみたいです 👀
7
4
5

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
7
4