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.
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:,}"
The Perl solution uses the Number::Format module.
use Number::Format 'format_number';
sub main ($int) {
say format_number($int);
}
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
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:
-
arr.length >= 3, and - There exists some
iwith0 < i < arr.length - 1such 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.
- Check that there are at least three items in the
intslist (array in Perl). ReturnFalseif there are not. - Set the variable
last_intto the first item in theintslist, and the variabledirectiontoup. - Check that the second item is higher than the first, and return
Falseif it is not. This ensures that descents won't return the wrong result. - Loop through the remaining items in
intssetting the value tocurrent_int.- If
current_intandlast_intare the same, returnFalse. - If
directionisupand thecurrent_intvalue is less thanlast_int, setdirectiontodown. - If
directionisdownand thecurrent_intvalue is higher thanlast_int, returnFalse. - Set
last_intto thecurrent_intvalue.
- If
- If
directionis stillup, returnFalse. This ensures ascents won't return the wrong result. - 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
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
Top comments (0)