Question
PYTHON REGULAR EXPRESSIONS (2A and 2B are related to each other, please answer ALL parts) 2a) ** ONLY ONE LINE EXPRESSION NEEDED(Start with ^ and
PYTHON REGULAR EXPRESSIONS (2A and 2B are related to each other, please answer ALL parts)
2a) **ONLY ONE LINE EXPRESSION NEEDED(Start with ^ and end with $!! Write a regular expression pattern that describes a single page specification: the integers you specify here must start with a non-0 digit. Here are examples that should match/should not match a single page specification:
Match 3 and 5-8 and 12:3 and 5-8 and 6:4 and 10-20/3 and 10:10/3 NotMatch 03 and 5-08 and 3 4 and 3 to 8 and 4/3and 4-:3 and 4-6:3
MORE INFO IF NEEDED: When we print computer documents, there is a common form used to specify the page numbers to print. Generally, commas separate page specifications, where each page specification is a single page number, or a contiguous range of pages. In a page specification, a dash or a colon can separate two numbers: for a dash, these numbers specify the first and last pages in a range to print (inclusive); for a colon, they specify the first page and how many subsequent pages to print (so 10:3 means 3 pages starting at 10: 10, 11, and 12). Finally, if either of these forms is used, we can optionally write a slash followed by a number (call it n), which means for the page specification, print every nth page in the range (so 10-20/3 means 10 through 20 , but only every 3rd page: 10, 13, 16, and 19). Write a regular expression that ensures group 1 is the first page; group 2 is a dash or colon (or None if not present); group 3 is the number after the dash or colon (or None if not present); group 4 is the number after the slash (or None if not present).
2b) PYTHON 3 LANGUAGE**** Define a function named pages that takes one str as an argument, representing a list of page specifications separated by commas, and a bool value controlling whether the pages are unique: printed only once; it returns a list, sorted in ascending order, of all the pages (ints) in the page specifications. This functionmust use the regular expression pattern you wrote for part 2a and extract (using the group function) information to create the numbers in the page specification. For example, if we called the function aspages('5-8,10:2,3,7:10/3,unique=True) it would return the list [3,5,6,7,8,10,11,13,16]; if we calledpages('5-8,10:2,3,7:10/3,unique=False) it would return the list [3,5,6,7,7,8,10,10,11,13,16].
Here are some more examples of arguments to pages and their meanings:
Raise an AssertionError exception (using Pythons assert statement) if any page specifications fails to match the regular expression, or if any dash separator separates a higher first number from a lower second one: e.g., 10-8 raises/prints the exception AssertionError: pages: in page specification 10-8, 10 > 8.The page specification 8-8 is OK : it means only page 8.
Hint: My function body is 15 lines of code (this number is not a requirement). After using split to separate thestr argument to get a list of page specifications, use the re.match function (using your regular expression solution from Problem #2a) on each, then call the group function to extract the required information, and finally process it. The range class is very helpful in determining the pages in each page specification.
13 3,5-8,12:3'pages [3,5,6,7,8,12,13,14] (3, 5 to 8, 12 and 2 more pages) 6-10,4-8 6-10/2,4-10/2' pages (4,6,8,10] page 3] pages [4.5,6,6,7,7,8,8,9,101 (pages are ordered; assume unique is False) (pages are ordered; assume unique is True) 13 3,5-8,12:3'pages [3,5,6,7,8,12,13,14] (3, 5 to 8, 12 and 2 more pages) 6-10,4-8 6-10/2,4-10/2' pages (4,6,8,10] page 3] pages [4.5,6,6,7,7,8,8,9,101 (pages are ordered; assume unique is False) (pages are ordered; assume unique is True)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started