Programming Contest (DIFFICULTY: EASY) #1

Status
This thread has been locked.

/usr/bin/

Linux Warrior & Software Developer
Supreme
Feedback score
14
Posts
358
Reactions
291
Resources
0
Thread Title
Programming Contest (DIFFICULTY: EASY) #1

Prize name
$5 PayPal G&S

Requirements and other relevant information
Depending on the responses, and the activity for these type of contests I might do more in the future.
Cool way to practice your problem solving skills and make some money :)

Very simple, solve the following programming problem:

Write a method public String toPalindrome(String input) which converts the input string into a palindromic number and returns it.
A palindromic number is an integer that is the same when the digits are reversed. Fo example, 232 and 320023 are palindromic, but 324 is not a palindromic number.

Sample input and expected output your method must provide:
toPalindrome("5.") => "5."
toPalindrome("12333.") => "1233321."
toPalindrome("92837465.") => "928374656473829."
toPalindrome("1000000000000000123.") => "1000000000000000123210000000000000001."


Requirements:
You must code your solution in Java.
The first working solution will win the contest. PM me your solution and post on this thread once you have.
It's YOUR solution, if you copy paste some code off the internet or ask for help online you will be disqualified.

If no entries are made by the end-date the contest will close.
If no winning entries are made by the end-date, the contest will close with no winner and a solution will be provided.

End date
Aug 30, 2020

If award date is different from contest end date, please specify.
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Uxon

Building Oraxen, HackedServer and more!
Ultimate
Feedback score
12
Posts
2
Reactions
27
Resources
2
Do we have to find the smallest solution?
 

Erwan

Java Developer
Premium
Feedback score
10
Posts
172
Reactions
51
Resources
0
Done
 

/usr/bin/

Linux Warrior & Software Developer
Supreme
Feedback score
14
Posts
358
Reactions
291
Resources
0
Code:
public static void main(String[] args) {
    System.out.println(toPalindrome("5").equals("5"));
    System.out.println(toPalindrome("555").equals("555"));
    System.out.println(toPalindrome("15555").equals("155551"));
    System.out.println(toPalindrome("12333").equals("1233321"));
    System.out.println(toPalindrome("2462166").equals("246216612642"));
    System.out.println(toPalindrome("92837465").equals("928374656473829"));
    System.out.println(toPalindrome("1000000000000000123").equals("1000000000000000123210000000000000001"));
}

private static String toPalindrome(String input) {
    int matching = endMatching(input) - 1;
    if(matching == -1) return input;
    StringBuilder output = new StringBuilder(input);
    for(int i = output.length() - 2 - matching; i >= 0; i--) {
        output.append(input.charAt(i));
    }
    return output.toString();
}

private static int endMatching(String input) {
    char matching = '?';
    int count = 0;
    char[] chars = input.toCharArray();
    for(char c : chars) {
        if(matching != c) {
            matching = c;
            count = 1;
        } else {
            count++;
        }
    }
    return input.length() == count ? 0 : count;
}

Nice, solution. Missing small details, which should be easy to add. All strings for input and the output for this method should be terminated by a period (.). As posted above in the original post. Your solution also fails for the following input: toPalindrome("12221.") => "12221." as it's already a palindrome, your current soltUntil these fixes are added this solution is incomplete.

Also if your are interested there is a solution for this problem that uses a single for loop.

Do we have to find the smallest solution?

No just a working solution.[DOUBLEPOST=1597617694][/DOUBLEPOST]

Your solution also fails for the following toPalindrome("12221.") => "12221." It's already a palindrome.
 
Last edited:

ida64

Supreme
Feedback score
10
Posts
207
Reactions
204
Resources
0
Nice, solution. Missing small details, which should be easy to add. All strings for input and the output for this method should be terminated by a period (.). As posted above in the original post. Your solution also fails for the following input: toPalindrome("12221.") => "12221." as it's already a palindrome, your current soltUntil these fixes are added this solution is incomplete.

Also if your are interested there is a solution for this problem that uses a single for loop.



No just a working solution.[DOUBLEPOST=1597617694][/DOUBLEPOST]

Your solution also fails for the following toPalindrome("12221.") => "12221." It's already a palindrome.

Couldn't bother working on the other half, if anyone is interested here is the other half.


Code:
public void isPalindrome( String input ) {
        String reversed = "";

        for ( int i = input.length( ) - 1; i >= 0; i-- )
            reversed = reversed + input.charAt( i );
            
        return input.equals( reversed );
    }
 

Sullybash12

Get Your Python Programs and Discord Bots!
Premium
Feedback score
31
Posts
1,198
Reactions
522
Resources
0
rip for the python bois
 

/usr/bin/

Linux Warrior & Software Developer
Supreme
Feedback score
14
Posts
358
Reactions
291
Resources
0
This contest has concluded.
The first one to solve this problem was Ember Congratulations (PM me your PayPal email address).

Here is the solution that Ember came up with: https://repl.it/repls/AnimatedMistyCubase#Main.java

Other users who have also completed this programming problem Erwan and qweqweqwe (Well done to both of you).

The best solution was given by qweqweqwe it uses recursion and is clean as hell : https://repl.it/repls/MicroCalmFiles
Only missing a small detail not terminating number strings with a period (.) like description of the method in the original post does, but that's an easy fix. Well done qweqweqwe very nice solution!


Things I will do for future contests:
Review submissions only couple days prior to contest deadline. This will hopefully give more people an opportunity to partcipate as some people are quicker at problem solving than others. So if they have revisions that need to be done they won't know about it until couple days before deadline (then they have that remaining time to submit a fix). So those who do it right the first time even if they aren't a early submission can still win!

And yes, I do review submissions in the order they are received.

Awesome thanks to those who participated, do leave comments on my pofile page if you have any ideas or improvements for the future :)
 
Status
This thread has been locked.
Top