python: def shakespeare_position(role, section): """
python:
def shakespeare_position(role, section):
"""
Question 2 - Regex
You are reading a Shakespeare play with your friends (as one frequently does) and are given a role.
You want to know what line immediately precedes YOUR first line in a given section so that you are ready to go
when it is your turn. Return this line as a string, excluding the character's name.
Lines will always begin with the character's name followed by a ':' and end in a "." or a "?"
Each line is separated by a single space.
THIS MUST BE DONE IN ONE LINE.
""
Args:
role (str)
section (str)
Returns:
str
section_1 = 'Benvolio: By my head, here come the Capulets. Mercutio: By my heel, I care not. ' +
'Tybalt: Gentlemen, good den - a word with one of you. Mercutio: And but one word with one of us?'
>>> shakespeare_position('Tybalt', section_1)
'By my heel, I care not.'
>>> shakespeare_position('Mercutio', section_1)
'By my head, here come the Capulets.'
"""
# section_1 = 'Benvolio: By my head, here come the Capulets. Mercutio: By my heel, I care not. ' + \
# 'Tybalt: Gentlemen, good den: a word with one of you. Mercutio: And but one word with one of us?'
# pprint(shakespeare_position('Tybalt', section_1))
# pprint(shakespeare_position('Mercutio', section_1))
![](/static/compass_v2/shared-icons/check-mark.png)
Program to solve above problem using Python.
Let us Assume character name is followed by :
and Each line is end with '.'
Let us consider the section of Shakespeare playlet
Section: 'Benvolio: By my head, here come the Capulets. Mercutio: By my heel, I care not. ' +
'Tybalt: Gentlemen, good den - a word with one of you. Mercutio: And but one word with one of us?'
Roles: are Benvolio, Mercutio, Tybalt
First line by Benvolio : By my head, here come the Capulets.
Second line by Mercutio: By my heel, I care not.
Third line by Tybalt: Gentlemen, good den - a word with one of you.
Fourth line by Mercutio: And but one word with one of us?
We give Section and Roles are input to the function shakespeare_position()
You want to know what line immediately precedes, so that given role should get ready for his dialogue.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
could you do it in one line of code and using regex please
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)