Hey, everyone. If yesterday was our rest day, today is the warm-up.
We have another great challenge from user g964 on CodeWars:
Consider a sequ...
For further actions, you may consider blocking this person and/or reporting abuse
For n = 100,000 , on an ok laptop, it runs in roughly 45 seconds
Here's my solution in Perl. I actually think its a bit too slow, even when using
state
variables to cache the results in subsequent calls. Maybe I'll look into optimizing it later.I'll write an explainer for it when I get a few minutes later.
Here is my Rust solution! I think my answer is pretty good except I should be using a better data structure than a
Vec
here, since I really want to push/pop from both ends. If I did that I would be able to avoid needing to sort and reverse my vec, which would help on the processing speed!Take a look at std::collections::VecDeque :)
Thank you! I knew there was one that fit the bill better!
Nim
Time:
./main 0.04s user 0.01s system 39% cpu 0.113 total
Not familiar with nim. It looks like result is a list, and that duplicate values are being added to it. Is that not the case?
I think you're right! Was testing with too small of an input-set to see any duplicates. :)
Cheap fix (./main 341.16s user 0.52s system 99% cpu 5:42.40 total):
JavaScript
I've been running late lately... but here is a live demo on CodePen
This one times out on bigger input because of generating
Object.keys(series)
every time inside the loopI like em, but I think it's fun to have a good mix of different types of challenges!
One suggestion is to provide more examples/test cases. Especially for the mathy ones, where the correct answers might not be super obvious to everyone, having many examples/test cases can definitely make it easier! Just a thought!
Not sure I have well understood this one ... Am I correct with this JS function ? 🙈
EDIT: added the line
u = Array.from(new Set(u))
to get unique values !Not too good with maths, hope I understood well. Here is a (very inefficient, so I added a constraint to the max num to give) attempt in bash:
Haskell, featuring tail recursion!
A little late to the party (3am my time), but oh well! Here's my submission:
Full code w/ some tests: gist.github.com/kerrishotts/029c8f...
This is one where it would have been super helpful to have some answers to compare against. Beyond the initial few digits, I'm just assuming things are correct, which may not be true.
Python
Nice challenge.
This fails for larger values of n. For n = 100, for instance, it returns 463 rather than 447.