-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cde7a42
Showing
9 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
def reverse(string): | ||
"""Reverse a given string.""" | ||
return string[::-1] | ||
|
||
if __name__ == '__main__': | ||
print reverse('a') | ||
print reverse('abcd') | ||
print reverse('hello world') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
def remove_duplicates(string): | ||
"""Remove duplicate characters in a string.""" | ||
return set(string) | ||
|
||
|
||
def remove_with_dict(string): | ||
"""Implement my own set function.""" | ||
string_dict = {} | ||
for letter in string: | ||
string_dict[letter] = True | ||
return string_dict.keys() | ||
|
||
if __name__ == '__main__': | ||
print remove_duplicates('a') | ||
print remove_duplicates('abcd') | ||
print remove_duplicates('abcda') | ||
print remove_with_dict('a') | ||
print remove_with_dict('abcd') | ||
print remove_with_dict('abcda') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
def anagram(first, second): | ||
"""Decide if two strings are anagrams of one another.""" | ||
return sorted(first) == sorted(second) | ||
|
||
if __name__ == '__main__': | ||
print anagram('a', 'b') | ||
print anagram('a', 'A') | ||
print anagram('hi', 'hi') | ||
print anagram('hello there', 'hello there') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env python | ||
|
||
import re | ||
|
||
|
||
def replace_space(string): | ||
"""Replace all spaces in a word with `%20`.""" | ||
return re.sub(' ', '%20', string) | ||
|
||
if __name__ == '__main__': | ||
print replace_space('abcd') | ||
print replace_space('hello world') | ||
print replace_space('another working example') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
|
||
"""If an element in an MxN matrix is 0, it's row and column are set to 0.""" | ||
|
||
|
||
def adjust_matrix(matrix): | ||
""" | ||
Given a matrix of size MxN, if an element is zero, set it's row and | ||
column to zeros. | ||
""" | ||
zero_rows = {} | ||
zero_columns = {} | ||
# Get location of all the zeros. | ||
for i, row in enumerate(matrix): | ||
for j, num in enumerate(row): | ||
if num is 0: | ||
zero_rows[i] = True | ||
zero_columns[j] = True | ||
# Adjust the matrix accordingly. | ||
for i, row in enumerate(matrix): | ||
for j, num in enumerate(row): | ||
# Dictionary lookup is O(1). | ||
if i in zero_rows or j in zero_columns: | ||
matrix[i][j] = 0 | ||
return matrix | ||
|
||
|
||
if __name__ == '__main__': | ||
matrix = [[1],[2],[3]] | ||
print adjust_matrix(matrix) | ||
|
||
matrix = [[1, 2], [1, 0]] | ||
print adjust_matrix(matrix) | ||
|
||
matrix = [[1, 2, 3], [1, 2, 0], [1, 2, 3]] | ||
print adjust_matrix(matrix) | ||
|
||
matrix = [[1, 2, 3, 4], [1, 2, 3, 4], | ||
[1, 2, 0, 4], [1, 2, 3, 4]] | ||
print adjust_matrix(matrix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
def is_rotation(first, second): | ||
"""Given two strings, is one a rotation of the other.""" | ||
if len(first) is not len(second): | ||
return False | ||
double_second = second + second | ||
return first in double_second | ||
|
||
if __name__ == '__main__': | ||
print is_rotation('apple', 'pleap') # True | ||
print is_rotation('apple', 'ppale') # False | ||
print is_rotation('zach', 'chza') # True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
Design a stack, which in addition to push and pop, also has a function to | ||
return the minimum element. | ||
""" | ||
|
||
class Stack(list): | ||
|
||
def push(self, number): | ||
"""Push method -- similar to other languages.""" | ||
if len(self) > 0: | ||
last = self[-1] | ||
minimum = self._find_minimum(number, last) | ||
else: | ||
minimum = number | ||
self.minimum = minimum | ||
self.append(NodeWithMin(number, minimum)) | ||
|
||
def _find_minimum(self, number, last_number): | ||
"""Internal method to compare two numbers.""" | ||
if number < last_number.minimum: | ||
return number | ||
return last_number.minimum | ||
|
||
def min(self): | ||
"""Return the minimum element.""" | ||
return self.minimum | ||
|
||
|
||
class NodeWithMin(object): | ||
|
||
def __init__(self, number, minimum): | ||
self.number = number | ||
self.minimum = minimum | ||
|
||
def __repr__(self): | ||
return str(self.number) | ||
|
||
def min(self): | ||
return self.minimum | ||
|
||
|
||
def main(): | ||
z = Stack() | ||
z.push(1) | ||
z.push(2) | ||
z.push(3) | ||
node = z.pop() | ||
print node.minimum | ||
z.push(0) | ||
z.push(4) | ||
node = z.pop() | ||
print node.min() | ||
print z.min() | ||
print z | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
Towers of Hanoi in Python. | ||
Notice how it takes 2^n - 1 number of moves to complete. | ||
To see this for yourself, run the following from the commandline: | ||
python 3_4_hanoi.py | wc -l | ||
""" | ||
|
||
|
||
def hanoi(n, a='A', b='B', c='C'): | ||
"""Move n discs from a to c using b as middle.""" | ||
if n == 0: | ||
return | ||
hanoi(n-1, a, c, b) | ||
print a, '->', c | ||
hanoi(n-1, b, a, c) | ||
|
||
|
||
def main(): | ||
hanoi(4) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Cracking the Coding Interview - Python | ||
====================================== | ||
|
||
I've been reading the book [Cracking the Coding | ||
Interview](http://amzn.to/crack_code), and decided to write the answers in | ||
Python (all of the book's solutions are in Java). |