-
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
3 changed files
with
100 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,23 @@ | ||
# Description | ||
3 Things can be done to a sting in this problem: | ||
1. insert character | ||
2. remove character | ||
3. replace character | ||
Given 2 strings, write a function that checks if they are one edit or zero edits away | ||
|
||
# Test Cases | ||
pale, pal (true) - remove one | ||
pales, pale (true) - add one | ||
pale, bale (true) - change one | ||
pale, bake (false) - two steps away | ||
|
||
# Thought Process | ||
1. Check for all 3 cases | ||
2. Check for amount of instances of each case | ||
3. Consider edge case - only having one character | ||
4. Does uppercase count? - no | ||
5. Do spaces count? - no | ||
6. Can we check for both insert and remove at the same time? - probably yes | ||
7. Can you check for all 3 at the same time? - probably yes | ||
8. Do a separate check for 0 edits away | ||
9. Thought of this case later: removing a char from the beginning of string |
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,73 @@ | ||
import time | ||
import os.path | ||
|
||
# timer for speed testing of program logic | ||
def run_timer(): | ||
tests = 30 | ||
start_time = time.time() # start | ||
for i in xrange(tests): | ||
run_tests() | ||
end_time = time.time() | ||
total_time = (end_time - start_time) / tests # end | ||
print("Runtime: %.10f seconds" % total_time) | ||
# save results | ||
file_name = "speed.csv" | ||
if os.path.isfile(file_name) is False: | ||
with open(file_name, "a") as results: | ||
results.write("time,tests\n") | ||
with open(file_name, "a") as results: | ||
results.write("%.10f" % total_time) | ||
results.write(",%s\n" % tests) | ||
|
||
# program logic | ||
def base(old,new): | ||
if old == new: | ||
return 'true' | ||
|
||
found = 'false' | ||
len1 = len(old) | ||
len2 = len(new) | ||
diff = len1 - len2 | ||
first = 'false' | ||
second = 'false' | ||
|
||
# check lengths | ||
if abs( diff ) > 1: # break out early if leg diff is 2 or more | ||
return 'false' | ||
elif len1 > len2: # first string is longer | ||
first = 'true' | ||
elif len1 < len2: # second string is longer | ||
second = 'true' | ||
|
||
i1 = 0 | ||
i2 = 0 | ||
while i1 < len1 and i2 < len2: | ||
if old[i1] != new[i2]: # could be spacing or char difference | ||
if found == 'true': | ||
return 'false' | ||
found = 'true' | ||
|
||
# move longer string up by 1 | ||
if first == 'true': # first is longer | ||
i1 += 1 | ||
pass | ||
elif second == 'true': # second is longer | ||
i2 += 1 | ||
pass | ||
|
||
i1 += 1 | ||
i2 += 1 | ||
|
||
return 'true' | ||
|
||
# test cases | ||
def run_tests(): | ||
print base('pale','pale') # true - 0 edits away | ||
print base('pale','pal') # true - add one | ||
print base('pales','pale') # true - remove one | ||
print base('pale','bale') # true - change one | ||
print base('pale','bake') # false - two steps away | ||
print base('bbake','bake') # true - remove one | ||
|
||
# run_tests() | ||
run_timer() |
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,4 @@ | ||
time,tests | ||
0.0000138998,30 | ||
0.0000143369,30 | ||
0.0000136296,30 |