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 of g964’s “Hidden ‘Cubic’ Numbers” kata on CodeWars.
Primary Objective
The first part of this challenge is to create a method that can determine whether a number is a cubic number or not. If the number has been determined to not be cubic, the output should be either
null
or the string "Unlucky."Cubic numbers are numbers with at most three digits, such that the sum of the cubes of their digits is the number itself.
For example: 153 is a cubic number, because
1^3 + 5^3 + 3^3 = 153
.
Secondary Objective
If you are looking for more of a challenge, you can attempt to create a function that can find cubic numbers in a string. In the output, the numbers should be returned in the order in which they are encountered in the input string.
Example:
s = “aqdf& 0 1 xyz 153 777.777"
should return“0 1 153 154”
s = “QK29 45[&erui”
should return “Unlucky” ornull.
Note: In a string where three digits or more follow each other, the function should examine the numbers in sets of three digits starting from the left. In string “001234”, the function should see 001, then 234, evaluate each, then return any cubic numbers it found (e.g. “24172410” becomes “241”, “724”, and “10”).
Good luck, happy coding!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (13)
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