Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2012 20:25
Show Gist options
  • Select an option

  • Save anonymous/1575913 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1575913 to your computer and use it in GitHub Desktop.
Deckmaster's code for NPCC 2
Deckmaster's solutions for the second NPCC. Labeled by number
Problem 1:
def match_all(s):
count = 0
for lett in s:
if "(" == lett:
count += 1
if ")" == lett:
count -= 1
if 0 > count:
return False
return 0 == count
Problem 2:
def do_this(a, s, b):
if s == "add":
if a == 1 and b == 1:
return 11
return a + b
elif s == "subtract":
return a - b
elif s == "divide":
if b == 0:
return "undefined"
return (1.0 * a) / b
elif s == "multiply":
return a * b
elif s == "to the power of":
if a == 0 and b == 0:
return "indeterminate"
return a ** b
elif s == "SHAZAM":
return "potato"
Problem 3:
def in_arabic_please(s):
if 0 == len(s):
return 0
s = s.split()
num = s.pop(0)
nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
n = 0
if num in nums:
n = nums.index(num)
if 0 < n and 10 > n and 0 != len(s):
if "hundred" == s[0]:
n *= 100
s.pop(0)
elif "thousand" == s[0]:
n *= 1000
s.pop(0)
else:
tens = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
if num in tens:
n = tens.index(num)
n *= 10
else:
print s, n, num
return "invalid"
return n + in_arabic_please(" ".join(s))
Problem 4:
# Because I couldn't figure out how to do this with standard functions. So I wrote my own!
def my_split(s):
out = []
word = ""
for lett in s.lower():
if (lett >= "a" and lett <= "z") or "'" == lett:
word += lett
else:
if 0 != len(word):
out.append(word)
word = ""
if 0 != len(word):
out.append(word)
return out
def most_used(s):
d = {}
for word in my_split(s):
if word not in d:
d[word] = 0
d[word] += 1
tmp = {v:k for k,v in d.iteritems()}
res1 = tmp[max(tmp)]
del tmp[max(tmp)]
res2 = tmp[max(tmp)]
del tmp[max(tmp)]
res3 = tmp[max(tmp)]
del tmp[max(tmp)]
return [res1, res2, res3]
Problem 5:
def factor(num):
if 1 == num:
return [1]
factors = []
i = 2
while 1 != num:
if num % i == 0:
factors.append(i)
num = num / i
else:
i += 1
return factors
def recompose(factors):
out = 1
for i in factors:
out *= i
return out
def break_it_down(a, b):
afact = factor(a)
bfact = factor(b)
newa = []
for num in afact:
if num in bfact:
bfact.remove(num)
else:
newa.append(num)
return (recompose(newa), recompose(bfact))
Problem 6:
Note: I did not get this to work in the contest. I did figure out the issue 20 minutes later, though.
def count_neighbors(grid, i, j):
neighbors = 0
if i > 0:
if j > 0:
if grid[i-1][j-1]:
neighbors += 1
if grid[i-1][j]:
neighbors += 1
if j + 1 < len(grid[i-1]):
if grid[i-1][j+1]:
neighbors += 1
print neighbors
if j > 0:
if grid[i][j-1]:
neighbors += 1
if j + 1 < len(grid[i]):
if grid[i][j+1]:
neighbors += 1
print neighbors
if i + 1 < len(grid):
if j > 0:
if grid[i+1][j-1]:
neighbors += 1
if grid[i+1][j]:
neighbors += 1
if j + 1 < len(grid[i+1]):
if grid[i+1][j+1]:
neighbors += 1
return neighbors
def grid_to_string(grid):
out = ""
for line in grid:
for val in line:
if val:
out += "x"
else:
out += " "
out += "\n"
return out[:len(out)-2] # Trim off the final \n
def thats_life(blargh, iters):
grid = []
for line in blargh.split("\n"):
gridline = []
for val in line:
gridline.append("x" == val)
grid.append(gridline)
return grid
for i in xrange(iters):
newgrid = grid
for i in xrange(len(grid)):
for j in xrange(len(grid[i])):
neighbors = count_neighbors(grid, i, j)
if grid[i][j]:
if 1 < neighbors and 4 > neighbors:
newgrid[i][j] = True
else:
newgrid[i][j] = False
else:
if neighbors == 3:
newgrid[i][j] = True
else:
newgrid[i][j] = False
grid = newgrid
print grid_to_string(grid) + "\n------\n"
return grid_to_string(grid)
Problem 6 revisited:
This is what is above...but working properly
def count_neighbors(grid, i, j):
neighbors = 0
if i > 0:
if j > 0:
if grid[i-1][j-1]:
neighbors += 1
if grid[i-1][j]:
neighbors += 1
if j + 1 < len(grid[i-1]):
if grid[i-1][j+1]:
neighbors += 1
if j > 0:
if grid[i][j-1]:
neighbors += 1
if j + 1 < len(grid[i]):
if grid[i][j+1]:
neighbors += 1
if i + 1 < len(grid):
if j > 0:
if grid[i+1][j-1]:
neighbors += 1
if grid[i+1][j]:
neighbors += 1
if j + 1 < len(grid[i+1]):
if grid[i+1][j+1]:
neighbors += 1
return neighbors
def grid_to_string(grid):
out = ""
for line in grid:
for val in line:
if val:
out += "x"
else:
out += " "
out += "\n"
return out[:-1] # Trim off the final \n
def thats_life(blargh, iters):
grid = []
for line in blargh.split("\n"):
gridline = []
for val in line:
gridline.append("x" == val)
grid.append(gridline)
for i in xrange(iters):
newgrid = [grid[j][:] for j in xrange(len(grid))]
for i in xrange(len(grid)):
for j in xrange(len(grid[i])):
neighbors = count_neighbors(grid, i, j)
if grid[i][j]:
if 1 < neighbors and 4 > neighbors:
newgrid[i][j] = True
else:
newgrid[i][j] = False
else:
if neighbors == 3:
newgrid[i][j] = True
else:
newgrid[i][j] = False
grid = newgrid
return grid_to_string(grid)
Problem 7:
def find_common(list1, list2):
out = []
for num in list1:
if num in list2:
if num not in out:
out.append(num)
return out
Problem 8:
def is_it_square(l):
grid = {}
for tup in l:
if tup[0] not in grid:
grid[tup[0]] = {}
grid[tup[0]][tup[1]] = True
min1 = min(grid)
max1 = max(grid)
min2 = min(grid[min1])
max2 = max(grid[min1])
for i in xrange(min1 + 1, max1):
if i not in grid: # If this isn't in the grid, it's not a square
return False
if min2 != min(grid[i]) or max2 != max(grid[i]): # Also check extremes
return False
for i in xrange(min2, max2 + 1): # Check top and bottom
if i not in grid[min1] or i not in grid[max1]:
return False
if min2 != min(grid[max1]) or max2 != max(grid[max1]):
return false
return True
Problem 9:
def isPrime(n):
if n < 5:
return n in (2, 3)
if 0 == n % 2 or 0 == n % 3:
return False
i = 0
while i < n ** 0.5:
i += 6
if 0 == n % (i-1) or 0 == n % (i+1):
return n != (i-1) or n != (i+1)
return True
def am_i_surrounded(n):
return isPrime(n-1) and isPrime(n+1)
Problem 10:
def rainbow_science(l, x, y):
col = [False, False, False]
for rect in l:
if rect[0] <= x and rect[0] + rect[2] >= x:
if rect[1] <= y and rect[1] + rect[3] >= y:
if rect[4] == "red":
col[0] = True
elif rect[4] == "yellow":
col[1] = True
elif rect[4] == "blue":
col[2] = True
if not col[0] and not col[1] and not col[2]:
return "white"
elif col[0] and not col[1] and not col[2]:
return "red"
elif not col[0] and col[1] and not col[2]:
return "yellow"
elif not col[0] and not col[1] and col[2]:
return "blue"
elif col[0] and col[1] and not col[2]:
return "orange"
elif not col[0] and col[1] and col[2]:
return "green"
elif col[0] and not col[1] and col[2]:
return "purple"
elif col[0] and col[1] and col[2]:
return "brown"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment