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('');
}
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.
Top comments (3)
Hi tommy-3, your post is funny. It reminded me of various tasks at the university.
Congratulations! Keep going!
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!
Well, I'm still a student and do not feel with the necessary knowledge for a post.
Maybe I'll try later.