RegEx + Lambda Functions Practice
1
Check if String is a Digit
Easy
Write a program that uses a regular expression to check whether the given string contains only digits (0-9). Print True if it does, else False.
Input Format
A single line containing a string.
Output Format
Print True or False.
Sample Test Cases
|
Sample Input 1
12345 |
Sample Output 1
True |
|
Sample Input 2
12a45 |
Sample Output 2
False |
|
Sample Input 3
007 |
Sample Output 3
True |
2
Count Vowels with a Lambda
Easy
Write a program that uses a lambda function to count the number of vowels (a, e, i, o, u) in a given string.
Input Format
A single line containing a string.
Output Format
Print the number of vowels in the string.
Sample Test Cases
|
Sample Input 1
hello |
Sample Output 1
2 |
|
Sample Input 2
python |
Sample Output 2
1 |
|
Sample Input 3
aeiou |
Sample Output 3
5 |
3
Extract All Digits from a String
Easy
Write a program that uses a regular expression to extract all digit characters from a string and prints them together.
Input Format
A single line containing an alphanumeric string.
Output Format
Print a single string containing only the digits found, in order.
Sample Test Cases
|
Sample Input 1
a1b2c3 |
Sample Output 1
123 |
|
Sample Input 2
abc |
Sample Output 2
|
|
Sample Input 3
xyz9q0 |
Sample Output 3
90 |
4
Square Each Number Using Lambda
Easy
Write a program that uses a lambda function with map() to print the squares of a list of integers, space-separated.
Input Format
A single line containing space-separated integers.
Output Format
Print the square of each integer, space-separated.
Sample Test Cases
|
Sample Input 1
1 2 3 |
Sample Output 1
1 4 9 |
|
Sample Input 2
5 6 |
Sample Output 2
25 36 |
|
Sample Input 3
0 1 2 |
Sample Output 3
0 1 4 |
1
Replace Multiple Spaces with a Single Space
Medium
Write a program that uses a regular expression to replace multiple consecutive spaces in a string with a single space.
Input Format
A single line containing a sentence with irregular spacing.
Output Format
Print the cleaned sentence with single spaces.
Sample Test Cases
|
Sample Input 1
a b c |
Sample Output 1
a b c |
|
Sample Input 2
hello world |
Sample Output 2
hello world |
|
Sample Input 3
x y z |
Sample Output 3
x y z |
2
Sort Numbers by Absolute Value Using Lambda
Medium
Write a program that uses a lambda function to sort a list of integers by their absolute value. Print the sorted list.
Input Format
A single line containing space-separated integers (may be negative).
Output Format
Print the integers sorted by absolute value, space-separated.
Sample Test Cases
|
Sample Input 1
-5 3 -1 2 |
Sample Output 1
-1 2 3 -5 |
|
Sample Input 2
4 -2 1 -3 |
Sample Output 2
1 -2 -3 4 |
|
Sample Input 3
0 -1 1 |
Sample Output 3
0 -1 1 |
3
Validate an Email Address with Regex
Medium
Write a program that uses a regular expression to validate whether a given string is a valid email address of the form username@domain.tld. Print Valid or Invalid.
Input Format
A single line containing a string.
Output Format
Print Valid or Invalid.
Sample Test Cases
|
Sample Input 1
user@example.com |
Sample Output 1
Valid |
|
Sample Input 2
plainaddress |
Sample Output 2
Invalid |
|
Sample Input 3
name@site.co |
Sample Output 3
Valid |
4
Filter Even Numbers Using Lambda
Medium
Write a program that uses a lambda function with filter() to keep only the even numbers from a list and prints them.
Input Format
A single line containing space-separated integers.
Output Format
Print the even numbers, space-separated.
Sample Test Cases
|
Sample Input 1
1 2 3 4 5 6 |
Sample Output 1
2 4 6 |
|
Sample Input 2
10 15 20 |
Sample Output 2
10 20 |
|
Sample Input 3
1 3 5 |
Sample Output 3
|
1
Extract Phone Numbers from Text
Hard
Write a program that uses a regular expression to find all phone numbers of the form 10-digit numbers (e.g. 9876543210) in a block of text and prints each one on a new line.
Input Format
A single line (or multiple lines) of text possibly containing phone numbers.
Output Format
Print each found 10-digit number on its own line; if none, print nothing.
Sample Test Cases
|
Sample Input 1
Call 9876543210 now |
Sample Output 1
9876543210 |
|
Sample Input 2
No numbers here |
Sample Output 2
|
|
Sample Input 3
1234567890 and 0987654321 |
Sample Output 3
1234567890 0987654321 |
2
Multiply List Using Lambda
Hard
Write a program that uses a lambda function with reduce() to compute the product of all integers in a list and prints it.
Input Format
A single line containing space-separated integers.
Output Format
Print the product of all the integers.
Sample Test Cases
|
Sample Input 1
2 3 4 |
Sample Output 1
24 |
|
Sample Input 2
1 2 3 4 |
Sample Output 2
24 |
|
Sample Input 3
7 |
Sample Output 3
7 |
Competitive MCQs — RegEx + Lambda Functions
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 33
Q1
What does re.match() do?
Correct!
Wrong — correct answer is .
match() anchors at the beginning; search() looks anywhere.
Q2
Which of these creates a lambda that adds 1?
Correct!
Wrong — correct answer is .
lambda parameters: expression — here x → x + 1.
Q3
Which regex quantifier means "zero or more"?
Correct!
Wrong — correct answer is .
* repeats the previous token zero or more times.
Q4
What is the output of this code?
python
1
2
2
import re
print(re.sub("a", "o", "banana"))Correct!
Wrong — correct answer is .
sub replaces every "a" with "o": b-o-n-o-n-o.
Q5
What is the output of this code?
import re
print(re.sub("a", "o", "banana"))
import re
print(re.sub("a", "o", "banana"))
Correct!
Wrong — correct answer is .
sub replaces every "a" with "o": b-o-n-o-n-o.
Q6
What does \d match?
Correct!
Wrong — correct answer is .
\d is shorthand for [0-9].
Q7
Which function splits a string by a regex pattern?
Correct!
Wrong — correct answer is .
re.split(pattern, text) divides text at every pattern match.
Q8
What is the output of this code?
python
1
2
2
add = lambda a, b: a + b
print(add(2, 3))Correct!
Wrong — correct answer is .
add(2, 3) evaluates the expression 2 + 3 = 5.
Q9
What is the output of this code?
add = lambda a, b: a + b
print(add(2, 3))
add = lambda a, b: a + b
print(add(2, 3))
Correct!
Wrong — correct answer is .
add(2, 3) evaluates the expression 2 + 3 = 5.
Q10
Which is NOT a valid lambda?
Correct!
Wrong — correct answer is .
A lambda body must be an expression; assignment (x = 5) is a statement.
Q11
What does ^ inside a character class like [^0-9] mean?
Correct!
Wrong — correct answer is .
A ^ at the start of a character class negates the set.
Q12
What is the output of this code?
python
1
2
2
import re
print(re.findall("\d+", "abc123def45"))Correct!
Wrong — correct answer is .
findall returns each run of digits: "123" and "45".
Q13
What is the output of this code?
import re
print(re.findall("\d+", "abc123def45"))
import re
print(re.findall("\d+", "abc123def45"))
Correct!
Wrong — correct answer is .
findall returns each run of digits: "123" and "45".
Q14
What does re.search(pattern, text) do?
Correct!
Wrong — correct answer is .
search() scans the whole string and returns the first match or None.
Q15
What is the output of this code?
import re
print(bool(re.search("\d", "abc123")))
import re
print(bool(re.search("\d", "abc123")))
Correct!
Wrong — correct answer is .
search() finds a digit, returning a match object; bool() makes it True.
Q16
Which regex matches a word boundary?
Correct!
Wrong — correct answer is .
\b anchors at a word boundary between word and non-word characters.
Q17
What is the output of this code?
import re
print(re.findall("\w+", "hi, there!"))
import re
print(re.findall("\w+", "hi, there!"))
Correct!
Wrong — correct answer is .
\w+ matches word characters, splitting the text at punctuation/space.
Q18
What does the ? quantifier mean?
Correct!
Wrong — correct answer is .
? makes the preceding token optional (0 or 1 times).
Q19
What is the output of this code?
import re
print(re.sub("\s", "-", "a b"))
import re
print(re.sub("\s", "-", "a b"))
Correct!
Wrong — correct answer is .
sub replaces the single space with "-", producing "a-b".
Q20
Which function compiles a regex for reuse?
Correct!
Wrong — correct answer is .
compile() returns a pattern object for efficient repeated matching.
Q21
What is the output of this code?
import re
print(re.match("\d+", "123abc").group())
import re
print(re.match("\d+", "123abc").group())
Correct!
Wrong — correct answer is .
match() anchors at the start, capturing the leading digits "123".
Q22
Which pattern matches an email-ish local part with letters/digits/_/.?
Correct!
Wrong — correct answer is .
The character class allows letters, digits, dot, and underscore repeatedly.
Q23
What does lambda capture from its enclosing scope?
Correct!
Wrong — correct answer is .
Lambdas close over enclosing names and can read them.
Q24
What is the output of this code?
mult = lambda x: x * 2
print(mult(10))
mult = lambda x: x * 2
print(mult(10))
Correct!
Wrong — correct answer is .
The lambda doubles its input: 10 × 2 = 20.
Q25
Which built-in pairs well with lambda for filtering?
Correct!
Wrong — correct answer is .
filter(function, iterable) keeps items where the function is truthy.
Q26
What is the output of this code?
print(list(filter(lambda x: x > 1, [1, 2, 3])))
print(list(filter(lambda x: x > 1, [1, 2, 3])))
Correct!
Wrong — correct answer is .
filter keeps only the values greater than 1: [2, 3].
Q27
Which of these is a valid lambda with two parameters?
Correct!
Wrong — correct answer is .
Parameters are comma-separated before the colon: lambda a, b: expr.
Q28
What is the output of this code?
names = ["ann", "Bo", "chris"]
print(sorted(names, key=lambda s: len(s)))
names = ["ann", "Bo", "chris"]
print(sorted(names, key=lambda s: len(s)))
Correct!
Wrong — correct answer is .
The key sorts by length: 2, 3, then 5 characters.
Q29
Which is TRUE about lambda bodies?
Correct!
Wrong — correct answer is .
Lambda bodies are expressions only; return is not allowed.
Q30
What is the output of this code?
f = lambda x, y=10: x + y
print(f(5))
f = lambda x, y=10: x + y
print(f(5))
Correct!
Wrong — correct answer is .
x=5 and y defaults to 10, giving 15.
Q31
Which lambda returns the smaller of two values?
Correct!
Wrong — correct answer is .
A ternary expression inside the lambda picks the smaller value.
Q32
What is the output of this code?
import re
print(re.split("[,;]", "a,b;c"))
import re
print(re.split("[,;]", "a,b;c"))
Correct!
Wrong — correct answer is .
split() divides at every comma or semicolon.
Q33
What does re.IGNORECASE do?
Correct!
Wrong — correct answer is .
The re.IGNORECASE flag makes letters match regardless of case.