Skip to content

Instantly share code, notes, and snippets.

@tobynet
Last active July 4, 2018 07:03
Show Gist options
  • Save tobynet/eabecb12738dc0e56a7309e1ade6900c to your computer and use it in GitHub Desktop.
Save tobynet/eabecb12738dc0e56a7309e1ade6900c to your computer and use it in GitHub Desktop.
#54文字の文学賞 のジェネレータ https://ujiqn.github.io/54-novel/ に入れる用スクリプト
#!/usr/bin/env python
import numpy as np
message = \
'''
 
 あなたと
  ジャバ
 
 今すぐ
 ダウンロ|
  ド
'''
WIDTH = 9
HEIGHT = 6
# string -> [string]
lines = message.split("\n")
# Cut head and tail for unnecessary \n
if lines[0] == '' : lines = lines[1:]
if lines[-1] == '': lines = lines[:-1]
# 足りない長さを得る
# [string] -> int-> [string]
def fill_lines(lines, max_len):
# [string] -> [int]
missing_lens = [ max_len - len(x) for x in lines ]
# [string] -> [string]
filled_lines = [ s + " " * ml for s, ml in zip(lines, missing_lens) ]
return filled_lines
# [string] -> [[char]]
def to_list_list(xs):
return list(map(list, xs))
# [[char]] -> [string]
def to_str_list(xs):
return list(map("".join, xs))
# [string] -> [[string]]
array_lines = to_list_list(fill_lines(lines, HEIGHT))
# rotation
# [[string]] -> [[string]]
rotated = np.rot90(array_lines, 1)
# [[string]] -> [string]
rotated_lines = fill_lines(to_str_list(rotated), WIDTH)
print("".join(rotated_lines))
@tobynet
Copy link
Author

tobynet commented Jul 4, 2018

動作例:

$ python rotation-string-for-54-novel.py
     |    とバ  ロ    たャ ぐンド   なジ すウ    あ  今ダ

@tobynet
Copy link
Author

tobynet commented Jul 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment