Skip to content

Instantly share code, notes, and snippets.

@mudassaralichouhan
Last active December 19, 2024 20:12
Show Gist options
  • Save mudassaralichouhan/178bfd172f6745a53b4543275645d11e to your computer and use it in GitHub Desktop.
Save mudassaralichouhan/178bfd172f6745a53b4543275645d11e to your computer and use it in GitHub Desktop.
bool isEven(int n) {
return (n & 1) == 0;
}
/*
* Sieve of Eratosthenes: An efficient algorithm to find all prime numbers up to a given limit.
* Time complexity: O(n log(log n))
* Space complexity: O(n)
*/
#include <iostream>
#include <vector>
#include <cstdlib>
std::vector<bool> sieveOfEratosthenes(int n) {
std::vector<bool> primes(n + 1, true);
primes[0] = primes[1] = false;
for (int i = 2; i * i <= n; i++) {
if (primes[i]) {
for (int j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
return primes;
}
bool isPrime(int number) {
if (number <= 1) return false;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) return false;
}
return true;
}
int main() {
{
int n = 100, i = 1, count = 0;
std::vector<bool> primes = sieveOfEratosthenes(n);
for (int i = 2; i <= primes.size(); i++) {
if (primes[i]) {
std::cout << i << ' ';
}
}
}
std::cout << std::endl;
{
int n = 100, count = 0, number = 2;
while (count < n) {
if (isPrime(number)) {
std::cout << number << " ";
count++;
}
number++;
}
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
void swap(int &a, int &b) {
a ^= b;
b ^= a;
a ^= b;
}
@mudassaralichouhan
Copy link
Author

function findMatchingWords(words, pattern) {
    // Convert the pattern to a regular expression
    // Replace '?' with '.' (matches any single character)
    // Replace '_' with '.' (matches any single character)
    const regexPattern = pattern.replace(/\?/g, '.').replace(/_/g, '.');
    const regex = new RegExp(`^${regexPattern}$`); // Anchors the regex to match the whole word

    // Filter the words that match the regex
    return words.filter(word => regex.test(word));
}

// Example usage
const wordsList = ['cat', 'bat', 'hat', 'heat', 'helt', 'help', 'hell', 'he'll', 'heatwave'];
const pattern1 = '?at'; // Matches 'cat', 'bat', 'hat'
const pattern2 = 'he_l'; // Matches 'help', 'hell'

const matches1 = findMatchingWords(wordsList, pattern1);
const matches2 = findMatchingWords(wordsList, pattern2);

console.log(`Matches for '${pattern1}':`, matches1);
console.log(`Matches for '${pattern2}':`, matches2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment