Hackerrank Answers
Hackerrank Answers
python if else:
n=int(input())
if(n%2==0 and 2<n<5):
print("Not Weird")
elif(n%2==0and 6<n<20):
print("Weird")
elif(n%2==1):
print("Weird")
elif(n%2==0 and n>20):
print("Not Weird")
else:
print("Weird")
2.Arithmetic operators:
a=int(input())
b=int(input())
print(a+b)
print(a-b)
print(a*b)
3.python:division
a=int(input())
b=int(input())
print(a//b)
print(a/b)
4.loops
a=int(input())
for i in range(a):
print(i*i)
5.write a function
def is_leap(year):
leap = False
print function:
n = int(input())
for i in range(1,n+1):
print(i,end="")
6.list comprehensions:
x = int(input())
y = int(input())
z = int(input())
n = int(input())
10.tuples:
n = int(input())
integer_list = map(int, input().split())
tuple=tuple(integer_list)
print(hash(tuple))
11.swap case:
def swap_case(s):
swap=""
for char in s:
if char.islower():
swap+=char.upper()
elif char.isupper():
swap+=char.lower()
else:
swap+=char
return swap
def split_and_join(line):
# write your code here
b=line.split(" ")
result="-".join(b)
print(result)
if __name__ == '__main__':
line = input()
result = split_and_join(line)
17.calender module
from datetime import datetime
def get_day_of_week(date):
date_obj = datetime.strptime(date, "%m %d %Y")
day_of_week = date_obj.strftime("%A")
return day_of_week.upper()
# Test the function with sample input
date = input()
day_of_week = get_day_of_week(date)
print(day_of_week)
18.exceptions
n=int(input())
for _ in range(n):
try:
a, b = map(int, input().split())
result = a // b
print(result)
except ZeroDivisionError as e:
print("Error Code:", e)
except ValueError as e:
print("Error Code:", e)
19.Time delta
from datetime import datetime, timedelta
# Function to convert the timestamp string to a datetime object
def parse_timestamp(timestamp):
return datetime.strptime(timestamp, "%a %d %b %Y %H:%M:%S %z")
20.No idea!
def calculate_happiness(array, set_A, set_B):
happiness = 0
for num in array:
if num in set_A:
happiness += 1
elif num in set_B:
happiness -= 1
return happiness
# Read input from stdin
n, m = map(int, input().split())
array = list(map(int, input().split()))
set_A = set(map(int, input().split()))
set_B = set(map(int, input().split()))
22.symmetric difference
M = int(input())
set_a = set(map(int, input().split()))
N = int(input())
set_b = set(map(int, input().split()))
# Calculate the symmetric difference
symmetric_diff = set_a.symmetric_difference(set_b)
# Sort the symmetric difference in ascending order
sorted_diff = sorted(symmetric_diff)
# Print the symmetric difference, one per line
for num in sorted_diff:
print(num)
23.itertools().combinations:
from itertools import combinations
# Read input from stdin
string, size = input().split()
size = int(size)
# Sort the string
string = sorted(string)
# Generate and print the combinations
for r in range(1, size+1):
for comb in combinations(string, r):
print(''.join(comb))
24.set.add()
def count_stamps(n):
stamps=set()
for i in range(n):
country=input()
stamps.add(country)
return len(stamps)
n=int(input())
b=count_stamps(n)
print(b)
25.maximize it:
from itertools import product
n, m = map(int, input().split())
lists = []
for _ in range(n):
lists.append(list(map(int, input().split()[1:])))
max_value = 0
for elements in product(*lists):
value = sum(x**2 for x in elements) % m
max_value = max(max_value, value)
print(max_value)