Skip to content

Commit

Permalink
Added Longest Palindromic Substring Tests (#2275)
Browse files Browse the repository at this point in the history
* 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
izexi and jrg94 committed Oct 26, 2020
1 parent 706cf4f commit 0b7c467
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 97 deletions.
6 changes: 6 additions & 0 deletions .glotter.yml
Expand Up @@ -72,6 +72,12 @@ projects:
acronyms:
- "lcs"
requires_parameters: true
lps:
words:
- "lps"
acronyms:
- "lps"
requires_parameters: true
linearsearch:
words:
- "linear"
Expand Down
55 changes: 0 additions & 55 deletions archive/g/go/longest_palindrome.go

This file was deleted.

58 changes: 58 additions & 0 deletions archive/g/go/lps.go
@@ -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]))
}
}
14 changes: 7 additions & 7 deletions archive/j/javascript/lps.js
@@ -1,11 +1,14 @@
const [, , input] = process.argv;

const getLongestPalindromic = (string) => {
if (!string) return;

let longestPal = '';

for (let i = 1; i < string.length; i++) {
for (let j = 0; j < string.length - i; j++) {
let possiblePal = string.substring(j, j + i + 1);
let possiblePal = string.substring(j, j + i + 1).toLowerCase();

if (
possiblePal === [...possiblePal].reverse().join('') &&
possiblePal.length > longestPal.length
Expand All @@ -14,13 +17,10 @@ const getLongestPalindromic = (string) => {
}
}

return longestPal
? `Longest Palindromic Substring is: ${longestPal}`
: 'No Palindromic substring present.';
return longestPal;
};

console.log(
input
? getLongestPalindromic(input)
: 'Incorrect input provided. Program Terminated'
getLongestPalindromic(input) ||
'Usage: please provide a string that contains at least one palindrome'
);
35 changes: 0 additions & 35 deletions archive/p/python/longest_palindrome_substring.py

This file was deleted.

44 changes: 44 additions & 0 deletions archive/p/python/lps.py
@@ -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)
1 change: 1 addition & 0 deletions runner.py
Expand Up @@ -17,6 +17,7 @@ class ProjectType(Enum):
InsertionSort = auto()
JobSequencing = auto()
LCS = auto()
LPS = auto()
LinearSearch = auto()
MergeSort = auto()
MST = auto()
Expand Down
64 changes: 64 additions & 0 deletions test/projects/test_lps.py
@@ -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

0 comments on commit 0b7c467

Please sign in to comment.