Learning Algorithms with JS, Python and Java 1: String Reversal
Tags: algorithms, beginners, learning, java
Published: 2018-07-31 00:47:33

---
title: Learning Algorithms with JS, Python and Java 1: String Reversal
published: true
description: An attempt to follow Stephen Grider's Udemy algorithm course in three different languages
tags: algorithms, beginners, learning, java
---

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̥-"](https://en.wiktionary.org/wiki/Reconstruction:Proto-Indo-European/n%CC%A5-). 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](https://www.udemy.com/coding-interview-bootcamp-algorithms-and-data-structure/), 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('');
}
```

Its translation into Python would be:
```
def reverse(str):
    chars = [char for char in str]
    chars.reverse()
    return ''.join(chars)
```

In idiomatic Python:
```
def reverse(str):
    return str[::-1]
```

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);
}
```

The standard Java way to achieve it seems to be:
```
static String reverse(String str) {
	return new StringBuilder(str).reverse().toString();
}
```

##2. Using a For Loop
JavaScript:
```
function reverse(str) {
    let reversed = '';
    
    for (let char of str) {
        reversed = char + reversed;
    }
    
    return reversed;
}
```

Python:
```
def reverse(str):
    reversed = ''

    for char in str:
        reversed = char + reversed

    return reversed
```

Java:
```
static String reverse(String str) {
	StringBuilder reversed = new StringBuilder();
	
	for (char character : str.toCharArray()) {
		reversed.insert(0, character);
	}
	
	return reversed.toString();
}
```
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();
}
```

#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, '');
}
```

Python:
```
from functools import reduce

def reverse(str):
    return reduce(lambda reversed, char: char + reversed, str)
```

Java:
```
static String reverse6(String str) {
	return str.chars()
			.mapToObj(c -> String.valueOf((char) c))
			.reduce("", (reversed, chr) -> chr + reversed);
}
```


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