Question

Write a python function score_formatter() that takes one parameter, which is a formatted string that contains...

Write a python function score_formatter() that takes one parameter, which is a formatted string that contains information about the score received by a student in a particular subject. The function analyzes the string and returns a reformatted string for the score. You will need to use the function re.sub that is discussed in the lecture notes. If the string does not match the pattern, the function returns the string "error".

The input string is always given in the format specified below. When there is a space in the output, there is exactly one space; when there’s an identifier like (1) or (2), those are the parts that can be replaced (which means you should use regular expression syntax to identify them in the pattern).

Format:

STUDENT got SCORE in SUBJECT on    DATE
  (1)   got  (2)  in   (3)   on (4) (5) (6)
  • STUDENT (1): This part represents the name of the student. It will always be a string that starts with a letter.

  • SCORE (2): This part represents the score of the student. It will always be a non-negative decimal number.

  • SUBJECT (3): This part represents the subject. It will always be a string that starts with a letter.

  • DATE (4) (5) (6): These three parts all belong to the date. (4) represents the spelled-out month (e.g., June); (5) represents the two-digit day; and (6) represents the four-digit year. These three parts are separated by spaces.

For this part you can safely assume that valid months are entered, so simply use \w+ to match a month name.

What the Function Returns

As mentioned above, your program should take the input string and reformat it in a specific way. The return string has the format indicated below (where bold parts are the parts that are non-static and which you’ll need regular expressions to extract). Your function should return a string containing information about Subject, Score, Date, and Student each on a separate line. When this string is printed out, it should look something like this:

Subject: The name of the subject
Score: The score obtained by the studentDate: Day-Month-Year
Student: The student’s name

Note: Please follow the format and do not include excessive elements or spaces, since we will be expecting the string to be formatted exactly as specified.

Example #1:
Function argument: "Yupeng got 100 in CSE101 on August 11 2019"Returnvalue:"Subject: CSE101\nScore: 100\nDate: 11-August-2019\nStudent: Yupeng"Example #2:
Function argument: "Ravi got 90 in CSE101 on July 12 2018"
Return value: "Subject: CSE101\nScore: 90\nDate: 12-July-2018\nStudent: Ravi"Example #3:
Function argument: "Virat got 99 in Biology on July 02 2018"
Return value: "Subject: Biology\nScore: 99\nDate: 02-July-2018\nStudent: Virat"Example #4:
Function argument: "alena got .5 in physics on July 22 2019"
Return value: "error"
Example #5:
Function argument: "Sam got 75.5 in physics on July 22 2019"

Return value: "error"

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import re

def score_formatter(string):
    pattern = '(\w+) got (\d{1,3}) in (\w+) on (\w+) (\d{2}) (\d{4})'
    match = re.search(pattern,string)
    if match:
        student = match.group(1)
        score = match.group(2)
        subject = match.group(3)
        month = match.group(4)
        day = match.group(5)
        year = match.group(6)
        return "Subject: "+subject+"\n"+"Score:"+score+"\nDate: "\
               +day+"-"+month+"-"+year+"\nStudent: "+student
    else:
        return "error"

Add a comment
Know the answer?
Add Answer to:
Write a python function score_formatter() that takes one parameter, which is a formatted string that contains...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the...

    javascript-Write a function named "json_average" that takes a JSON formatted string as a parameter in the format of an array of objects where each object has keys "mass", "density", "temperature", and "velocity" and each key maps to a floating point number. This function should return the average "velocity" of all the objects in the array as a JSON string in the format {"velocity": }

  • Write a Python function makeDict() that takes a filename as a parameter. The file contains DNA...

    Write a Python function makeDict() that takes a filename as a parameter. The file contains DNA sequences in Fasta format, for example: >human ATACA >mouse AAAAAACT The function returns a dictionary of key:value pairs, where the key is the taxon name and the valus is the total number of 'A' and 'T' occurrences. For example, for if the file contains the data from the example above the function should return: {'human':4,'mouse':7}

  • Define a Python function named getResponse() that takes a string as its only argument and returns...

    Define a Python function named getResponse() that takes a string as its only argument and returns a new string. If the argument ends with a question mark and has an even number of characters, the function should return the string "Yes". If the argument ends with a question mark and contains an odd number of characters, the function should return the string "No". If the argument ends with an exclamation point, the function should return the string "Wow". Otherwise, the...

  • 6 Formatting getLocalPhone * Takes one string parameter (phone number) Returns a formatted local ...

    C++ Function name: getLocalPhone 6 Formatting getLocalPhone * Takes one string parameter (phone number) Returns a formatted local phone number string Country code +1 -"+12062812000" "(206) -281-2000" Country code +33 "+33140205990" "01 40 20 59 90" Country code +852 "+85223677065" "2367 7065" 6 Formatting getLocalPhone * Takes one string parameter (phone number) Returns a formatted local phone number string Country code +1 -"+12062812000" "(206) -281-2000" Country code +33 "+33140205990" "01 40 20 59 90" Country code +852 "+85223677065" "2367 7065"

  • Using C, Write a function reverse which takes a string as an argument, reverses the string...

    Using C, Write a function reverse which takes a string as an argument, reverses the string and returns the reversed string. Note; you should not return a string that you created inside the reverse function! For example: Test char str[]-"hello" printf("%s", reverse (str)); Result olleh

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • Convert date format in Python 3 US_to_EU: This function takes a string representing a US style...

    Convert date format in Python 3 US_to_EU: This function takes a string representing a US style date and returns a European style date string. US strings have the month first, European strings have the day first. US uses slashes as separators, Europe uses dots. Example calls: US_to_EU('3/13/18') --> '13.3.18' US_to_EU('3/13/2018') --> '13.3.2018' US_to_EU('03/13/2018') --> '13.03.2018'

  • The istime function takes a string as its parameter and return True if the string represent...

    The istime function takes a string as its parameter and return True if the string represent a valid time in the format of hh:mm or hh-mm format. It returns False otherwise. For example, istime("12:30"), istime("04-15"), istime("02:09") all return True. istime("ab:cd"), istime("1234"), and istime("04-98") all return False. It's critical that you include test case code to show proper testing.

  • write C program that contains a function that gets a string as a parameter and returns...

    write C program that contains a function that gets a string as a parameter and returns an integer value represents the number of uppercase letters in this string example : "AbcDeFgh" output:"3"

  • c++ Write a function Count_m_z that takes a string as an input parameter argument and counts...

    c++ Write a function Count_m_z that takes a string as an input parameter argument and counts the number of m, n, o, ... z characters in it. The function returns an integer value for the number of times those 14 lowercase letters appear in the input string. Your function should be named Count_m_z Your function should take one string parameter Your function should return the number of m, n, o, ... z characters as an integer Your function should not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT