|
| 1 | +"""Kata - Simple Fun #66: Obtain Max Number |
| 2 | +
|
| 3 | +completed at: 2024-04-20 20:35:01 |
| 4 | +by: Jakub Červinka |
| 5 | +
|
| 6 | +# Task |
| 7 | + CodeBots decided to make a gift for CodeMaster's birthday. They got a pack of candies of various sizes from the store, but instead of giving the whole pack they are trying to make the biggest possible candy from them. On each turn it is possible: |
| 8 | + |
| 9 | + ``` |
| 10 | + to pick any two candies of the same size and merge |
| 11 | + them into a candy which will be two times bigger; |
| 12 | +
|
| 13 | + to pick a single candy of an even size and split it |
| 14 | + into two equal candies half of this size each.``` |
| 15 | +What is the size of the biggest candy they can make as a gift? |
| 16 | +
|
| 17 | +# Example |
| 18 | +
|
| 19 | + For `arr = [2, 4, 8, 1, 1, 15]`, the output should be 16. |
| 20 | +``` |
| 21 | +[2, 4, 8, 1, 1, 15] --> [2, 4, 8, 2, 15] |
| 22 | +-->[4, 4, 8, 15] --> [8, 8, 15] --> [16, 15] -->choose 16 |
| 23 | +``` |
| 24 | +
|
| 25 | +# Input/Output |
| 26 | +
|
| 27 | +
|
| 28 | + - [input] integer array `arr` |
| 29 | +
|
| 30 | + Array of positive integers. |
| 31 | +
|
| 32 | + Constraints: |
| 33 | + |
| 34 | + `5 ≤ inputArray.length ≤ 50,` |
| 35 | + |
| 36 | + `1 ≤ inputArray[i] ≤ 100.` |
| 37 | +
|
| 38 | +
|
| 39 | + - `[output]` an integer |
| 40 | +""" |
| 41 | + |
| 42 | +from itertools import pairwise |
| 43 | + |
| 44 | +def obtain_max_number(lst): |
| 45 | + while True: |
| 46 | + lst.sort() |
| 47 | + for i, (a, b) in enumerate(pairwise(lst)): |
| 48 | + if a == b: |
| 49 | + lst[i:i + 2] = [a + b] |
| 50 | + break |
| 51 | + else: |
| 52 | + return max(lst) |
0 commit comments