Python में कोडिंग इंटरव्यू प्रश्नों का अभ्यास
Kirill Smirnov
Data Science Consultant, Altran
Regular expression - विशेष कैरेक्टर्स (metacharacters) का अनुक्रम, जो टेक्स्ट में खोजने के लिए एक पैटर्न तय करता है.
cat
"I have a cat. My cat likes to eat a lot. It also catches mice."
Regular expression - विशेष कैरेक्टर्स (metacharacters) का अनुक्रम, जो टेक्स्ट में खोजने के लिए एक पैटर्न तय करता है.
cat
"I have a cat. My cat likes to eat a lot. It also catches mice."
उदाहरण:
[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected] पर फ़ॉरवर्ड हो जाते हैं.
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
साधारण अक्षर और अंक खुद पर मैप होते हैं:
a $\rightarrow$ aA $\rightarrow$ A1 $\rightarrow$ 1डॉट किसी भी कैरेक्टर पर मैप होता है:
. $\rightarrow$ कोई भी कैरेक्टर. $\rightarrow$ 'a', '1', '"', ' ', ...
\. $\rightarrow$ .निम्न metacharacters \ के बाद एक अक्षर दर्शाते हैं:
\w $\rightarrow$ कोई भी अल्फ़ान्यूमेरिक कैरेक्टर या अंडरस्कोर\w $\rightarrow$ '1', 'a','_', ...
\d $\rightarrow$ कोई भी digit\d $\rightarrow$ '1', '2','3', ...
\s $\rightarrow$ कोई भी whitespace कैरेक्टर\s $\rightarrow$ ' ', '\t', ...
कई metacharacters को square brackets में रखा जा सकता है:
[aAbB] $\rightarrow$ a, A, b, B[a-z] $\rightarrow$ a, b, c, ...[A-Z] $\rightarrow$ A, B, C, ...[0-9] $\rightarrow$ 0, 1, 2, ...[A-Za-z] $\rightarrow$ A, B, C, ..., a, b, c, ...* $\rightarrow$ कोई कैरेक्टर नहीं या अनिश्चित बार दोहरावa* $\rightarrow$ '', 'a', 'aa', ...
+ $\rightarrow$ कैरेक्टर कम-से-कम एक बार मौजूदa+ $\rightarrow$ 'a', 'aa', 'aaa', ...
? $\rightarrow$ कैरेक्टर हो भी सकता है या नहीं भीa? $\rightarrow$ '', 'a'
{n, m} $\rightarrow$ कैरेक्टर n से m बार तक मौजूदa{2, 4} $\rightarrow$ 'aa', 'aaa', 'aaaa'
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
[\w\.]+@[a-z]+\.[a-z]+
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
[\w\.]+@[a-z]+\.[a-z]+
[\w\.]+ $\rightarrow$ john.smith, boss, info
कम-से-कम एक अक्षर, अंक, अंडरस्कोर या डॉट
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
[\w\.]+@[a-z]+\.[a-z]+
@ $\rightarrow$ @
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
[\w\.]+@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ mailbox, company
कम-से-कम एक लोअरकेस अक्षर
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
[\w\.]+@[a-z]+\.[a-z]+
\. $\rightarrow$ .
उदाहरण:
**[email protected] John का e-mail है. वह अक्सर अपने बॉस को [email protected] पर लिखते हैं. लेकिन संदेश उनकी सचिव को [email protected]** पर फ़ॉरवर्ड हो जाते हैं.
[\w\.]+@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ com
कम-से-कम एक लोअरकेस अक्षर
import repattern = re.compile(r'[\w\.]+@[a-z]+\.[a-z]+')
text = '[email protected] is the e-mail of '\
'John. He often writes to his boss at '\
'[email protected]. But the messages get forwarded '\
'to his secretary at [email protected].'
result = re.finditer(pattern, text)
print(result)
<callable_iterator object at 0x7f5dff81af98>
for match in result:
print(match)
<_sre.SRE_Match object; span=(0, 22), match='[email protected]'>
<_sre.SRE_Match object; span=(77, 93), match='[email protected]'>
<_sre.SRE_Match object; span=(146, 162), match='[email protected]'>
result = re.finditer(pattern, text)
print(result)
<callable_iterator object ...>
for match in result:
print(match.group())
print(match.start())
print(match.end())
[email protected]
0
22
[email protected]
77
93
[email protected]
146
162
substrings = re.findall(pattern, text)
print(substrings)
['[email protected]', '[email protected]', '[email protected]']
split_list = re.split(pattern, text)
print(split_list)
['',
' is the e-mail of John. He often writes to his boss at ',
'. But the messages get forwarded to his secretary at ',
'.']
Python में कोडिंग इंटरव्यू प्रश्नों का अभ्यास