RabbitFarm

2020-09-20

Perl Weekly Challenge 078

Part 1

You are given an array @A containing distinct integers. Write a script to find all leader elements in the array @A. Print (0) if none found.

Solution

“ch-1.pl”

Sample Run

$ perl perl/ch-1.pl 6
@A = (9,10,7,5,6,1)
Leaders = (10,7,6,1)
@A = (3,4,5)
Leaders = (5)

Notes

The approach here is to just repeating the checks for smaller elements. Nothing too fancy. In fact, I actually thought that there might be room for some fun and over engineered way of doing this but I couldn’t really come up with anything that wouldn’t just be obfuscated!

Part 2

You are given array @A containing positive numbers and @B containing one or more indices from the array @A. Write a script to left rotate @A so that the number at the first index of @B becomes the first element in the array. Similary, left rotate @A again so that the number at the second index of @B becomes the first element in the array.

“ch-2.pl”

Sample Run

$ perl perl/ch-2.pl
@A = (10,20,30,40,50)
@B = (3,4)
Rotations:
[40,50,10,20,30]
[50,10,20,30,40]
@A = (7,4,2,6,3)
@B = (1,3,4)
Rotations:
[4,2,6,3,7]
[6,3,7,4,2]
[3,7,4,2,6]

Notes

Another straight forward one. For each value in @B I shift and push the respective number of times.

posted at: 00:27 by: Adam Russell | path: /perl | permanent link to this entry