Tuesday, March 15, 2022

Hacker Rank 30 day code Challenge (Inheritance and constructor in python)

 Hacker Rank 30 day code Challenge (Inheritance and constructor in python)

Problem:

The parent class is Person. Make a subclass Student. The Student has attributes first name, last name, Id, Scores.

Input: First Name, Last Name, ID, Number of scores (e.g 2 or 3) , Scores

Take the average of scores and print.

(Starting Person class code and Ending code is already provided in hacker rank editor)

class Person:
    def __init__(self, firstName, lastName, idNumber):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
    def printPerson(self):
        print("Name:"self.lastName + ","self.firstName)
        print("ID:"self.idNumber)

        
class Student(Person):
    def __init__(self, firstName, lastName, idNumber,scores):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
        self.scores = scores


    def calculate(self):
        sumscore=0
        for i in range(numScores):
            sumscore = sumscore +int(scores[i])
            avg = sumscore / numScores
       
        if avg >=90.0 and avg<=100.0:
            return 'O'
        elif avg >=80.0 and avg<=90.0:
            return 'E'
        elif avg>=70.0 and avg<=80.0:
            return 'A'
        elif avg>=55.0 and avg<=70.0:
            return 'P'
        elif avg>=40.0 and avg<=55.0:
            return 'D'
        else:
            return'T'



  

line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = listmap(intinput().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())

Monday, March 14, 2022

Hacker Rank 30 day code challenge Python

 Problem:

Convert a number 'n' to its binary base 2. Find base 10 integers 0,1. Find max number of consecutive 1's.

import math

no = int(input())
arr = []
while no // 2 > 0:
remainder = no % 2
arr.append(remainder)
no = no // 2

arr.append(no)
max = []

count = 0
for i in arr:

if i == 1:
count = count + 1
max.append(count)
else:
count = 0

max.sort()

print(max[-1]) 

Tuesday, March 8, 2022

Hacker Rank 30 Day Challenge(Recursion-Factorial Program)

 Hacker Rank 30 Day Challenge(Recursion-Factorial Program)

Problem:

For a number n entered by the user, calculate its factorial. Use recursion to calculate.

Example:

n=4

Factorial of 4 is : 4x3x2x1

Factorial of 5 is: 5x4x3x2x1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'factorial' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#

def factorial(n):
i=1
r=1
for i in range(n):
r=r*(i+1)
i=i+1
return r


n=int(input().strip())

result = factorial(n)
print(result)

Monday, March 7, 2022

Split() method in Python

Split() method:

  • Splits the string into a list of words. 
  • You can specify a separator (e.g ' '' !)
  • The default separator is whitespace.
  • We can take string input from the user. Use the split method and add the input to dictionary key-value pairs.

For example:

for i in range(n):
name=input()
phoneNum=name.split()
PhoneBook[phoneNum[0]]= phoneNum[1]

Another example:

string="Hello click this link"
list=string.split()
print(list)
OUTPUT:
['Hello', 'click', 'this', 'link']

Process finished with exit code 0

HackerRank 30 day challenge Python (Dictionaries)

HackerRank 30 day challenge Python (Dictionaries)
Problem:
Creating a phone book with names and phone numbers. Taking Input from user to search names in the dictionary and display the numbers in the format name=number.

n = int(input().strip())
PhoneBook={}


for i in range(n):
 name=input()
 phoneNum=name.split()
 PhoneBook[phoneNum[0]]= phoneNum[1]


while True:
  try:
   find = input().strip()
  except EOFError as e:
   break
  if find not in PhoneBook.keys():
    print("Not found")
  elif find in PhoneBook.keys():
   print(f'{find}={PhoneBook.get(find)}')




Sunday, March 6, 2022

HackerRank 30 day code challenge Day 7

 HackerRank 30 day code challenge Day 7

Problem:

Take input N this is the length of the array. Take input elements of the array of (length N). Elements are all integers. Print this array in reverse order on the same line and space separated.


import math
import os
import random
import re
import sys

n = int(input().strip())
arr=[]
arr = list(map(int, input().rstrip().split()))

i=len(arr)-1
#print(i)
while i<len(arr) and i>-1:
print(f'{arr[i]}' + " " ,end='')
i=i-1


Output:
6
1 2 3 4 5 6
6 5 4 3 2 1 
Process finished with exit code 0

Thursday, March 3, 2022

 HackerRank 30 Day Code Challenge(Day 6)

Problem:

Taking n input strings, separate each string's odd and even indexes and print the even-odd characters in form of two space-separated strings on a single line.

n=int(input())
x=0
evenstr=""
oddstr=""
i=0
for x in range(0,n):
s=input()
evenstr = ""
oddstr = ""

for i in range(0, len(s)):
if i % 2 == 0:
evenstr = evenstr + s[i]
elif i % 2 != 0:
oddstr = oddstr + s[i]
print(evenstr +' '+ oddstr )




 HackerRank Problem(30-day challenge)

Writing a table of number n:

import math
import os
import random
import re
import sys



n = int(input().strip())
i=1
for i in range(1,11):
print(f'{n}*{i}={n*i}')
i+=1