DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Little, Big and Bigger

Weekly Challenge 368

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

Task 1: Make it Bigger

Task

You are given a given a string number and a character digit.

Write a script to remove exactly one occurrence of the given character digit from the given string number, resulting the decimal form is maximised.

My solution

As all the examples are positive integers, I've assumed that the string number is always this. Sometimes brute force is the easiest way to write a solution a task.

I start my turning the number and digit values into strings, and check the digit is in the number. I set the max_number variable to 0.

def make_it_bigger(number, digit) -> int:
    number = str(number)
    digit = str(digit)

    if digit not in number:
        raise ValueError(f"The digit {digit} is not in {number}")
    max_number = 0
Enter fullscreen mode Exit fullscreen mode

I then create a loop with the variable i from 0 to one less than the length of the number. If the digit at that position is the digit value, I compute the number with that digit removed, and update max_number if required.

    for i in range(len(number)):
        if number[i] != digit:
            # Don't consider removing this digit
            continue

        new_number = int(number[:i] + number[i+1:])
        if new_number > max_number:
            max_number = new_number

    return max_number
Enter fullscreen mode Exit fullscreen mode

The Perl solution follows the same logic, and uses the substr method to extract parts of the string.

Examples

$ ./ch-1.py 15456 5
1546

$ ./ch-1.py 7332 3
732

$ ./ch-1.py 2231 2
231

$ ./ch-1.py 543251 5
54321

$ ./ch-1.py 1921 1
921
Enter fullscreen mode Exit fullscreen mode

Task 2: Big and Little Omega

Task

You are given a positive integer $number and a mode flag $mode. If the mode flag is zero, calculate little omega (the count of all distinct prime factors of that number). If it is one, calculate big omega (the count of all prime factors including duplicates).

My solution

For this task, I start by computing all the prime factors and storing them in the factors list (array in Perl). To do this, I start with the variable i as 2 (the smallest prime number). I have a loop that continues until the number variable is 1. For each iteration, I check if number is evenly divisible by i. If it is, I add i to the factors list, and divide number by i. If it isn't, I increment i by 1.

To avoid an endless loop, I have a safety check to see that i isn't greater than number. That should never happen, but you never know with user input, or dodgy code :)

The final part of the task is to return a number based on the mode value. If the mode is non-zero, I return the number of items in the factors list. If it is zero, I convert this to set first to count the number of unique factors.

def omega(number: int, mode: int) -> int:
    factors = []
    i = 2

    while number > 1:
        if number % i == 0:
            factors.append(i)
            number //= i
        else:
            i += 1

            if i > number:
                raise ValueError("Cannot calculate prime digits")

    return len(factors) if mode else len(set(factors))
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py 100061 0
3

$ ./ch-2.py 971088 0
3

$ ./ch-2.py 63640 1
6

$ ./ch-2.py 988841 1
2

$ ./ch-2.py 211529 0
2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)