0% found this document useful (0 votes)
534 views

Hacker Rank Python

This document contains Python code snippets and explanations for solving problems on HackerRank related to Python fundamentals like lambdas, loops, string formatting, regular expressions, and more. It includes 9 code snippets with comments to read input, perform operations, and print output based on various tasks.

Uploaded by

Ankita Kukreja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
534 views

Hacker Rank Python

This document contains Python code snippets and explanations for solving problems on HackerRank related to Python fundamentals like lambdas, loops, string formatting, regular expressions, and more. It includes 9 code snippets with comments to read input, perform operations, and print output based on various tasks.

Uploaded by

Ankita Kukreja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Hacker Rank Python:

if __name__ == '__main__':
a = int(input())
b = int(input())
sumn = lambda a,b : a+b
print(sumn(a,b))
diff = lambda a,b : a-b
print(diff(a,b))
prod = lambda a,b : a*b
print(prod(a,b))
'''The first line contains the sum of the two numbers.
The second line contains the difference of the two numbers (first -
second).
The third line contains the product of the two numbers.'''

'''Task
Read an integer . For all non-negative integers , print . See the
sample for details.'''
if __name__ == '__main__':
n = int(input())
for i in range(n):
print(i**2)
'''Task
Read two integers and print two lines. The first line should
contain integer division, // . The second line should contain
float division, / .

You don't need to perform any rounding or formatting operations.'''

'''floor division // always give floor answer


division / always give floating point ans'''

if __name__ == '__main__':
a = int(input())
b = int(input())
intdiv = lambda a,b : a//b
print(intdiv(a,b))
decdiv = lambda a,b : a/b
print(decdiv(a,b))

'''Read an integer N.

Without using any string methods, try to print the following:


123...N

Note that "" represents the values in between'''

if __name__ == '__main__':
n = int(input())
for i in range(1,n+1):
print(i,end='')

# Enter your code here. Read input from STDIN. Print output to
STDOUT

'''A valid email address meets the following criteria:

It's composed of a username, domain name, and extension assembled


in this format: [email protected]
The username starts with an English alphabetical character, and any
subsequent characters consist of one or more of the following:
alphanumeric characters, -,., and _.
The domain and extension contain only English alphabetical
characters.
The extension is 1,2, or 3 characters in length.
Given n pairs of names and email addresses as input, print each
name and email address pair having a valid email address on a new
line.

Hint: Try using Email.utils() to complete this challenge. For


example, this code:

import email.utils
print email.utils.parseaddr('DOSHI <[email protected]>')
print email.utils.formataddr(('DOSHI', '[email protected]'))
produces this output:

('DOSHI', '[email protected]')
DOSHI <[email protected]>'''

from email.utils import parseaddr,formataddr


import re
n = int(input())
pattern = r'^[A-Za-z][\w\.\-\_]+@[a-z]+\.([a-z]{1,3})$'
for i in range(n):
nameemail = input()
a = parseaddr(nameemail)
if re.match(pattern,a[1]):
print(nameemail)
'''CSS colors are defined using a hexadecimal (HEX) notation for
the combination of Red, Green, and Blue color values (RGB).

Specifications of HEX Color Code

■ It must start with a '#' symbol.


■ It can have 3 or 6 digits.
Each digit is in the range of 0 to F.
(0,1,2,3,4,5,6,7,8,9,A,B,C,D,E and F ).'''

import re
pattern = r'[\s:](#[0-9a-f]{3,6})'
for i in range(int(input())):
css = input()
matches = re.findall(pattern,css,re.IGNORECASE)
for m in matches:
print(m)

You might also like