Giter VIP home page Giter VIP logo

palindrome's Introduction

Palindrome

Description

In this project I am creating and testing methods checking if a given String is a palindrom.

Definition of a Palindrome

A palindrome is a word, phrase, number, or other sequences of characters which reads the same backward as forward, such as “madam” or “racecar”.

Getting Started

In the project I have tested created methods using Junit unit-testing framework. In order to implement it first I added junit-jupiter dependency to my pom.xml file.

Dependencies

<dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
        </dependency>

Solutions of the problem

In the Palindrome class I created 3 different methods checking if a particular input is or contains one or many palindromes

In the first method I used a for loop:

public static boolean isPalindromeFor(String input) {
        if (input.isEmpty()) {
            return false;
        }
        String editedInput = input.replaceAll("\\s+", "").toLowerCase();
        for (int i = 0; i < editedInput.length() / 2; i++) {
            if (editedInput.charAt(i) != editedInput.charAt(editedInput.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }

In the second method I used a while loop:

 public static boolean isPalindromeWhile(String input) {
        if (input.isEmpty()) {
            return false;
        }

        String editedInput = input.replaceAll("\\s+", "").toLowerCase();
        int left = 0;
        int right = editedInput.length() - 1;

        while (left < right) {
            if (editedInput.charAt(left) != editedInput.charAt(right))
                return false;
            left++;
            right--;
        }
        return true;
    }

The last method takes as an input an Array of Strings and return a List of palindromes present in that array:

 public static List<String> isPalindromeArray(String[] input) {
        if (input.length == 0) {
            return Collections.emptyList();
        }
        List<String> result = new ArrayList<>();
        for (String s : input) {
            s.replaceAll("\\s+", "").toLowerCase();
            if (isPalindromeFor(s)) {
                result.add(s);
            }
        }
        return result;
    }

Notice that in all of the above methods first I am checking if the input String is empty or String array length is zero accordingly:

if (input.isEmpty()) {
            return false;
        }
if (input.length == 0) {
            return Collections.emptyList();
        }

Also before checking that the particular input is a palindrome or contains a palindrome I process the input to ignore case sensitivity using .toLowerCase() method. Also in order to remove spaces between words in the strings I use a replceAll() method which is accepting a regex pattern for a sequence that needs to replced and an empty String tas a replacement:

String editedInput = input.replaceAll("\\s+", "").toLowerCase();

Testing

For each othe methods I created a test checking if the method given an input that is a pilindrome will return true or if an input is an array of palindromes the method will return a list of all palindromes in an array

I also use parameterized tests using @ParameterizedTest and @CsvSource() annotations allowing me to test mny different inputs in one testing method:

 @ParameterizedTest
    @CsvSource({"A", "a", "civic", "Level", "RaDaR","Taco cat", "Aibohphobia",
            "Was it a car or a cat I saw", "Too bad I hid a boot", "Mr Owl ate my metal worm"})
    @DisplayName("Given a palindrome input should return true")
    public void isPalindromeWhileTest_shouldReturnTrue(String input) {
        boolean result = Palindrome.isPalindromeWhile(input);
        assertTrue(result);
    }

Author

Patryk Blakala

Useful resources

baeldung.com

geeksforgeeks.org

dictionary.com

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.