Essentially zips up digit by digit in reverse order, sums and concats the result. zipRemain utility function helps retrieving leftover part of the longer of two zipped lists.
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
A quickie in Typescript
Each digit is enumerated in turn by skimming off the ones digit off each number (using num%10, then dividing the number by 10) until there are no more digits.
The accumulated sum is simply a string with each sum added to its left-hand side.
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
Benchmark here -- numeric method has it by a nose most of the time you run it, sometimes a bit more. For smaller inputs they're pretty much even.
In C or Rust or something closer to the metal Mihail's post is definitely the way to go (imo).
In JS strings are pretty quick and numbers are surprisingly slow to deal with (there's no support for integers, etc) and I find the language doesn't really reward that kind of close-knit optimization, so I felt it wasn't worth the additional complication.
I wonder what the result with asm.js would be.
There are some claims that in the process of implementing asm.js V8 in particular just optimized normal code to the speed of corresponding asm.js, but some extra assertions might still help?
defsum_(first:int,second:int)->int:first_mapped=str(first)second_mapped=str(second)# Fill the smallest list with '0' on left
iflen(first_mapped)>len(second_mapped):second_mapped=second_mapped.zfill(len(first_mapped))eliflen(second_mapped)>len(first_mapped):first_mapped=first_mapped.zfill(len(second_mapped))result=''forx,yinzip(first_mapped,second_mapped):result+=str(int(x)+int(y))returnint(result)
Technically correct, but I tended to think of the solution backward, so it does way more reversing then it should to get things back in place to finish it.
We're a place where coders share, stay up-to-date and grow their careers.
EDIT:
I quite liked purely arithmetic solutions by @willsmart and @qm3ster , so I reimplemented same idea in Haskell:
Solution in Haskell:
Essentially
zips up digit by digit in reverse order, sums and concats the result.zipRemainutility function helps retrieving leftover part of the longer of two zipped lists.Example for the zipping problem:
A quickie in Typescript
Each digit is enumerated in turn by skimming off the ones digit off each number (using
num%10, then dividing the number by 10) until there are no more digits.The accumulated sum is simply a string with each sum added to its left-hand side.
Testn
You already did the skimming, but you could also avoid stringification altogether: dev.to/qm3ster/comment/14en6
Thanks, that's a fair point. Here's the tweaked code:
Benchmark here -- numeric method has it by a nose most of the time you run it, sometimes a bit more. For smaller inputs they're pretty much even.
In C or Rust or something closer to the metal Mihail's post is definitely the way to go (imo).
In JS strings are pretty quick and numbers are surprisingly slow to deal with (there's no support for integers, etc) and I find the language doesn't really reward that kind of close-knit optimization, so I felt it wasn't worth the additional complication.
I wonder what the result with asm.js would be.
There are some claims that in the process of implementing asm.js V8 in particular just optimized normal code to the speed of corresponding asm.js, but some extra assertions might still help?
Solution in Bash and some general commands:
This reminds me of the quote: "Whatever you do, there is an asian doing it better."
Nice solution!!
Took a slightly bigger approach, but it works :)
Old school JS version, not super optimized, but should be faster than, map, join, split etc
Here is my simple solution with Python:
Rust
(without stringification)
// test
(look at it go)
Python 🐍
Not the solution i like but it does the job.
Hardcore? Yeah, I'm diggin' into that.
Ruby, because Ruby has built-in
divmod, because you need both at least as often as you need either.Technically correct, but I tended to think of the solution backward, so it does way more reversing then it should to get things back in place to finish it.