Guest User

Untitled

a guest
Oct 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from itertools import permutations, zip_longest
  3.  
  4.  
  5. def get_result():
  6. checked = []
  7. for permutation in permutations_of_items_in_bins(items='aabbb', number_of_bins=3, items_per_bin=2):
  8. if permutation in checked:
  9. continue
  10. checked.append(permutation)
  11. if is_valid_result(permutation):
  12. return permutation
  13. # this is for debug only
  14. for x in sorted(checked):
  15. print(_format(x))
  16. return None
  17.  
  18.  
  19. def is_valid_result(result):
  20. return False # in reality, this may return True
  21.  
  22.  
  23. # from: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
  24. def _get_chunks(n, iterable, padvalue=None):
  25. "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
  26. return zip_longest(*[iter(iterable)]*n, fillvalue=padvalue)
  27.  
  28.  
  29. def _format(iterable):
  30. return ','.join([''.join(c) for c in iterable])
  31.  
  32.  
  33. def permutations_of_items_in_bins(items: str, items_per_bin: int, number_of_bins: int, pad='z'):
  34. # if not enough items to fit in all the bins, pad them with a value representing "empty"
  35. items = items.rjust(number_of_bins * items_per_bin, pad)
  36. # get all possible sort orders of the items
  37. _permutations = permutations(items)
  38. # that are unique
  39. unique_permutations = set(_permutations)
  40.  
  41. # for each sort order, split them into bins in that order
  42. for permutation in unique_permutations:
  43. # split items into bins
  44. chunks = list(_get_chunks(items_per_bin, permutation))
  45. # item sort order in bins doesn't matter, so let's always sort the items in each bin
  46. normalized = [tuple(sorted(x)) for x in chunks]
  47. yield normalized
  48.  
  49.  
  50. get_result()
  51.  
  52. """
  53. aa,bb,bz
  54. aa,bz,bb
  55. ab,ab,bz
  56. ab,az,bb
  57. ab,bb,az
  58. ab,bz,ab
  59. az,ab,bb
  60. az,bb,ab
  61. bb,aa,bz
  62. bb,ab,az
  63. bb,az,ab
  64. bb,bz,aa
  65. bz,aa,bb
  66. bz,ab,ab
  67. bz,bb,aa
  68. """
Add Comment
Please, Sign In to add comment