-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackerrank.py
More file actions
236 lines (167 loc) · 6.71 KB
/
Copy pathhackerrank.py
File metadata and controls
236 lines (167 loc) · 6.71 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
*** '''Task 1'''
https://www.hackerrank.com/challenges/py-if-else/problem
Given an integer, , n perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5 , print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20 , print Not Weird
Input Format
A single line containing a positive integer, n .
Constraints
1<=n<=100
Output Format
Print Weird if the number is weird. Otherwise, print Not Weird.
-----------
import sys
N = int(input().strip())
if N % 2 == 1 :
print ('Weird')
else:
if N <= 5 :
print ("Not Weird")
elif N <= 20:
print ("Weird")
else:
print ("Not Weird")
'''Task 2'''
https://www.hackerrank.com/challenges/python-arithmetic-operators/problem
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.
-------
a=int(input())
b=int(input())
print (a+b)
print (a-b)
print (a*b)
-------------------------
a = 3
b = 5
for i in [a+b, a-b, a*b]: print(i)
-----------------------------------------------
'''Task 3'''kod isleyir inpu edende error cixir
https://www.hackerrank.com/challenges/python-division/problem?isFullScreen=true
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.
#I)
a=input()
b=input()
x=a/b
y=float(a)/b
print (x)
print (y)
#2)
a = input()
b = input()
print (a//b)
print (a/b)
--------------------------------------------
'''Task 4'''
https://www.hackerrank.com/challenges/python-loops/problem
The provided code stub reads and integer,n, from STDIN. For all non-negative integers i<n, print i^^2.
n=int(input())
for i in range(n):
print (i**2)
---------------------------
'''Task5'''
https://www.hackerrank.com/challenges/write-a-function/problem?isFullScreen=true
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.
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.
def is_leap(year):
leap = False
if year % 4 == 0:
leap = True
if year % 100 == 0:
leap = False
if year % 400 == 0:
leap = True
return leap
--------------------------------
'''Task6'''
https://www.hackerrank.com/challenges/python-print/problem?isFullScreen=true
The included code stub will read an integer,n, from STDIN.
Without using any string methods, try to print the following:
123...n
Note that "" represents the consecutive values in between.
for i in range(1,int(input())+1):
print(i,sep='',end='')
------------------------------
'''Task7'''
https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list/problem?isFullScreen=true
Given the participants score sheet for your University Sports Day, you are required to find the runner-up score.
You are given n scores. Store them in a list and find the score of the runner-up.
Input Format
The first line contains n. The second line contains an array A[] of n integers each separated by a space.
Output Format
Print the runner-up score.
n=int(input())
a=list(map(int, input().split()))
a=list(set(a))
a.sort()
print(a[len(a)-2])
--------------
'Task8'
https://www.hackerrank.com/challenges/nested-list/problem?isFullScreen=true
Given the names and grades for each student in a class of N students,
store them in a nested list and print the name(s) of any student(s) having the second lowest grade.
xs = [(input(), float(input())) for _ in range(int(input()))]
min_mark = min(x[1] for x in xs)
xs = [x for x in xs if x[1] > min_mark]
min2_mark = min(x[1] for x in xs)
xs = sorted([x[0] for x in xs if x[1] == min2_mark])
for x in xs:
print(x)
-------------------
'Task9'
https://www.hackerrank.com/challenges/finding-the-percentage/problem?isFullScreen=true
The provided code stub will read in a dictionary containing key/value pairs of name:[marks] for a list of students.
Print the average of the marks array for the student name provided, showing 2 places after the decimal.
Example
marks key:value pairs are
'alpha':[20,30,40]
'beta':[30,50,70]
query_name='beta'
The query_name is 'beta'. beta''s average score is .(30+50+70)/3=50.0
Input Format
(30+50+70)/3=50.0
The first line contains the integer n , the number of students records.
The next n lines contain the names and marks obtained by a student, each value separated by a space.
The final line contains query_name, the name of a student to query.
Output Format
Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.
-----------
data = {}
for _ in range(int(input())):
name, *marks = input().split()
data[name] = [float(m) for m in marks]
marks = data[input()]
print("%.2f" % (sum(marks)/len(marks)))
-----------------
'Task10'
https://www.hackerrank.com/challenges/swap-case/problem?isFullScreen=true
You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.
For Example:
Www.HackerRank.com → wWW.hACKERrANK.COM
Pythonist 2 → pYTHONIST 2
Function Description
Complete the swap_case function in the editor below.
swap_case has the following parameters:
*string s: the string to modify
Returns
*string: the modified string
Input Format
A single line containing a string s.
print(input().swapcase())
----------------------------------------------