-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge103easy.py
64 lines (52 loc) · 1.89 KB
/
challenge103easy.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
#!/usr/bin/env python
# encoding: utf-8
# Daily Programmer Challenge 103 Easy
#
# http://www.reddit.com/r/dailyprogrammer/comments/11erhd/10132012_challenge_103_easydifficult_text/
#
# May.07.2015
from random import random, choice
def make_trans_table(fname='leet_table.txt'):
""" make_trans_table: create a conversion table {'A': ['4'], 'B': ['8', '6']...
:fname: filename
:returns: translation_table
"""
table = {}
with open(fname) as f:
for line in f:
cols = line.strip().split()
table[cols[0]] = cols[1:]
return table
def text2leet(text, table, conv_chance=1.0):
""" text2leet: transcode text according to a given translation_table,
with the conv_chance probability of each letter to be
replaced.
:text: text string
:table: translation table
:conv_chance: chance for each letter to be converted. 0 for no conversion, 1.0
for whole conversion.
:returns: translated text
"""
output = []
for char in text.upper():
if char in table:
if random() <= conv_chance:
output.append(choice(table[char]))
else:
output.append(char)
else:
output.append(char)
return ''.join(output)
def main():
l33t_table = make_trans_table("leet_table.txt")
text = open("bible.txt").read()
print text2leet(text, l33t_table, 0.4)
if __name__ == '__main__':
main()
# I MUST NOT (=EAR.
# FEA2 IS TH& emIND-|XIL|_ER.
# F3AR IS THE Leye+TLE-DEAT|-| THAT B|`INC-es T[]†AL OBL!TERZTIΩN.
# I WILL |=/\<E MY ʃE4R.
# 3y3 WI7L P&R/|/|I1 IT TO PA§5 ¤V3R M€ AND THR0UG(-) (\/)E.
# AND WHE// I7 HAS (_+ohNə PAzT I WI|1 TUR₪ TH€ INNElz EYE TO 5EE |Tes PATH.
# UU:-:ERE THE ]=E@R HAS GO//\\//[- THERə WILL BE |\|OTHI|\|cj. O~L'/ eye WILL RE44AIN.