diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..c43b14a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,15 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml + +out/ +.idea/ +.idea_modules/ +*.iml +*.ipr +*.iws \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..a6d1bc1 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..03f405d --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..067963c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ae8bede --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problems/pom.xml b/problems/pom.xml new file mode 100644 index 0000000..3ce6efa --- /dev/null +++ b/problems/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.interview + problems + 1.0 + + + 21 + 21 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.9.2 + test + + + org.assertj + assertj-core + 3.24.2 + + + + \ No newline at end of file diff --git a/problems/src/main/java/org/interview/recursive/palindrome/Palindrome.java b/problems/src/main/java/org/interview/recursive/palindrome/Palindrome.java new file mode 100644 index 0000000..c8c4560 --- /dev/null +++ b/problems/src/main/java/org/interview/recursive/palindrome/Palindrome.java @@ -0,0 +1,27 @@ +package org.interview.recursive.palindrome; + +public record Palindrome(String input) { + private static final int STARTING_INDEX = 0; + private static final int OFFSET = 1; + + public boolean check() { + if (input.isEmpty()) + return true; + return isValid(STARTING_INDEX, endingIndex()); + + } + + public boolean isValid(int startingIndex, int endIndex) { + if (startingIndex >= endIndex) + return true; + if (input.charAt(startingIndex) != input.charAt(endIndex)) + return false; + + return isValid(startingIndex + OFFSET, endIndex - OFFSET); + + } + + private int endingIndex() { + return input.length() - 1; + } +} diff --git a/problems/src/main/java/org/interview/recursive/palindrome/explain b/problems/src/main/java/org/interview/recursive/palindrome/explain new file mode 100644 index 0000000..2028e3f --- /dev/null +++ b/problems/src/main/java/org/interview/recursive/palindrome/explain @@ -0,0 +1,12 @@ +Given a string, write a recursive function that checks if the given string is a palindrome or not. + +Examples: + +Input : malayalam +Output : Yes +Reverse of malayalam is also +malayalam. + +Input : max +Output : No +Reverse of max is not max. \ No newline at end of file diff --git a/problems/src/test/java/org/interview/recursive/PalindromeShould.java b/problems/src/test/java/org/interview/recursive/PalindromeShould.java new file mode 100644 index 0000000..ffb109c --- /dev/null +++ b/problems/src/test/java/org/interview/recursive/PalindromeShould.java @@ -0,0 +1,22 @@ +package org.interview.recursive; + +import org.assertj.core.api.Assertions; +import org.interview.recursive.palindrome.Palindrome; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class PalindromeShould { + + @ParameterizedTest + @CsvSource( + {"ali , false", + "amin ,false", + "ddd , true", + "aia ,true", + "atda ,false", + "a ,true"}) + void chick_is_or_not(String input, boolean expected) { + var palindrome = new Palindrome(input); + Assertions.assertThat(palindrome.check()).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/problems/target/classes/org/interview/recursive/Palindrome.class b/problems/target/classes/org/interview/recursive/Palindrome.class new file mode 100644 index 0000000..e0b2048 Binary files /dev/null and b/problems/target/classes/org/interview/recursive/Palindrome.class differ diff --git a/problems/target/test-classes/org/interview/recursive/PalindromeShould.class b/problems/target/test-classes/org/interview/recursive/PalindromeShould.class new file mode 100644 index 0000000..24c9ffc Binary files /dev/null and b/problems/target/test-classes/org/interview/recursive/PalindromeShould.class differ