DEV Community

loading...

Weekly Challenge 116

simongreennet profile image Simon Green ・3 min read

Challenge, My solutions

Tipsy

The Perl Conference is this week, and there are many speakers I'm looking forward too. I do have a ticket to support the effort, but won't watch too many live. The event is 1am - 8am AEST, which kinda sucks for me.

On to this week's task.

What is a number?

Ask I stranger on a street (in a Covid safe way of course!) to pick a number between one and 6, and they are likely to say one of six answers (one, two, three, four, five or six). Never 3.4, or 2.6. This was featured in an episode of Numb3rs. We are programmers, and think differently. So whenever a task mentions numbers, I always consider the case of a non-whole number. No, I don't think about complex numbers :P

TASK #1 › Number Sequence

Task

You are given a number $N >= 10.

Write a script to split the given number such that the difference between two consecutive numbers is always 1 and it shouldn’t have leading 0.

Print the given number if it impossible to split the number.

My solution

Firstly, the straight forward bit. I have a loop called $length from 1 to the length of the number. Within this loop I have an array @sequence. The array starts with the first $length digits, and adds the next number to the array until the length of all the digits in the sequence is equal or greater than the length of the input. If the numbers match, we have a solution. If they don't we increase the $length and try again.

However, it would appear that the number 910.011 would also be a valid solution (9, 10.0, 11). The difference between these numbers is always one and doesn't have any leading zeros. This hack only works if the digits after the decimal point is 0 or we have a single value.

Based on this, I have some extra logic in the code to handle the situation where the regular expression \.0+ (a dot followed by one or more zeros) appears in the code. Maybe I'm just overthinking this, or maybe it's intentional.

As Colin noted in this week's Perl review, "Given any ambiguity in a text, given enough people and enough time every possible position will eventually be witnessed. It's just a given to me now"

Examples

$ ./ch-1.pl 1234
1,2,3,4

$ ./ch-1.pl 91011
9,10,11

$ ./ch-1.pl 10203
10203

$ ./ch-1.pl 910.011
9,10.0,11
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Sum of Squares

Task

You are given a number $N >= 10.

Write a script to find out if the given number $N is such that sum of squares of all digits is a perfect square. Print 1 if it is otherwise 0.

My solution

This is relatively straight forward. If the number is not an integer, we remove the decimal point. It servers no use for the purpose of this task (assuming $N >= 10).

We can find the square of all digits using the code sum( map { $_**2 } split //, $number ), which separates the digits, gets the square and sums up all the results.

We then use the sqrt function to get the square root of the sum. If this is an integer, we print 1, otherwise we print 0.

Examples

$ ./ch-2.pl 34
1

$ ./ch-2.pl 50
1

$ ./ch-2.pl 52
0

$ ./ch-2.pl 30.4
1
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app