Group() Groups() Groupdict() in Python HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Group() Groups() Groupdict() in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

Ezoicreport this adGroup() Groups() Groupdict() in Python HackerRank Solution
Group() Groups() Groupdict() in Python HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Group() Groups() Groupdict() in Python HackerRank Solutions

Ezoicreport this adProblem

group()
A group() expression returns one or more subgroups of the match.
Code :

>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','[email protected]')
>>> m.group(0)       # The entire match
'[email protected]'
>>> m.group(1)       # The first parenthesized subgroup.
'username'
>>> m.group(2)       # The second parenthesized subgroup.
'hackerrank'
>>> m.group(3)       # The third parenthesized subgroup.
'com'
>>> m.group(1,2,3)   # Multiple arguments give us a tuple.
('username', 'hackerrank', 'com')

groups()
A groups() expression returns a tuple containing all the subgroups of the match.
Code :

>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','[email protected]')
>>> m.groups()
('username', 'hackerrank', 'com')

groupdict()
A groupdict() expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.
Code :

>>> m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','[email protected]')
>>> m.groupdict()
{'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}

Task :

You are given a string S.
Your task is to find the first occurrence of an alphanumeric character in S(read from left to right) that has consecutive repetitions.

Input Format :

A single line of input containing the string S.

Constraints :

  • 0 < len(S) < 100

Output Format :

Print the first occurrence of the repeating character. If there are no repeating characters, print -1.

Sample Input :

..12345678910111213141516171820212223

Sample Output :

1

Explanation :

.. is the first repeating character, but it is not alphanumeric.
1 is the first (from left to right) alphanumeric repeating character of the string in the substring 111.

Group() Groups() Groupdict() in Python HackerRank Solutions

import re
expression=r"([a-zA-Z0-9])\1+"
m = re.search(expression,input())
if m:
    print(m.group(1))
else:
    print(-1)

Disclaimer: The above Problem (Group() Groups() Groupdict() in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Re.findall() & Re.finditer() in Python HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad