Created
June 26, 2014 09:15
-
-
Save matts1/219f193d8256499fbdb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def decode(x): | |
return re.sub("%[0-9A-F][0-9A-F]", lambda x: chr(int(x.group(0)[1:], 16)) , x, flags=re.I, count=100) | |
def eq(a, b): | |
if a != b: | |
print (a, "!=", b) | |
raise AssertionError | |
eq(decode("Hello"), "Hello") | |
eq(decode("http://%77%77%77%2E%6D%61%6C%77%61%72%65%52%75%73%2E%63%6F%6D/www.furrykittens.com"), "http://www.malwareRus.com/www.furrykittens.com") | |
eq(decode("http://www.example.com.au/spaces%20%26%20funny%20%21%23%7c%20chars%20in%20name.pdf"), "http://www.example.com.au/spaces & funny !#| chars in name.pdf") | |
for line in """progcomp%40cse | |
%3Ca%20href%3D%22www.don%27tgothere.info%22%3ETesting%3C%2Fa%3E | |
%77%77%77%2E%75%72%6D%79%62%2A%2A%2A%2A%2E%72%75%2F%63%6C%69%63%6B%68%65%72%65/kiddysafe.org | |
%66%74%70%3A%2F%2F%74%72%6F%6A%61%6E%73%34%75%2E%6F%72%67%2F%70%77%6E%65%64%21 | |
100%5exy | |
ENCODED%20%25%37%34%25%37%37%25%36%39%25%36%33%25%36%35 | |
!*%21%2a()%28%29;:%3b%3a@&%40%26=+%3d%2b$,/%24%2c?#%2f%3f%23 | |
[]%5b%5d'%27<>%3c%3e{}%7b%7d"%22^~%5e%7e`.%60%2e""".split("\n"): | |
print(decode(line)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def happy(x): | |
return sum(int(n) ** 2 for n in str(x)) | |
assert(happy(97) == 130) | |
def loop(x): | |
nums = [] | |
while x not in nums and 1 not in nums: | |
nums.append(x) | |
x = happy(x) | |
if nums[-1] == 1: | |
return nums | |
else: | |
return (len(nums), nums + [x]) | |
results = dict([(x, loop(x)) for x in range(1, 1000)]) | |
happy = list(filter(lambda x: isinstance(results[x], list), sorted(results))) | |
unhappy = list(filter(lambda x: isinstance(results[x], tuple), sorted(results))) | |
despair = set([results[x][1][-1] for x in unhappy]) | |
maxdespair = max(results[x][0] for x in unhappy if x > 99) | |
# q1 | |
print(happy[9]) | |
#q2 | |
print(despair) | |
#q3 | |
maxnum = len(results[max(happy, key=lambda x: len(results[x]))]) | |
print(maxnum - 1) | |
#q4 | |
print(min(list(filter(lambda x: len(results[x]) == maxnum, sorted(happy, key=lambda x: len(results[x]))[::-1])))) | |
#q5 | |
print(maxdespair - 1) | |
#q6 | |
print(max([x for x in unhappy if x > 99 and results[x][0] == maxdespair])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from string import ascii_uppercase | |
letters = " " + ascii_uppercase | |
nums = ['111', '112', '113', '121', '122', '123', '131', '132', '133', '211', '212', '213', '221', '222', '223', '231', '232', '233', '311', '312', '313', '321', '322', '323', '331', '332', '333'] | |
def passphrase(p): | |
digittable = {} | |
code = 111 | |
plist = list(p) + list(letters) | |
newphrase = "" | |
for letter in plist: | |
if letter not in newphrase: | |
newphrase += letter | |
return (dict(zip(newphrase, nums)), dict(zip(nums, newphrase))) | |
# print(passphrase("PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS")) | |
def fn(intext, p, preverse): | |
nums = "".join(p[x] for x in intext) | |
l = len(nums) // 3 | |
nums = [nums[i::l] for i in range(len(nums)//3)] | |
print(''.join(list(preverse[num] for num in nums))) | |
p = passphrase("SECRET") | |
for val in """FESEGJCELMMYEJKK HPW | |
QHLAH OERSPIWFJCFLKMVOEK | |
EIR OA COORRA ISCSVPFLTVLFETFNXFZLIPFWMO GXXHCMOUUXVUE | |
RBOEWSYTQOTOFRDEEKWJOUINGTTRFWA NADGKXD TATUKUR""".split("\n"): | |
fn(val, *p) | |
p = passphrase("PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS") | |
for val in """ZKVNXPVNEAOBJMCIGELVXMHWXVURNVFFMWG Z | |
HOKHZIMAHNNSYP QUWCAZBFVNTRNIDFTKCRU IFURMRL EUMQFDHARFFDNXGWQQEUR | |
HNTPNNS WHM Q QUWP DYBM EDKUXHUOITU IFUUPZE HTHSFRDWTGDFHXGWQRWDDV | |
SSSSSSSSSPPPPPPPPPPPPPPPPPP | |
EX WFHTKSHYUEIKUEEOSW WTQFQH | |
X""".split("\n"): | |
fn(val, *p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
column = [] | |
row = [] | |
all_nums = [] | |
tests = [] | |
inp = [list(map(int, x.split())) for x in """7 2 5 14 | |
6 3 1 10 | |
3 9 8 20 | |
16 14 14""".split("\n")] | |
result = "solved" | |
for row in enumerate(inp[:-1]): | |
if not sum(row[:-1]) == row[-1]: | |
result = "sum error" | |
for x in range(len(inp[0]) - 1): | |
if not sum([inp[y][x] for y in range(len(inp) - 1)]) == inp[-1][x]: | |
result = "sum error" | |
if not sorted(all_nums) == range(1, len(all_nums) + 1): | |
result ="sequence error" | |
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run(length, cuts): | |
cuts = list(map(float, cuts.split())) | |
cost = {} | |
cuts = [0] + cuts + [length] | |
done = {} | |
for l in range(1, len(cuts)): | |
for start in range(len(cuts) - l): | |
end = start + l | |
cost[(start, end)] = float('inf') | |
done[(start, end)] = [] | |
for cut in range(start + 1, end): | |
amount = cost[(start, cut)] + cost[(cut, end)] | |
if amount < cost[(start, end)]: | |
cost[(start, end)] = amount | |
done[(start, end)] = [cuts[cut]] + done[(start, cut)] + done[(cut, end)] | |
cost[(start, end)] += cuts[end] - cuts[start] | |
if start == end - 1: | |
cost[(start, end)] = 0 | |
print("$" + str(cost[0, len(cuts) - 1]) + " " + " ".join(map(str, done[(start, end)]))) | |
# run(10, "4 5 7") | |
# Solvable without dynamic programming. | |
run(20, "4 5 7 8 10 13 15") | |
# Cuts need not be integers, direct solution still possible. | |
run(56.325, "3.9 11.7 15.2 17.2 19.3 23.8 28.6 31.0 37.9 48.3 48.9 52.4") | |
# The first 25 of Euler’s “lucky numbers” (see Wolfram Mathworld). Direct methods are impractical. | |
run(384.88, "1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 87 93 99 105 111") | |
# 50 pseudorandom values | |
run(1000,"6 13 17 55 75 90 105 126 127 177 203 233 244 248 256 296 306 363 411 418 430 462 466 470 475 481 486 494 509 533 555 597 605 608 625 629 659 668 761 775 814 815 823 833 842 845 850 898 906 946") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment