0
0

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.

怪文書で自己紹介に対して返信する

Last updated at Posted at 2021-03-21

 ゼミの自己紹介でPythonやってます!と言っていた人が居たので、怪文書コードで返信しようとした。
 以下コード

a = [[875079.8095873848], [-2882947.5355600277], [4056234.642952851], [-3283373.044987327], [1728271.5227123285], [-631498.0970303839], [166468.3240147154], [-32394.07511043425], [4710.754802641195], [-513.6959614565977], [41.7881579194053], [-2.496902614930717], [0.10632426026426153], [-0.0030523592139228553], [5.292142586414675e-05], [-4.184215771782444e-07]]
print(*['%s'%chr(round(sum(a[j][0] * i ** j for j in range(len(a))))) for i in range(1,len(a)+1)], sep='')

 実行すると

Nice to see you.

 と表示される。

日本語で喋りましょう

 print文内は以下のように書き換えられる。


for i in range(1,len(a)+1):
    letter_code = 0
    for j in range(len(a)):
        letter_code += a[j][0] * i ** j
    print(chr(round(letter_code)), end='')

 Pythonではchr(num)で、numに対応したASCIIコードの文字に変換できる。つまり、一文字づつASCIIコードを計算し、それを表示している。
 計算式はlen(sentence)次の方程式になるが、それもPythonでよしなに計算できる。ただし16次までしか計算できないっぽいので、送れる文字数の最大数は16文字になる。
 コード内変数aを求めるコードは以下。

import numpy as np

sentence = 'Nice to see you.'

targetList = [ord(i) for i in sentence]
length = len(targetList)
a = [[i**j for j in range(length)] for i in range(1,length+1)]

a = np.matrix(a)
y = np.matrix([[i] for i in targetList])

ans = np.linalg.solve(a,y).tolist()
print(ans)

感想

 行列で計算したものをそのままリストにしてたけど、普通に考えて2次元リストは1次元リストに変換してからやればよかった。
 1次元リストに変換するならこうなる

import numpy as np

sentence = 'Nice to see you.'

targetList = [ord(i) for i in sentence]
print(len(targetList))
length = len(targetList)
a = [[i**j for j in range(length)] for i in range(1,length+1)]

a = np.matrix(a)
y = np.matrix([[i] for i in targetList])

ans = np.linalg.solve(a,y).tolist()
print([i[0] for i in ans])

a = [875079.8095873848, -2882947.5355600277, 4056234.642952851, -3283373.044987327, 1728271.5227123285, -631498.0970303839, 166468.3240147154, -32394.07511043425, 4710.754802641195, -513.6959614565977, 41.7881579194053, -2.496902614930717, 0.10632426026426153, -0.0030523592139228553, 5.292142586414675e-05, -4.184215771782444e-07]
print(*['%s'%chr(round(sum(a[j] * i ** j for j in range(len(a))))) for i in range(1,len(a)+1)], sep='')

# わかりやすいほう
for i in range(1,len(a)+1):
    letter_code = 0
    for j in range(len(a)):
        letter_code += a[j] * i ** j
    print(chr(round(letter_code)), end='')

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?