Today we are going to solve HackerRank Day 28: RegEx Patterns and Intro to Database 30 days of code solutions in C, C++, Java, Javascript & Python.
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 lowercase letters [a – z] only.
- Each of the email IDs consists of lowercase letters [a – z], @ 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 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
HackerRank Day 28: RegEx Patterns and Intro to Database 30 days of code solutions
RegEx Patterns and Intro to Database Solutions in C
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <ctype.h>
int mycmp(const char *a, const char *b) {
const char *cp1 = a, *cp2 = b;
for (; toupper(*cp1) == toupper(*cp2); cp1++, cp2++)
if (*cp1 == '\0')
return 0;
return ((toupper(*cp1) < toupper(*cp2)) ? -1 : +1);
}
int cmp(const void *p1, const void *p2) {
return mycmp(* (char * const *) p1, * (char * const *) p2);
}
char* validate_local_address(char* email) {
char *ptr;
for (ptr = email; *ptr; ptr++) {
if (*ptr == '@' && ptr != email) {
// check that we saw at least one character
return ptr+1;
} else if (!((*ptr >= 'a' && *ptr <= 'z') || (*ptr == '.'))) {
return 0;
}
}
return 0;
}
int main(){
int N;
scanf("%d",&N);
char **result = (char **)malloc(N * sizeof(char *));
int index = 0;
for(int a0 = 0; a0 < N; a0++){
char* firstName = (char *)malloc(1024 * sizeof(char));
memset(firstName, 0, 1024);
char* emailID = (char *)malloc(1024 * sizeof(char));
memset(emailID, 0, 1024);
scanf("%s %s",firstName,emailID);
int len = strlen(emailID);
if (strlen(firstName)) {
char *ptr = validate_local_address(emailID);
if (ptr) {
if (!strcmp(ptr, (const char *)"gmail.com")) {
char *p = firstName;
for ( ; *p; ++p) *p = tolower(*p);
result[index++] = firstName;
}
}
}
}
if (index) {
// sort the names alphabetically
qsort(result, index, sizeof(char *), cmp);
int i;
for (i = 0; i < index; i++) {
printf("%s\n", result[i]);
}
}
return 0;
}
RegEx Patterns and Intro to Database Solution in C++
#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>
using namespace std;
bool end_with(string& s) {
string t = "@gmail.com";
if (s.size() < t.size()) return false;
for (int i = s.size() - 1, j = t.size() - 1; i >= 0 && j >= 0; --i, --j) {
if (s[i] != t[j]) return false;
}
return true;
}
int main(){
int N;
cin >> N;
vector<string> ans;
for(int a0 = 0; a0 < N; a0++){
string firstName;
string emailID;
cin >> firstName >> emailID;
if (end_with(emailID))
ans.push_back(firstName);
}
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); ++i)
cout << ans[i] << endl;
return 0;
}
RegEx Patterns and Intro to Database Solution in Java
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
ArrayList<String> results = new ArrayList<String>();
for (int i = 0; i < n; i++) {
String line = in.nextLine();
String[] tokens = line.split(" ");
if (tokens[1].endsWith("@gmail.com"))
results.add(tokens[0]);
}
Collections.sort(results);
for (String s : results) {
System.out.println(s);
}
}
}
RegEx Patterns and Intro to Database Solution in Javascript
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 N = parseInt(readLine());
var patt = new RegExp("@gmail.com");
var names = [];
for(var a0 = 0; a0 < N; a0++){
var firstName_temp = readLine().split(' ');
var firstName = firstName_temp[0];
var emailID = firstName_temp[1];
if(patt.test(emailID)){
names.push(firstName);
}
}
names.sort().forEach(function(name){
console.log(name);
});
}
RegEx Patterns and Intro to Database Solution in Python 2
#!/bin/python
import sys
N = int(raw_input().strip())
nameList = []
for a0 in xrange(N):
firstName,emailID = raw_input().strip().split(' ')
firstName,emailID = [str(firstName),str(emailID)]
if (emailID.split('@')[1] == 'gmail.com'):
nameList.append(firstName)
nameList = sorted(nameList)
for name in nameList:
print name
RegEx Patterns and Intro to Database Solution in Python 3
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
N = int(input())
nw = []
for N_itr in range(N):
firstNameEmailID = input().split()
firstName = firstNameEmailID[0]
emailID = firstNameEmailID[1]
if '@gmail.com' in emailID:
nw.append(firstName)
nw = sorted(nw)
for item in nw:
print(item)
NEXT: HackerRank Day 29: Bitwise AND 30 days of code solution