-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
203 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
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,31 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Name: base_conversion.py | ||
Developer: Chris Page | ||
Date: 2015-04-01 | ||
Purpose: convert a base 10 <-> base 2 vs versa | ||
""" | ||
import sys | ||
|
||
def get_binary(number): | ||
""" convert an integer to binary string """ | ||
number = int(number) | ||
binary_keys = {idx:val for idx, val in enumerate("01")} | ||
if number == 0: | ||
return binary_keys[number] | ||
binary = str() | ||
while number > 0: | ||
binary += binary_keys[number % len(binary_keys)] | ||
number /= len(binary_keys)) | ||
return binary[::-1] | ||
|
||
def get_decimal(binary): | ||
""" convert a binary string to a decimal number """ | ||
binary = str(binary) | ||
number_keys = {val:idx for idx, val in enumerate("01")} | ||
number = 0 | ||
for val in binary: | ||
if not number_keys.get(val, None) and number_keys.get(val, None) != 0: | ||
raise ValueError("Incorrect binary string") | ||
number = number * len(number_keys) + number_keys[val] | ||
return number |
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
Empty file.
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,69 @@ | ||
""" | ||
Developer: Chris Page | ||
Date: 2014-04-28 | ||
Purpose: Given a pattern and a string, find all occurances of that pattern | ||
in the string. | ||
""" | ||
|
||
import re | ||
|
||
def _check_params(pattern, unknown): | ||
""" check the parameters are valid """ | ||
try: | ||
pattern = str(pattern) | ||
unknown = str(unknown) | ||
except ValueError as error: | ||
print "Do something with error: %s" % error | ||
raise | ||
return pattern, unknown | ||
|
||
|
||
def get_match_with_re(pattern, unknown): | ||
""" return all matches of the pattern in the unknown string | ||
Args: | ||
pattern (str): pattern string | ||
unknown (str): full unknown string | ||
Returns: | ||
True/False | ||
""" | ||
pattern, unknown = _check_params(pattern, unknown) | ||
regex = re.compile(pattern) | ||
if not regex.search(unknown): | ||
return False | ||
return True | ||
|
||
|
||
def get_match_with_string(pattern, unknown): | ||
""" return match index of the pattern in the unknown string | ||
Args: | ||
pattern (str): pattern string | ||
unknown (str): full unknown string | ||
Returns: | ||
True/False | ||
""" | ||
pattern, unknown = _check_params(pattern, unknown) | ||
if pattern not in unknown: | ||
return False | ||
return True | ||
|
||
|
||
def get_match_with_iteration(pattern, unknown): | ||
""" return first match of the pattern in the unknown string | ||
Args: | ||
pattern (str): pattern string | ||
unknown (str): full unknown string | ||
all_flag (bol): find more than one if True | ||
Returns: | ||
True/False | ||
""" | ||
pattern, unknown = _check_params(pattern, unknown) | ||
if len(pattern) == 0 or len(pattern) > len(unknown): | ||
return False | ||
if pattern == unknown: | ||
return True | ||
matches = 0 | ||
for val in xrange(len(unknown) + 1 - len(pattern)): | ||
if unknown[val:val+len(pattern)] == pattern: | ||
matches += 1 | ||
return True | ||
return False |
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,79 @@ | ||
""" | ||
Developer: Chris Page | ||
Date: 2014-08-19 | ||
Purpose: Test the string_search | ||
""" | ||
import unittest | ||
from string_search import get_match_with_iteration, get_match_with_re, \ | ||
get_match_with_string | ||
|
||
|
||
class TestStringSearch(unittest.TestCase): | ||
""" test some use cases """ | ||
|
||
def test_one_char_first_position(self): | ||
""" test one character in the first position of string """ | ||
values = ('a', 'abc') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_one_char_first_and_last_position(self): | ||
""" test one character in the first and last position of string """ | ||
values = ('a', 'abca') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_one_char_middle_position(self): | ||
""" test one character in the middle position of string """ | ||
values = ('b', 'abc') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_one_char_last_position(self): | ||
""" test one character in the last position of string """ | ||
values = ('c', 'abc') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_one_char_not_in(self): | ||
""" test one character not in string """ | ||
values = ('d', 'abc') | ||
self.assertEqual(get_match_with_iteration(*values), False) | ||
self.assertEqual(get_match_with_re(*values), False) | ||
self.assertEqual(get_match_with_string(*values), False) | ||
|
||
def test_two_char_first_position(self): | ||
""" test two character in the first position of string """ | ||
values = ('ab', 'abc') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_two_char_middle_position(self): | ||
""" test two character in the middle position of string """ | ||
values = ('bb', 'abbc') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_two_char_last_position(self): | ||
""" test one character in the last position of string """ | ||
values = ('bc', 'abbc') | ||
self.assertEqual(get_match_with_iteration(*values), True) | ||
self.assertEqual(get_match_with_re(*values), True) | ||
self.assertEqual(get_match_with_string(*values), True) | ||
|
||
def test_two_char_not_in(self): | ||
""" test two character not in string """ | ||
values = ('aa', 'abc') | ||
self.assertEqual(get_match_with_iteration(*values), False) | ||
self.assertEqual(get_match_with_re(*values), False) | ||
self.assertEqual(get_match_with_string(*values), False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |