Dev.to daily challenge Duplicate Encoder

CraigDillabaugh craig.dillabaugh at gmail.com
Wed Jun 24 18:18:46 UTC 2020


On Wednesday, 24 June 2020 at 17:21:34 UTC, Jesse Phillips wrote:
> The Dev.to community does a Daily Challenge[1]. I didn't really 
> set out to solve the  problem and instead took the top 
> implementations done in different languages and implemented 
> them in D[2].
>
> I need to emphasize I reference languages but this is not 
> performance of those languages all data is from D. Also I did 
> not play too much with optimizations on the compiler (using 
> LDC) but feel free to recommend a change.
>
> I find the changes to Haskell's performance to be interesting 
> as it went from very unreasonable to competing with Go (Ranges 
> vs foreach)
>
> This was just a fun little experiment.
>
> 1. 
> https://dev.to/thepracticaldev/daily-challenge-259-duplicate-encoder-2e8l
> 2. 
> https://github.com/JesseKPhillips/devto-challenge259-dupencoder

For your pointer based implementation you might be able to finish 
with a single for loop over the array (psuedocode).

for each char ch in str:
      if this is the first time you see ch, replace it with '(' and
          store a pointer to it.
      else if this is the second time replace it with ')' and
          follow your pointer back, replacing the first occurrence 
with ')'
      else replace it with ')'

Likely results in somewhat uglier code.


More information about the Digitalmars-d mailing list