Skip to content

Commit

Permalink
some tidying-up
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudblois committed Jan 21, 2018
1 parent a7a2fed commit d52b65f
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 69 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ To get more info about coverage and such::

pytest --cov-report term-missing --cov=. --verbose

Continuous Integration testing is done by Travis

.. image:: https://travis-ci.org/arnaudblois/pythonic_interviews.svg?branch=master
:alt: build-status


Contributing
Expand Down
37 changes: 10 additions & 27 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
alabaster==0.7.9
argh==0.26.2
Babel==2.3.4
coverage==4.3.4
docutils==0.13.1
imagesize==0.7.1
Jinja2==2.9.4
livereload==2.5.1
MarkupSafe==0.23
numpy==1.12.0
pathtools==0.1.2
port-for==0.3.1
py==1.4.32
Pygments==2.2.0
pytest==3.0.6
pytest-cov==2.4.0
pytz==2016.10
PyYAML==3.12
requests==2.12.5
six==1.10.0
snowballstemmer==1.2.1
Sphinx==1.5.2
sphinx-autobuild==0.6.0
sphinx-rtd-theme==0.1.9
sphinxcontrib-disqus==1.1.0
tornado==4.4.2
watchdog==0.8.3
coverage
numpy
flake8
pytest
pytest-cov
requests
setuptools
Sphinx
sphinxcontrib-disqus

Empty file added src/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion src/challenges/CtCI/arrays/P1_is_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
determines if all characters in a string are unique
"""

import structures

from collections import Counter


Expand Down
1 change: 0 additions & 1 deletion src/challenges/CtCI/arrays/P5_one_away.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

def is_one_letter_away(string1, string2):
"""
We use the difference of counters, making sure there is at most one letter
with a count > 0
"""
Expand Down
2 changes: 1 addition & 1 deletion src/challenges/CtCI/arrays/P6_string_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
>>> compress(qqqwwwwweerty)
q3w5e2t1y1
Trivia: oddly enough there a word in English with three consecutive
Trivia: oddly enough, there are English words with three consecutive
identical letters but none for which this compression would yield a shorted
result (closest to succeed is 'aaaaba', some sort of bee)
(https://en.wikipedia.org/wiki/List_of_words_in_English_with_tripled_letters)_
Expand Down
15 changes: 7 additions & 8 deletions src/challenges/CtCI/arrays/P7_matrix_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""

from math import ceil
from pprint import pprint


def rotate_90(matrix):
Expand All @@ -20,11 +19,11 @@ def rotate_90(matrix):
"""
N = len(matrix)
for i in range(ceil(N/2)):
lim = N - i - 1
for step in range(N - 2 * i - 1):
current_value = matrix[i][i + step]
matrix[i][i + step] = matrix[i + step][lim]
matrix[i + step][lim] = matrix[lim][lim - step]
matrix[lim][lim - step] = matrix[lim - step][i]
matrix[lim - step][i] = current_value
lim = N - i - 1
for step in range(N - 2 * i - 1):
current_value = matrix[i][i + step]
matrix[i][i + step] = matrix[i + step][lim]
matrix[i + step][lim] = matrix[lim][lim - step]
matrix[lim][lim - step] = matrix[lim - step][i]
matrix[lim - step][i] = current_value
return matrix
7 changes: 3 additions & 4 deletions src/challenges/CtCI/arrays/P8_zero_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def zero_matrix_no_numpy(matrix):
"""
First solution relying only on pure Python
Scans through the matrix once, storing in sets the indices where zeroes
have been detected (sets automatically removes duplicates).
have been detected (sets automatically remove duplicates).
A second loop then zeroes the rows and cols as appropriate.
Algo is 0(M x N) which is the best possible since all elements have to be
Algo is O(M x N) which is the best possible since all elements have to be
touched in the general case.
"""
if matrix == []:
Expand All @@ -25,7 +25,6 @@ def zero_matrix_no_numpy(matrix):
zero_col_indices = set()
zero_row_indices = set()

M = len(matrix)
N = len(matrix[0])

# We scan the matrix to locate all zeros
Expand Down Expand Up @@ -55,7 +54,7 @@ def zero_matrix_numpy(matrix):
a = np.where(matrix == 0)
zero_row_indices = list(set(a[0]))
zero_col_indices = list(set(a[1]))
#We then set the rows and cols to zero
# We then set the rows and cols to zero
matrix[::, zero_col_indices] = 0
matrix[zero_row_indices, ::] = 0
return matrix.tolist()
4 changes: 2 additions & 2 deletions src/challenges/CtCI/arrays/P9_string_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def is_rotated_string_naive(s1, s2):
s1, s2 = s1.lower(), s2.lower()
length = len(s1)
if s1 == '' and s2 == '':
return True # Convention: '' in '' evaluates to True
return True # Convention: '' in '' evaluates to True
if not len(s2) == length:
return False
is_rotated = False
Expand All @@ -33,7 +33,7 @@ def is_rotated_string(s1, s2):
"""
The pythonic solution is found by considering the clue: use "in" only once.
Assuming the string is rotated, it can be thought as being split in this way
s1 = part1 + part2 and s2 = part2 + part1. We can notice that s2 + s2 =
s1 = part1 + part2 and s2 = part2 + part1. We can notice that s2 + s1 =
part2 + part1 + part2 + part1 = part2 + s1 + part1
-> s1 in s2 + s2 is the answer we need, this comparison is done is O(n)
"""
Expand Down
2 changes: 1 addition & 1 deletion src/challenges/misc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""
Challenges found on miscellaneous places
Challenges found in miscellaneous places
"""
2 changes: 1 addition & 1 deletion src/classic_algo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Package containing examples of famous algorithms on a selection of subject:
Package containing examples of famous algorithms on a selection of subjects:
* dynamic programming (knapsack problem, change-making, etc)
* primality
* sorting algorithms
Expand Down
11 changes: 5 additions & 6 deletions src/classic_algo/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
"""


from utils.decorators import Memoize
from src.utils.decorators import Memoize
import math
import pdb


def max_subarray(array):
"""
finds the contiguous subarray of an array which as the largest sum.
finds the contiguous subarray of an array which has the largest sum.
>>> max_subarray([3, -2, 1, 5, 1, -2, 2, 4])
12
>>> max_subarray([3, -2, 1, 5, 1, -30, 2, 4])
Expand Down Expand Up @@ -71,9 +70,9 @@ def solve_knapsack(items, max_weight):
raise NotImplementedError


#------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Change Making algorithms
#------------------------------------------------------------------------------
# ------------------------------------------------------------------------------


def change_making_greedy(target, coin_set):
Expand Down Expand Up @@ -145,7 +144,7 @@ def change_making_bottom_up(target, coin_set):
# (this may not be the case for some sets not containing 1)
if minimum_coin:
best_combo[change] = (best_combo[change - minimum_coin] +
[minimum_coin])
[minimum_coin])
coin_count[change] = coin_count[change - minimum_coin] + 1

return best_combo[target]
Expand Down
2 changes: 2 additions & 0 deletions src/classic_algo/primality.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
** Section under construction **
"""

from math import ceil


def sieve_of_erathosthene(n):
"""
Expand Down
30 changes: 14 additions & 16 deletions src/classic_algo/sort.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Module containing the most common sorting algorithms
- bubble sort
Expand All @@ -10,10 +11,10 @@
- merge sort
- heapsort
For the record, Python sorted uses by default timsort which is a hybrid stable
sorting algorithm, running in O(n) for best case (list already sorted) to
O(n log n). It does so by taking advantage of the fact that real-life lists
often have some partial ordering.
For the record, Python built-in `sorted` uses by default timsort which
is a hybrid stable sorting algorithm, running in O(n) for best case
(list already sorted) to O(n log n). It does so by taking advantage of
the fact that real-life lists often have some partial ordering.
"""


Expand All @@ -24,8 +25,8 @@ def bubble_sort(a):
n-i. The biggest element 'bubbles up' to the n-i position. This runs in
O(n^2): n-1 passes of O(n) comparisons.
Note the use of the pythonic swap operation "a, b = b, a", not requiring the
use of a temporary storage variable.
Note the use of the pythonic swap operation "a, b = b, a", not requiring
the use of a temporary storage variable.
"""
length = len(a)
for pass_number in range(length):
Expand All @@ -37,8 +38,8 @@ def bubble_sort(a):

def short_bubble_sort(a):
"""
Variant of the short bubble, taking advantage of the fact we know that if no
value has been swaped, the list is sorted and we can return early.
Variant of the short bubble, taking advantage of the fact we know that if
no value has been swaped, the list is sorted and we can return early.
"""
length = len(a)
for pass_number in range(length):
Expand All @@ -55,7 +56,7 @@ def short_bubble_sort(a):
def selection_sort(a):
"""
Swapping values can be an expensive operation. At the i-th pass, the
selection sort finds the largest values and swap it with the value at n - i,
selection sort finds the largest values and swap it with the value at n - i
performing faster than bubble sort. Note this can be shortened same as
above (not shown here for clarity)
"""
Expand Down Expand Up @@ -90,14 +91,12 @@ def insertion_sort(a):
return a



def shell_sort(list_to_order):
"""
"""
raise NotImplementedError



def merge(a, b):
"""
helper function used by merge_sort
Expand Down Expand Up @@ -150,16 +149,15 @@ def quick_sort(a):
return a



def quicksort_helper(a, first, last):
"""
Helper function splitting the list at the pivot and recursively calling
itself on the left and right parts of this splitpoint.
"""
if first < last:
split_point = partition_helper(a, first, last)
quicksort_helper(a, first, split_point - 1)
quicksort_helper(a, split_point + 1, last)
split_point = partition_helper(a, first, last)
quicksort_helper(a, first, split_point - 1)
quicksort_helper(a, split_point + 1, last)


def partition_helper(a, first, last):
Expand Down Expand Up @@ -188,7 +186,7 @@ def partition_helper(a, first, last):
if right_mark < left_mark:
done = True
else:
a[left_mark], a[right_mark] = a[right_mark], a[left_mark]
a[left_mark], a[right_mark] = a[right_mark], a[left_mark]

a[first], a[right_mark] = a[right_mark], a[first]
return right_mark
Expand Down
2 changes: 1 addition & 1 deletion src/structures/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Node:
"""
Nodes contain a key and may carry some extra data called payload
"""
def __init__(key, payload=None):
def __init__(self, key, payload=None):
self.key = key
self.payload = payload

Expand Down

0 comments on commit d52b65f

Please sign in to comment.