Open In Colab

Before you turn this problem in, make sure everything runs as expected. First, restart the kernel (in the menubar, select Kernel \(\rightarrow\) Restart) and then run all cells (in the menubar, select Cell \(\rightarrow\) Run All).

Make sure you fill in any place that says YOUR CODE HERE or “YOUR ANSWER HERE”, as well as your name and collaborators below:

NAME = ""
COLLABORATORS = ""

Tutorial 1: Introduction to Python#

In this tutorial, we will cover the basics of Python programming language. We will cover the following topics:

  • lists

  • dictionaries

  • for loops

  • functions

Suppose that we have some data about YouTube videos. For instance, we have the following data:

  1. video_id: the id of the video

  2. title: the title of the video

  3. view_count: the number of times the video was viewed

  4. like_count: the number of times the video was liked

  5. comment_count: the number of comments on the video

  6. duration: the duration of the video

One way to store this information is as a list that contains a list of the 6 elements above. So, it is a list of lists where each inner list contains 6 elements, the first is the video_id, the second is the title, and so on.

data = [['DE1-cD3pTkA', 'Walker Texas Ranger clip LIVE', 224, 1, 0, 'PT32S'],
 ['XZqSz_X-j8Y', 'Egg mcmuffin of a crappy commercial', 1919, 7, 3, 'PT44S'],
 ['vzpD6OogahQ', 'Potter Puppet Pals: School Is For Losers', 14958231, 87806, 11321, 'PT33S'],
 ['_OpzEHBDwQE', 'Very Funny Commercial  Think Beyond Your Ear (UNRELEASED)', 61696,  179, 2, 'PT38S'],
 ['yzGWOpop6i8', 'Happy Valentines Day', 15852, 315, 77, 'PT1M14S'],
 ['fCmkdzl6b84', 'Hermione and Scabior - E.T.', 805, 14, 1, 'PT42S'],
 ['DOD38Y72zyk', 'Minecraft: TNT Rocket', 2174, 16, 3, 'PT1M9S'],
 ['UYQqu53mFng',
  'Gears Of War 3 - Official Trailer (Cenizas a las cenizas) (Ashes to Ashes)',
51,0,0,'PT1M15S'],
 ['c1NaGio2Geo', 'Caller X episode 3', 13716, 525, 130, 'PT1M18S'],
 ['BpKQEoN5M2g', 'COOLEST GUY ON THE RIDE', 22065, 450, 114, 'PT1M13S']]

1. Write a function that accepts a list of the above 6 pieces of information about the videos and return the average number of views of the videos.#

def avg_view_count(data):
    """
    This function takes a list of lists as input. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. The function should return the average view count of the videos in the input list.
    """

    # YOUR CODE HERE
    raise NotImplementedError()
"""Check that the output is correct"""
assert avg_view_count(data) == 1507673.3
assert avg_view_count(data[0:5]) == 3007584.4
assert avg_view_count([data[0]]) == 224.0
assert avg_view_count([]) == 0
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[4], line 2
      1 """Check that the output is correct"""
----> 2 assert avg_view_count(data) == 1507673.3
      3 assert avg_view_count(data[0:5]) == 3007584.4
      4 assert avg_view_count([data[0]]) == 224.0

Cell In[3], line 7, in avg_view_count(data)
      2 """
      3 This function takes a list of lists as input. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. The function should return the average view count of the videos in the input list.
      4 """
      6 # YOUR CODE HERE
----> 7 raise NotImplementedError()

NotImplementedError: 

2. Write a function that accepts a list of the above 6 pieces of information about the videos and return the average number of likes of the videos.#

def avg_num_likes(data):
    """
    This function takes a list of lists as input. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. 
    
    The function should return the average number of likes of the videos in the input list.
    """
    # YOUR CODE HERE
    raise NotImplementedError()
"""Check that the output is correct"""
assert avg_num_likes(data) == 8931.3
assert avg_num_likes(data[0:5]) == 17661.6
assert avg_num_likes([data[0]]) == data[0][3]
assert avg_num_likes([]) == 0

3. Write a function that accepts a list of the above 6 pieces of information about the videos and returns a dictionary with the keys the id of the video and the value the number of likes.#

def lists_to_dict(data):
    """
    This function takes a list of lists as input. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. 
    
    The function should return a dictionary where the keys are the video_id and the values are the number of likes.
    """
    # YOUR CODE HERE
    raise NotImplementedError()
"""check that the output is correct"""
assert lists_to_dict(data) == {'DE1-cD3pTkA': 1,
                               'XZqSz_X-j8Y': 7,
                               'vzpD6OogahQ': 87806,
                               '_OpzEHBDwQE': 179,
                               'yzGWOpop6i8': 315,
                               'fCmkdzl6b84': 14,
                               'DOD38Y72zyk': 16,
                               'UYQqu53mFng': 0,
                               'c1NaGio2Geo': 525,
                               'BpKQEoN5M2g': 450}
assert lists_to_dict(data[0:5]) == {'DE1-cD3pTkA': 1,
                                    'XZqSz_X-j8Y': 7,
                                    'vzpD6OogahQ': 87806,
                                    '_OpzEHBDwQE': 179,
                                    'yzGWOpop6i8': 315}
assert lists_to_dict([]) == {}

4. Write a function that accepts a list of the above 6 pieces of information about the videos and returns the title with the most likes.#

def find_most_popular(data):
    """
    This function takes a list of lists as input. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. 
    
    The function should return the title of the video with the most views.
    """
    # YOUR CODE HERE
    raise NotImplementedError()
assert find_most_popular(data) == 'Potter Puppet Pals: School Is For Losers'
assert find_most_popular(data[0:5]) == 'Potter Puppet Pals: School Is For Losers'
assert find_most_popular(data[5::]) == 'COOLEST GUY ON THE RIDE'

5. Write a function that accepts a list of the above 6 pieces of information about the videos and returns the title with the most views, using the sorted function.#

def find_most_popular_using_sorted(data):
    """
    This function takes a list of lists as input. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. The function should return the title of the video with the highest number of views.
    """
    # YOUR CODE HERE
    raise NotImplementedError()
assert find_most_popular_using_sorted(data) == 'Potter Puppet Pals: School Is For Losers'
assert find_most_popular_using_sorted(data[0:5]) == 'Potter Puppet Pals: School Is For Losers'
assert find_most_popular_using_sorted(data[5::]) == 'COOLEST GUY ON THE RIDE'

6. Write a function that accepts a list of the above 6 pieces of information about the videos and a video id and returns the title of the video and the number of likes.#

def get_title_num_likes(data, video_id):
    """
    This function takes a list of lists as input and a video_id. Each list contains the following elements: video_id, title, view_count, like_count, comment_count, duration. 
    
    The function should return the title of the video and the number of likes.
    """
     
    # YOUR CODE HERE
    raise NotImplementedError()
"""Check that the output is correct"""

assert get_title_num_likes(data, '_OpzEHBDwQE') == ('Very Funny Commercial  Think Beyond Your Ear (UNRELEASED)', 179)
assert get_title_num_likes(data, 'yzGWOpop6i8') == ('Happy Valentines Day', 315)
assert get_title_num_likes(data, 'test') is None