-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHackerrank_solutions.py
More file actions
106 lines (87 loc) · 2.92 KB
/
Hackerrank_solutions.py
File metadata and controls
106 lines (87 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#Questions 1:
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):
Person.__init__(self, firstName, lastName,idNumber)
self.scores= scores
# Class Constructor
#
# Parameters:
# firstName - A string denoting the Person's first name.
# lastName - A string denoting the Person's last name.
# id - An integer denoting the Person's ID number.
# scores - An array of integers denoting the Person's test scores.
#
# Write your constructor here
# Function Name: calculate
# Return: A character denoting the grade.
#
# Write your function here
def calculate (self):
sum=0
for score in scores:
sum += score
average =sum /len(scores)
if average < 40:
return "T"
elif average <55:
return "D"
elif average <70:
return "P"
elif average < 80:
return "A"
elif average <90:
return "E"
else:
return "O"
line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())
#Questions 2:
import math
class Complex(object):
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, no):
return Complex(self.real+no.real,self.imaginary+no.imaginary)
def __sub__(self, no):
return Complex(self.real-no.real,self.imaginary-no.imaginary)
def __mul__(self, no):
r=self.real*no.real-self.imaginary*no.imaginary
i=self.real*no.imaginary+self.imaginary*no.real
return Complex(r,i)
def __truediv__(self, no):
d=no.real**2+no.imaginary**2
n=self*Complex(no.real,-1*no.imaginary)
return Complex(n.real/d,n.imaginary/d)
def mod(self):
d=self.real**2+self.imaginary**2
return Complex(math.sqrt(d),0)
def __str__(self):
if self.imaginary == 0:
result = "%.2f+0.00i" % (self.real)
elif self.real == 0:
if self.imaginary >= 0:
result = "0.00+%.2fi" % (self.imaginary)
else:
result = "0.00-%.2fi" % (abs(self.imaginary))
elif self.imaginary > 0:
result = "%.2f+%.2fi" % (self.real, self.imaginary)
else:
result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
return result
if __name__ == '__main__':