Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- int n - the number to transform
- int length - bit number
- '''
- def transformNumberToBinaryVector(n,length):
- result = []
- # transform to binary vector
- while n != 0:
- remainder = n % 2
- result.append(remainder)
- n = int(n / 2)
- # complete with 0
- while len(result) < length:
- result.append(0)
- return result
- '''
- Get all numbers <= n, in binary vectors
- '''
- def getAllCombinations(n):
- result = []
- upperLimit = pow(2,n)
- for i in range(0, upperLimit):
- result.append(transformNumberToBinaryVector(i,n))
- return result
- '''
- If corespondent position in combination is 0
- item at above mentioned poition in input becomes negative.
- Assumes all input is positive
- '''
- def applyCombination(input, combination):
- for i in range(len(input)):
- if(combination[i] == 0):
- input[i] = input[i] * -1
- return input
- '''
- Solver Method.
- '''
- def solve(input):
- result = 0
- allCombinations = getAllCombinations(len(input))
- for combination in allCombinations:
- cleanInput = list(input)
- candidate = applyCombination(cleanInput, combination)
- if sum(candidate) >= 0:
- result += 1
- return result
- '''
- Entry Point
- '''
- input = [1,2,3]
- result = solve(input)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement