0% found this document useful (0 votes)
436 views

HackerRank Algorithm Problem Solving

The document contains code for 4 algorithm problems from HackerRank - Simple Array Sum, Compare the Triplets, A Very Big Sum, and Diagonal Difference. Each problem defines a function that takes an array or arrays as a parameter, performs some calculation on the elements, and returns the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
436 views

HackerRank Algorithm Problem Solving

The document contains code for 4 algorithm problems from HackerRank - Simple Array Sum, Compare the Triplets, A Very Big Sum, and Diagonal Difference. Each problem defines a function that takes an array or arrays as a parameter, performs some calculation on the elements, and returns the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

HackerRank Algorithm Problem Solving

Simple Array Sum


1. #!/bin/python3  
2.   
3. import os  
4. import sys  
5.   
6. #  
7. # Complete the simpleArraySum function below.  
8. #  
9. def simpleArraySum(ar):  
10.    return sum(ar)  
11.   
12.       
13.       
14.   
15.   
16. if __name__ == '__main__':  
17.     fptr = open(os.environ['OUTPUT_PATH'], 'w')  
18.   
19.     ar_count = int(input())  
20.   
21.     ar = list(map(int, input().rstrip().split()))  
22.   
23.     result = simpleArraySum(ar)  
24.   
25.     fptr.write(str(result) + '\n')  
26.   
27.     fptr.close()  

Compare the Triplets


1. #!/bin/python3  
2.   
3. import math  
4. import os  
5. import random  
6. import re  
7. import sys  
8.   
9. # Complete the compareTriplets function below.  
10. def compareTriplets(a, b):  
11.     alice = 0  
12.     bob = 0  
13.     for i in range(3):  
14.         if a[i] > b[i]:  
15.             alice += 1  
16.         elif a[i] < b[i]:  
17.             bob += 1  
18.     return (alice,bob)  
19.   
20. if __name__ == '__main__':  
21.     fptr = open(os.environ['OUTPUT_PATH'], 'w')  
22.   
23.     a = list(map(int, input().rstrip().split()))  
24.   
25.     b = list(map(int, input().rstrip().split()))  
26.   
27.     result = compareTriplets(a, b)  
28.   
29.     fptr.write(' '.join(map(str, result)))  
30.     fptr.write('\n')  
31.   
32.     fptr.close()  

A Very Big Sum


1. #!/bin/python3  
2.   
3. import math  
4. import os  
5. import random  
6. import re  
7. import sys  
8.   
9. # Complete the aVeryBigSum function below.  
10. def aVeryBigSum(ar):  
11.     return sum(ar)  
12.   
13. if __name__ == '__main__':  
14.     fptr = open(os.environ['OUTPUT_PATH'], 'w')  
15.   
16.     ar_count = int(input())  
17.   
18.     ar = list(map(int, input().rstrip().split()))  
19.   
20.     result = aVeryBigSum(ar)  
21.   
22.     fptr.write(str(result) + '\n')  
23.   
24.     fptr.close()  

Diagonal Difference
1. #!/bin/python3  
2.   
3. import math  
4. import os  
5. import random  
6. import re  
7. import sys  
8.   
9. #  
10. # Complete the 'diagonalDifference' function below.  
11. #  
12. # The function is expected to return an INTEGER.  
13. # The function accepts 2D_INTEGER_ARRAY arr as parameter.  
14. #  
15.   
16. def diagonalDifference(arr):  
17.     prim = 0  
18.     sec = 0  
19.     length = len(arr[0])  
20.     for count in range(length):  
21.         prim += arr[count][count]  
22.         sec += arr[count][(length-count-1)]  
23.     return abs(prim-sec)  
24.   
25.   
26.   
27. if __name__ == '__main__':  
28.     fptr = open(os.environ['OUTPUT_PATH'], 'w')  
29.   
30.     n = int(input().strip())  
31.   
32.     arr = []  
33.   
34.     for _ in range(n):  
35.         arr.append(list(map(int, input().rstrip().split())))  
36.   
37.     result = diagonalDifference(arr)  
38.   
39.     fptr.write(str(result) + '\n')  
40.   
41.     fptr.close() 

Plus Minus
1. #!/bin/python3  
2.   
3. import math  
4. import os  
5. import random  
6. import re  
7. import sys  
8.   
9. # Complete the plusMinus function below.  
10. def plusMinus(arr):  
11.     pos = 0  
12.     neg = 0  
13.     zero = 0  
14.      
15.     for i in range(len(arr)):  
16.         if arr[i] > 0:  
17.             pos += 1  
18.         elif arr[i] < 0:  
19.             neg += 1  
20.         else:  
21.             zero +=1  
22.       
23.     l = print(pos/len(arr))  
24.     m = print(neg/len(arr))  
25.     n = print(zero/len(arr))  
26.     
27.     return(l,m,n)  
28.   
29. if __name__ == '__main__':  
30.     n = int(input())  
31.   
32.     arr = list(map(int, input().rstrip().split()))  
33.   
34.     plusMinus(arr)  

You might also like