Welcome to day 11 of our Daily Challenge series, hope you're having a wonderful Monday.
In this challenge, we’ll be looking at a modified version...
For further actions, you may consider blocking this person and/or reporting abuse
Perl:
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 ass
was able to make a substitution. Each match is captured and saved in the special variable$1
, which I use as input to theis_cubic
function from the primary objective. If theis_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.
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.
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
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
Pascal
Here is a live demo on RexTester.
{ % gist gist.github.com/devparkk/c33b160e4... % }
I managed to complete the primary objective in C#!
You can find my code here: pastebin.com/kgKzprGN