python re data structure

11.1k views Asked by At

The dataset file in assets/grades.txt contains a line separated list of people with their grade in a class. Create a regex to generate a list of just those students who received a B in the course.

def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()
        for line in grades:
            line = line.rstrip()
            if re.search('^X\S*: [0-9.]+', line):
            print(line)
    
         

assert len(grades()) == 16

The easy solution is to add the following regular expression '([A-Z][a-z]+ [A-Z][a-z]+): B'. this solution is provided from abd-elrhman-mohey

8

There are 8 answers

1
Abd-elrhman Mohey On

I can see where you did go wrong my friend

first of all grades it's the lines u don't need the for loop to get them second of all, I don't know why did u use '^X\S*: [0-9.]+' as it seems out of what u want you need to see if the grades ar numbers or normal grades (A B C D) and appears from the question that u need to get B so we can say the final code will be for the find

'([A-Z][a-z]+ [A-Z][a-z]+): B'

and final code I guess will be

def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()
    w = '([A-Z][a-z]+ [A-Z][a-z]+): B'
    grades = re.findall(w , grades)
    return grades
0
Jay Patel On
import re
def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()
        list_of_grades = grades.split('\n')
        print(list_of_grades)        
        list_of_students = [ li.split()[0] + ' ' + li.split()[1] for li in list_of_grades if li.split()[-1] == 'B']
        print(list_of_students)        
    
    return list_of_students
0
Muhamed Youssry On

this works for me well as a piece of cake

import re
def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()

    # YOUR CODE HERE
    result = re.findall(': B', grades)
    return result

grades()
len(grades())
1
Syed Imtinan Ahmed On

#Here is my code:

` import re

 def grades():

      with open ("assets/grades.txt", "r") as file:
           grades = file.read()
           a=re.findall("([\w ]*)(?=\:\sB)",grades)
           return a


# YOUR CODE HERE
raise NotImplementedError()

#the output looks like a this: #['Bell Kassulke', '', 'Simon Loidl', '', 'Elias Jovanovic', '', 'Hakim Botros', '', 'Emilie Lorentsen',#

i want the output to return only names and not the spaces.

0
Vijayalakshmi Ramesh On

This has worked well for me

import re
def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()
    
    # YOUR CODE HERE
    pattern=re.findall('([A-Za-z]+ [A-Za-z]+): B',grades)
    return pattern
        
    
    #raise NotImplementedError()

You can check by running the following code

   assert len(grades()) == 16
        

To learn more about regex, visit: https://docs.python.org/3/library/re.html#module-re

1
Asad Khanzada On

This is what I did

    import re
def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()

    # YOUR CODE HERE
    result = re.findall('\w* \w*: B',grades )
    return result
print(grades())
0
Niloy Chatterjee On
import re
    def grades():
    with open ("assets/grades.txt", "r") as file:
        grades = file.read()

    only_B_grades = re.findall("[A-Z][a-z]* [A-Z][a-z]*\: [B]",grades)
    person = [grade_B.split(':')[0] for grade_B in only_B_grades]
    return person
    raise NotImplementedError()

grades()
0
NJUGUNA WAINAINA On
def grades():

      with open ("assets/grades.txt", "r") as file:
           grades = file.read()
           pattern=re.findall("[\w ]*)(\:\ B)",grades)
           return pattern
grades()