Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Longest Palindromic Substring Tests #2275

Merged
merged 8 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .glotter.yml
Original file line number Diff line number Diff line change
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
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]))
}
}
14 changes: 7 additions & 7 deletions archive/j/javascript/lps.js
Original file line number Diff line number Diff line change
@@ -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
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)
1 change: 1 addition & 0 deletions runner.py
Original file line number Diff line number Diff line change
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
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