Skip to content

Instantly share code, notes, and snippets.

@quackhouse
Created September 27, 2013 00:50
Show Gist options
  • Save quackhouse/6722744 to your computer and use it in GitHub Desktop.
Save quackhouse/6722744 to your computer and use it in GitHub Desktop.
Sorts students into groups of selected size and assigns nicknames to each group. Adds remainder to last group.
students = [ # Array of students' names.
"Alex Hint",
"Amy Ruan",
"Ana Giraldo-Wingler",
"Cooper Mayne",
"Diego Palma",
"Edward Shin",
"Enoch Riese",
"Harrison Powers ",
"Jaclyn Jimenez",
"James Rothpearl",
"Jimmy Davis",
"Jorge Colindres",
"Joshua Oynick",
"Katherine Santiago",
"Matt Lucas",
"Matthew Korporaal",
"Morgan Neiman",
"Natasha Green",
"Quin Cogdell ",
"Tom Metzger",
"Travis Vanderhoop",
"Will Smith",
"Yaritza Rodriguez"
]
allnicknames = [ # Array of nicknames.
"Shoeshine",
"Wiggle-bum",
"Left Eye",
"Sirius",
"Letter-Writing",
"Squirrel Cheeks",
"Happy Days",
"Gangsta Walk",
"High Socks",
"House of La-Code-Sha",
"My Humps",
"Give It Up",
"Bumblefart",
"Slightly Askew",
"Numbnuts",
"Curly Pubes",
"Weird Nostrils",
"Shiny Teeth",
"Clacky Nails",
"Loony Bin",
"Funny Face"
]
puts "Welcome to Group Sorter"
puts "How many people per group would you like?"
people_per_group_str = gets.chomp!
# Gets input from user (how many people per group)...
# and stores it as a string
people_per_group = people_per_group_str.to_i
# Converts the previous variable into an integer
students_random = students.shuffle!
# Randomizes the order of the "students" array
student_groups = students_random.each_slice(people_per_group).to_a
# Splits the array of students into the amount of groups...
# designated as people_per_group...
# and stores them as new arrays within...
# the larger array "student_groups"
if student_groups.last.size < people_per_group
# Checks if the final group of students is actually the...
# remainder (i.e. less than the people_per_group value).
extra_students = student_groups.last
# Sets the last group in the student_groups array as new variable.
student_groups.pop
# Deletes last group of students from student_groups array.
last_group = student_groups.last
# Sets the new last group in student_groups array as new variable.
student_groups.pop
# Deletes last group of students from student_groups array.
new_last_group = last_group + extra_students
# Merges the two last groups we popped from student_groups...
# array into a new array, new_last_group
student_groups.push new_last_group
# Pushes new array new_last_group into student_groups array.
end
student_groups.each do |x|
a_nickname = allnicknames.sample
# Generates random group name.
puts "Group #{a_nickname} is: #{x}"
# Prints group name and group content.
allnicknames.delete(a_nickname)
# Deletes used group name from allnicknames array...
# to prevent redundancies.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment