In this HackerRank Day 28: RegEx, Patterns, and Intro to Databases problem solution 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.

HackerRank Day 28: RegEx, Patterns, and Intro to Databases problem solution

Problem solution in Python programming.

#!/bin/python3

import sys
import re

N = int(input().strip())
names = []
p = re.compile('.*@gmail\.com.*')
for a0 in range(N):
    firstName,emailID = input().strip().split(' ')
    firstName,emailID = [str(firstName),str(emailID)]
    if (p.match(emailID)):
        names.append(firstName)
names.sort()
for name in names:
    print(name)



Problem solution in Java programming.

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

public class Solution {

    public static void main(String[] args) {
        String regEx = ".+[a-z].@";
        Pattern pattern1 = Pattern.compile(regEx);
        String regEx2 = ".+[a-z]";
        Pattern pattern2 = Pattern.compile(regEx2);
        Scanner in = new Scanner(System.in);
        String firstNames = "";
        int N = in.nextInt();
        for(int a0 = 0; a0 < N; a0++){
            String firstName = in.next();
            String emailID = in.next();
            if(firstName.length() <= 20 && emailID.length()<=50)
                {
                Matcher m1 = pattern1.matcher(emailID);
                Matcher m2 = pattern2.matcher(firstName);
                if(m1.find() && m2.find())
                    {
                 if(emailID.endsWith("gmail.com") && !(emailID.contains(" ")))  {
                     String[] email = emailID.split("@");
                     if(email.length == 2 && email[1].equals("gmail.com"))
                      firstNames = firstNames + firstName + " ";
                 } 
               // System.out.println(emailID);
                  
                }
            }
        }
         //   System.out.println(firstNames);
            String[] firstNameArr = firstNames.split(" ");
            for(int i = 0; i< firstNameArr.length; i++)
                {
                for(int j = 0; j < firstNameArr.length - 1; j++)
                    {
                    String temp;
                    if(firstNameArr[j].compareTo((firstNameArr[j+1])) > 0)
                        {
                        temp = (firstNameArr[j]);
                        firstNameArr[j] = firstNameArr[j+1];
                        firstNameArr[j+1] = temp;
                    }
                }
            }
            
            for(int i = 0; i<firstNameArr.length; i++)
                {
                System.out.println(firstNameArr[i]);
            }
        }
    
}




Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <regex>

using namespace std;


int main(){
    int N;
    cin >> N;
    //unordered_map<string, string> studentList;
    vector<string> studentList;
    for(int a0 = 0; a0 < N; a0++){
        string firstName;
        string emailID;
        regex cstr( "\\@gmail.com");

        cin >> firstName >> emailID;
        //cout << emailID << endl;
        if(regex_search(emailID,cstr)){
           // studentList[firstName] = emailID;
            studentList.push_back(firstName);
        }
    }
   // map<string, string> ordered(studentList.begin(),studentList.end());
    sort(studentList.begin(), studentList.end());
    for(auto it = studentList.begin(); it != studentList.end(); ++it)
       std::cout << *it <<endl;
  
  
    return 0;
}



Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int N,h=0; 
    char temp[10];

    scanf("%d",&N);
    char arr[N][20];
    char arr3[20];
    char arr4[20];
    for(int a0 = 0; a0 < N; a0++){
        char* firstName = (char *)malloc(100 * sizeof(char));
        char* emailID = (char *)malloc(100 * sizeof(char));
        scanf("%s %s",firstName,emailID);
        int a;
         if(strlen(firstName)>20){
             printf("Invalid length of name");
             a0--;
             continue;
         }
        if(strlen(emailID)>50){ 
            printf("Invalid length of email");
             a0--;
             continue;
            
        }
       
         int x=0,y=10;
        	
        	for(int j=0;y<51;j++){
        		int m=0;
        		
                for(int l=0;l<10;l++){
        			temp[l]='\0';
				}
        	for(int z=x;z<y;z++){
        		temp[m]=emailID[z];
        		m++;
			}
			if(strncmp(temp,"@gmail.com",10)==0){
                strcpy(arr[h],firstName);
                h++;
                break;
			}
				x++;y++;
			}
    }
    char arr2[20];
    int p=0;
    for(int i=0;i<h;i++){
        for(int j=i+1;j<h;j++){
            if(arr[i][0]>arr[j][0]){
                strcpy(arr2,arr[i]);
                strcpy(arr[i],arr[j]);
                strcpy(arr[j],arr2);
            }
            else if((arr[i][1]==arr[j][1]) && (arr[i][2]>arr[j][2])){
                	strcpy(arr3,arr[i]);
                    strcpy(arr[i],arr[j]);
                    strcpy(arr[j],arr3);
				}
        	/*else if( (arr[i][1]==arr[j][1]) && (arr[i][0]==arr[j][j])&&(arr[i][2]>arr[j][2]) ){
                	strcpy(arr4,arr[i]);
                strcpy(arr[i],arr[j]);
                strcpy(arr[j],arr4);
				}*/
        }
    }
    for(int i=0;i<h;i++){
        puts(arr[i]);
    }
    return 0;
}



Problem solution in Java programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var re = /^[a-z\.]+@gmail.com/;
    var names = [];
    var N = parseInt(readLine());
    for(var a0 = 0; a0 < N; a0++){
        var firstName_temp = readLine().split(' ');
        var firstName = firstName_temp[0];
        var emailID = firstName_temp[1];
        
        if (emailID.match(re)) {
            names.push(firstName);
        }
    }
    names.sort();
    for (var i = 0; i < names.length; i++) {
        console.log(names[i]);
    }
}