Python2 HackerRank刷题ing(持续更新)

Python2 HackerRank刷题ing(持续更新)

一月 16, 2019
1
2
刷题时在YouTube上看到一个外国小哥在刷题讲解,收藏下
https://www.youtube.com/watch?v=WUeKQc_sRBw

Solve Me First

1
2
3
4
5
6
7
8
def solveMeFirst(a,b):
# Hint: Type return a+b below
return int(a) + int(b)

num1 = input()
num2 = input()
res = solveMeFirst(num1,num2)
print res

Simple Array Sum

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
#!/bin/python

from __future__ import print_function

import os
import sys

#
# Complete the simpleArraySum function below.
#
def simpleArraySum(ar):
#
# Write your code here.
#
return sum(ar)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

ar_count = int(raw_input())

ar = map(int, raw_input().rstrip().split())

result = simpleArraySum(ar)

fptr.write(str(result) + '\n')

fptr.close()

Compare the Triplets

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
#!/bin/python

import math
import os
import random
import re
import sys

# Complete the compareTriplets function below.
def compareTriplets(a, b):
out1=0
out2=0
for i in range(len(a)):
if a[i] > b[i]:
out1+=1
elif a[i] == b[i]:
pass
elif a[i] < b[i]:
out2+=1
return out1,out2

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

a = map(int, raw_input().rstrip().split())

b = map(int, raw_input().rstrip().split())

result = compareTriplets(a, b)

fptr.write(' '.join(map(str, result)))
fptr.write('\n')

fptr.close()

A Very Big Sum

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
#!/bin/python

import math
import os
import random
import re
import sys

# Complete the aVeryBigSum function below.
def aVeryBigSum(ar):
# the_sum = 0
# for i in ar:
# the_sum += i
# return the_sum
res = 0
for i in range(len(ar)):
res += ar[i]
return res
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

ar_count = int(raw_input())

ar = map(long, raw_input().rstrip().split())

result = aVeryBigSum(ar)

fptr.write(str(result) + '\n')

fptr.close()

Diagonal Difference

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
#!/bin/python

import math
import os
import random
import re
import sys

# Complete the diagonalDifference function below.
def diagonalDifference(arr):

n=len(arr)
sum1 = 0
sum2 = 0
for i in range(n):
sum1 += int(arr[i][i])
sum2 += int(arr[i][n-i-1])
return abs(sum1 - sum2)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(raw_input())

arr = []

for _ in xrange(n):
arr.append(map(int, raw_input().rstrip().split()))

result = diagonalDifference(arr)

fptr.write(str(result) + '\n')

fptr.close()

Plus Minus

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
#!/bin/python

import math
import os
import random
import re
import sys
from decimal import Decimal
# Complete the plusMinus function below.
def plusMinus(arr):

positive = 0
negative = 0
zero = 0
n=len(arr)
for i in range(len(arr)):
if arr[i] > 0:
positive += 1
elif arr[i] < 0:
negative += 1
elif arr[i] == 0:
zero += 1
print(positive/Decimal(n))
print(negative/Decimal(n))
print(zero/Decimal(n))


if __name__ == '__main__':
n = int(raw_input())

arr = map(int, raw_input().rstrip().split())

plusMinus(arr)

hacker.png
虽然刷的题很简单,但看到这个还是很感动,因为自己真的用心去做了,接下来要更加努力继续学习刷题ing

Staircase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/python

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
for i in range(1,n+1):
print(('#' * i).rjust(n,' '))
if __name__ == '__main__':
n = int(raw_input())

staircase(n)

Mini-Max Sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/python

import math
import os
import random
import re
import sys

# Complete the miniMaxSum function below.
def miniMaxSum(arr):
arr=sorted(arr)
min = sum(arr[:-1])
max = sum(arr[1:])
print('{} {}'.format(min,max))

if __name__ == '__main__':
arr = map(int, raw_input().rstrip().split())

miniMaxSum(arr)

Birthday Cake Candles

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
#!/bin/python

import math
import os
import random
import re
import sys

# Complete the birthdayCakeCandles function below.
def birthdayCakeCandles(ar):
return ar.count(max(ar))
//return(sum([i==max(ar) for i in ar]) )

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

ar_count = int(raw_input())

ar = map(int, raw_input().rstrip().split())

result = birthdayCakeCandles(ar)

fptr.write(str(result) + '\n')

fptr.close()

Time Conversion

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
#!/bin/python

from __future__ import print_function

import os
import sys

#
# Complete the timeConversion function below.
#
def timeConversion(s):
#
# Write your code here.
#
if(int(s[0:2])<12 and s[8:10]=='PM'):
hours=str(int(s[0:2])+12)
if(int(s[0:2])<12 and s[8:10]=='AM'):
hours=s[0:2]
if(int(s[0:2])==12 and s[8:10]=='AM'):
hours='00'
if(int(s[0:2])==12 and s[8:10]=='PM'):
hours='12'

return(hours+s[2:8])

if __name__ == '__main__':
f = open(os.environ['OUTPUT_PATH'], 'w')

s = raw_input()

result = timeConversion(s)

f.write(result + '\n')

f.close()

这个题遇到2个问题,一个是时间,原来12AM是00,12PM是12,我一直以为晚上12点就是12PM,也就是00,其实到现在我都是这么认为的。。被时间搞糊涂了。还有一个问题是数组切片,数组[起点:终点:步长]

easy.png
刷到这里,提示之前的题都是热身项目!!!挑战才刚刚开始。 /(ㄒoㄒ)/~ 以泪洗面