The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once ...
For further actions, you may consider blocking this person and/or reporting abuse
Javascript solution:
Neat!
What is this syntax at the end?
join``
I always use
.join('').
That is Tagged template literals, to save 2 character :D
I forgot all about that syntax. I've probably only used it a few times.
My Haskell solution:
Go
Python solution 🐍
Here is the simple approach with nested for loops with PHP:
In Rust – playground link (with tests)
Why not use std::str::matches to iterate over the same chars?
I suppose that is a valid alternative. Just not the first thing that came to mind ...
I just love how Rust has a helpful method for about everything in its std/core libs.
A simple (and somewhat slow) JS solution:
Not that slow :) Better than anything that searched/scanned or did an indexOf for sure! You know I like that || 1 as well, I always do (acc[val] || 0) + 1 - but that is much neater. Borrowing that.
Same to me
|| 0
Python:
TypeScript:
Here is a C++ solution,
Rust 🦀🦀🦀🦀solution:🦀🦀🦀🦀🦀 🦀
A magical Python solution:
A boring Python solution:
Boring is good. I usually avoid oneliners and magical solutions.
A more "production ready" version of the above:
I'm a little late, but rather than solving the problem I instead took the different approaches people posted, wrote them in D (dlang.org) and did some basic performance ploting.
JesseKPhillips / devto-challenge259-dupencoder
An attempt to capture performance with different implementation approaches for a Challenge
Dev.to has a daily challenge and I happened upon the Duplicate Encoder #259 for 2020
I didn't really want to solve the challenge per se, so instead I took the top comments for implementation and wrote them in D Lang.
For clarity these are all implemented in D and do not reflect the language performance that the implementation is based on.
The "Haskel" implementation utilizes a range based map/filter approach to detecting duplicates.
The "PHP" implementation utilizes a nested loop apporach to intentify that the character occurs again.
The "Pointer" implementation was just something I wanted to try. It duplicates the array so it is mutable after which point it does not loop over the arry twice and instead stores pointers to the location of the same character. It takes quite a bit for this approach to see any performance gain.
I might do more work on this and write up my own post about what I did. Keep in mind that I use the terms 'php' and 'haskel' not to indicate the performance of those languages... it was just the language utilized for the approach taken. All timings are from D.
Erlang solution, though not sure this would be the most efficient. I am still learning the language :)
Clojure
🐍
Here is a simple python code: