Exemple #1
0
# 14. 先頭からN行を出力
# 自然数Nをコマンドライン引数などの手段で受け取り,
# 入力のうち先頭のN行だけを表示せよ.確認にはheadコマンドを用いよ.

from knock10 import read_file
import sys


def head(target: str, line: int) -> str:
    target = target.split("\n")
    return "\n".join(target[:line])


if __name__ == "__main__":
    args = sys.argv
    n = int(args[1])

    target = read_file("hightemp.txt")
    print(head(target, n))
Exemple #2
0
# 13. col1.txtとcol2.txtをマージ
# 12で作ったcol1.txtとcol2.txtを結合し,
# 元のファイルの1列目と2列目をタブ区切りで並べたテキストファイルを作成せよ
# 確認にはpasteコマンドを用いよ.

from knock10 import read_file


def write_file(filename: str, target: str) -> None:
    with open(filename, "w") as f:
        f.write(target)


def merge_two_column(target1: str, target2: str) -> None:
    col1 = target1.split("\n")
    col2 = target2.split("\n")

    output = ""
    for c1, c2 in zip(col1, col2):
        output += c1 + "\t" + c2 + "\n"

    write_file("output.txt", output)


if __name__ == "__main__":
    target1 = read_file("col1.txt")
    target2 = read_file("col2.txt")

    merge_two_column(target1, target2)
Exemple #3
0
# Code
from knock10 import read_file

lines = read_file()

id_value_dic = {}
for index, line in enumerate(lines):
    words = line.split('\t')
    value = int(words[3])
    id_value_dic[index] = value

sorted_ids = sorted(id_value_dic.items(), key=lambda x: x[1], reverse=True)

for id_value in sorted_ids:
    index = id_value[0]
    print(lines[index])

# Unix command
# $sort popular-names.txt --key=3,3 --numeric-sort --reverse
Exemple #4
0
# Code
from knock10 import read_file

col1_path = './col1.txt'
col2_path = './col2.txt'

col1_lines = read_file(col1_path)
col2_lines = read_file(col2_path)

with open('./combine.txt', 'w') as fp:
    for col1, col2 in zip(col1_lines, col2_lines):
        fp.write(f'{col1}\t{col2}\n')

# Unix command
# $paste -d '\t' col1_unix col2_unix > combine