DEV Community

tommy-3
tommy-3

Posted on • Updated on

Learning Algorithms with JS, Python and Java 1: String Reversal

When I study natural languages, I've always enjoyed comparing them. It was intriguing to know, for example, that the English and German prefix "un-", the Latin prefix "in-" and the Greek prefix "แผ€-" are all thought to stem from the same Proto-Indo-European prefix "*nฬฅ-". As a (semi-)beginner of programming, I thought it might be also fun to try to learn them in a comparative manner.

I'll follow Stephen Grider's Udemy course The Coding Interview Bootcamp: Algorithms + Data Structures, which is completely in JavaScript, and solve the same question in JavaScript, Python and Java, which are just three languages I happen to know the basics of.

The first task in the course is to reverse the given string. Stephen introduces three JavaScript answers.

1. Simple Solution

JavaScript:

function reverse(str) {
    return str.split('').reverse().join('');
}
Enter fullscreen mode Exit fullscreen mode

Its translation into Python would be:

def reverse(str):
    chars = [char for char in str]
    chars.reverse()
    return ''.join(chars)
Enter fullscreen mode Exit fullscreen mode

In idiomatic Python:

def reverse(str):
    return str[::-1]
Enter fullscreen mode Exit fullscreen mode

The Java equivalent of the first JS code would be:

import org.apache.commons.lang3.ArrayUtils;

static String reverse(String str) {
    char[] chars = str.toCharArray();
    ArrayUtils.reverse(chars);
    return String.valueOf(chars);
}
Enter fullscreen mode Exit fullscreen mode

The standard Java way to achieve it seems to be:

static String reverse(String str) {
    return new StringBuilder(str).reverse().toString();
}
Enter fullscreen mode Exit fullscreen mode

2. Using a For Loop

JavaScript:

function reverse(str) {
    let reversed = '';

    for (let char of str) {
        reversed = char + reversed;
    }

    return reversed;
}
Enter fullscreen mode Exit fullscreen mode

Python:

def reverse(str):
    reversed = ''

    for char in str:
        reversed = char + reversed

    return reversed
Enter fullscreen mode Exit fullscreen mode

Java:

static String reverse(String str) {
    StringBuilder reversed = new StringBuilder();

    for (char character : str.toCharArray()) {
        reversed.insert(0, character);
    }

    return reversed.toString();
}
Enter fullscreen mode Exit fullscreen mode

which is actually very slow when a long string is given, because it searches for the 0th position every time.

This one works fast enough:

static String reverse(String str) {
    StringBuilder reversed = new StringBuilder();

    for (int i = str.length() - 1; i >= 0; i--) {
        reversed.append(str.charAt(i));
    }

    return reversed.toString();
}
Enter fullscreen mode Exit fullscreen mode

3. Using the Reduce Method

This is not very practical or efficient, but just to explore what the languages offer.

JavaScript:

function reverse(str) {
    return str.split('').reduce((reversed, char) => char + reversed, '');
}
Enter fullscreen mode Exit fullscreen mode

Python:

from functools import reduce

def reverse(str):
    return reduce(lambda reversed, char: char + reversed, str)
Enter fullscreen mode Exit fullscreen mode

Java:

static String reverse6(String str) {
    return str.chars()
            .mapToObj(c -> String.valueOf((char) c))
            .reduce("", (reversed, chr) -> chr + reversed);
}
Enter fullscreen mode Exit fullscreen mode

I hope someone would find this interesting or helpful and I'd appreciate any suggestion or comment.

Top comments (3)

Collapse
 
zerokoriel profile image
ZeroKoriel

Hi tommy-3, your post is funny. It reminded me of various tasks at the university.

Congratulations! Keep going!

Collapse
 
tommy3 profile image
tommy-3

Thanks for your comment!
I didn't study computer science at the university, so everything feels quite new to me. I'm sure I wouldn't be able to keep learning if I were alone, but the comments and the likes here really make me want to continue posting.
I hope you'll post something as well, and you'll surely come to like dev.to more!

Collapse
 
zerokoriel profile image
ZeroKoriel

Well, I'm still a student and do not feel with the necessary knowledge for a post.
Maybe I'll try later.