importData.Bool(bool)importData.List(intercalate)importData.List.Split(chunksOf,splitOn)importData.Maybe(isJust,fromJust)importText.Read(readMaybe)failString::StringfailString="Unlucky"-- e.g. 153 becomes [1,5,3]intoDigits::Int->[Int]intoDigits0=[]intoDigitsn=intoDigits(divn10)++[modn10]-- e.g [1,5,3] becomes 153fromDigits::[Int]->IntfromDigits=foldladdDigit0whereaddDigitnumd=10*num+d-- Primary-- Ideally, this'd have returned a Maybe String, but spec and allisCubic::Int->StringisCubicn=letcubes=map(^3)$intoDigitsninbool(failString)(shown)(lengthcubes<=3&&(sumcubes)==n)-- SecondaryshowCubes::String->StringshowCubess=letmaybeDigits=filter(isJust)$map(\s->readMaybes::MaybeInt)$wordsssplitLongerThanThrees=mapfromDigits$concat$map(chunksOf3)$mapintoDigits$mapfromJust$maybeDigitsjustCubes=filter(/=failString)$mapisCubicsplitLongerThanThreesinbool(failString)(intercalate" "justCubes)(lengthjustCubes>0)
Note - splitting the long numbers up drops the zero from the success - it never even makes it to the check. I got it working either with the zero and no splitting or vice versa, and the fix for me at this point would be a special case to catch zero specifically. Which I should do, just not this second.
Edit: Problem is fixed by adding a the check when we map intoDigits over the input:
showCubes::String->StringshowCubess=letmaybeDigits=filter(isJust)$map(\s->readMaybes::MaybeInt)$wordsssplitLongerThanThrees=mapfromDigits$concat$map(chunksOf3)$mapintoDigitsCatchingZero$mapfromJust$maybeDigitsjustCubes=filter(/=failString)$mapisCubicsplitLongerThanThreesinbool(failString)(intercalate" "justCubes)(lengthjustCubes>0)where-- Prevents it from becoming []intoDigitsCatchingZeron=ifn==0then[0]elseintoDigitsn
This problem happened because my implementation of intoDigits is recursive and requires a base case (0) that returns an empty list. Just gotta sidestep it in that one special instance. Could have also inserted a check in the let binding of isCubic but either way I coudln't figure out how to not have to check for it specifically. Now produces correct output, at least.
Haskell:
Note - splitting the long numbers up drops the zero from the success - it never even makes it to the check. I got it working either with the zero and no splitting or vice versa, and the fix for me at this point would be a special case to catch zero specifically. Which I should do, just not this second.
Edit: Problem is fixed by adding a the check when we map
intoDigits
over the input:This problem happened because my implementation of
intoDigits
is recursive and requires a base case (0) that returns an empty list. Just gotta sidestep it in that one special instance. Could have also inserted a check in the let binding ofisCubic
but either way I coudln't figure out how to not have to check for it specifically. Now produces correct output, at least.Spec be damned, my version just returns a
bool