DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Commify every mountain

Weekly Challenge 355

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. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Thousand Separator

Task

You are given a positive integer, $int.

Write a script to add thousand separator, , and return as string

My solution

Both Perl and Python have modules do this. There is absolutely no need to reinvent a perfectly round wheel. As int is a reserved word in Python, I use the variable number instead. The Python solution is as follows.

def thousand_separator(number: int) -> str:
    return f"{number:,}"
Enter fullscreen mode Exit fullscreen mode

The Perl solution uses the Number::Format module.

use Number::Format 'format_number';

sub main ($int) {
    say format_number($int);
}
Enter fullscreen mode Exit fullscreen mode

It should of course be noted that not all countries use the comma to separate grouping of digits. Some countries use the dot character instead. India also groups numbers by the hundred after the first thousand (e.g. 12,45,67,890).

Examples

$ ./ch-1.py 123
123

$ ./ch-1.py 1234
1,234

$ ./ch-1.py 1000000
1,000,000

$ ./ch-1.py 1
1

$ ./ch-1.py 12345
12,345
Enter fullscreen mode Exit fullscreen mode

Task 2: Mountain Array

Task

You are given an array of integers, @ints.

Write a script to return true if the given array is a valid mountain array.

An array is mountain if and only if:

  1. arr.length >= 3, and
  2. There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

My solution

This turned out to be harder than I thought it would be. I'm not sure if this is the best solution. I advantage of this solution is I loop over the list once.

These are the steps I take.

  1. Check that there are at least three items in the ints list (array in Perl). Return False if there are not.
  2. Set the variable last_int to the first item in the ints list, and the variable direction to up.
  3. Check that the second item is higher than the first, and return False if it is not. This ensures that descents won't return the wrong result.
  4. Loop through the remaining items in ints setting the value to current_int.
    1. If current_int and last_int are the same, return False.
    2. If direction is up and the current_int value is less than last_int, set direction to down.
    3. If direction is down and the current_int value is higher than last_int, return False.
    4. Set last_int to the current_int value.
  5. If direction is still up, return False. This ensures ascents won't return the wrong result.
  6. Return True.
def mountain_array(ints: list) -> bool:
    if len(ints) < 3:
        return False

    direction = 'up'
    last_int = ints[0]

    if ints[1] <= last_int:
        return False

    for current_int in ints[1:]:
        if current_int == last_int:
            return False
        if direction == 'up':
            if current_int < last_int:
                direction = 'down'
        else:
            if current_int > last_int:
                return False

        last_int = current_int

    if direction == 'up':
        return False

    return True
Enter fullscreen mode Exit fullscreen mode

The Perl solution follows the same logic.

Examples

$ ./ch-2.py 1 2 3 4 5
False

$ ./ch-2.py 0 2 4 6 4 2 0
True

$ ./ch-2.py 5 4 3 2 1
False

$ ./ch-2.py 1 3 5 5 4 2
False

$ ./ch-2.py 1 3 2
True

$ ./ch-2.py 1 3 
False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)