Here’s the problem:

File Fix It

with open('filefixitlarge.txt', 'r') as f:
	numCases = int(f.readline())
	for i in range(numCases):
		a = f.readline().split()
		n = int(a[0])
		m = int(a[1])
		# n is number of paths that exist
		# m is number of new paths
		existingDirs = {}
		for j in range(n):
			b = f.readline().replace('\n', "")
			existingDirs[b] = True
		counter = 0
		for k in range(m):
			c = f.readline().replace('\n', "")	
			subpaths = c.split('/')
			del subpaths[0]
			lsub = len(subpaths)
			for kk in range(lsub):
				filematch = '/' + '/'.join(subpaths[:kk+1])
				if not filematch in existingDirs:
					existingDirs[filematch] = True
					counter += 1
		print "Case #%d: %d" % (i+1, counter)

{ 0 comments }

Web designer tutorial

by Rob on October 4, 2013

in tutorial

{ 0 comments }

Newton’s method for calculating square roots in Scala and Haskell

September 18, 2013

Here are notes from Martin Odersky’s excellent class on Functional Programming Principles in Scala. We are coding using the Eclipse IDE, so we can see the results of our calculations in the worksheet. package week1 object session { def abs(x: Double) = if (x < 0) -x else x //> abs: (x: Double)Double def sqrtIter(guess: […]

Read the full article →

Hacker Rank: Sherlock And The Beast

September 15, 2013

Here’s the problem: Sherlock and the Beast # Enter your code here. Read input from STDIN. Print output to STDOUT import sys # this is a dynamic programming problem # like a variation of knapsack memo = {} # the key 3 represents the number of times we took a three # the key 5 […]

Read the full article →

Project Euler 149: Searching for a maximum-sum subsequence

September 14, 2013

Here’s the problem: Project Euler 149 #Euler 149 # First, let’s generate our pseudo random number generator import time import numpy as np t1 = time.time() class pseudoEuler: def __init__(self): self.k = int(1) self.value = (100003 – 200003 + 300007) % 1000000 – 500000 # note modulo operator takes precedence over subtraction self.aDict = {} […]

Read the full article →

Euclid’s algorithm (GCD) in Scala and Haskell and Python

September 14, 2013

In Scala: def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) In Haskell: myGcd :: Int -> Int -> Int myGcd a b | b == 0 = a | otherwise = gcd b (a `mod` b) In Python: def gcd(a,b): while b != 0: a, b […]

Read the full article →

Google Code Jam: Alien Language

September 14, 2013

Here’s the problem: Alien Language # this looks like a regular expressions problem import re with open(“alienLanguage.txt”, ‘r’) as f: firstLine = f.readline() parsedFirstLine = firstLine.split(” “) intFirstLine = [int(x) for x in parsedFirstLine] numLetters, numWords, numExpr = intFirstLine wordList = [] exprDict = {} for i in range(numWords): aWord = f.readline() aWord = aWord.replace(‘\n’, […]

Read the full article →

Google Code Jam: Bullseye

September 10, 2013

Here’s the problem: Bullseye Puzzler’s World has a great write up of the solution. I coded up a solution in Haskell: import System.IO import Control.Monad import Data.List.Split — here’s the sqrt from haskell.org (^!) :: Num a => a -> Int -> a (^!) x n = x^n squareRoot :: Integer -> Integer squareRoot 0 […]

Read the full article →

Google Code Jam: Rope Intranet

September 8, 2013

Here’s the problem: Rope Intranet def merge(leftHalf, rightHalf): leftIndex = 0 rightIndex = 0 leftLength = len(leftHalf) rightLength = len(rightHalf) returnArray = [] numSwaps = 0 while leftIndex < leftLength and rightIndex < rightLength: if leftHalf[leftIndex]

Read the full article →

Google Code Jam: T9 Spelling

September 7, 2013

Here’s the problem: T9 Spelling ourDict = { ‘a’: “2”, ‘b’: “22”, ‘c’: “222”, ‘d’: “3”, ‘e’: “33”, ‘f’: “333”, ‘g’: “4”, ‘h’: “44”, ‘i’: “444”, ‘j’: “5”, ‘k’: “55”, ‘l’: “555”, ‘m’: “6”, ‘n’: “66”, ‘o’: “666”, ‘p’: “7”, ‘q’: “77”, ‘r’: “777”, ‘s’: “7777”, ‘t’: “8”, ‘u’: “88”, ‘v’: “888”, ‘w’: “9”, ‘x’: […]

Read the full article →