HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases

Today we will see the HackerRank Day 28 Solution in Python. The problem is named RegEx, Patterns, and Intro to databases part of 30 Days of code on HackerRank. Let’s get started!

Day 28: RegEx, Patterns, and Intro to the databases Problem statement

Let’s consider a database table, Emails, with the attributes First Name and Email ID. We are given n rows of data simulating the Emails table, our task is to print an alphabetically-ordered list of people whose email address ends in @ gmail.com

Sample Input

6
riya riya@gmail.com
julia julia@julia.me
julia sjulia@gmail.com
julia julia@gmail.com
samantha samantha@gmail.com
tanya tanya@gmail.com

Sample Output

julia
julia
riya
samantha
tanya

You can solve the problem here.

HackerRank Day 28 Solution in Python

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    #Get number of testcase as input
    N = int(input().strip())
    
    #list to store output
    names = []

    for i in range(N):
        #Get name and mail as input
        inp = input().rstrip().split()
        
        #Assign name to firstName variable
        firstName = inp[0]
        
        #Assign mailid to emailID
        emailID = inp[1]
        
        #Append names with @gmail.com in mailid to the list
        if re.search(r'@gmail\.com$', emailID):
            names.append(firstName)
            
    #Sort and print the list
    print(*sorted(names), sep='\n')

Code Explanation

  • First, get the number of input from the user
  • Then we split the input into two strings and store the first one in firstName and the second in emailId
  • Then we used the Regex concept to check whether any input emaiId contains @gmail.com in the end
  • If the string contains it, then we add the corresponding first name to the list created to store the output
  • Finally, sort and print the list

Also Read:

Share:

Author: Ayush Purawr