{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Make Random Student Groups" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This page demonstrates how to make a Python function called `make_random_groups()` that will take a list of student names and randomly create a desired number of groups from the list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import random module" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make list of students" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a list of student names. Yes, in this scenario, I'm teaching the NBA's all-star basketball players." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "students = ['LeBron James',\n", "'Giannis Antetokounmpo', \n", "'Kevin Durant',\n", "'Steph Curry',\n", "'Kyrie Irving',\n", "'Joel Embiid', \n", "'Kawhi Leonard', \n", "'Paul George', \n", "'James Harden', \n", "'Kemba Walker', \n", "'Khris Middleton', \n", "'Anthony Davis', \n", "'Nikola Jokić', \n", "'Klay Thompson', \n", "'Ben Simmons', \n", "'Damian Lillard', \n", "'Blake Griffin', \n", "'Russell Westbrook', \n", "'D\\'Angelo Russell', \n", "'LaMarcus Aldridge', \n", "'Nikola Vučević', \n", "'Karl-Anthony Towns', \n", "'Kyle Lowry', \n", "'Bradley Beal', \n", "'Dwyane Wade', \n", "'Dirk Nowitzki']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create `make_random_groups()` function" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def make_random_groups(students, number_of_groups):\n", " \n", " #Shuffle list of students\n", " random.shuffle(students)\n", " \n", " #Create groups\n", " all_groups = []\n", " for index in range(number_of_groups):\n", " group = students[index::number_of_groups]\n", " all_groups.append(group)\n", " \n", " #Format and display groups\n", " for index, group in enumerate(all_groups):\n", " print(f\"✨Group {index+1}✨: {' / '.join(group)}\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use `make_random_groups()` function" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✨Group 1✨: Kemba Walker / Dwyane Wade / James Harden\n", "\n", "✨Group 2✨: Ben Simmons / D'Angelo Russell / Nikola Jokić\n", "\n", "✨Group 3✨: Steph Curry / LaMarcus Aldridge / Kyrie Irving\n", "\n", "✨Group 4✨: Kawhi Leonard / Damian Lillard / Bradley Beal\n", "\n", "✨Group 5✨: Dirk Nowitzki / Kyle Lowry / Joel Embiid\n", "\n", "✨Group 6✨: Khris Middleton / Kevin Durant / Karl-Anthony Towns\n", "\n", "✨Group 7✨: Klay Thompson / LeBron James\n", "\n", "✨Group 8✨: Blake Griffin / Anthony Davis\n", "\n", "✨Group 9✨: Nikola Vučević / Giannis Antetokounmpo\n", "\n", "✨Group 10✨: Russell Westbrook / Paul George\n", "\n" ] } ], "source": [ "make_random_groups(students, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make Random Student Groups Explained in More Detail" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Shuffle the order of the students" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.shuffle(students)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extract a certain number of groups from the list of randomly shuffled students" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "all_groups = []\n", " \n", " for index in range(number_of_groups):\n", " \n", " group = students[index::number_of_groups]\n", " \n", " all_groups.append(group)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `all_groups = []`\n", " - creates an empty list\n", "- `range(number_of_groups)`\n", " - creates a sequence of numbers from 0 to the desired number of groups\n", "- `index`\n", " - represents each number in that sequence\n", "- `students[index::number_of_groups]`\n", " - extracts a group of students from the list by selecting the student at whatever position in the list `index` represents, then jumping forward by `number_of_groups` spots to select a student at that position in the list, etc\n", "- `all_groups.append(group)`\n", " - adds each group to a master list\n", "\n", "Let's say we have 15 students and want 5 random groups. For each `index` aka number in `range(5)` — (0,1,2,3,4) — we make a group by selecting `students[index::5]`.\n", "\n", "**Group 1** \n", "`students[0::5]` \n", "We select the 0th student in the randomly shuffled list, then jump by 5 to select the 5th person in the list, and then jump by 5 to take the 10th person in the list.\n", "\n", "**Group 2** \n", "`students[1::5]` \n", "We select the 1st student in the randomly shuffled list, then jump by 5 to select the 6th person in the list, and then jump by 5 to take the 11th person in the list.\n", " \n", "**Group 3** \n", "`students[2::5]` \n", "We select the 2nd student in the randomly shuffled list, then jump by 5 to select the 7th person in the list, and then jump by 5 to take the 12th person in the list.\n", "\n", "**Group 4** \n", "`students[3::5]` \n", "We select the 3rd student in the randomly shuffled list, then jump by 5 to select the 8th person in the list, and then jump by 5 to take the 13th person in the list.\n", "\n", "**Group 5** \n", "`students[4::5]`\n", "We select the 4th student in the randomly shuffled list, then jump by 5 to select the 9th person in the list, and then jump by 5 to take the 14th person in the list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Format and display each group" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for index, group in enumerate(all_groups):\n", " print(f\"✨Group {index+1}✨: {' / '.join(group)}\\n\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }