python代写 Assignment 1

Instructions

Assignment 1

  1. The homework is worth 15 marks and is due on 29th September, 12pm.
  2. Use colab notebooks to do the assignment and when you are done, submit

    the link to the file to this google form.

  3. Make sure that you have set the permission of the file as “can edit”.
  4. Ifyouhaveanyissues/doubts,writeanemailatshagun.sodhani@umontreal.ca

1. (3 marks) Iterating over ranges of integers is such a common need that Python provides a special function called range(· · ·) for that purpose

Change the code above to print out a triangle of asterisks, like this:

2. (3 marks) When we iterate through a dictionary using a for loop, we actually iterate over the keys:

Modify the code above to print just the keys associated to values that are greater than 1.
3. (3 marks) A while loop keeps iterating as long as a condition evaluates to True:

1

for i in range(10) : print ( i )


∗∗ ∗∗∗ ∗∗∗∗ ∗∗∗∗∗

d = { ”key1”:1 , ”key2”:2 , ”key3”:1 , ”key4”:3 ,

”key5”:1 , ”key6”:4 , ”key7”:2 } for k in d :

print(”key=”, k, ” value=”, d[k], sep=””)

count = 0
while count < 5 :

print(”count =” , count) count += 1

Use a while loop to print a triangle of astericks, like this:

4. (6 marks) We provide you with a file (named “olympics.csv”). It contains information about the medals won by different countries at the summer and winter Olympics games. Each line contains data about one country. The first line of the file lists all the headers (explained below). Each subsequent line contains data about one country.

  1. team name: Name of the team. This is typically the name of the country.
  2. summer games played: Number of summer games that this team played.
  3. summer games gold won: Number of gold medals won by this team in summer Olympics.
  4. summer games silver won: Number of silver medals won by this team in summer Olympics.
  5. summer games bronze won: Number of bronze medals won by this team in summer Olympics.
  6. summer games medals won: Number of total medals won by this team in summer Olympics.
  7. winter games played: Number of winter games that this team played.
  8. winter games gold won: Number of gold medals won by this team in win-

    ter Olympics.

  9. winter games silver won: Number of silver medals won by this team in winter Olympics.
  10. winter games bronze won: Number of bronze medals won by this team in winter Olympics.
  11. winter games medals won: Number of total medals won by this team in winter Olympics.
  12. total games played: Total number of games that this team played.
  13. total games gold won: Total number of gold medals won by this team in

    Olympics.

  14. total games silver won: Total number of silver medals won by this team in Olympics.

    2

∗ ∗∗∗ ∗∗∗∗∗ ∗∗∗∗∗∗∗

∗∗∗∗∗∗∗∗∗

  1. total games bronze won: Total number of bronze medals won by this team in Olympics.
  2. total games medals won: Total number of medals won by this team in Olympics.

Consider the second line in the file. We show an example of how to map different values in the line with relevant headers. We begin by splitting the line at commas and obtain a list. The first value of this list (Afghanistan(AFG)) gives the name of the team. The second value gives the number of summer Olympics that this team has participated in (13) and so on.

Read the input file into Python. Store each line in the file as a list so we would have one list per line. Then we would store all these lists in another list. So the data would be represented as a list of lists. Assign this list to a variable called as data. Make sure that you do not added the first header line into data.

Note that you must use Python code for answering following questions. Feel free to manually analyse the data to cross-check your answers. Save your answer in variables as specified in each part.

  1. Which country has won the maximum number of bronze medals in winter Olympics? Save the answer in the variable ans1.
  2. What is the median number of summer Olympic games? Save the answer in the variable ans2.
  3. How many countries have played in both summer and winter Olympics but have never won a gold medal. Save the answer in the variable ans3.

3