In: Computer Science
This is an exercise on basic python grammar.
Instructions
Assume there are 10 teams in a competition. Their names are "team1", "team2" .. "team10".
Write a function to generate their order of performance randomly.
Submit your code in Jupyter notebook format (.ipynb) on Canvas
Hint
Note: You may certainly use another data structure or another algorithm without following the above suggested steps.
Solution:
Look at the code and comments for better understanding............
Screenshot of the code and output in Jupyter Notebook:
Code to copy:
import random
def order(teams):
new_list = []
#find the length of the list
l = len(teams)
#loop untill the length becomes zero
while l!=0:
#generate a random index in the range 0 to l-1
index = random.randint(0,l-1)
#add the element at index to the new list
new_list.append(teams[index])
#remove the element at index from old list
teams.pop(index)
#decrement the length
l-=1
#return the new list
return new_list
teams = ['team1','team2','team3','team4','team5','team6','team7','team8','team9','team10']
print(order(teams))
I hope this would help...................:-))