DEV Community

Simon Green
Simon Green

Posted on

Numbers Challenges

Weekly Challenge 210

Firstly, thanks to Mohammad. Last weekend marked the four year anniversary of providing us with weekly challenges. I'm sure I speak for all of Team PWC that his tireless work is really appreciated.

Challenge, My solutions

Task 1: Destroy and Win

Task

You are given a list of integers.

Write a script to get the maximum points. You are allowed to take out (destroy) any integer and remove from the list. However if you do that then all integers exactly one-less or one-more would also be removed. Find out the total of integers removed.

My solutions

This appears relatively straight forward. The first thing I do is iterate through a list of unique numbers, giving this the variable i. In Python, this is done by converting a list to a set. A set - by definition - cannot have duplicate numbers. In Perl, I use the uniq function from List::Util.

For each iteration, I get the sum of all values between i-1 and i+1. In Python, we do this by using the for ... if method. In Perl, we can use the grep function. To calculate the sum, Python has a sum function builtin while Perl uses the function from List::Util.

Once the score for this iteration is calculated, I set the score value if it is higher than the current value. And finally, I print the score as output.

Examples

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

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

Task 2: Number Collision

Task

You are given an array of integers which can move in right direction if it is positive and left direction when negative. If two numbers collide then the smaller one will explode. And if both are same then they both explode. We take the absolute value in consideration when comparing.

All numbers move at the same speed, therefore any 2 numbers moving in the same direction will never collide.

Write a script to find out who survives the collision.

My solutions

This is one of those tasks where I'm really interested in how other people tackle it. I did think about how to do this in one pass, but ended up with a solution that probably isn't the most efficient, but is probably more readable.

For this task, I have two loops. The outer loop continues while should_continue is set to True. The inner loop iterates from zero to 2 less than the length of the array, setting the variable i.

For each iteration, I set the variables left for the value at that position and right for the next value. If the left is positive and right is negative, we have a collision. Doing an absolute comparison, if the right is not larger, I remove it. If the left is not larger, I remove that. I then break from the inner loop so the outer loop is called again.

If I reach the end of the inner loop, I set should_continue to false, and the outer loop will also in. In Python, a for loop can have an else block which is only called if the loop isn't interrupted (e.g. with a break statement).

My Perl solution is similar except I can jump to the outer loop from the inner loop directly, so there is no need for the should_continue variable.

Examples

$ ./ch-2.py 2 3 -1
[2, 3]

$ ./ch-2.py 3 2 -4
[-4]

$ ./ch-2.py 1 -1
[]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)