-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_ex2.py
243 lines (176 loc) · 4.31 KB
/
script_ex2.py
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
237
238
239
240
241
242
243
#1-----Birthday Cake Candles-----
import math
import os
import random
import re
import sys
#
# Complete the 'birthdayCakeCandles' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY candles as parameter.
#
def birthdayCakeCandles(candles):
# Write your code here
max = 0
for h in candles:
if h > max:
max = h
return candles.count(max)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
candles_count = int(input().strip())
candles = list(map(int, input().rstrip().split()))
result = birthdayCakeCandles(candles)
fptr.write(str(result) + '\n')
fptr.close()
#2-----Number Line Jumps-----
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'kangaroo' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. INTEGER x1
# 2. INTEGER v1
# 3. INTEGER x2
# 4. INTEGER v2
#
def kangaroo(x1, v1, x2, v2):
# Write your code here
if v1 != v2:
t = (x1-x2)/(v2-v1)
check = str(t).split('.')
if t >= 0 and check[-1] == '0':
return 'YES'
return 'NO'
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
x1 = int(first_multiple_input[0])
v1 = int(first_multiple_input[1])
x2 = int(first_multiple_input[2])
v2 = int(first_multiple_input[3])
result = kangaroo(x1, v1, x2, v2)
fptr.write(result + '\n')
fptr.close()
#3-----Viral Advertising-----
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'viralAdvertising' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#
def viralAdvertising(n):
# Write your code here
people = 5
result = 0
for _ in range(n):
people = math.floor(people/2)
result += people
people *= 3
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
result = viralAdvertising(n)
fptr.write(str(result) + '\n')
fptr.close()
#4-----Recursive Digit Sum-----
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'superDigit' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING n
# 2. INTEGER k
#
def superDigit(n, k):
# Write your code here
if len(n) == 1:
return int(n)
n = list(map(int, n))
super_digit = sum(n)*k
return superDigit(str(super_digit),1)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = first_multiple_input[0]
k = int(first_multiple_input[1])
result = superDigit(n, k)
fptr.write(str(result) + '\n')
fptr.close()
#5-----Insertion Sort - Part 1-----
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'insertionSort1' function below.
#
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER_ARRAY arr
#
def insertionSort1(n, arr):
# Write your code here
for i in range(1, len(arr)):
x = arr[i]
j = i-1
while j >=0 and x < arr[j]:
arr[j+1] = arr[j]
print(' '.join(map(str,arr)))
j -= 1
arr[j+1] = x
print(' '.join(map(str,arr)))
if __name__ == '__main__':
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
insertionSort1(n, arr)
#6-----Insertion Sort - Part 2-----
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'insertionSort2' function below.
#
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER_ARRAY arr
#
def insertionSort2(n, arr):
# Write your code here
for i in range(1, len(arr)):
x = arr[i]
j = i-1
while j >=0 and x < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = x
print(' '.join(map(str,arr)))
if __name__ == '__main__':
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
insertionSort2(n, arr)