Programming > QUESTIONS & ANSWERS > DATA MISC Homework 4 | University of California, Berkeley (All)
hw04 September 19, 2018 1 Homework 4: Functions, Histograms, and Groups Reading: • Visualizing Numerical Distributions • Functions and Tables Please complete this notebook by filling in the ... cells provided. Before you begin, execute the following cell to load the provided tests. Each time you start your server, you will need to execute this cell again to load the tests. Homework 4 is due Thursday, 9/20 at 11:59pm. Start early so that you can come to office hours if you’re stuck. Check the website for the office hours schedule. You will receive an early submission bonus point if you turn in your final submission by Wednesday, 9/19 at 11:59pm. Late work will not be accepted as per the policies of this course. Throughout this homework and all future ones, please be sure to not re-assign variables throughout the notebook! For example, if you use max_temperature in your answer to one question, do not reassign it later on. Moreover, please be sure to only put your written answers in the provided cells. In [1]: # Don't change this cell; just run it. import numpy as np from datascience import * # These lines do some fancy plotting magic.\n", import matplotlib %matplotlib inline import matplotlib.pyplot as plots plots.style.use('fivethirtyeight') from client.api.notebook import Notebook ok = Notebook('hw04.ok') _ = ok.auth(inline=True) ===================================================================== Assignment: Homework 4: Functions, Histograms, and Groups OK, version v1.12.5 ===================================================================== 1Open the following URL: https://okpy.org/client/login/ After logging in, copy the code from the web page and paste it into the box. Then press the "Enter" key on your keyboard. Paste your code here: 5POB28fuQPmPY3f7RRVqdWxlLNiE5r Successfully logged in as [email protected] 1.1 1. Working with Text using Functions The following table contains the words from four chapters of Charles Dickens’ A Tale of Two Cities. We’re going to compute some simple facts about each chapter. Since we’re performing the same computation on each chapter, it’s best to encapsulate each computational procedure in a function, and then call the function several times. Run the cell to get a table with one column. In [2]: # Just run this cell to load the data. tale_chapters = Table.read_table("tale.csv") tale_chapters Out[2]: Chapter text I. The Period It was the best of times, it was the wor ... II. The Mail It was the Dover road that lay, on a Frid ... III. The Night Shadows A wonderful fact to reflect upo ... IV. The Preparation When the mail got successfully to ... Question 1. Write a function called word_count that takes a single argument, the text of a single chapter, and returns the number of words in that chapter. Assume that words are separated from each other by spaces. Hint: Try the string method split and the function len. 2In [3]: def word_count(table): return len(table.split(' ')) word_count(tale_chapters.column("Chapter text").item(0)) Out[3]: 911 In [4]: _ = ok.grade('q1_1') ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 2. Create an array called chapter_lengths which contains the length of each chapter in tale_chapters. Hint: Consider using apply along with the function you have defined in the previous question. In [7]: chapter_lengths = tale_chapters.apply(word_count, 'Chapter text') chapter_lengths Out[7]: array([ 911, 1827, 1468, 3994]) In [8]: _ = ok.grade('q1_2') ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 3. Write a function called character_count. It should take a string as its argument and return the number of characters in that string that aren’t spaces (" "), periods ("."), exclamation marks ("!"), or question marks ("?"). Remember that tale_chapters is a table, and that the function takes in only the text of one chapter as input. Hint: Try using the string method replace several times to remove the characters we don’t want to count. 3In [12]: def character_count(x): return len(x.replace(' ', '').replace('.', '').replace('!', '').replace('?', '')) In [13]: _ = ok.grade('q1_3') ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 4. Write a function called chapter_number. It should take a single argument, the text of a chapter from our dataset, and return the number of that chapter, as a Roman numeral. (For example, it should return the string "I" for the first chapter and "II" for the second.) If the argument doesn’t have a chapter number in the same place as the chapters in our dataset, chapter_number can return whatever you like. To help you with this, we’ve included a function called text_before. Its documentation describes what it does. In [22]: def text_before(full_text, pattern): """Finds all the text that occurs in full_text before the specified pattern. Parameters ---------- full_text : str The text we want to search within. pattern : str The thing we want to search for. Returns ------- str All the text that occurs in full_text before pattern. If pattern doesn't appear anywhere, all of full_text is returned. Examples -------- >>> text_before("The rain in Spain falls mainly on the plain.", "Spain") 'The rain in ' >>> text_before("The rain in Spain falls mainly on the plain.", "ain") 'The r' >>> text_before("The rain in Spain falls mainly on the plain.", "Portugal") 'The rain in Spain falls mainly on the plain.' [Show More]
Last updated: 1 year ago
Preview 3 out of 12 pages
Loading document previews ...
Buy this document to get the full access instantly
Instant Download Access after purchase
Buy NowInstant download
We Accept:
Can't find what you want? Try our AI powered Search
Connected school, study & course
About the document
Uploaded On
Oct 01, 2022
Number of pages
12
Written in
All
This document has been written for:
Uploaded
Oct 01, 2022
Downloads
0
Views
121
Scholarfriends.com Online Platform by Browsegrades Inc. 651N South Broad St, Middletown DE. United States.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Scholarfriends · High quality services·