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))
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
could you do it in one line of code and using regex please