Day 28: RegEx, Patterns, and Intro to Databases

Sort by

recency

|

635 Discussions

|

  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    from typing import List
    
    def print_gmail(first_names: List[str], emails: List[str]):
        paired = dict(zip(emails, first_names))
        filtered = dict(filter(lambda data: "@gmail.com" in data[0], paired.items()))
        ordered_name = []
        ordered_email = []
        for name in filtered.values():
            ordered_name.append(name)
        ordered_name.sort()
        for name in ordered_name:
            print(name)
    
    
    if __name__ == '__main__':
        N = int(input().strip())
        
        names = []
        emails = []
        
        for N_itr in range(N):
            first_multiple_input = input().rstrip().split()
    
            firstName = first_multiple_input[0]
            names.append(firstName)
    
            emailID = first_multiple_input[1]
            emails.append(emailID)
        print_gmail(names, emails)
    
  • + 0 comments

    JAVA:

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
            int N = Integer.parseInt(bufferedReader.readLine().trim());
    				
            HashMap<String, String> emails = new HashMap<>();
    
            IntStream.range(0, N).forEach(NItr -> {
                try {
                    String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
                    String firstName = firstMultipleInput[0];
    
                    String emailID = firstMultipleInput[1];
                    
                    emails.put(emailID, firstName);
    
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
    
            //Stream emails, filter those that contains @gmail.com, collect to  list, sort list, print list.
            List<String> sortedNames = emails.entrySet().stream().filter(entry -> entry.getKey().contains("@gmail.com"))
                .map(entry -> entry.getValue()).sorted().collect(Collectors.toList());
            
            sortedNames.forEach(System.out::println);
        }
    }
    
  • + 0 comments

    My solution in Python

    import math import os import random import re import sys

    if name == 'main':

    new_list = []
    N = int(input().strip())
    
    for N_itr in range(N):
        first_multiple_input = input().rstrip().split()
    
        firstName = first_multiple_input[0]
    
        emailID = first_multiple_input[1]
    
        if '@gmail' in emailID:
          new_list.append([firstName, emailID])
    
    [print(L[0]) for L in sorted(new_list, key=lambda x: x[0])]
    
  • + 0 comments

    My solution

    import math import os import random import re import sys

    if name == 'main':

    new_list = []
    N = int(input().strip())
    
    for N_itr in range(N):
        first_multiple_input = input().rstrip().split()
    
        firstName = first_multiple_input[0]
    
        emailID = first_multiple_input[1]
    
        if '@gmail' in emailID:
          new_list.append([firstName, emailID])
    
    [print(L[0]) for L in sorted(new_list, key=lambda x: x[0])]
    
  • + 0 comments

    In C#

    class Solution
    {
        public static void Main(string[] args)
        {
            int N = Convert.ToInt32(Console.ReadLine().Trim());
            List<string> names = new List<string>();
    
            for (int NItr = 0; NItr < N; NItr++)
            {
                string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');
    
                string firstName = firstMultipleInput[0];
    
                string emailID = firstMultipleInput[1];
                
                var regex = new Regex("[A-Za-z._]+@gmail.com");
                if(regex.IsMatch(emailID))
                {
                    names.Add(firstName);
                }
            }
            
            names.Sort();
            names.ForEach(n => Console.WriteLine(n));
        }
    }