DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 192

Challenge, My solutions

Task 1: Binary Flip

Task

You are given a positive integer, $n.

Write a script to find the binary flip.

My solution

So there are at least two ways you could take this task. The obvious option is to convert $n to a binary, use string.translate (or tr in Perl) to flip the bits, and convert this back to an integer number. And given that this is a one off with a very small number, that's a perfectly acceptable solution.

The solution I chose was to find the next power of 2 higher than $n, and then subtract one less than that from the original number.

As an example, the next power of 2 for the number 5 is 8 = 2³. One less than that is 7 (binary 111). Since we know that in subtraction that effectively will flip all the bits, we will have the correct solution.

Examples

$ ./ch-1.py 5
2

$ ./ch-1.py 4
3

$ ./ch-1.py 6
1
Enter fullscreen mode Exit fullscreen mode

Task 2: Equal Distribution

Task

You are given a list of integers greater than or equal to zero, @list.

Write a script to distribute the number so that each members are same. If you succeed then print the total moves otherwise print -1.

Please follow the rules:

  1. You can only move a value of '1' per move
  2. You are only allowed to move a value of '1' to a direct neighbor/adjacent cell

My solution

Like most weeks, I wrote the code for this task on Monday evening (morning UK time). It was pretty straight forward (a couple of lines). Also like most weeks, I tend to write my blog on a Sunday evening. So I went to write this blog post and the rules have changed :)

Which I don't really mind as the new rules makes the task more challenging. The original solution was just a simple calculation (the sum of all numbers greater than the average). I'm sure some cleverer Team PWC members were able to do the same with the new challenge, but in the end I took the approach of actually moving the numbers.

We know that a solution is only possible if the sum of all numbers is even divisible by the number of numbers. If this isn't the case, I print -1 and exit. If it is I store this as the target value.

I then work through the list from left to right. If the number is less than the target, we know we need to take value away from a different number. This is the needed value. I then have an inner loop that also works from left to right to decide where to take the value from. The logic behind this is taking values from the leftmost item in the excess of the target will result in less moves.

The value we can take is the minimum of needed or the excess value from the target, which I have called take. We take the value from one and give to another. The needed value is reduced, and we add the take value by the number of positions to the moves count. If the needed value is still not zero we continue this inner loop moving to the next leftmost number greater than the target.

Examples

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

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

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

Top comments (0)