View the step-by-step solution to:

Question

Problem 1 and 2, description below

Screen Shot 2019-10-01 at</h1><p class='abPRemoveTitle'> 3.14.42 PM.pngScreen Shot 2019-10-01 at 3.15.55 PM.pngScreen Shot 2019-10-01 at 3.16.23 PM.png


utils.py =

Screen Shot 2019-10-01 at 3.17.40 PM.png

Screen Shot 2019-10-01 at 3.14.42 PM.png

def choose (paragraphs, select, k) :
&quot;&quot; &quot;Return the Kth paragraph from PARAGRAPHS for which SELECT called on
the
paragraph returns true. If there are fewer than K such paragraphs,
return
the empty string.
# BEGIN PROBLEM 1
&quot; *** YOUR CODE HERE *&gt;*&gt;* &quot;
# END PROBLEM 1
def about (topic):
&quot;&quot;&quot;Return a select function that returns whether a paragraph contains
one
of the words in TOPIC.
&gt; &gt;&gt; about_dogs = about ([ 'dog' , 'dogs' , 'pup' , 'puppy' ])
&gt; &gt;&gt; choose ([ 'Cute Dog! ' , 'That is a cat. ', 'Nice pup! '], about_dogs, 0)
'Cute Dog! '
&gt; &gt;&gt; choose ([ 'Cute Dog! ' , 'That is a cat. ', 'Nice pup. '], about_dogs, 1)
'Nice pup. '
assert all([lower (x) == x for x in topic]), 'topics should be
lowercase. '
# BEGIN PROBLEM 2
&quot;** YOUR CODE HERE *=&gt;* &quot;
# END PROBLEM 2

Screen Shot 2019-10-01 at 3.15.55 PM.png

Problem1 (1 pt) Implement choose , which selects which paragraph the user will type. It takes a list of
paragraphs (strings), a select function that returns True for paragraphs that can be selected,
and a non-negative index k . The choose function return's the k th paragraph for which select
returns True . If no such paragraph exists (because k is too large), then choose returns the
empty string.

Screen Shot 2019-10-01 at 3.16.23 PM.png

Problem 2 (2 pt) Implement about , which takes a list of topic words. It returns a function that can be passed to
choose as the select argument. The returned function takes a paragraph and returns whether
that paragraph contains any of the words in topic. To make this comparison accurately, you will need to ignore case (that is, assume that uppercase
and lowercase letters don't change what word it is) and punctuation. Assume that all words in the topic list are already lowercased and do not contain punctuation. Hint: You may use the string utiltiy functions in utils.py.

Screen Shot 2019-10-01 at 3.17.40 PM.png

&quot;Utility functions for file and string manipulation&quot;
F W N
import string
6
def lines_from_file (path) :
&quot;&quot; &quot;Return a list of strings, one for each line in a file. &quot;um
8
with open (path, 'r' ) as f:
return [line. strip( ) for line in f. readlines() ]
10
11
12 punctuation_remover = str . maketrans ( '', '', string . punctuation)
13
14
15
def remove_punctuation(s) :
16
&quot;&quot; &quot;Return a string with the same contents as s, but with punctuation removed.
17
18
&gt; &gt;&gt; remove_punctuation ( &quot;It's a lovely day, don't you think?&quot;)
19
'Its a lovely day dont you think'
20
21
return s. strip () . translate (punctuation_remover)
22
23
24
def lower (s) :
25
&quot;&quot; &quot;Return a lowercased version of s. &quot;&quot;&quot;
26
return s. lower( )
27
28
29
def split(s) :
30
&quot;&quot; &quot;Return a list of words contained in s, which are sequences of characters
31
separated by whitespace (spaces, tabs, etc. ).
32
33
&gt; &gt;&gt; split(&quot;It's a lovely day, don't you think?&quot;)
34
[ &quot;It's&quot;, 'a', 'lovely', 'day, ', &quot;don't&quot;, 'you', 'think?' ]
35
36
return s. split()
37

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes