Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. '''
  2. int n - the number to transform
  3. int length - bit number
  4. '''
  5. def transformNumberToBinaryVector(n,length):
  6. result = []
  7. # transform to binary vector
  8. while n != 0:
  9. remainder = n % 2
  10. result.append(remainder)
  11. n = int(n / 2)
  12.  
  13. # complete with 0
  14. while len(result) < length:
  15. result.append(0)
  16. return result
  17.  
  18. '''
  19. Get all numbers <= n, in binary vectors
  20. '''
  21. def getAllCombinations(n):
  22. result = []
  23. upperLimit = pow(2,n)
  24. for i in range(0, upperLimit):
  25. result.append(transformNumberToBinaryVector(i,n))
  26. return result
  27.  
  28. '''
  29. If corespondent position in combination is 0
  30. item at above mentioned poition in input becomes negative.
  31. Assumes all input is positive
  32. '''
  33. def applyCombination(input, combination):
  34. for i in range(len(input)):
  35. if(combination[i] == 0):
  36. input[i] = input[i] * -1
  37. return input
  38.  
  39. '''
  40. Solver Method.
  41. '''
  42. def solve(input):
  43. result = 0
  44. allCombinations = getAllCombinations(len(input))
  45. for combination in allCombinations:
  46. cleanInput = list(input)
  47. candidate = applyCombination(cleanInput, combination)
  48. if sum(candidate) >= 0:
  49. result += 1
  50. return result
  51.  
  52. '''
  53. Entry Point
  54. '''
  55. input = [1,2,3]
  56. result = solve(input)
  57. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement