Weekly Challenge: The maximum difference
Weekly Challenge 320 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 Mohammad made a comment in The Perl Weekly newsletter about "No perl?". I've not always submitting Perl solutions, especially if the task is relatively straight forward. It has been a long time since Perl was my primary language for my day job (six years). Gabor also mentioned in the same newsletter that "there are less and less jobs that are for 'Perl developers'.", and that's definitely the case in Australia. There are very few jobs around, and lot of the companies I have had the privilege at working for have moved onto to other languages for new projects. Task 1: Maximum Count Task You are given an array of integers. Write a script to return the maximum between the number of positive and negative integers. Zero is neither positive nor negative. My solution For this task, I count the number of positive integers, the number of negative integers and return the maximum of those two counts. def maximum_count(ints: list) -> int: pos_count = sum(1 for i in ints if i > 0) neg_count = sum(1 for i in ints if i 0 } @ints; my $neg_count = grep { $_

Weekly Challenge 320
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.
Mohammad made a comment in The Perl Weekly newsletter about "No perl?". I've not always submitting Perl solutions, especially if the task is relatively straight forward.
It has been a long time since Perl was my primary language for my day job (six years). Gabor also mentioned in the same newsletter that "there are less and less jobs that are for 'Perl developers'.", and that's definitely the case in Australia. There are very few jobs around, and lot of the companies I have had the privilege at working for have moved onto to other languages for new projects.
Task 1: Maximum Count
Task
You are given an array of integers.
Write a script to return the maximum between the number of positive and negative integers. Zero is neither positive nor negative.
My solution
For this task, I count the number of positive integers, the number of negative integers and return the maximum of those two counts.
def maximum_count(ints: list) -> int:
pos_count = sum(1 for i in ints if i > 0)
neg_count = sum(1 for i in ints if i < 0)
return max(pos_count, neg_count)
The Perl solution has the same logic. To get the count, I use the grep
function to filter out the results. In Perl, assigning an array to a scalar will return the number of items in that array.
sub main (@ints) {
my $pos_count = grep { $_ > 0 } @ints;
my $neg_count = grep { $_ < 0 } @ints;
say max( $pos_count, $neg_count );
}
Examples
$ ./ch-1.py -3 -2 -1 1 2 3
3
$ ./ch-1.py -2 -1 0 0 1
2
$ ./ch-1.py 1 2 3 4
4
Task 2: Sum Difference
Task
You are given an array of positive integers.
Write a script to return the absolute difference between digit sum and element sum of the given array.
My solution
There's an interesting story about my solution to this task. When I do PWC challenges, I use TDD. That is, I write the test.py
script and then I write the code. I then use these tests to confirm that the code works as expected. As usual, I did that for this task, tests passed.
However all the supplied examples on had one (or zero) integers greater than nine (i.e. more than one digit). I only picked up the issue - using =
instead of +=
when I copied the Python code to Perl.
For this task, I only look at integers with two or more digits. Single digit numbers have no difference. After checking that I only have positive integers, I calculate the difference between the integer and the sum of the digits and add this to the difference
value.
def sum_difference(ints: list) -> int:
if any(i < 1 for i in ints):
raise ValueError('Only positive integers allowed')
difference = 0
for i in ints:
if i > 9:
difference += i - sum(int(d) for d in str(i))
return abs(difference)
In the second last line:
-
str(i)
converts the integer to a string, -
for d in
loops through each character in the string, -
int(d)
turns that character back to an integer, and -
sum()
returns the sum of the values.
As Perl generally doesn't care about types (yes, there are a few exceptions), the code is a little more straight forward. split //
turns a string into an array of characters.
$difference += $i - sum( split( //, $i ) );
Examples
$ ./ch-2.py 1 23 4 5
18
$ ./ch-2.py 1 2 3 4 5
0
$ ./ch-2.py 1 2 34
27