14
14
15
15
16
16
__examples__ = {
17
- 'enc(rail-5-3)' : {'this is a test' : 'it sss etiath ' },
18
- 'dec(rail-7-4)' : {'a stiet shsti' : 'this is a test' }
17
+ 'enc(rail-5-3|rail_5_3)' : {'this is a test' : 'it sss etiath ' },
18
+ 'enc(railup-5-3|railup_5_3)' :{'this is a test' : 'h tiats e ssit' },
19
+ 'dec(rail-7-4|rail_7_4)' : {'a stiet shsti' : 'this is a test' }
19
20
}
20
21
21
22
22
23
23
- def __buildf (text , rails , offset = 0 ) :
24
- l , rail , dr = len (text ), offset , 1
24
+ def __buildf (text , rails , offset = 0 , up = 0 ) :
25
+ l , rail = len (text ), offset
26
+ if up != '' :
27
+ dr = - 1
28
+ rail = rails - offset - 1
29
+ else :
30
+ dr = 1
25
31
f = [["#" ] * l for i in range (rails )]
26
32
for x in range (l ) :
27
33
f [rail ][x ] = text [x ]
@@ -30,26 +36,22 @@ def __buildf(text, rails, offset = 0) :
30
36
elif rail <= 0 :
31
37
dr = 1
32
38
rail += dr
33
- for elem in f :
34
- print (elem )
35
39
return f
36
40
37
- def railfence_encode (rails , offset = 0 ) :
41
+ def railfence_encode (up = 0 , rails = 3 , offset = 0 ) :
38
42
def encode (text , errors = "strict" ) :
39
- print (len (text ))
40
-
41
43
c ,l = '' , len (text )
42
- f = __buildf (text ,rails ,offset )
44
+ f = __buildf (text ,rails ,offset , up )
43
45
for r in range (rails ) :
44
46
for x in range (l ) :
45
47
if f [r ][x ] != '#' :
46
48
c += f [r ][x ]
47
49
return c , l
48
50
return encode
49
51
50
- def railfence_decode (rails , offset = 0 ) :
52
+ def railfence_decode (up = 0 , rails = 3 , offset = 0 ) :
51
53
def decode (text , errors = 'strict' ) :
52
- f = __buildf ("x" * len (text ), rails , offset )
54
+ f = __buildf ("x" * len (text ), rails , offset , up )
53
55
plain , i = '' , 0
54
56
ra , l = range (rails ), range (len (text ))
55
57
@@ -69,7 +71,4 @@ def decode(text, errors = 'strict') :
69
71
70
72
return decode
71
73
72
- add ("rail" , railfence_encode , railfence_decode , r"rail-(\d+)\-(\d+)$" )
73
-
74
- #rail-(\d+)\-(\d+)
75
- #rail-(\d+)(\-*(\d+))
74
+ add ("rail" , railfence_encode , railfence_decode , r"rail(up)?[-_](\d+)[-_](\d+)$" )
0 commit comments