#!/usr/bin/env perlusestrict;usewarnings;usefeatureqw(signatures);nowarnings"experimental::signatures";useList::Utilqw(sum0);# Primary objectivesub is_cubic($num) {returnlength($num)<=3&&sum0(map{$_**3}split//,$num)==$num;}# Secondary objectivesub find_cubes($string) {my@out;while($string=~s/(\d{1,3})//){push@out,$1ifis_cubic($1);}returnjoin"",@outif(@out);return"Unlucky";}useTest::Moretests=>5;is(is_cubic(153),1,"153 is cubic");isnt(is_cubic(152),1,"152 is not cubic");is(find_cubes("aqdf& 0 1 xyz 153 777.777"),"0 1 153","Found '0 1 153'");is(find_cubes("QK29 45[&erui"),"Unlucky","Found no cubic numbers in 'QK29 45[&erui'");is(find_cubes("24172410"),"Unlucky","Found no cubic numbers in '24172410'");is(find_cubes("2417241530"),"153 0","Found '153 0'");
Primary objective
In the is_cubic function, I first check if the input has at most 3 digits. If that check passes, the rest of the logical statement is executed.
Next, the input is split up into digits by splitting on the empty string. The map function goes over that list of digits and cubes each one. I make use of the sum0 aggregate function from List::Util to calculate the sum of the cubes. The sum is finally compared to the original input value.
Secondary objective
In the find_cubes function, I simply use the substitution function in Perl to find and remove 1 to 3 successive digits in the input string. The while loop will be entered as long as s was able to make a substitution. Each match is captured and saved in the special variable $1, which I use as input to the is_cubic function from the primary objective. If the is_cubic function returns a true value, the number is saved to a list.
If any values at all have been saved to the list, the list items are joined together and a space-separated string is returned. Otherwise, the string "Unlucky" is returned.
I've provided some tests to show that it works at least with the test data provided in the challenge. Regex quantifiers are greedy by default, so the engine will try to match 3 digits as much as it can and to prove this, the last checks to see that I can find two known cubes, one of which is 3 digits, in a longer digit string.
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.
Perl:
Primary objective
In the
is_cubicfunction, I first check if the input has at most 3 digits. If that check passes, the rest of the logical statement is executed.Next, the input is split up into digits by splitting on the empty string. The map function goes over that list of digits and cubes each one. I make use of the sum0 aggregate function from List::Util to calculate the sum of the cubes. The sum is finally compared to the original input value.
Secondary objective
In the
find_cubesfunction, I simply use the substitution function in Perl to find and remove 1 to 3 successive digits in the input string. The while loop will be entered as long asswas able to make a substitution. Each match is captured and saved in the special variable$1, which I use as input to theis_cubicfunction from the primary objective. If theis_cubicfunction returns a true value, the number is saved to a list.If any values at all have been saved to the list, the list items are joined together and a space-separated string is returned. Otherwise, the string "Unlucky" is returned.
I've provided some tests to show that it works at least with the test data provided in the challenge. Regex quantifiers are greedy by default, so the engine will try to match 3 digits as much as it can and to prove this, the last checks to see that I can find two known cubes, one of which is 3 digits, in a longer digit string.
Primary Objective Code golf-ished:
P.S. @thepracticaldev , I think the secondary objective example 1 output is incorrect:
The 154 shouldn't be there, right?
Seems like it was supposed to be in the input, not expected output.
Pascal
Here is a live demo on RexTester.
Wow ... That's too good ...
But how about the secondary objective ? .. .
Rust Answer!
Clocked in at just under 100 lines so decided I can post that as a comment 😆
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
intoDigitsover the input:This problem happened because my implementation of
intoDigitsis 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 ofisCubicbut 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{ % gist gist.github.com/devparkk/c33b160e4... % }
I managed to complete the primary objective in C#!
You can find my code here: pastebin.com/kgKzprGN