code_junkie

Algorithm to split an array into N groups based on item index (should be something simple)

Nov 12th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. group1: [e1, ..., e10]
  2. group2: [e11, ..., e20]
  3. group3: [e21, ..., e30]
  4.  
  5. for(i=0;i<array_size;i++) {
  6. if(i>=0*(array_size/3) && i<1*(array_size/3) {
  7. print "group1";
  8. } else if(i>=1*(array_size/3) && i<2*(array_size/3) {
  9. print "group2";
  10. } else if(i>=2*(array_size/3) && i<3*(array_size/3)
  11. print "group3";
  12. }
  13. }
  14.  
  15. for(i=0;i<array_size;i++) {
  16. print "group" + (Math.floor(i/(array_size/N)) + 1)
  17. }
  18.  
  19. const int g = 3; // number of groups
  20. const int n = (array_size + g - 1)/g; // elements per group
  21.  
  22. for (i=0,j=1; i<array_size; ++i) {
  23. if (i > j*n)
  24. ++j;
  25. printf("Group %dn", j);
  26. }
  27.  
  28. int group[3][10];
  29. int groupIndex = 0;
  30. int itemIndex = 0;
  31. for(i = 0; i < array_size; i++)
  32. {
  33. group[groupIndex][itemIndex] = big_array[i];
  34. itemIndex++;
  35. if (itemIndex == 10)
  36. {
  37. itemIndex = 0;
  38. groupIndex++;
  39. }
  40. }
  41.  
  42. struct group {foo * ptr; size_t count };
  43. group * pgroups = new group [ngroups];
  44. size_t objects_per_group = array_size / ngroups;
  45. for (unsigned u = 0; u < ngroups; ++u ) {
  46. group & g = pgroups[u];
  47. size_t index = u * objects_per_group;
  48. g.ptr = & array [index];
  49. g.count = min (objects_per_group, array_size - index); // last group may have less!
  50. }
  51. ...`
  52. for (unsigned u = 0; u < ngroups; ++u) {
  53. // group "g" is an array at pgroups[g].ptr, dimension pgroups[g].count
  54. group & g = pgroups[u];
  55. // enumerate the group:
  56. for (unsigned v = 0; v < g.count; ++v) {
  57. fprintf (stdout, "group %u, item %u, %sn",
  58. (unsigned) u, (unsigned) v, (const char *) g.ptr[v]->somestring);
  59. } }
  60.  
  61. delete[] pgroups;
  62.  
  63. split:{[values;n] / define function split with two parameters
  64. enum:!n / ! does enumerate from 0 through n exclusive, : is assign
  65. floor:_(#values)%n / 33 for this sample, % is divide, _ floor, # count
  66. cut:floor*enum / 0 33 66 for this sample data, * multiplies atom * vector
  67. :cut _ values / cut the values at the given cutpoints, yielding #cut lists
  68. }
  69.  
  70. values:1+!30 / generate values 1 through 30
  71. n:3 / how many groups to split into
  72. groups:split[values;n] / set the groups
  73.  
  74. (1 2 3 4 5 6 7 8 9 10
  75. 11 12 13 14 15 16 17 18 19 20
  76. 21 22 23 24 25 26 27 28 29 30)
  77.  
  78. split:{((_(#x)%y)*!y)_ x}
  79. groups:split[1+!30;3]
  80.  
  81. (1 2 3 4 5 6 7 8 9 10
  82. 11 12 13 14 15 16 17 18 19 20
  83. 21 22 23 24 25 26 27 28 29 30)
  84.  
  85. function arrayToGroups(source, groups) {
  86.  
  87. //This is the array of groups to return:
  88. var grouped = [];
  89.  
  90. //work out the size of the group
  91. groupSize = Math.ceil(source.length/groups);
  92.  
  93. //clone the source array so we can safely splice it
  94. var queue = source;
  95.  
  96. for (var r=0;r<groups;r++) {
  97. //Grab the next groupful from the queue, and append it to the array of groups
  98. grouped.push(queue.splice(0, groupSize));
  99. }
  100. return grouped;
  101. }
  102.  
  103. var herbs = ['basil', 'marjoram', 'aniseed', 'parsely', 'chives', 'sage', 'fennel', 'oregano', 'thyme', 'tarragon', 'rosemary'];
  104.  
  105. var herbGroups = arrayToGroups(herbs, 3);
  106.  
  107. herbGroups[0] = ['basil', 'marjoram', 'aniseed', 'parsely']
  108. herbGroups[1] = ['chives', 'sage', 'fennel', 'oregano']
  109. herbGroups[2] = ['thyme', 'tarragon', 'rosemary']
  110.  
  111. function arrayToGroups($source, $pergroup) {
  112. $grouped = array();
  113. $groupCount = ceil(count($source)/$pergroup);
  114. $queue = $source;
  115. for ($r=0; $r<$groupCount; $r++) {
  116. array_push($grouped, array_splice($queue, 0, $pergroup));
  117. }
  118. return $grouped;
  119. }
Add Comment
Please, Sign In to add comment