Skip to content

Instantly share code, notes, and snippets.

@kingoflolz
Created August 22, 2015 12:01
Show Gist options
  • Save kingoflolz/8a3d0d7828fa79bab821 to your computer and use it in GitHub Desktop.
Save kingoflolz/8a3d0d7828fa79bab821 to your computer and use it in GitHub Desktop.
def pprint(obj, level=1, nospace=False, noback=False):
out = ""
if isinstance(obj, list):
if not nospace:
out += ((level - 1) * " ") + "["
else:
out += "["
first = True
for element in obj:
if length(element) == 1:
noback = True
if first:
if length(obj) != 1:
out += pprint(element, level + 1, True, noback) + ",\n"
else:
out += pprint(element, level + 1, True, noback)
first = False
else:
if length(obj) == 1:
out += pprint(element, level + 1, False, noback)
else:
out = out + pprint(element, level + 1, False, False) + ",\n"
if length(obj) == 0 or length(obj) == 1:
out += "]"
else:
if length(obj) == 1:
if not noback:
out = out + ((level - 1) * " ") + "]"
else:
out += "]"
else:
out += ((level - 1) * " ") + "]"
elif isinstance(obj, str):
if not nospace:
out = ((level - 1) * " ") + "'" + obj + "'"
else:
out = "'" + obj + "'"
elif isinstance(obj, int):
if not nospace:
out = ((level - 1) * " ") + str(obj)
else:
out = str(obj)
return out
def length(obj):
if isinstance(obj, list):
return len(obj)
return 1
#print(pprint(['one', 'two', 'three', [1, 2, 3], 'four', [], 'five', [['six']]]))
## print("\n" * 5)
##print('[[[[],\n [],\n ],\n [[],\n [],\n ],\n ],\n [[[],\n [],\n ],\n [[],\n [],\n ],\n ],\n]')
#print("\n" * 5)
#print(pprint([0,[[1,2]],3]))
#print(pprint([[[[], []], [[], []]], [[[], []], [[],
# []]]]) == '[[[[],\n [],\n ],\n [[],\n [],\n ],\n ],\n [[[],\n [],\n ],\n [[],\n [],\n ],\n ],\n]')
import re
RE_TOKENS = re.compile(r'[()\\/]|[a-zA-Z]+')
def tokenize(cat):
return RE_TOKENS.findall(cat)
def ccgparse(ccg):
try:
brackets = 1
maxbrackets = 1
first = True
right = False
i = 0
#print(ccg)
for tokens in ccg:
if brackets == 2 and i + 1 != len(ccg):
if ccg[i] == "\\" or ccg[i] == "/":
# print(ccg[1:i], ccg[i+1:-1])
if ccgparse(ccg[1:i]) and ccgparse(ccg[i + 1:-1]):
right = True
if str(tokens) == "(":
brackets += 1
maxbrackets += 1
first = False
elif str(tokens) == ")":
brackets -= 1
first = False
i += 1
if len(ccg) == 1 and (ccg[0] == "NP" or ccg[0] == "PP" or ccg[0] == "N" or ccg[0] == "S"):
return True
if len(ccg) != 1 and maxbrackets < 2:
return False
return right
except:
return False
def parse(tokens):
result = ccgparse(tokens)
return result
def is_valid(cat):
tokens = tokenize(cat)
return parse(tokens) and len(tokens) != 0
cat = input('Enter category: ')
while cat:
if is_valid(cat):
print(cat, 'is valid')
else:
print(cat, 'is invalid')
cat = input('Enter category: ')
import json
puzzle = []
for row in range(9):
puzzle.append([])
for number in str(input('Enter line: ')):
puzzle[-1].append(int(number))
i = 1
for line in puzzle:
seen = []
errored = []
for number in line:
if number not in seen:
seen.append(number)
else:
if number not in errored:
errored.append(number)
print("duplicate "+str(number)+" in row "+str(i))
i += 1
i = 1
for line in range(9):
seen = []
errored = []
for number in puzzle:
if number[line] not in seen:
seen.append(number[line])
else:
if number[line] not in errored:
errored.append(number[line])
print("duplicate "+str(number[line])+" in column "+str(i))
i += 1
for row in range(3):
for column in range(3):
seen = []
errored = []
for row_s in range(3):
for column_s in range(3):
if puzzle[column*3+column_s][row*3+row_s] not in seen:
seen.append(puzzle[column*3+column_s][row*3+row_s])
else:
if puzzle[column*3+column_s][row*3+row_s] not in errored:
errored.append(puzzle[column*3+column_s][row*3+row_s])
print("duplicate " + str(puzzle[column*3+column_s][row*3+row_s]) + " in grid " +str(row+column*3+1))
#print("\n"+json.dumps(puzzle))
#543678912
#672195348
#198342567
#859761423
#426853791
#713924856
#961537284
#287419631
#345286179
#928614735
#451397628
#673852149
#235786491
#714935862
#896421357
#162548973
#387269514
#949173286
#543678912
#674195348
#898342567
#659761423
#426853791
#713924856
#661537284
#287419631
#345286111
#in row 8
#in column 2
#in column 3
#in column 9
#in grid 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment