This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP34.py
220 lines (159 loc) · 6.68 KB
/
P34.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# MIT License
# Copyright (c) [2020] [Jean-Sébastien Gonsette]
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
__author__ = "Jean-Sébastien Gonsette"
__year__ = 2019
import re
import numpy as np
from Tools.vocabulary import Vocabulary
from Tools.countries import countries, cities
from Tools.mendeliev import build_mendeleiev_list
def develop_middle (string, direction='up'):
"""Develop each row of the diamond from a starting row"""
up = string.upper ()
ups = []
while len (up) > 1:
out = ''
for idx, s in enumerate (up [:-1]):
sn = up [idx +1]
if direction == 'up':
out += chr ((ord (s)-ord('A') + ord (sn)-ord('A'))%26 + ord('A'))
elif direction == 'down':
out += chr ( (ord(s)-ord('A') - ord(sn)+ord('A')) % 26 + ord('A') )
up = out
ups.append (up)
return ups
def next_char (s):
"""Next alphabet character, modulo 26"""
return chr (ord('A') + ((ord (s)-ord('A') +1) % 26))
def get_diamond_coeffs (length, direction='up'):
"""Return weigths of each letter at the middle of a diamond shape,
regarding their contribution to the top letter"""
coeffs = np.ones ([length], dtype=int)
half = length //2
odd = length % 2
# Do the first half
for idx in range (half+odd):
for i in range (1, length -idx-1):
coeffs [i] += coeffs [i-1]
# Mirror the second part
for idx in range (half):
coeffs [idx] = coeffs [length-1-idx]
# For down direction, we have to negate one coeff out of 2
if direction == 'down':
parity = [1, -1] * (half +1)
coeffs = [c * p for c, p in zip (coeffs, parity)]
return coeffs
def predic_top (string, coeffs=None):
"""Predict top letter of diamond from starting row, without explicitly developing each intermediate row
"""
# For computation coefficients of each base letter
if coeffs is None: coeffs = get_diamond_coeffs (len (string))
top = 0
# Process base letters, 2 by 2, starting from extremities
for s, c in zip (string, coeffs):
top += (ord (s)-ord ('A')) * c
return chr (ord('A') + top%26)
def predic_down (string, coeffs=None):
"""Predict down letter of diamond from starting row, without explicitly developing each intermediate row
"""
if coeffs is None:
coeffs = get_diamond_coeffs (len (string), direction='down')
return predic_top (string, coeffs)
# def find_base (voc, base_pattern, final_letter, direction='up', prefix=''):
# coeffs = get_diamond_coeffs (len (base_pattern), direction)
# var = [idx for idx, s in enumerate (base_pattern) if s == '*']
# fill = ['A'] * len (var)
# pos = 0
# soluces = []
# scores = []
# while True:
# # Make word to test by replacing '*' with letters
# w = list (base_pattern)
# for idx, f in zip (var, fill):
# w [idx] = f
# word = ''.join (w)
# # Make the word evolve and check the top letter
# # top = develop_middle (word, direction='up')
# top = predic_top (word, coeffs=coeffs)
# if top [-1] == final_letter:
# score = voc.is_valid_start (prefix + word, can_extend=True)
# if score > 0:
# soluces.append (prefix + word)
# scores.append (score)
# # Move on the pattern to replace '*'
# while pos < len (fill):
# fill [pos] = next_char (fill [pos])
# if fill [pos] == 'A':
# pos += 1
# continue
# break
# if pos >= len (fill): break
# pos = 0
# return soluces, scores
# def find_base_list (voc, pattern_list, final_letter, direction='up', prefix_list=None):
# soluces = []
# scores = []
# if not prefix_list: prefix_list = [''] * len (pattern_list)
# for prefix, p in zip (prefix_list, pattern_list):
# soluce_list, score_list = find_base (voc, p, final_letter, direction=direction, prefix=prefix)
# soluces.extend (soluce_list)
# scores.extend (score_list)
# return soluces, scores
def print_diamond (lines, direction='up'):
width = len (lines [0])
if direction=='up':
for idx, line in enumerate (reversed (lines)):
print (' ' * (width-idx) + ' '.join (line) )
else:
for idx, line in enumerate (lines):
print (' ' * idx + ' '.join (line) )
def check_mendeleiev (rows):
mendel_list, dico_reverse = build_mendeleiev_list ()
for row in rows:
for item in mendel_list:
if item in row:
if len (item) < 4: continue
print (item)
print (dico_reverse [item])
print ("Some checks on the exemple\n----------------")
print (predic_top ("EXEMPLE"))
print (predic_top ("EXEMPL"))
print (predic_top ("XEMPLE"))
print (predic_down ("EXEMPLE"))
print (predic_down ("EX"))
print (predic_down ("XE"))
print (predic_down ("EXE"))
print (predic_down ("EXEMPL"))
print (predic_down ("XEMPLE"))
print (predic_down ("XEMPL"))
print ("\nPrint coefficients to solve the system of equations\n----------------")
up = [3,4,5,6,10,11,13,18]
down = [2,6,7,10,12,19]
for u in up:
print ('up ' + str (u) + str (get_diamond_coeffs (u, direction='up')))
for d in down:
print ('down ' + str (d) + str (get_diamond_coeffs (d, direction='down')))
print ("\nSolution\n----------------")
ups = develop_middle ("MONSLALOUVIERENAMUR", direction='up')
downs = develop_middle ("MONSLALOUVIERENAMUR", direction='down')
print_diamond (ups, direction='up')
print_diamond (downs, direction='down')
print ("\nChecking Mendeleiev")
check_mendeleiev (ups)
check_mendeleiev (downs)
exit ()