Skip to content

Instantly share code, notes, and snippets.

@kingoflolz
Created September 5, 2015 11:52
Show Gist options
  • Save kingoflolz/40240fe6e99e233afa78 to your computer and use it in GitHub Desktop.
Save kingoflolz/40240fe6e99e233afa78 to your computer and use it in GitHub Desktop.
__author__ = 'Ben'
from pprint import pprint
budget = int(input("Budget: "))
cartrages = [
]
v = (input("Cartridge: "))
while v != "":
x = v.split(" ")
x[1] = int(x[1])
x[2] = int(x[2])
cartrages.append(tuple(x))
v = input("Cartridge: ")
tablep = {}
for cartrage in cartrages:
tablep[cartrage[1]] = (cartrage[2], [cartrage[0]])
def change_dp(price):
tableb = {}
for cartrage in cartrages:
for row, pages in tablep.items():
if cartrage[1]+row not in tablep:
tableb[cartrage[1]+row] = (tablep[row][0] + cartrage[2], tablep[row][1]+[cartrage[0]])
else:
if tablep[row][0] + cartrage[2] > tablep[cartrage[1]+row][0]:
tableb[cartrage[1]+row] = (tablep[row][0] + cartrage[2], tablep[row][1]+[cartrage[0]])
tablep.update(tableb)
table = {}
for r, c in tablep.items():
if r <= budget:
table[r] = c
return table
first = True
oldt = {}
while oldt != tablep:
oldt = tablep.copy()
tablep = change_dp(tablep)
#pprint((oldt==tablep))
try:
result = (tablep[list(sorted(tablep, key=lambda x: tablep[x][0], reverse=True))[0]], list(sorted(tablep, key=lambda x: tablep[x][0], reverse=True))[0])
cartragesout = {}
for cartrage in cartrages:
cartragesout[cartrage[0]] = 0
for c in result[0][1]:
cartragesout[c] += 1
resultp = list(sorted(cartragesout, key=lambda x: (0-cartragesout[x], x)))
#pprint((resultp, cartragesout))
print("Best option is " + str(result[0][0]) + " page(s) costing " + str(result[1])+ ":")
for r in resultp:
print(r + " " + str(cartragesout[r]))
except:
print("Best option is 0 page(s) costing 0:")
sorts = []
for x in cartrages:
sorts.append(x[0])
for r in sorted(sorts):
print(r + " 0")
__author__ = 'Ben'
from pprint import pprint
lines = [line.rstrip('\n') for line in open('crossword.txt')]
crossword = []
for line in lines:
m = []
for a in line:
if a == " ":
m.append(None)
elif a.isalpha():
m.append(" ")
else:
m.append(a)
crossword.append(m)
adescription = {}
i = 0
while i < len(crossword):
j = 0
length = 0
start = (0, 0)
inc = False
word = []
while j < len(crossword[i]):
if crossword[i][j] == " " or crossword[i][j] is None:
length += 1
word.append(crossword[i][j])
if not inc:
start = (i, j)
inc = True
# else:
# inc = True
if j == len(crossword[i]) - 1:
if length > 1:
adescription[start] = word
elif crossword[i][j] == "#":
if length > 1:
adescription[start] = word
length = 0
word = []
inc = False
j += 1
i += 1
crossword = list(zip(*crossword))
ddescription = {}
i = 0
while i < len(crossword):
j = 0
length = 0
start = (0, 0)
inc = False
word = []
while j < len(crossword[i]):
if crossword[i][j] == " " or crossword[i][j] is None:
length += 1
word.append(crossword[i][j])
if not inc:
start = (j, i)
inc = True
if j == len(crossword[i]) - 1:
if length > 1:
ddescription[start] = word
# else:
# inc = True
elif crossword[i][j] == "#":
if length > 1:
ddescription[start] = word
length = 0
word = []
inc = False
j += 1
i += 1
def incomplete(word):
for letter in word:
if letter is None:
return True
return False
out = []
for across, word in adescription.items():
if incomplete(word):
out.append("("+str(across[0])+","+str(across[1]) + ") across, " + str(len(word)) + " letters.")
for across, word in ddescription.items():
if incomplete(word):
out.append("("+str(across[0])+","+str(across[1]) + ") down, " + str(len(word)) + " letters.")
for p in out:
print(p)
#(0,0) down, 3 letters.
#(1,7) down, 6 letters.
#(2,0) across, 2 letters.
#(5,3) across, 2 letters.
#(5,3) down, 2 letters.
#(6,0) across, 4 letters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment