<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Problematic Sets</title>
	<atom:link href="http://problematicsets.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://problematicsets.com</link>
	<description>Learning to solve problems, one blog post at a time</description>
	<lastBuildDate>Wed, 01 Feb 2012 15:11:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler 86 &#8211; Exploring the shortest path from opposite corners of a cuboid</title>
		<link>http://problematicsets.com/project-euler-86-exploring-the-shortest-path-from-opposite-corners-of-a-cuboid/</link>
		<comments>http://problematicsets.com/project-euler-86-exploring-the-shortest-path-from-opposite-corners-of-a-cuboid/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 15:11:46 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=921</guid>
		<description><![CDATA[http://projecteuler.net/problem=86 #euler 86 import math import time t0 = time.time() def gcd(a,b): while b!= 0: a, b = b, a%b return a def isCoprime(a,b): if gcd(a,b) == 1: return True else: return False mlimit = 65 # generate a list of primitives prim = [] for m in range(2, mlimit): for n in range(1, m): [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://projecteuler.net/problem=86">http://projecteuler.net/problem=86</a></p>
<pre>
#euler 86
import math
import time
t0 = time.time()

def gcd(a,b):
    while b!= 0:
        a, b = b, a%b
    return a

def isCoprime(a,b):
    if gcd(a,b) == 1:
        return True
    else:
        return False

mlimit = 65  # generate a list of primitives
prim = []
for m in range(2, mlimit):
    for n in range(1, m):
        if (n+m) % 2 == 1 and  isCoprime(m,n):
            a = m*m - n*n
            b = 2*m*n
            c = m*m + n*n
            temp = [a,b,c]
            temp.sort()
            ttup = tuple(temp)
            prim.append(ttup)

print len(prim), "primitive triples were generated"
highest = max(x[1] for x in prim)
print "the greatest of the first 2 parts of the triple was ", highest

# from your list of primitives, generate all pythagorean triples where the longer side (not the hypotenuse)
# is less than the longest side observed
allTriples = prim[:]
for x in prim:
    side = x[1]
    counter = highest / side
    if counter > 1:
        for j in range(1, counter):
            factor = j+1
            temp = tuple([factor * y for y in x])
            allTriples.append(temp)

# mergesort (based on second number)
def merge(left, right):
    result = []
    i ,j = 0, 0
    while i < len(left) and j < len(right):
        if left[i][1] <= right[j][1]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]
    result += right[j:]
    return result

def mergesort(list):
    if len(list) < 2:
        return list
    else:
        middle = len(list) / 2
        left = mergesort(list[:middle])
        right = mergesort(list[middle:])
        return merge(left, right)

sortedTriples = mergesort(allTriples)

def takeTriple(triple):  #takes triple, returns number of shortest distance cubes
    shorter = triple[0]
    longer = triple[1]
    # when M is shorter, let's count ways to split longer
    # i.e. for triple (6,8,10)
    # shorter is 6, longer is 8
    # we set M to 6, and we want to split 8 into (6,2),  (5,3), (4,4)
    output = {}
    base = longer / 2
    if shorter > base:
        output[shorter] = shorter - base + 1 - (longer % 2)
    base2 = shorter / 2
    output[longer] = shorter - base2 - (shorter % 2)
    return output

mDict = {}
for i in range(1, highest+1):
    mDict[i] = 0

for i in sortedTriples:
    temp = takeTriple(i)
    for j in temp:
        mDict[j] += temp[j]

sumDict = {0:0}
for i in range(1, highest+1):
    sumDict[i] = sumDict[i-1] + mDict[i]

#iterate through dictionary, and stop when you find 1000000

counter = 1
while True:
    counter += 1
    if sumDict[counter] >= 1000000:
        print "The answer is ", counter
        print "There were ", sumDict[counter], "cuboids less than the MxMxM dimensions"
        break

t1 = time.time()
total = t1-t0
print total, "seconds"
</pre>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/project-euler-86-exploring-the-shortest-path-from-opposite-corners-of-a-cuboid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Who&#8217;s Who Wiki &#8211; Wiki driven corporate transparency Latin America</title>
		<link>http://problematicsets.com/whos-who-wiki-wiki-driven-corporate-transparency-latin-america/</link>
		<comments>http://problematicsets.com/whos-who-wiki-wiki-driven-corporate-transparency-latin-america/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 17:34:20 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Social Entrepreneurship]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=918</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p></p><p><iframe width="300" height="403" src="http://startsomegood.com/venture/who_s_who_wiki/campaigns/widget/wiki_driven_corporate_transparency_latin_america" frameborder="0"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/whos-who-wiki-wiki-driven-corporate-transparency-latin-america/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Node.js</title>
		<link>http://problematicsets.com/introduction-to-node-js/</link>
		<comments>http://problematicsets.com/introduction-to-node-js/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 20:57:58 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=916</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p></p><p><iframe width="640" height="360" src="http://www.youtube.com/embed/jo_B4LTHi3I" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/introduction-to-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linked Lists video lecture &#8211; UC Berkeley</title>
		<link>http://problematicsets.com/linked-lists-video-lecture-uc-berkeley/</link>
		<comments>http://problematicsets.com/linked-lists-video-lecture-uc-berkeley/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 16:11:11 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=913</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p></p><p><iframe width="640" height="480" src="http://www.youtube.com/embed/htzJdKoEmO0" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/linked-lists-video-lecture-uc-berkeley/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Video: 3 Big Data Tech Talks You Can’t Miss</title>
		<link>http://problematicsets.com/video-3-big-data-tech-talks-you-can%e2%80%99t-miss/</link>
		<comments>http://problematicsets.com/video-3-big-data-tech-talks-you-can%e2%80%99t-miss/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 16:03:51 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=911</guid>
		<description><![CDATA[Would like to watch these someday&#8230;. http://engineering.linkedin.com/event/video-3-big-data-tech-talks-you-can%E2%80%99t-miss]]></description>
			<content:encoded><![CDATA[<p></p><p>Would like to watch these someday&#8230;.</p>
<p><a href="http://engineering.linkedin.com/event/video-3-big-data-tech-talks-you-can%E2%80%99t-miss">http://engineering.linkedin.com/event/video-3-big-data-tech-talks-you-can%E2%80%99t-miss</a></p>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/video-3-big-data-tech-talks-you-can%e2%80%99t-miss/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 83 &#8211; Minimal path sums using Dijkstra&#8217;s algorithm</title>
		<link>http://problematicsets.com/project-euler-83-minimal-path-sums-using-dijkstras-algorithm/</link>
		<comments>http://problematicsets.com/project-euler-83-minimal-path-sums-using-dijkstras-algorithm/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 03:32:59 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=909</guid>
		<description><![CDATA[http://projecteuler.net/problem=83 I used the pseudocode from Wikipedia on Dijkstra&#8217;s algorithm to solve this problem. # euler 82 # grid is a dictionary with key as tuple (i,j) and value as value of node # i is row, j is column of grid import time t0 = time.time() f = open('matrix.txt', 'r') grid = {} counter [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://projecteuler.net/problem=83">http://projecteuler.net/problem=83</a></p>
<p>I used the pseudocode from Wikipedia on<a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm"> Dijkstra&#8217;s algorithm </a>to solve this problem.</p>
<pre>
# euler 82
# grid is a dictionary with key as tuple (i,j) and value as value of node
# i is row, j is column of grid

import time
t0 = time.time()

f = open('matrix.txt', 'r')
grid = {}
counter = 1
for line in f:
   temp = line.replace('\n','')
   temp = temp.split(',')
   for a in range(len(temp)):
       grid[(counter,a+1)] = int(temp[a])
   counter += 1
rows = 80
cols = 80

def findNeighbors(vertex):
   i, j = vertex[0], vertex[1]
   output = [(i-1,j), (i+1,j), (i,j-1), (i,j+1)]  # all neighbors
   remove = []
   for v in output:
      if v[0] < 1 or v[0] > rows or v[1] < 1 or v[1] > cols:
         remove.append(v)
   final = [x for x in output if not x in remove]
   return final

# Let's implement Dijkstra's algo from Wikipedia
def dka(graph, source):    # graph will be our grid, source will be (1,1)
   dist = {}
   for v in graph:
      dist[v] = float("inf")
   dist[source] = graph[source]
   Q = dist.copy()   #make a copy of graph, as a hash
   length = len(Q)
   while length > 0:
      u = min(Q, key=Q.get)
      if dist[u] == float("inf"):
         break
      if u == (rows, cols):
         break
      del Q[u]
      length -= 1
      n = findNeighbors((u))
      neighbors = [x for x in n if x in Q]
      for v in neighbors:
         alt = dist[u] + grid[v]
         if alt < dist[v]:
            dist[v] = alt
            Q[v] = alt
   return dist

answerGrid = dka(grid,(1,1))

print "The answer is ", answerGrid[(rows,cols)]
t1= time.time()
total = t1-t0
print total, "seconds"
</pre>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/project-euler-83-minimal-path-sums-using-dijkstras-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: get key with the least value from a dictionary</title>
		<link>http://problematicsets.com/python-get-key-with-the-least-value-from-a-dictionary/</link>
		<comments>http://problematicsets.com/python-get-key-with-the-least-value-from-a-dictionary/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 23:51:02 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=907</guid>
		<description><![CDATA[From StackOverflow: http://stackoverflow.com/questions/3282823/python-get-key-with-the-least-value-from-a-dictionary min(d, key=d.get)]]></description>
			<content:encoded><![CDATA[<p></p><p>From StackOverflow:</p>
<p><a href="http://stackoverflow.com/questions/3282823/python-get-key-with-the-least-value-from-a-dictionary">http://stackoverflow.com/questions/3282823/python-get-key-with-the-least-value-from-a-dictionary</a></p>
<p>min(d, key=d.get)</p>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/python-get-key-with-the-least-value-from-a-dictionary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 82 &#8211; Find the minimal path sum from the left column to the right column</title>
		<link>http://problematicsets.com/project-euler-82-find-the-minimal-path-sum-from-the-left-column-to-the-right-column/</link>
		<comments>http://problematicsets.com/project-euler-82-find-the-minimal-path-sum-from-the-left-column-to-the-right-column/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 13:39:29 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=904</guid>
		<description><![CDATA[http://projecteuler.net/problem=82 I&#8217;m looking forward to reading the forums in Euler for this one, as I&#8217;m curious to find a more elegant way to solve this problem&#8230; # euler 82 f = open('matrix.txt', 'r') # grid is a dictionary with key as tuple (i,j) and value as value of node # i is row, j is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://projecteuler.net/problem=82">http://projecteuler.net/problem=82</a></p>
<p>I&#8217;m looking forward to reading the forums in Euler for this one, as I&#8217;m curious to find a more elegant way to solve this problem&#8230;</p>
<pre>
# euler 82

f = open('matrix.txt', 'r')

# grid is a dictionary with key as tuple (i,j) and value as value of node
# i is row, j is column of grid
grid = {}
counter = 1
for line in f:
   temp = line.replace('\n','')
   temp = temp.split(',')
   for a in range(len(temp)):
       grid[(counter,a+1)] = int(temp[a])
   counter += 1

rows = 80
cols = 80

minGrid = {}
# first column of min grid is just the value of the node
for i in range(1,rows+1):
    minGrid[(i,1)] = grid[(i,1)]

def compare(location):  # takes a node (i,j) returns cheapest way to get there
    i, j = location[0], location[1]
    lookleft = minGrid[(i,j-1)]
    upSum = 0
    cellsUp = 0
    maxUp = i-1 #this is the max number of cells up to check
    while cellsUp < maxUp:
        cellsUp += 1
        upSum += grid[(i-cellsUp, j)]
        if upSum > lookleft:   #going up is too expensive
            cellsUp -= 1
            break
    downSum = 0
    cellsDown = 0
    maxDown = rows - i
    while cellsDown < maxDown:  #checking cells down
        cellsDown += 1
        downSum += grid[(i+cellsDown, j)]
        if downSum > lookleft:
            cellsDown -= 1
            break
    upMemo = {0:0}
    upMin = []
    downMemo = {0:0}
    downMin = []
    if cellsUp > 0:
        for cu in range(1, cellsUp+1):
            upMemo[cu] = upMemo[cu-1] + grid[(i-cu,j)]
            upMin.append(upMemo[cu] + minGrid[(i-cu, j-1)])
    if cellsDown > 0:
        for cd in range(1, cellsDown+1):
            downMemo[cd] = downMemo[cd-1] + grid[(i+cd,j)]
            downMin.append(downMemo[cd] + minGrid[(i+cd, j-1)])
    bigList = [lookleft]
    for a in upMin:
        bigList.append(a)
    for b in downMin:
        bigList.append(b)
    lowest = min(bigList)
    return grid[(i,j)] + lowest

for c in range(2, cols+1):
    for d in range(1, rows+1):
        minGrid[(d,c)] = compare((d,c))

cheapestList = []
for a in range(1, rows+1):
    cheapestList.append(minGrid[(a,cols)])

print "Cheapest path is ", min(cheapestList)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/project-euler-82-find-the-minimal-path-sum-from-the-left-column-to-the-right-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If I&#8217;m using a for loop to generate a list of strings, how do I create a list of the variables, and not a list of the strings?</title>
		<link>http://problematicsets.com/if-im-using-a-for-loop-to-generate-a-list-of-strings-how-do-i-create-a-list-of-the-variables-and-not-a-list-of-the-strings/</link>
		<comments>http://problematicsets.com/if-im-using-a-for-loop-to-generate-a-list-of-strings-how-do-i-create-a-list-of-the-variables-and-not-a-list-of-the-strings/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 18:46:25 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=902</guid>
		<description><![CDATA[I posed a question on StackExchange today&#8230;. http://stackoverflow.com/q/9020500/1171561]]></description>
			<content:encoded><![CDATA[<p></p><p>I posed a question on StackExchange today&#8230;.</p>
<p><a href="http://stackoverflow.com/q/9020500/1171561">http://stackoverflow.com/q/9020500/1171561</a></p>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/if-im-using-a-for-loop-to-generate-a-list-of-strings-how-do-i-create-a-list-of-the-variables-and-not-a-list-of-the-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 79 &#8211; By analysing a user&#8217;s login attempts, find the secret passcode</title>
		<link>http://problematicsets.com/project-euler-79-by-analysing-a-users-login-attempts-find-the-secret-passcode/</link>
		<comments>http://problematicsets.com/project-euler-79-by-analysing-a-users-login-attempts-find-the-secret-passcode/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 21:45:13 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://problematicsets.com/?p=893</guid>
		<description><![CDATA[http://projecteuler.net/problem=79 As I was researching the web for approaches to solving this problem, I found Kristian&#8217;s MathBlog article quite useful (as usual), and so I took up his suggestion to try implementing a topological sorting algorithm, which I found described in Wikipedia. I had some problems just using the pseudocode described in Wikipedia, mostly because [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://projecteuler.net/problem=79">http://projecteuler.net/problem=79</a></p>
<p>As I was researching the web for approaches to solving this problem, I found <a href="http://www.mathblog.dk/project-euler-79-secret-numeric-passcode/">Kristian&#8217;s MathBlog article </a>quite useful (as usual), and so I took up his suggestion to try implementing a <a href="http://en.wikipedia.org/wiki/Topological_sorting">topological sorting algorithm</a>, which I found  described in Wikipedia.</p>
<p>I had some problems just using the pseudocode described in Wikipedia, mostly because Python won&#8217;t let you iterate over a set if you change the set during the iteration, so I had to figure out some workarounds.</p>
<pre>
#euler 79

f = open('keylog.txt', 'r')
attempts = []
for line in f:
    attempts.append(line.replace('\n',''))

candidates = []   #possible numbers in passcode
for i in attempts:
    for j in i:
        if not j in candidates:
            candidates.append(j)

# let's take attempts, and turn it into a set of directed edges
# each edge is a tuple (a, b) where a precedes b

edges = set()
for i in attempts:
    e1 = (i[0],i[1])
    e2 = (i[1],i[2])
    edges.add(e1)
    edges.add(e2)

#let's implement the Kahn topological sorting algo (see Wikipedia)
def findNoEdge(candidates, edges):  # returns list of candidates with no incoming edge
    output = []
    for a in candidates:
        test = True
        for b in edges:
            if b[1] == a:
                test = False
                break
        if test:
            output.append(a)
    return output

L = []  #empty list of sorted elements
S = findNoEdge(candidates, edges)

def removeNode(n, S, L, edges):
    S.remove(n)
    L.append(n)
    edgesToRemove = []
    for i in edges:  # i is a tuple of node a pointing to node b
        if i[0] == n:
            edgesToRemove.append(i)
    for j in edgesToRemove:
        edges.remove(j)
    for j in edgesToRemove:
        temp = findNoEdge(j, edges)  #this is a list of nodes that have no incoming edges
        for k in temp:
            if not k in L:
                S.append(k)
    return S, L, edges

keepGoing = True
while keepGoing:
    a = len(S)
    sCopy = S[:]
    for counter in range(a):
        S, L, edges = removeNode(sCopy[counter], S, L, edges)
    if len(S) == 0:
        print "".join(str(x) for x in L)
        break
</pre>
]]></content:encoded>
			<wfw:commentRss>http://problematicsets.com/project-euler-79-by-analysing-a-users-login-attempts-find-the-secret-passcode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

