Re: [Haskell-beginners] GCJ 2015 - Round 1A Problem - Haircut

Can someone tell me how I could have avoided or fixed this?
The trouble manifested as stack overflow on solving
https://code.google.com/codejam/contest/4224486/dashboard#s=p1
For efficiency, you'll probably need more cleverness in math than in Haskell. You can predict an approximate start time for the Nth customer's service from the average per-customer service time M, where 1/M = sum 1/M_k. Starting from that estimate, one can skip over almost the entire service simulation. Doug McIlroy

Thanks Sumit, I changed my folds to a strict foldl'. I'll check how long it
runs now.
Doug, I think you are absolutely correct. Taking the harmonic mean probably
factored into the solution, in order to make it algorithmically feasible
for the large input! Lesson learnt, next time I'll probably think harder
about the problem than the code ;)
On Sat, Apr 18, 2015 at 7:29 AM, Doug McIlroy
Can someone tell me how I could have avoided or fixed this?
The trouble manifested as stack overflow on solving
https://code.google.com/codejam/contest/4224486/dashboard#s=p1
For efficiency, you'll probably need more cleverness in math than in Haskell. You can predict an approximate start time for the Nth customer's service from the average per-customer service time M, where 1/M = sum 1/M_k. Starting from that estimate, one can skip over almost the entire service simulation.
Doug McIlroy

The link doesn't work because the hyperlink doesn't include the apostrophe
at the end.
Also, I'm sorry for having written off foldr as being worthless, I didn't
think much about it when I replied.
Foldl is the bad one here, whereas foldr and foldl' are both good and
recommended options.
Foldr can cause stack overflow as it also creates unevaluated thunks, but
then you can use foldl' to get over that.
How did it go with strict foldl?
On 18 April 2015 at 21:04, Sourabh
Thanks Sumit, I changed my folds to a strict foldl'. I'll check how long it runs now.
Doug, I think you are absolutely correct. Taking the harmonic mean probably factored into the solution, in order to make it algorithmically feasible for the large input! Lesson learnt, next time I'll probably think harder about the problem than the code ;)
On Sat, Apr 18, 2015 at 7:29 AM, Doug McIlroy
wrote: Can someone tell me how I could have avoided or fixed this?
The trouble manifested as stack overflow on solving
https://code.google.com/codejam/contest/4224486/dashboard#s=p1
For efficiency, you'll probably need more cleverness in math than in Haskell. You can predict an approximate start time for the Nth customer's service from the average per-customer service time M, where 1/M = sum 1/M_k. Starting from that estimate, one can skip over almost the entire service simulation.
Doug McIlroy
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards Sumit Sahrawat

Strict foldl' solved the stack overflow issue, thanks for catching this Sumit. However it is still taking way too long to compute, which means this wasn't the intended solution for the problem. I'll need to compute the average service time w/ the harmonic mean as Doug suggested and then massage the code some more to get exactly what is asked.
participants (3)
-
Doug McIlroy
-
Sourabh
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)