DEV Community

DouglasParanhos
DouglasParanhos

Posted on

Reports of a problem #1

This is my first post ever in dev.to and I always wanted to report the struggling about solving challenging problems, that kind of problems that just test your logic to do stuff, and I also wanted to kick off the moss from my Python abilities.

To achieve that I'm starting this new thread "Reports of a problem", which I'll report all steps until i get the final solution of a specific problem.

So, if anyone has different solutions of the problems, tips of how can I make it better, please let me know and we discuss about it.

I'll be solving problems from Beecrowd. It has lots of problems divided by categories and difficulties. Excellent site for practicing, BTW.

So, the first problem, as I'm returning to python now is a not very difficult, random chosen one: To Carry or not to Carry

Basically, there's a story telling that a student created a 32-bit adder, but the carry-on bit was broken, so the sum 4 + 6 = 2.

And, for that, he converted both numbers to 32-bit and, as the carry-on bit was broken, the final 32-bit result was 2.

The idea is to implement this broken adder.

So, my idea was to reproduce exactly what he did, convert both numbers to 32-bit and do the sum but, to do that, there's a lot of casting and looping, etc, as you can see here.

This solution worked, but the problem set the time limit as 3s, and this solution took more than that to run, due these loops and converting.

Then, I decided to think the whole problem again and realised the obvious, all that story told was to hide that all the student "created" was a Xor. Finally, a much more simple solution was created, and worked successfully.

It's always, always important to think straight about the problem even before considering start coding, it will spare you a lot of pain.

Top comments (0)