0
1

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.

[Python]文字列並び替え問題作成スクリプト

Last updated at Posted at 2021-02-05

「与えられた文字列を並び替えて、意味のある言葉にする」って問題ありますよね

こんな感じの奴です
image.png
引用 https://noutore.link/474/

この問題を作れるPythonスクリプト作ってみました

shuffle_text.py
import random

def shuffle_text(text):
    text_list = [word for word in text]
    result = ""
    while len(text_list) > 0:
        pick_index = random.randrange(0,len(text_list))
        result += text_list.pop(pick_index)
    return result

if __name__ == "__main__":
    text = input("文字列を入力:")
    print(shuffle_text(text))

text_list = [word for word in text]
Pythonの文字列は変更不可(途中の文字を削除とか出来ない)ので、一旦リストにしています

例えば「こんにちは」というtextを渡した場合、
text_listは[こ,ん,に,ち,は]になります

あとは、これからランダムに一個ずつ抽出してresultとして返すだけ

0
1
3

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?