開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)をおまけの章(ソートと入れ子の反復 - リストに戻って強力な能力を追加する - データを整理する)の自分で考えてみよう(237ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 import random def bubble_sort(scores: list, numbers: list) -> None: swapped = True while swapped: swapped = False for i, _ in enumerate(scores[:-1]): if scores[i] < scores[i + 1]: scores[i], scores[i + 1] = scores[i + 1], scores[i] numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i] swapped = True if __name__ == '__main__': scores = [random.randrange(100) for _ in range(36)] solution_numbers = [i for i, _ in enumerate(scores)] bubble_sort(scores, solution_numbers) for i, (score, solution_number) in enumerate(zip(scores, solution_numbers), 1): print(f'{i:2}) Bubble solution #{solution_number:<2} score: {score}')
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample3.py 1) Bubble solution #34 score: 98 2) Bubble solution #3 score: 97 3) Bubble solution #21 score: 90 4) Bubble solution #27 score: 90 5) Bubble solution #6 score: 86 6) Bubble solution #9 score: 78 7) Bubble solution #0 score: 76 8) Bubble solution #4 score: 75 9) Bubble solution #7 score: 72 10) Bubble solution #19 score: 66 11) Bubble solution #26 score: 64 12) Bubble solution #20 score: 62 13) Bubble solution #18 score: 59 14) Bubble solution #23 score: 54 15) Bubble solution #35 score: 54 16) Bubble solution #24 score: 51 17) Bubble solution #31 score: 51 18) Bubble solution #5 score: 48 19) Bubble solution #22 score: 44 20) Bubble solution #17 score: 43 21) Bubble solution #28 score: 43 22) Bubble solution #25 score: 42 23) Bubble solution #33 score: 37 24) Bubble solution #15 score: 35 25) Bubble solution #10 score: 34 26) Bubble solution #13 score: 32 27) Bubble solution #30 score: 32 28) Bubble solution #14 score: 26 29) Bubble solution #11 score: 22 30) Bubble solution #16 score: 19 31) Bubble solution #1 score: 17 32) Bubble solution #32 score: 17 33) Bubble solution #8 score: 10 34) Bubble solution #12 score: 10 35) Bubble solution #29 score: 10 36) Bubble solution #2 score: 2 C:\Users\...>
0 コメント:
コメントを投稿