DEV Community

Simon Green
Simon Green

Posted on

Moving and Removing

Congratulations to Mohammad for receiving the White Camel Award this year. I cannot think of a better recipient for his tireless work within the Perl Community.

Weekly Challenge 226

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: Shuffle String

Task

You are given a string and an array of indices of same length as string.

Write a script to return the string after re-arranging the indices in the correct order.

My solution

Usually my Perl solution is pretty much a transliteration of my Python solution. This time it isn't. Strings in Python are immutable, which means that cannot be changed like they can in Perl.

Python solution

For the Python solution, I create a mapping dict where the key is the integer and the value is the letter.

mapping = {ints[i]: word[i] for i in range(len(ints))}
Enter fullscreen mode Exit fullscreen mode

I then sort the key numerically and print the value (letter) in that position

for i in sorted(mapping):
    solution += mapping[i]
Enter fullscreen mode Exit fullscreen mode

Perl solution

The Perl solution is more straight forward. I work through each pair of letter and position and put the letter in the correct position. In Perl, the substr function can be used as both a getter and setter

foreach my $i ( 0 .. $#ints ) {
    substr( $solution, $ints[$i], 1, substr( $word, $i, 1 ) );
    }
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py lacelengh 3 2 0 5 4 8 6 7 1
challenge

$ ./ch-1.py rulepark 4 7 3 1 0 5 2 6
perlraku
Enter fullscreen mode Exit fullscreen mode

Task 2: Zero Array

You are given an array of non-negative integers, @ints.

Write a script to return the minimum number of operations to make every element equal zero.

In each operation, you are required to pick a positive number less than or equal to the smallest element in the array, then subtract that from each positive element in the array.

My solution

Unless I am missing something very obvious, the solution is the number of unique positive integers. In Python, we know that a set will remove duplicate numbers, so my solution is as follows:

count = len(set(i for i in ints if i != 0))
Enter fullscreen mode Exit fullscreen mode

Meanwhile in Perl, the uniq function from List::Util will achieve the same functionality.

my $count = scalar( uniq grep { $_ != 0 } @ints );
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py 1 5 0 3 5
3

$ ./ch-2.py 0
0

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

Top comments (1)

Collapse
 
matthewpersico profile image
Matthew O. Persico

Without List::Util:

my %u = map { $_=>1 } grep {$_ !=0} @ints;
my $count = scalar(keys(%u));
Enter fullscreen mode Exit fullscreen mode