fork download
  1. import math
  2.  
  3. def allocate_students(schools, groups):
  4. """
  5. Allocates students from schools into groups, prioritizing the most even
  6. distribution possible.
  7.  
  8. Args:
  9. schools: A dictionary mapping school names to student counts.
  10. groups: A list of group names.
  11.  
  12. Returns:
  13. A dictionary mapping school names to a list of (group, student_count) tuples.
  14. """
  15.  
  16. num_schools = len(schools)
  17. num_groups = len(groups)
  18. total_students = sum(schools.values())
  19.  
  20. # Check if allocation is possible
  21. if num_schools < 2 or num_groups < 2:
  22. return None # Not enough schools or groups
  23.  
  24. # Calculate ideal group size
  25. ideal_group_size = total_students // num_groups
  26.  
  27. # Create the allocation dictionary (now with schools as keys)
  28. allocation = {school: [] for school in schools}
  29.  
  30. # Sort schools by student count in descending order
  31. sorted_schools = sorted(schools.items(), key=lambda item: item[1], reverse=True)
  32.  
  33. # Allocate students
  34. group_counts = {group: 0 for group in groups} # Track group sizes
  35. for school, count in sorted_schools:
  36. remaining_count = count
  37. assigned_groups = 0
  38. while remaining_count > 0:
  39. # Find the group with the fewest students
  40. min_group = min(group_counts, key=group_counts.get)
  41.  
  42. # Calculate how many students to allocate to this group
  43. allocate_count = min(remaining_count, ideal_group_size // 2)
  44.  
  45.  
  46. allocation[school].append((min_group, allocate_count))
  47. group_counts[min_group] += allocate_count
  48. remaining_count -= allocate_count
  49. assigned_groups += 1
  50.  
  51. # Distribute remaining students one by one
  52. while remaining_count > 0:
  53. min_group = min(group_counts, key=group_counts.get)
  54. allocation[school].append((min_group, 1))
  55. group_counts[min_group] += 1
  56. remaining_count -= 1
  57.  
  58. return allocation
  59.  
  60. # Example usage
  61. schools = {
  62. "kpu": 39,
  63. "bij": 27,
  64. "Mullahikheda": 34,
  65. "SKP": 56,
  66. "kamalpur": 17,
  67. "Alinagar": 14
  68. }
  69. groups = ["Tushar red", "Adheep", "Valentina", "Kavita", "Manjari", "Surabhi", "Vishal", "Sukhmani", "planetarium"]
  70.  
  71. allocation = allocate_students(schools, groups)
  72.  
  73. if allocation:
  74. for school, groups_and_counts in allocation.items():
  75. print(f"{school}:")
  76. for group, count in groups_and_counts:
  77. print(f" {group}: {count}")
  78. else:
  79. print("Allocation not possible.") # This should rarely happen
  80.  
  81.  
Success #stdin #stdout 0.03s 9744KB
stdin
Standard input is empty
stdout
kpu:
  Vishal: 10
  Sukhmani: 10
  planetarium: 10
  Surabhi: 9
bij:
  Manjari: 10
  Vishal: 10
  Sukhmani: 7
Mullahikheda:
  Tushar red: 10
  Adheep: 10
  Valentina: 10
  Kavita: 4
SKP:
  Tushar red: 10
  Adheep: 10
  Valentina: 10
  Kavita: 10
  Manjari: 10
  Surabhi: 6
kamalpur:
  planetarium: 10
  Kavita: 7
Alinagar:
  Surabhi: 10
  Sukhmani: 4