# TASK #1 › Words Length

This is relatively straight forward task. I take the string and use the regex `/^\s*\S+?\s+(.*)\s+\S+\s*$/`

to strip out the first and last words (and any leading or trailing white space). I then strip out white-space from the string, and return the length.

## Examples

```
» ./ch-1.pl "The Weekly Challenge"
6
» ./ch-1.pl "The purpose of our lives is to be happy"
23
```

# TASK #2 › Flip Array

This is another task where I will be interested to see the other solutions Team PWC members offer up. Abigail made an excellent post last week explaining the number theory behind her work.

Firstly I've retrieve the values, and verified they are all positive numbers.

I then go through a loop from 0 to 2^{n}-2 (2^{n}-1 is all negative, and thus won't provide a non-negative sum). If the binary bit is true, then I flip the number. For example if the numbers are `2 4 6 8`

and the loop count is 5 (binary 101), then the combination I am using is `2 -4 6 -8`

.

Once I have the combination in the loop, I sum the numbers. If the sum is less than 0, I proceed to the next iteration. If the solution is better than any previous numbers (either a lower sum, or the same sum with a lower number of flipped values), then I record it.

Once the loop is complete, I then present the result.

This solution requires 2^{n}-1 calculations. So even for 10 numbers, there is only 1023 iterations through the main loop. There may be more efficient ways to solve this challenge. I guess I'll find out during this week.

## Examples

```
» ./ch-2.pl 3 10 8
RESULT IS: 1 (-10 3 8)
» ./ch-2.pl 12 10 2
RESULT IS: 1 (-12 2 10)
```

## Top comments (0)