Day 28: RegEx, Patterns, and Intro to Databases

Hello coders, today we are going to solve Day 28: RegEx, Patterns, and Intro to Databases HackerRank Solution in C++, Java and Python.

Day 28: RegEx, Patterns, and Intro to Databases

Objective

Today, we’re working with regular expressions.

Task

Consider a database table, Emails, which has the attributes First Name and Email ID. Given N rows of data simulating the Emails table, print an alphabetically-ordered list of people whose email address ends in @gmail.com.

Input Format

The first line contains an integer, N, total number of rows in the table.
Each of the N subsequent lines contains 2 space-separated strings denoting a person’s first name and email ID, respectively.

Constraints

  • 2 <= N <= 30
  • Each of the first names consists of lower case letters [az] only.
  • Each of the email IDs consists of lower case letters [az]@ and  only.
  • The length of the first name is no longer than 20.
  • The length of the email ID is no longer than 50.

Output Format

Print an alphabetically-ordered list of first names for every user with a gmail account. Each name must be printed on a new line.

Sample Input

6
riya [email protected]
julia [email protected]
julia [email protected]
julia [email protected]
samantha [email protected]
tanya [email protected]

Sample Output

julia
julia
riya
samantha
tanya

Solution – Day 28: RegEx, Patterns, and Intro to Databases

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N, i;

    cin >> N;

    regex exp(".+@gmail\\.com$");

    vector<string> strarray;

    for(i = 0; i < N; i++)
 {
        string Fname;

        string Eid;

        cin >> Fname >> Eid;

        if(regex_match(Eid, exp))
 {
            strarray.push_back(Fname);
        }
    }

    sort(strarray.begin(), strarray.end()); 

    for(i = 0; i < strarray.size();i++) 
    { 
        cout << strarray[i] << endl; 
    }

    return 0;
}

Java

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String Regrex=".+@gmail\\.com$";
        List<String> list= new ArrayList();
        Pattern pattern=Pattern.compile(Regrex);
        for(int i=0;i<n;i++){
            String name=scanner.next();
            String email=scanner.next();
            Matcher matcher=pattern.matcher(email);
            if(matcher.find()){
                list.add(name);
            }
        }
        Collections.sort(list);
        for(String names:list){
            System.out.println(names);
        }
        scanner.close();
    }
}

Python

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    N = int(input())
    validNamesList = []
    for N_itr in range(N):
        firstNameEmailID = input().split()
        firstName = firstNameEmailID[0]
        emailID = firstNameEmailID[1]
        if emailID.find("@gmail.com") != -1: #gmail email found
            validNamesList.append(firstName)
    for i in sorted(validNamesList):
        print(i)

Disclaimer: The above Problem (Day 28: RegEx, Patterns, and Intro to Databases) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *