In this HackerRank Day 28 RegEx Patterns and Intro to Database 30 days of code problem set, we have given a database table in which we have first names and Emails. we need to print the list of the user using regular expression in alphabetical order whose email ends to @gmail.com
Problem solution in Python 2 programming.
#!/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
Problem solution in Python 3 programming.
#!/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)
Problem solution in java programming.
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); } } }
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> 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; }
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> #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 == ' ') 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("%sn", result[i]); } } return 0; }
Problem solution in Javascript 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 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); }); }