Weekly Challenge: Work it

python dev.to

Weekly Challenge 379

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.

Challenge, My solutions

The etymology of the title is the name of the song by Missy Elliot, which has the line "I put my thing down, flip and reverse it" in reverse. Seems apt for this weeks tasks.

Task 1: Reverse String

Task

You are given a string.

Write a script to reverse the given string without using standard reverse function.

My solution

There are two ways this task can be completed. For the Python solution, I have a loop called i that starts from one less than the length of the string down to zero. For each iteration, I add the character at the position and at it to the result_string variable.

def reverse_string(input_string) -> str:
    result_string = ''

    for i in range(len(input_string) - 1, -1, -1):
        result_string += input_string[i]

    return result_string
Enter fullscreen mode Exit fullscreen mode

For the Perl solution, I iterate over each character in the input_string variable and prepend it to the result_string variable.

sub main ($input_string) {
    my $result_string = "";
    for my $char ( split //, $input_string ) {
        $result_string = $char . $result_string;
    }
    say $result_string;
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py ""
""

$ ./ch-1.py "reverse the given string"
"gnirts nevig eht esrever"

$ ./ch-1.py "Perl is Awesome"
"emosewA si lreP"

$ ./ch-1.py "v1.0.0-Beta!"
"!ateB-0.0.1v"

$ ./ch-1.py "racecar"
"racecar"
Enter fullscreen mode Exit fullscreen mode

Task 2: Armstrong Number

Task

You are given two integers, $base and $limit.

Write a script to find all Armstrong numbers in base $base that are less than $limit.

If raising each of the digits of a nonnegative integer to the power of the total number of digits, then taking the sum, equals the original number, it is an Armstrong number.

My solution

Lets start with the boring stuff :) The main function iterates from 0 to one less than limit. If the result of calling the armstrong_sum fuction is the same as the expected integer, I add it to the results_list list (array in Perl).

def armstrong_numbers(base: int, limit: int) -> list[int]:
    result_list = []
    for i in range(limit):
        if armstrong_sum(i, base) == i:
            result_list.append(i)

    return result_list
Enter fullscreen mode Exit fullscreen mode

The armstrong_sum fuction takes the num that is to be calculated, and the base provided from the input. The first step is to calculate each of the digits that make up this number in specified base. For this I use the divmod function and add the modulus to the beginning of the digits list. I repeat the process until the number has been expanded to all digits.

For bases greater than ten, a value in the digits list might be two or more digits long. For example if base is 16, and num is 31, the list would be [1, 15].

def armstrong_sum(num: int, base: int) -> int:
    digits = []
    while True:
        i, j = divmod(num, base)
        digits.insert(0, j)

        if i == 0:
            break

        num = i
Enter fullscreen mode Exit fullscreen mode

The last part is to calculate the sum of each digit to the power of the length of the list.

    l = len(digits)
    return sum(digit ** l for digit in digits)
Enter fullscreen mode Exit fullscreen mode

The Perl solution follows the same logic.

Examples

$ ./ch-2.py 10 1000
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]

$ ./ch-2.py 7 1000
[0, 1, 2, 3, 4, 5, 6, 10, 25, 32, 45, 133, 134, 152, 250]

$ ./ch-2.py 16 1000
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 342, 371, 520, 584, 645]
Enter fullscreen mode Exit fullscreen mode

Source: dev.to

arrow_back Back to Tutorials