-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Longest Palindromic Substring Tests (#2275)
* test: lps * fix: remove duplicated test case * refactor: to match updated outputs see 9801987026e4e35aba0b96442b5d9517dc066b5e * refactor(lps): change programs for updated tests see 7dd9eff9a6c1edfb66bd1091c693a05d2f885eb8 * fix: pass multi word args with quotes * Rename longest_palindrome.go to lps.go * fix: remove debugging Co-authored-by: Jeremy Grifski <jeremy.grifski@gmail.com>
- Loading branch information
Showing
8 changed files
with
180 additions
and
97 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
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const errorMessage = "Usage: please provide a string that contains at least one palindrome" | ||
|
||
func reverse(s string) string { | ||
result := "" | ||
for _, v := range s { | ||
result = string(v) + result | ||
} | ||
return result | ||
} | ||
|
||
func longestPalSubstr(str string) string { | ||
result := "" | ||
|
||
if len(str) < 2 || str == "" { | ||
return errorMessage | ||
} | ||
|
||
for i := 1; i < len(str); i++ { | ||
for j := 0; j < len(str)-i; j++ { | ||
possiblePal := strings.ToLower(str[j : j+i+1]) | ||
|
||
if possiblePal == reverse(possiblePal) && len(possiblePal) > len(result) { | ||
result = possiblePal | ||
} | ||
|
||
} | ||
} | ||
|
||
if len(result) == 0 { | ||
result = errorMessage | ||
} | ||
return result | ||
} | ||
|
||
func printSubStr(str string, low, high int) string { | ||
s := "" | ||
for i := low; i <= high; i++ { | ||
s = s + string(str[i]) | ||
} | ||
|
||
return s | ||
} | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
fmt.Println(errorMessage) | ||
} else { | ||
fmt.Println(longestPalSubstr(os.Args[1])) | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,44 @@ | ||
import sys | ||
|
||
errorMessage = "Usage: please provide a string that contains at least one palindrome" | ||
|
||
def longestPalindrome(string): | ||
longest = "" | ||
string = string.lower() | ||
|
||
centres = [len(string) - 1] | ||
for diff in range(1, len(string)): | ||
centres.append(centres[0] + diff) | ||
centres.append(centres[0] - diff) | ||
|
||
for centre in centres: | ||
if (min(centre + 1, 2 * len(string) - 1 - centre) <= len(longest)): | ||
break | ||
if centre % 2 == 0: | ||
left, right = (centre // 2) - 1, (centre // 2) + 1 | ||
else: | ||
left, right = centre // 2, (centre // 2) + 1 | ||
|
||
while left >= 0 and right < len( | ||
string) and string[left] == string[right]: | ||
left -= 1 | ||
right += 1 | ||
|
||
if right - left > len(longest): | ||
longest = string[left + 1:right] | ||
|
||
return longest | ||
|
||
|
||
if __name__ == '__main__': | ||
if (len(sys.argv) < 2): | ||
print(errorMessage) | ||
else: | ||
string = sys.argv[1] | ||
if string == "" or string == None: | ||
print(errorMessage) | ||
sub = longestPalindrome(string) | ||
if len(sub) == 1: | ||
print(errorMessage) | ||
else: | ||
print(sub) |
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
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,64 @@ | ||
import pytest | ||
|
||
from runner import ProjectType | ||
from glotter import project_test, project_fixture | ||
from test.utilities import clean_list | ||
|
||
invalid_permutations = ( | ||
'description,in_params,expected', [ | ||
( | ||
'no input', | ||
None, | ||
'Usage: please provide a string that contains at least one palindrome' | ||
), ( | ||
'empty input', | ||
"", | ||
'Usage: please provide a string that contains at least one palindrome' | ||
), ( | ||
'invalid input: no palindromes', | ||
"polip", | ||
'Usage: please provide a string that contains at least one palindrome' | ||
) | ||
] | ||
) | ||
|
||
valid_permutations = ( | ||
'description,in_params,expected', [ | ||
( | ||
'sample input: one palindrome', | ||
'racecar', | ||
'racecar' | ||
), ( | ||
'sample input: two palindrome', | ||
'"kayak mom"', | ||
'kayak' | ||
), ( | ||
'sample input: complex palindrome', | ||
'"step on no pets"', | ||
'step on no pets' | ||
), | ||
] | ||
) | ||
|
||
|
||
@project_fixture(ProjectType.LPS.key) | ||
def lps(request): | ||
request.param.build() | ||
yield request.param | ||
request.param.cleanup() | ||
|
||
|
||
@project_test(ProjectType.LPS.key) | ||
@pytest.mark.parametrize(valid_permutations[0], valid_permutations[1], | ||
ids=[p[0] for p in valid_permutations[1]]) | ||
def test_lps_valid(description, in_params, expected, lps): | ||
actual = lps.run(params=in_params) | ||
assert actual.strip().lower() == expected | ||
|
||
|
||
@project_test(ProjectType.LPS.key) | ||
@pytest.mark.parametrize(invalid_permutations[0], invalid_permutations[1], | ||
ids=[p[0] for p in invalid_permutations[1]]) | ||
def test_lps_invalid(description, in_params, expected, lps): | ||
actual = lps.run(params=in_params) | ||
assert actual.strip() == expected |