Python AtCoder入門 第1講 Pythonの基礎と入出力

>100 Views

September 19, 26

スライド概要

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

ダウンロード

関連スライド

各ページのテキスト
1.

Python AtCoder入門 第1講 Pythonの基礎と入出力 第0講では、AtCoderがどういう場所で、なぜPythonで挑むのかをお話ししました。 ここからは実際に手を動かしていきます。 1

2.

今回のテーマ 最初のテーマは、 入出力 です。 地味に聞こえるかもしれませんが、ここは本書で最も重要な講です。 2

3.

なぜ入出力が重要なのか AtCoderのすべての問題は、 入力を受け取ることから始まり、出力することで終わる からです。 入力で詰まれば、どれだけ良い解き方を思いついても1点にもなりません。 3

4.

この講のゴール 問題文の入力形式を見た瞬間に、 受け取りのコードが指から出てくる状態 を目指します。 入力で迷わなくなれば、考えるべきことは「解き方」だけになります。 4

5.

コードファイル名の方針 本書では、コード例ごとにファイル名を付けます。 例: hello.py print_values.py read_integer.py for.py 手元で試すときは、同じ名前で保存して実行してください。 5

6.

1-1 AtCoderとPython AtCoderの問題を解くプログラムは、次の3つを行います。 1. 標準入力からデータを読む 2. そのデータを使って計算する 3. 標準出力に答えを書く 6

7.

標準入力とは 標準入力とは、 プログラムに流し込まれてくるデータ のことです。 Pythonでは、 input() で1行ずつ読み取ります。 7

8.

標準出力とは 標準出力とは、 プログラムが外に出す文字 のことです。 Pythonでは、 print() で出力します。 8

9.

AtCoderで使う基本関数 AtCoderの基本は、とてもシンプルです。 input() print() 読んで、書く。 この2つを自在に使えるようにします。 9

10.

問題文の「入力形式」を読む AtCoderの問題文には、必ず入力形式があります。 例: N A B これは、 1行目に整数 N 2行目に整数 A と B が与えられる、という意味です。 10

11.

実際の入力例 入力形式が、 N A B なら、実際の入力はたとえばこうなります。 5 3 7 入力形式とコードの対応を覚えることが、この講の中心です。 11

12.

提出から判定までの流れ コードが書けたら、提出フォームに貼り付けます。 1. 言語を選ぶ 2. コードを貼る 3. 提出する 4. 判定を待つ 5. AC なら正解 本書では基本的に PyPy3 を使います。 12

13.

提出前にコードテスト 提出する前に、 コードテスト で入力例を試しましょう。 入力例を入れる 実行する 出力例と一致するか確認する 一致したら提出する この習慣で、無駄なWAを減らせます。 13

14.

1-2 出力の基本 まずは出力から始めます。 答えを出力できなければ、合っているかどうかを確認できません。 使う関数は、 print() です。 14

15.

printの基本 は、渡されたものを標準出力に書き出します。 最後には自動で改行が付きます。 print() print("Hello") print(42) print(1 + 2) 15

16.

hello.py # Print a fixed message to standard output print("Hello, AtCoder!") 出力: Hello, AtCoder! 文字列は " " で囲みます。 16

17.

数値と式の出力 数値はそのまま出力できます。 print(42) 式を渡すと、計算結果が出力されます。 print(1 + 2) 出力: 3 17

18.

複数の値を出力する にカンマ区切りで複数の値を渡すと、 空白区切りで1行に出力 されます。 print() print(3, 7) print("A", 1, "B") 18

19.

出力例 print(3, 7) print("A", 1, "B") 出力: 3 7 A 1 B AtCoderでは、この形をよく使います。 19

20.

sepとend の区切り文字や行末文字は変更できます。 sep :値と値の間に入る文字 end :出力の最後に付く文字 既定値は、 print() sep=" " end="\n" です。 20

21.

print_options.py print(3, 7, sep=",") print(3, 7, sep="") print("A", end="") print("B") 出力: 3,7 37 AB 21

22.

sep="\n" sep="\n" を使うと、値ごとに改行できます。 print(3, 7, 10, sep="\n") 出力: 3 7 10 「1行ずつ出力せよ」という問題で使えます。 22

23.

print_values.py A = 3 B = 7 C = 10 # Print with spaces print(A, B, C) # Print with commas print(A, B, C, sep=",") # Print one value per line print(A, B, C, sep="\n") 23

24.

print_values.py の出力 3 7 10 3,7,10 3 7 10 同じ値でも、出力形式によって書き方が変わります。 24

25.

出力形式は問題文の通りに AtCoderでは、出力形式が厳密です。 たとえば、 Yes と YES を間違える 空白区切りを改行区切りにする デバッグ用の print を消し忘れる これらはすべてWAになります。 25

26.

1-3 入力の基本 ここからが本番です。 入力では、 input() を使います。 ただし、最初にとても大切な注意点があります。 26

27.

inputは文字列を返す input() が返すのは、必ず 文字列 です。 数字が書かれた行を読んでも、返ってくるのは数値ではありません。 27

28.

input_string.py 入力: 5 コード: N = input() print(N + N) 出力: 55 N は文字列 "5" なので、足し算ではなく連結になります。 28

29.

intで数値に変換する 数として計算したいなら、 int() で整数に変換します。 N = int(input()) print(N + N) 入力が 5 なら、出力は 10 になります。 29

30.

read_integer.py # Read one integer from standard input N = int(input()) # Calculate and print the answer print(N * N) 入力例: 6 出力例: 36 30

31.

覚える形 整数を1つ受け取るときは、 N = int(input()) です。 これは今後ずっと使います。 整数1つ = int(input()) と丸ごと覚えましょう。 31

32.

1行に複数の値 次は、1行に2つの整数がある場合です。 入力例: 3 7 このような入力では、 split() map() を使います。 32

33.

split split() は、文字列を空白で区切ります。 line = input() parts = line.split() 入力が、 3 7 なら、 parts は次のようになります。 ["3", "7"] 33

34.

map split() そこで、 しただけでは、中身は文字列のままです。 map(int, input().split()) と書きます。 これは、各要素に int を適用する、という意味です。 34

35.

read_two_ints.py # Read two integers separated by a space A, B = map(int, input().split()) # Print the sum and the product print(A + B, A * B) 入力例: 3 7 出力例: 10 21 35

36.

AtCoder最重要テンプレート 1行に2つの整数なら、 A, B = map(int, input().split()) です。 3つなら、 A, B, C = map(int, input().split()) です。 左辺の変数の数を増やすだけです。 36

37.

個数が変わる入力 次のような入力では、個数が N によって変わります。 N A_1 A_2 ... A_N この場合、変数を並べて受け取ることはできません。 37

38.

listで受け取る 個数が変わる場合は、リストにします。 A = list(map(int, input().split())) これで、たとえば次のようなリストになります。 [3, 1, 4, 1, 5] 38

39.

read_list.py # Read the number of integers N = int(input()) # Read N integers on one line A = list(map(int, input().split())) # Print the sum print(sum(A)) 入力例: 5 3 1 4 1 5 出力例: 14 39

40.

N行の入力 次のような形式もよく出ます。 N A_1 A_2 ... A_N 1行に1つずつ、N行にわたって与えられる形式です。 40

41.

forでN回読む この場合は、 for 文でN回読みます。 N = int(input()) total = 0 for i in range(N): A = int(input()) total = total + A print(total) 41

42.

for.py # Read the number of lines N = int(input()) # Read N integers, one per line, and sum them up total = 0 for i in range(N): A = int(input()) total = total + A print(total) 入力例: 4 10 20 30 40 42

43.

for.py の出力 100 for i in range(N): は、 N回繰り返す という意味です。 詳しくは第4講で扱います。 43

44.

1-4 AtCoder頻出の入力テンプレート ここまでの内容を、入力形式ごとに整理します。 この節が第1講の心臓部です。 まずは、よく出る形を丸ごと覚えましょう。 44

45.

テンプレート1:整数1つ 入力形式: N コード: N = int(input()) ファイル名例: input_one_int.py 45

46.

テンプレート2:整数2つ 入力形式: A B コード: A, B = map(int, input().split()) ファイル名例: input_two_ints.py 46

47.

テンプレート3:整数3つ 入力形式: A B C コード: A, B, C = map(int, input().split()) ファイル名例: input_three_ints.py 47

48.

テンプレート4:文字列1つ 入力形式: S コード: S = input() ファイル名例: input_string.py 文字列は int() で変換しません。 48

49.

テンプレート5:N個の整数が1行 入力形式: N A_1 A_2 ... A_N コード: N = int(input()) A = list(map(int, input().split())) ファイル名例: input_list.py 49

50.

テンプレート6:N行の整数 入力形式: N A_1 ... A_N コード: N = int(input()) A = [int(input()) for i in range(N)] ファイル名例: input_lines.py 50

51.

テンプレート7:整数と文字列 入力形式: N S コード: N, S = input().split() N = int(N) ファイル名例: input_int_string.py map(int, ...) は使えません。 51

52.

対応表 入力形式 コード N N = int(input()) A B A, B = map(int, input().split()) A B C A, B, C = map(int, input().split()) S S = input() N → A_1 ... A_N N = int(input()) / A = list(map(int, input().split())) 52

53.

文字列は変換しない 文字列を受け取るときは、 S = input() です。 int() を付けると、数字でない文字列のときに ValueError になります。 53

54.

N行を1行で受け取る A = [int(input()) for i in range(N)] これは、 N行読み取り、それぞれ整数に変換してリストにする という意味です。 リスト内包表記と呼ばれます。 54

55.

int_string.py # Read an integer and a string N, S = input().split() # Convert only N to an integer N = int(N) print(N * 2, S) 入力例: 3 abc 出力例: 6 abc 55

56.

template_single_int.py # Pattern 1: a single integer N = int(input()) print(N) 対応する入力: 5 56

57.

template_two_ints.py # Pattern 2: two integers on one line A, B = map(int, input().split()) print(A, B) 対応する入力: 3 7 57

58.

template_string.py # Pattern 3: a single string S = input() print(S) 対応する入力: AtCoder 58

59.

template_list.py # Pattern 4: N followed by N integers on one line N = int(input()) A = list(map(int, input().split())) print(N, sum(A)) 対応する入力: 5 1 2 3 4 5 59

60.

template_lines.py # Pattern 5: N followed by N lines N = int(input()) A = [int(input()) for i in range(N)] print(N, sum(A)) 対応する入力: 3 10 20 30 60

61.

template_int_string.py # Pattern 6: an integer and a string on the same line N, S = input().split() N = int(N) print(N * 2, S) 対応する入力: 4 apple 61

62.

6パターンでかなり戦える この6パターンを覚えるだけで、 A問題・B問題の入力形式はかなり処理できます。 入力で悩む時間を減らし、 問題を解くことに集中しましょう。 62

63.

章末まとめ AtCoderのプログラムは、 標準入力から読み、計算し、標準出力に書く だけです。 使う基本関数は、 input() print() です。 63

64.

章末まとめ:print print() は、複数の値を渡すと空白区切りで出力します。 print(A, B) sep で区切り文字を変更できます。 print(A, B, sep=",") 64

65.

章末まとめ:input が返すのは、必ず文字列です。 整数として使うなら、 input() N = int(input()) 1行に複数の整数なら、 A, B = map(int, input().split()) です。 65

66.

章末まとめ:リスト入力 個数が変わる場合は、リストで受け取ります。 A = list(map(int, input().split())) N行にわたる入力は、 A = [int(input()) for i in range(N)] で受け取れます。 66

67.

章末まとめ:文字列 文字列はそのまま受け取ります。 S = input() 整数と文字列が混ざる場合は、必要なものだけ個別に変換します。 N, S = input().split() N = int(N) 67

68.

練習問題 1-1 和と積 2つの整数 A , B が与えられます。 A + B と A × B を、この順に空白区切りで1行に出力してください。 68

69.

練習問題 1-1:入力と出力 入力: A B 出力: A+B A*B 入力例: 4 9 出力例: 13 36 69

70.

answer_1_1.py # Read two integers separated by a space A, B = map(int, input().split()) # Print the sum and the product separated by a space print(A + B, A * B) 1行に2つの整数なので、 map を使います。 70

71.

練習問題 1-2 合計点 人の生徒がテストを受けました。 i 番目の生徒の点数は A_i です。 全員の点数の合計を出力してください。 N 71

72.

練習問題 1-2:入力と出力 入力: N A_1 A_2 ... A_N 入力例: 5 10 20 30 40 50 出力例: 150 72

73.

answer_1_2.py # Read the number of students N = int(input()) # Read N scores on one line A = list(map(int, input().split())) # Print the total score print(sum(A)) N は使わなくても、入力として必ず読み取ります。 73

74.

練習問題 1-3 最高気温 ある町で N 日間の気温を記録しました。 i 日目の気温は T_i です。 1行ずつ与えられる気温の最大値を出力してください。 74

75.

練習問題 1-3:入力と出力 入力: N T_1 T_2 ... T_N 入力例: 4 12 25 18 7 出力例: 25 75

76.

answer_1_3.py # Read the number of days N = int(input()) # Read N temperatures, one per line T = [int(input()) for i in range(N)] # Print the maximum temperature print(max(T)) N行入力は、リスト内包表記で受け取れます。 76

77.

answer_1_3_for.py # Read the number of days N = int(input()) # Read N temperatures, one per line T = [] for i in range(N): T.append(int(input())) # Print the maximum temperature print(max(T)) for 文で書いても同じ結果になります。 77

78.

次回予告 次の第2講では、 受け取った数値を使って計算する方法 を学びます。 特に、Pythonの整数の割り算には注意点があります。 78