Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Class 5\n",
- "\n",
- "# Review"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1. Let's do it together!"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "What is wrong with the following statement? (there can be multiple things) How would you fix it to output a list with all lowercase words?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "mylist = [Cat, Elephant, Eggs, Georgaphy, 1021]\n",
- "mylist.lower()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Take the following string, parse out the band names, and then print them line by line. (You can use any method shown in class for looping through band names)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "lineup = \"Kendrick Lamar * Kygo * Mumford & Sons * Liam Gallagher * Bastille * Lykke Li * Parov Stelar * The Kooks * MØ * Kaleo* Zara Larsson * Gogol Bordello * Clean Bandit * Blossoms * Milky Chance * Wolf Alice * Little Dragon * Cigarettes After Sex * Goo Goo Dolls * Fink * Seasick Steve * Nothing But Thieves * Slaves * Everything Everything * Lewis Capaldi * Lemaitre * La Femme * Joe Goddard live * Above & Beyond * Ummet Ozcan * Sam Feldt live * Jay Hardway * The Him * Alle Farben * WhoMadeWho * Shame * Perturbator\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 2. Practicing dictionaries"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "1) Take a number N and make a dictionary that contains all elements smaller than that number as keys and N^2 as the values associated with each N. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "2) Take a number N and make a dictionary that contains all elements smaller than that number as keys, and as values make lists contaning N^2, N^3, and N/2. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "3) Take a number N and make a dictionary that contains all elements smaller than that number as keys and N^2 as the values associated with each N **if** the number is even. If it is odd, make the value 1 in every case. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3. Reading / Writing files\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "1) Create a file called \"numbers_0,30.txt\" in which you list the numbers from 0 to 30 with commas inbetween the numbers. (All in one line) "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "2) Create a file called \"numbers_30_0.txt\" in which you list numbers 30 to 0 and each number is in a new line. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "3) Read in file \"numbers_0,30.txt\" and make a dictionary out of it according to the exercise in 2./ 1). \n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "4) Read in file numbers_30_0.txt and make a dctionary out of it according to the exercise in 2./ 2). "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "5) Read in file \"zip_codes.txt\" and for each line, if the line starts with a number print the zip code and the rest of the line next to each other. If the line does not start with a number, print \"this is not an address\". "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "6) Read in file \"zip_codes.txt\" and if the line starts with a number, make that the key of a dictionary and set the value to be the full line. Every line will contain an invisible new line character, make sure to strip it off before you set the value to be the line. (using the strip() function) "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4. Splitting"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Iterate through the `lineup` list from the beginning of the notebook and print the name of each band that starts with the letters S or L. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "List all animals from the file `animals.txt`. Create a new file called `animals-sorted.txt` that lists the same animals alphabetically. Remove duplicates. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 5. Listing files"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Print all filenames that end with `.txt` in the `datafiles` dictionary."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 6. And now let's put together everything we did up to here.\n",
- "\n",
- "Take all files from the directory `datafiles`, if they end with `.txt`, read their content. Keep only the lines that begin with a number. Ultimately, we want to create a dictionary that contains all these numbers as keys and the rest of these lines as values. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Bonus excercise, splitting and math"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The spreadsheet we will work with consists of rows of apparently-random numbers, and in this excercise we will calculate the spreadsheet's checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.\n",
- "\n",
- "For example, given the following spreadsheet:\n",
- "\n",
- "5 1 9 5 <br>\n",
- "7 5 3 <br>\n",
- "2 4 6 8 <br>\n",
- "\n",
- "The first row's largest and smallest values are 9 and 1, and their difference is 8.\n",
- "The second row's largest and smallest values are 7 and 3, and their difference is 4.\n",
- "The third row's difference is 6.\n",
- "In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.\n",
- "\n",
- "What is the checksum for the spreadsheet given by the file \"extra_input.txt\"?"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.0"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment