Browse Source

convert all of them to python3

pull/1/head
wes 7 years ago
parent
commit
db8ac2d149
  1. 11
      bst.py
  2. 8
      intersection.py
  3. 4
      palindrome.py
  4. 8
      pascal.py
  5. 4
      phone.py
  6. 4
      regions.py

11
bst.py

@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
from random import randint
from pprint import PrettyPrinter
@ -18,7 +18,7 @@ class Branch:
return "(%s) %s (%s)" % (repr(self.left), self.value, repr(self.right))
def split(xs):
l = len(xs) / 2
l = int(len(xs) / 2)
return (xs[0:l], xs[l], xs[l+1:])
def bst(xs):
@ -33,7 +33,6 @@ def makeBST(xs):
def findBST(tree, x):
if tree is None:
return None
print tree.value
if tree.value == x:
return tree
if x < tree.value:
@ -43,6 +42,6 @@ def findBST(tree, x):
test = [randint(1,50) for _ in range(20)]
print test
print "finding %s" % test[4]
print findBST(makeBST(test), test[4])
print(test)
print("finding %s" % test[4])
print(findBST(makeBST(test), test[4]))

8
intersection.py

@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
# find the intersection of two lists including duplicate values
@ -15,9 +15,9 @@ def intersection(xs, ys):
freqs2 = frequencies(ys)
intersection = []
for k, v in freqs1.iteritems():
for k, v in freqs1.items():
n = min(v, freqs2[k])
intersection.extend([k for _ in xrange(n)])
intersection.extend([k for _ in range(n)])
return intersection
print intersection([1,4,2,6,10,4,4], [7,4,9,10,20,4,10])
print(intersection([1,4,2,6,10,4,4], [7,4,9,10,20,4,10]))

4
palindrome.py

@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
# check if a word is palindromic without reversing it
@ -16,4 +16,4 @@ def isPalindrome(word):
end -= 1
return retval
print isPalindrome("wwaabbaawwz")
print(isPalindrome("wwaabbaawwz"))

8
pascal.py

@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
# find the rows of pascal's triangle
@ -8,7 +8,7 @@ def choose(n, k):
return choose(n, k-1) * (n-k) / k
def pascal(n):
return [choose(n, i) for i in xrange(0, n)]
return [choose(n, i) for i in range(0, n)]
for i in xrange(1, 11):
print pascal(i)
for i in range(1, 11):
print(pascal(i))

4
phone.py

@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
# find all possible permutations of a sequence of phone pad digits (other than 1 and 0)
@ -34,4 +34,4 @@ def numToWords(num):
return genAll(lettersets)[0]
for num in numToWords("353346"):
print num
print(num)

4
regions.py

@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
# counts the number of regions in a matrix
# vertically and horizontally connected
@ -56,4 +56,4 @@ for y, row in enumerate(testregion):
count += 1
exploreField(testregion, x, y)
print count
print(count)

Loading…
Cancel
Save