Day 28: RegEx, Patterns, and Intro to Databases

Day 28: RegEx, Patterns, and Intro to Databases – Hackerrank 30 Days Of Code Solution

Let’s solve Day 28: RegEx, Patterns, and Intro to Databases. Regular expressions (RegEx) and databases are powerful tools in programming, allowing you to perform complex pattern matching and data manipulation operations. Join us on this exciting journey to explore RegEx, databases, and their practical application.😍

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.

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

Explanation

To solve this challenge, we need to process the input data, which simulates the Emails table, and extract the names of people whose email addresses end with @gmail.com. The extracted names should be sorted alphabetically and printed.

The solution requires the use of regular expressions to match the pattern @gmail.com in the email addresses, as well as string manipulation and sorting techniques.

Day 28: RegEx, Patterns, and Intro to Databases solution In Javascript

JavaScript
function main() {
    const N = parseInt(readLine().trim(), 10);
    let NamesArray = [];

    for (let NItr = 0; NItr < N; NItr++) {
        const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');

        const firstName = firstMultipleInput[0];

        const emailID = firstMultipleInput[1];
        
        let names = emailID.split(firstName);
        
        if(names[1] == "@gmail.com"){
            NamesArray.push(firstName);
        }
    }
    let sortedNames = NamesArray.sort();
        
    for(let i=0;i<sortedNames.length;i++){
        console.log(sortedNames[i]);
    }
}

Day 28: RegEx, Patterns, and Intro to Databases Solution In C++

C++
int main() {
    int N;
    cin >> N;
    cin.ignore();
    vector<string> NamesArray;

    for (int NItr = 0; NItr < N; NItr++) {
        string firstName, emailID;
        cin >> firstName >> emailID;

        size_t pos = emailID.find(firstName);

        if (pos != string::npos && emailID.substr(pos + firstName.length()) == "@gmail.com") {
            NamesArray.push_back(firstName);
        }
    }

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

    for (const string& name : NamesArray) {
        cout << name << endl;
    }

    return 0;
}

Day 28: RegEx, Patterns, and Intro to Databases Solution In Java

Java
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());
        List<String> namesArray = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            try {
                String[] firstMultipleInput = bufferedReader.readLine().trim().split(" ");
                
                String firstName = firstMultipleInput[0];
                String emailID = firstMultipleInput[1];
                
                if (emailID.endsWith("@gmail.com")) {
                    namesArray.add(firstName);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        bufferedReader.close();
        
        Collections.sort(namesArray);
        
        namesArray.forEach(System.out::println);
    }
}

Day 28: RegEx, Patterns, and Intro to Databases Solution In Python

Python
if __name__ == '__main__':
    N = int(input().strip())
    NamesArray = []

    for N_itr in range(N):
        first_multiple_input = input().rstrip().split()

        firstName = first_multiple_input[0]
        emailID = first_multiple_input[1]

        if emailID.endswith("@gmail.com"):
            NamesArray.append(firstName)

    sortedNames = sorted(NamesArray)

    for name in sortedNames:
        print(name)

Day 28: RegEx, Patterns, and Intro to Databases Solution In PHP

PHP
<?php

$N = intval(trim(fgets(STDIN)));
$NamesArray = array();

for ($N_itr = 0; $N_itr < $N; $N_itr++) {
    $first_multiple_input = explode(' ', rtrim(fgets(STDIN)));

    $firstName = $first_multiple_input[0];
    $emailID = $first_multiple_input[1];

    if (substr($emailID, -10) === "@gmail.com") {
        $NamesArray[] = $firstName;
    }
}

sort($NamesArray);

foreach ($NamesArray as $name) {
    echo $name . "\n";
}

Learning from this Question

By solving the “Day 28: RegEx, Patterns, and Intro to Databases” challenge, you can learn several important concepts:

  1. Regular expressions (RegEx): You will gain a deeper understanding of regular expressions and how to use them for pattern matching and string manipulation.
  2. Database concepts: You will be introduced to database concepts, such as tables, rows, and columns, and how to work with simulated data.
  3. String manipulation: You will practice manipulating strings using various techniques, such as splitting and concatenation.
  4. Sorting algorithms: You will learn about sorting algorithms and how to sort data in alphabetical order.
  5. Problem-solving skills: You will enhance your problem-solving skills by designing and implementing a solution that meets the given requirements.

Conclusion

The “Day 28: RegEx, Patterns, and Intro to Databases” challenge introduces the concepts of regular expressions and databases, as well as their practical application in extracting and manipulating data based on specific patterns. By implementing the solution, you will gain valuable experience in working with RegEx, string manipulation, sorting, and simulated database data. The complete code for this challenge is available in my GitHub repository✨.

This exercise reinforces the importance of regular expressions and databases in software development, as they are essential tools for data processing, pattern matching, and data storage and retrieval⭐.