Python Course, Hacker rank
Introduction
1
Task
Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive range of to , print Not Weird
If is even and in the inclusive range of to , print Weird
If is even and greater than , print Not Weird
#!/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")
# this can be done in other way
# check = {True: "Not Weird", False: "Weird"} # this is definin
g a dictionary
# 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:
1. The first line contains the sum of the two numbers.
2. The second line contains the difference of the two numbers (first - second).
3. The third line contains the product of the two numbers.
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
second line should contain the result of float division, a/b .
No rounding or formatting is necessary.
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
number on a separate line.
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:
The year can be evenly divided by 4, is a leap year, unless:
The year can be evenly divided by 100, it is NOT a leap year,
unless:
The year is also evenly divisible by 400. Then it is a leap
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
Boolean True, otherwise return False.
Note that the code stub provided reads from STDIN and passes arguments to
the is_leap function. It is only necessary to complete the is_leap function.
Input Format
Read , the year to test.
Output Format
The function must return a Boolean value (True/False). Output is handled by the
provided code stub.
Sample Input 0
1990
Sample Output 0
False
Explanation 0
1990 is not a multiple of 4 hence it's not a leap year.
def is_leap(year):
leap = False
# Write your logic here
if year%4 == 0:
leap = True
if year%100 == 0:
leap = False
if year%400 == 0:
leap = True
return leap
year = int(input())
print(is_leap(year))
6
The included code stub will read an integer, , from STDIN.
Without using any string methods, try to print the following:
Note that "" represents the consecutive values in between.
Example
Print the string .
Input Format
The first line contains an integer .
Constraints
Output Format
Print the list of integers from through as a string, without spaces.
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())))
)