Article
· May 15 3m read

【Coding Puzzle】 "Esthetic Numbers"

Hello,

I would like you to propose this challenge.

It has been created by the CodeWars community here: https://www.codewars.com/kata/6523a71df7666800170a1954/python

I will copy and paste the description:

 

🔡🟢 DESCRIPTION:

A number is Esthetic if, in any base from base2 up to base10, the absolute difference between every pair of its adjacent digits is constantly equal to 1.

num = 441 (base10)
// Adjacent pairs of digits:
// |4, 4|, |4, 1|
// The absolute difference is not constant
// 441 is not Esthetic in base10

441 in base4 = 12321
// Adjacent pairs of digits:
// |1, 2|, |2, 3|, |3, 2|, |2, 1|
// The absolute difference is constant and is equal to 1
// 441 is Esthetic in base4

Given a positive integer num, implement a function that returns an array containing the bases (as integers from 2 up to 10) in which num results to be Esthetic, or an empty array [] if no base makes num Esthetic.

Examples

10 ➞ [2, 3, 8, 10]
// 10 in base2 = 1010
// 10 in base3 = 101
// 10 in base8 = 12
// 10 in base10 = 10

23 ➞ [3, 5, 7, 10]
// 23 in base3 = 212
// 23 in base5 = 43
// 23 in base7 = 32
// 23 in base10 = 23

666 ➞ [8]
// 666 in base8 = 1232

 

The initial Python code is just:

def esthetic(num):
    pass

 

And the Unit Tests are:

import codewars_test as test
from solution import esthetic

@test.describe("Fixed tests")
def fixed_tests():
    @test.it('Basic test Cases')
    def basic_test_cases():
        test.assert_equals(esthetic(10), [2, 3, 8, 10], "Example #1")
        test.assert_equals(esthetic(23), [3, 5, 7, 10], "Example #2")
        test.assert_equals(esthetic(666), [8], "Example #3")
        test.assert_equals(esthetic(13), [5, 6])
        test.assert_equals(esthetic(1), [2, 3, 4, 5, 6, 7, 8, 9, 10])
        test.assert_equals(esthetic(9), [4, 7, 9, 10])
        test.assert_equals(esthetic(74), [])
        test.assert_equals(esthetic(740), [4, 6, 9])
        test.assert_equals(esthetic(928), [])
        test.assert_equals(esthetic(259259), [9])
        test.assert_equals(esthetic(883271), [])
        test.assert_equals(esthetic(1080898), [7])
        test.assert_equals(esthetic(1080899), [])

 

 

 

 

My solution was:

def esthetic(num):
    def to_base(n, base):
        """Convert number n to a given base and return the digits as a list."""
        digits = []
        while n:
            digits.append(n % base)
            n //= base
        return digits[::-1]  # reverse the list to get the correct order

    def is_esthetic_in_base(digits):
        """Check if the given list of digits is esthetic."""
        for i in range(1, len(digits)):
            if abs(digits[i] - digits[i - 1]) != 1:
                return False
        return True

    esthetic_bases = []

    for base in range(2, 11):
        digits = to_base(num, base)
        if is_esthetic_in_base(digits):
            esthetic_bases.append(base)

    return esthetic_bases

 

 

How would you like to solve it using ObjectScript or any other prefered programming language?

Discussion (4)1
Log in or sign up to continue

According to your definition, "A number is Esthetic if ... between every pair of its adjacent digits ...", the test example

test.assert_equals(esthetic(1), [2, 3, 4, 5, 6, 7, 8, 9, 10])

is wrong, because the number one (1) converted in whatever number base is always 1. Looking on that 1 I do NOT see any ADJACENT digit(s), except you define that a single digit is always an esthetic number (but I do not see such a definition).

justmy2cents

Thanks for your reply, Julius.

I understand your concern and would like to clarify the point of single digit numbers in this context.

The definition we have given is: “A number is Aesthetic if, in any base from base 2 to base 10, the absolute difference between each pair of adjacent digits is consistently equal to 1.” Now, when we talk about single digit numbers, they technically have no adjacent digits to compare, which may seem confusing.

However, in this context, we consider a single-digit number to be trivially aesthetic because there are no pairs of digits that can contradict the condition that the absolute difference is 1. In other words, there is no comparison to make that could invalidate the aesthetics of the number.

It is as if we were to say that a straight line is “smooth” because it has no curves that could make it rough. Similarly, a single digit is aesthetic because there are no differences between digits that would break the aesthetics.

In the case of the number 1, no matter what base you represent it in, it will always be a single digit. Therefore, there is no pair of adjacent digits to compare, and for this reason, it is considered to trivially meet the condition of being aesthetic.

I hope this clears up the doubt and justifies why a single digit number is considered aesthetic in any basis.

🙂🤔💭🟢 Thank you Julius for your deep reply and interesting observations, honestly they are totally right.

Greetings.

We have our own challenge with very specific conditions and goals: #Code Golf
And what is the purpose of your challenge, I do not quite understand: speed, code size, etc? Rewriting the code in ObjectScript is too simple and not interesting.

 
esthetic()