Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 134 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +14 −0 bitarrays.py
  3. +66 −0 intops.py
  4. +53 −0 linkedlist.py
View
1 .gitignore
@@ -0,0 +1 @@
+*.pyc
View
14 bitarrays.py
@@ -0,0 +1,14 @@
+
+# Bit arrays
+
+def ba_and(x, y):
+ return [i and j for i, j in zip(x, y)]
+
+def test_ba_and():
+ assert ba_and([0,1,0,1],[1,0,0,1]) == [0,0,0,1]
+
+def ba_or(x, y):
+ return [i or j for i, j in zip(x, y)]
+
+def test_ba_or():
+ assert ba_or([0,1,0,1],[1,0,0,1]) == [1,1,0,1]
View
66 intops.py
@@ -0,0 +1,66 @@
+
+import random
+
+
+# Integer operations
+
+def count_of(seq, test=lambda x: True):
+ # O(N)
+ counter = 0
+ for val in seq:
+ if test(val):
+ counter += 1
+ return counter
+
+def test_count_of():
+ seq = range(1000)
+ assert count_of(seq, lambda x: x == 4) == 1
+ assert count_of(seq, lambda x: x % 10 == 0) == 100
+
+# Find smallest number
+
+def smallest(seq):
+ # O(N)
+ m = None
+ for val in seq:
+ if m is None or val < m:
+ m = val
+ return m
+
+def test_smallest():
+ seq = [random.randrange(0,1000) for i in range(100)]
+ assert min(seq) == smallest(seq)
+
+def largest(seq):
+ return -smallest(-s for s in seq)
+
+def test_largest():
+ seq = [random.randrange(0,1000) for i in range(100)]
+ assert max(seq) == largest(seq)
+
+def is_palindrome(n):
+ s = str(n)
+ for i in range(len(s) // 2):
+ if s[i] != s[-(i+1)]:
+ return False
+ return True
+
+def test_is_palindrome():
+ assert is_palindrome(11211)
+ assert not is_palindrome(11212)
+ assert is_palindrome(123321)
+ assert not is_palindrome(123221)
+
+def closest_palindrome(n):
+ s = list(str(n))
+ for i in range(len(s) // 2):
+ s[-(i+1)] = s[i]
+ return int(''.join(s))
+
+def test_closest_palindrome():
+ assert closest_palindrome(112233) == 112211
+ for i in range(10):
+ n = random.randrange(0,100000)
+ assert is_palindrome(closest_palindrome(n))
+
+
View
53 linkedlist.py
@@ -0,0 +1,53 @@
+
+# Linked Lists
+
+class Node(object):
+ def __init__(self, value, next=None):
+ self.value = value
+ self.next = next
+
+class LinkedList(object):
+ def __init__(self, seq=None):
+ self.length = 0
+ self.last = None
+
+ if not seq:
+ self.first = None
+ return
+
+ self.last = None
+ for val in seq:
+ self.length += 1
+ if not self.last:
+ self.first = Node(val)
+ self.last = self.first
+ else:
+ self.last.next = Node(val)
+ self.last = self.last.next
+
+ def __repr__(self):
+ node = self.first
+ vals = []
+ while node:
+ vals.append(str(node.value))
+ node = node.next
+ return '[%s]' % ','.join(vals)
+
+ def append(self, val):
+ self.last.next = Node(val)
+ self.length += 1
+
+ def insert(self, pos, val):
+ node = self.first
+ while pos > 0:
+ node = node.next
+ pos -=1
+
+ new_node = Node(val, node.next)
+ node.next = new_node
+
+ self.length += 1
+
+ def __len__(self):
+ return self.length
+

No commit comments for this range

Something went wrong with that request. Please try again.