BRIAN: Let's take a look at how you might
have gone about solving World Cup.
In this problem, your task was to write a Python
program to run multiple simulations of some kind of a sports tournament.
And let's see how you would have done that.
Inside of the main function, the first thing you needed to do
was to read in all of the teams into memory,
because the teams were stored inside of a CSV file that
had the name of each team, well as a rating for each team representing
how good that particular team is.
That CSV file was specified as a command line argument, which in Python, you
can access using sys dot argv, where sys dot argv 1, in this case,
is going to be the name of the file.
So we'll store that inside of a variable called filename.
Next we're going to open that filename, opening that filename
and calling the file f.
And now we want to read that file as a CSV file.
To do so, we can use the CSV module.
And in particular, using something called
CSV dot DictReader, which will let us read a file, f, one row at a time,
treating each row of the CSV file as a dictionary,
with keys corresponding to the names of each of the columns,
and values corresponding to the values actually found inside that row of data
in the CSV file.
So once we have this DictReader, which in this case,
I'm just calling reader, let's loop over all of the data
that that reader is going to give us.
For team in that reader, each time, we're
going to get one dictionary representing that team that we
want to add to our list of teams.
But remember, when we read data from a CSV file, the data, by default,
is all going to be strings, just textual data.
But the rating is a number.
And to be able to do math with that number
to calculate probabilities and the likelihood
that one team wins over another team, we need
to turn those ratings into integers.
So what we're going to do here is say team square bracket rating--
and recall that you can use that square bracket
notation to access the value for a particular key inside of a dictionary--
is going to be updated to be equal to team square bracket rating casted
into an integer.
So we're going to take that value, turn it into an integer,
and store that value inside of the dictionary instead.
Once we've done that, we can add this new team to the list of teams
by using this append method, which is a function
we can use on an existing list, which just takes a value
and adds it to the end of the list.
The effect of this will be that we'll have read all of these teams
into memory.
And using those teams, we now want to be able to simulate tournaments.
So how might we gone about simulating tournaments.
Well, let's look at the simulate tournament
function down near the bottom of our file.
To simulate a tournament, we need to take a list of teams
and repeatedly run rounds on that list of teams,
until we're down to just a single team.
And to do so, we could have used the simulate round
function that accepts a list of teams and gives us back
a list of all of the winners in that round.
So we want to keep simulating rounds as long as we still haven't yet
gotten down to one team.
So as long as len teams is greater than 1-- here we're using a while loop
and taking advantage of the len function in Python,
which gets us the length of some sequence,
for example, the number of items in a list.
As long as there is more than one team, then
there's another round that we need to simulate.
So we'll go ahead and simulate a round with those teams and set
that to be the new value of this variable called teams.
This is going to repeatedly simulate rounds, and then update teams to just
be the list of all of the winners.
So by the end of it, teams is a list that has one team in it,
the winning team of the tournament.
And if I have a list with only one thing in it,
I can access that first and only element as teams
that square bracket 0 to get the first and, in this case,
only element in the list.
And then because each team is a dictionary
and I want to access the name of the team,
I'm going to say square bracket team to just access
the value for the team column of that CSV file
or the team key inside of this dictionary.
I'm then going to use that simulate tournament function
inside of my main function to keep track of how many times
each team has won a tournament.
I'm going to loop N times, where, by default, N is going to be 1,000.
And I'm going to simulate a tournament with those teams,
and then save the result in winner.
And now, I need to keep track of how many times
each team has won a tournament.
If the winner is already in my dictionary of counts,
then I'm going to increase their number of wins by 1.
If they've already won five games, for example,
I'm going to increase their win count up to 6
to now keep track of this additional tournament that they've won.
But otherwise, if they're not already in my dictionary of counts,
that means that this team hasn't won any tournaments before.
This is the first tournament they've won, according to our simulations.
And so we're going to set counts winner equal to 1 to mean that now they've won
one tournament.
And so at that point, counts by the end of this loop
will have counted up the number of times that every team wins
different tournaments.
And then we can use that data to figure out
the approximate probability that any team is
going to win the entire tournament.
My name is Brian.
And this was World Cup.