Python Hackerrank
Python Hackerrank
Introduction
1
Task
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input().strip())
if n%2 != 0:
print ("Weird")
else:
if 2 <= n <= 5:
print ("Not Weird")
else:
if 6<=n<=20:
print ("Weird")
else:
if n>20:
print ("Not Weird")
# print(check[
# n%2==0 and (
# n in range(2,6) or
# n > 20)
# ])
2
Task
The provided code stub reads two integers from STDIN, a and b. Add code to print three lines
where:
2. The second line contains the difference of the two numbers (first - second).
4. if __name__ == '__main__':
5. a = int(input())
6. b = int(input())
7. print (a+b)
8. print (a-b)
9. print (a*b)
10.
3
Task
The provided code stub reads two integers, and , from STDIN.
Add logic to print two lines. The first line should contain the result of integer division, a //b . The
if __name__ == '__main__':
a = int(input())
b = int(input())
print (a//b)
print (a/b)
4
Task
The provided code stub reads and integer,n , from STDIN. For all non-negative integers i<n,
print i2.
Example
N=3
The list of non-negative integers that are less than n=3 is[0,1,2] . Print the square of each
if __name__ == '__main__':
n = int(input())
for i in range(0,n):
print (i**2)
5
An extra day is added to the calendar almost every four years as February 29, and
the day is called a leap day. It corrects the calendar for the fact that our planet takes
approximately 365.25 days to orbit the sun. A leap year contains a leap day.
In the Gregorian calendar, three conditions are used to identify leap years:
unless:
year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years,
while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years. Source
Task
Given a year, determine whether it is a leap year. If it is a leap year, return the
Note that the code stub provided reads from STDIN and passes arguments to
Input Format
Output Format
The function must return a Boolean value (True/False). Output is handled by the
Sample Input 0
1990
Sample Output 0
False
Explanation 0
def is_leap(year):
leap = False
return leap
year = int(input())
print(is_leap(year))
6
The included code stub will read an integer, , from STDIN.
Example
Input Format
Constraints
Output Format
Sample Input 0
3
Sample Output 0
123
if __name__ == '__main__':
n = int(input())
for i in range(1,n+1):
print(i,end='') # this will not create a new line while
print()is executed
Regex and Parsing
7 Html Parser part-1
# Enter your code here. Read input from STDIN. Print output to
STDOUT
from html.parser import HTMLParser
import requests
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print ("Start :", tag)
for i in attrs:
print("->",i[0],">",i[1])
def handle_endtag(self, tag):
print ("End :", tag)
def handle_startendtag(self, tag, attrs):
print ("Empty :", tag)
for i in attrs:
print("->",i[0],">",i[1])
# instantiate the parser and fed it some HTML
parser = MyHTMLParser()
parser.feed(''.join(input().strip() for _ in range(int(input())))
)