DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 173

Two pretty straight forward tasks this week. This is good given how challenging my day job has been recently.

Challenge, My solutions

Task 1: Esthetic Number


You are given a positive integer, $n.

Write a script to find out if the given number is Esthetic Number. An esthetic number is a positive integer where every adjacent digit differs from its neighbor by 1.

My solution

One of the main difference between Python and Perl (other than the syntax) is variables are typed in Python where they aren't in vanilla Perl (I'm excluding Moose like variables here). This always means there is a little extra work required in Python to get the right solution.

For this challenge, I simply loop from the first character to the second last character. I compare the absolute difference (using the abs function) between that character and the next one. If the difference is not 1, we know the number is not esthetic, and can print 0 and exit the loop. If we reach the end of the loop, we can print 1.


$ ./ 5456

$ ./ 120
Enter fullscreen mode Exit fullscreen mode

Task 2: Sylvester’s sequence


Write a script to generate first 10 members of Sylvester's sequence.

My solution

The big issue with this challenge is that the numbers involved are very large, and most importantly > 231-1. Python will automatically convert an integer into a long and play along nicely.

For the Perl solution, using Math::BigInt will achieve the same outcome.

In my code, I seed the solutions list (array in Perl) with S0 of 2. I then have a loop that iterates until there are 10 values in the list. For each iteration, I take the product of the current values, and add one.

In the Python code, I use maths prod function, while I list List::Utils product function in my Perl code.


$ ./ 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)