Have you ever solved a problem using a language you are not familiar with? You did, either when you were beginner or you wanted to experiment with some language.
Consider this scenario,
We have to create a function which does the following,
- takes in two arrays of strings
- if any of them is empty, returns -1
- find the largest gap between array ones largest string and array twos smallest string.
- find the largest gap between array twos largest string and array ones smallest string.
- return the length of which ever of those two is bigger.
Pretty straightforward and simple problem. You can probably write the solution just with a quick try.
function maxDifference(a1, a2) {
if(!a1.length || !a2.length) return -1;
const list = []
for(let n1 of a1){
for(let n2 of a2){
list.push(Math.abs(n1.length-n2.length))
}
}
return Math.max(...list)
}
Pretty interesting, however to make it much more interesting, I decided to write this using nim
, a fairly new language which still did not even reach first version. What's more interesting is, I don't have almost any knowledge about it.
After some crude tries, I could write the following working code,
proc maxDifference*(s1, s2: seq[string]): int =
if (len(s1) == 0 or len(s2) == 0):
return -1
var z : seq[int] = @[]
for i, e in s1.pairs:
for j, f in s2.pairs:
z.add(abs(len(e) - len(f)))
return max(z)
The difference in run time between two was also noticeable, While nim
took some time to compile, it ran faster just because it's not interpreting it.
➜ time ./difference
13
./nimtest 0.00s user 0.00s system 79% cpu 0.001 total
➜ time node difference.js
13
node difference.js 0.04s user 0.00s system 101% cpu 0.043 total
I know I know, comparing a systems language with JavaScript is frown upon. I am just sharing the experience I had. It could be some other language.
I remember once I solved my friends C++ assignment without knowing a single piece of C++ code, or help python kids solve their coding problem without looking into python codebase.
Nim is a bit similar to python, I knew a bit of python, it's obvious I was able to write the code. Pretty impressive of me, patting my own back.
But reason why I am sharing is the how
I could write a solution without knowing the syntax much.
Here is the biggest secret,
Programming is not about a language, it's about solving a problem.
Steps I took:
- First I tried to find how to write an array.
@[]
, truly this was a pain to find out it won't work unless I wroteseq[int]
for type. - Then I tried to add some value to the array.
array.add(value)
. - I saw I could find length of array using
len
. - I checked the documentation of
for
loop with index and value. - I figured the
if
andor
is gonna be pretty straightforward.
Of course I had to search a lot and fail a lot.
The nim in action
book is good, but it's the age of videos. Right now there is not a single crash course about nim on the youtube. I had to rely on the documentation and forum, which means there are opportunities for this.
Next time I will try rust and crystal. Both are fairly new to me. I am just playing around with cool stuff and will probably give up at some point.
Do you have any such experience? How did you tackle that?
Discussion (0)