{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "nested_data_challenge.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "ZooiSRuSNqCe" }, "source": [ "# Written Part\n", "What data structure is top_songs?\n", "ANSWER HERE\n", "\n", "What data structure is each song in top_songs?\n", "ANSWER HERE\n", "\n", "What are some differences between lists and dictionaries?\n" ] }, { "cell_type": "code", "metadata": { "id": "8ol3b6hvNu5h" }, "source": [ "# uncomment the line below after you upload top_songs_2018.py to the folder\n", "# from top_songs_2018 import *\n", "import json \n", "import time" ], "execution_count": 15, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "-p0XZehjNyzw" }, "source": [ "# Exercise 1\n", "Get the number of songs each artists has in the top songs list\n", "```\n", "Implemention:\n", "create an empty dictionary called artists\n", "loop through top songs\n", "for each song artist, check if the artist is already in artists\n", "if the artist is NOT in artists, add artist and set the value to 1\n", "if the artist is ALREADY in artists, update the value by incrementing it by 1\n", "```" ] }, { "cell_type": "code", "metadata": { "id": "vpU3-11KN1FD" }, "source": [ "# code goes here" ], "execution_count": 16, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "v9cWNsUoN3iO" }, "source": [ "# Exercise 1a\n", "Check you did it correctly by uncommenting the code below\n", "assert statements will give an `AssertionError` if the condition returns false\n", "if the condition returns true nothing will print out which means the tests pass" ] }, { "cell_type": "code", "metadata": { "id": "JpxjW747N7Ij" }, "source": [ "# assert(artists['Drake'] == 4)\n", "# assert(artists['David Guetta'] == 1)\n", "# assert(artists['Selena Gomez'] == 2)\n", "# assert(artists['Post Malone'] == 6)\n" ], "execution_count": 17, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "iidFDcy6OBU6" }, "source": [ "# Exercise 1b\n", "write an assert function that gives an AssertionError, then comment it out\n" ] }, { "cell_type": "code", "metadata": { "id": "qHxbzQgvODlM" }, "source": [ "# your assert statement that gives an error" ], "execution_count": 18, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "7SnjQG3_OFZq" }, "source": [ "# Exercise 2\n", "Compute the average number of songs each artist has in the top songs list and print it out" ] }, { "cell_type": "code", "metadata": { "id": "2EIPDh-gOJQj" }, "source": [ "# code goes here" ], "execution_count": 19, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "ClpYhylIOIYN" }, "source": [ "# Exercise 3\n", "Append all artists with more than 1 song in the top songs list in the list, top_artists" ] }, { "cell_type": "code", "metadata": { "id": "pc2zi3paOMRU" }, "source": [ "top_artists = []\n", "# your code goes here" ], "execution_count": 20, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "cRvgFYWIOSEE" }, "source": [ "uncommment the assert statement to check your work" ] }, { "cell_type": "code", "metadata": { "id": "aJF04bv1OTgA" }, "source": [ "# assert(len(top_artists) == 18)" ], "execution_count": 21, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "GQF4-W2wOWy5" }, "source": [ "# Exercise 4\n", "Of the artists in the top_artists list, find the song that is most danceable.\n", "Print out the danceability score, the name of the song, and the name of the artist" ] }, { "cell_type": "code", "metadata": { "id": "rtaYgLzWOXsA" }, "source": [ "max_dance = 0\n", "dance_name = ''\n", "dance_artist = ''\n", "# your code goes here" ], "execution_count": 22, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "u2TizqPMOdlU" }, "source": [ "# Exercise 4a written\n", "How could the music metadata be stored to optimize this search?\n", "ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": { "id": "X1OUJFkJOfUM" }, "source": [ "# Exercise 4b\n", "Find the song with the highest danceability from **all** of top_songs\n", "Print out the danceability score, the name of the song, and the name of the artist" ] }, { "cell_type": "markdown", "metadata": { "id": "nY1_2CJOOnmR" }, "source": [ "" ] }, { "cell_type": "code", "metadata": { "id": "8ezCj83kOiBv" }, "source": [ "max_dance = 0\n", "dance_name = ''\n", "dance_artist = ''\n", "# your code goes here" ], "execution_count": 23, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "oekzQhb3Px3B" }, "source": [ "# Exercise 4c Written\n", "All the songs the same from 4 and 4b? ANSWERE HERE" ] }, { "cell_type": "markdown", "metadata": { "id": "rbhHBPSpOiWT" }, "source": [ "# Exercise 5\n", "Print out the average length of a song in seconds" ] }, { "cell_type": "code", "metadata": { "id": "uYxCaGOxOkcQ" }, "source": [ "# your code goes here" ], "execution_count": 24, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "zgIHBwEtOqdS" }, "source": [ "# Exercise 6\n", "Create a new dictionary using top_songs such that each artist is a key\n", "and the value for each key (artist) is a list containing the metadata about each of their songs\n", "artists name should only appear once as the key of top_songs_dict " ] }, { "cell_type": "code", "metadata": { "id": "Q9eIOGwLOsfL" }, "source": [ "top_songs_dict = {}\n", "# your code goes here" ], "execution_count": 25, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "StuDGkF-OxBF" }, "source": [ "uncomment the assert statement to check your code" ] }, { "cell_type": "code", "metadata": { "id": "6Po3i-mROyl0" }, "source": [ "#assert(len(top_songs_dict['Drake']) == 4)" ], "execution_count": 26, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "VSobIQ8kO2S0" }, "source": [ "the json library has a function that can print out a dictionary in a human readable format. use it to write your results to a new file." ] }, { "cell_type": "code", "metadata": { "id": "zmjFYl80O7Lj" }, "source": [ "f = open(\"top_songs_2018_by_artist.txt\", 'w')\n", "f.write(json.dumps(top_songs_dict, indent = 4))\n", "f.close()" ], "execution_count": 27, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "snpXxwEDPDPG" }, "source": [ "# Exercise 6\n", "Now your data is organized so you can easily access songs by an artist\n", "\n", "Rewrite your code from exercise 4 using top_songs_dict.\n", "Of the artists in the top_artists list, find the song that is most danceable\n", "Print out the danceability score, the name of the song, and the name of the artist" ] }, { "cell_type": "code", "metadata": { "id": "DoSIMzeUQMSK" }, "source": [ "max_dance = 0\n", "dance_name = ''\n", "dance_artist = ''\n", "# your code goes here" ], "execution_count": 28, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "sW2Wr120QQYJ" }, "source": [ "Which code block runs quicker between exercise 4 and 6? Use the `time` library to verify your answer by measuring the time it takes your code to get the most danceable song from the top_artists." ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eGANJfE9QZjt", "outputId": "59b74711-8baf-48fa-c099-254cac04e821" }, "source": [ "# Example using time library imported at the top\n", "start = time.time_ns() # use nanoseconds because code runs very fast\n", "\n", "# code you want to measure\n", "for i in range(100): \n", " pass\n", "\n", "end = time.time_ns()\n", "print(end-start)" ], "execution_count": 29, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "54459\n" ] } ] } ] }