Skip to content

Commit 890af98

Browse files
Merge pull request #64 from bharath-acchu/bharath
Added hackerrank solution
2 parents be4e497 + cc6f433 commit 890af98

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

HackerRank/Birthday_chocolate.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
3+
Lily has a chocolate bar that she wants to share it with Ron for his birthday. Each of the squares has an integer on it. She decides to share a contiguous segment of the bar selected such that the length of the segment matches Ron's birth month and the sum of the integers on the squares is equal to his birth day. You must determine how many ways she can divide the chocolate.
4+
5+
Consider the chocolate bar as an array of squares s = [2,2,1,3,2], .
6+
She wants to find segments summing to Ron's birth day,d = 4 with a length equalling his birth month, m = 2.
7+
In this case, there are two segments meeting her criteria: [2,2,] and [1,3].
8+
9+
Function Description
10+
11+
Complete the birthday function in the editor below. It should return an integer denoting the number of ways Lily can divide the chocolate bar.
12+
13+
birthday has the following parameter(s):
14+
15+
s: an array of integers, the numbers on each of the squares of chocolate
16+
d: an integer, Ron's birth day
17+
m: an integer, Ron's birth month
18+
19+
20+
21+
Input Format
22+
23+
The first line contains an integern , the number of squares in the chocolate bar.
24+
The second line contains n space-separated integers s[i], the numbers on the chocolate squares where 0<= i <n.
25+
The third line contains two space-separated integers, d and m, Ron's birth day and his birth month.
26+
27+
28+
Sample Input 0
29+
30+
5
31+
1 2 1 3 2
32+
3 2
33+
Sample Output 0
34+
35+
2
36+
37+
38+
'''
39+
40+
import math
41+
import os
42+
import random
43+
import re
44+
import sys
45+
46+
# Complete the birthday function below.
47+
def birthday(s, d, m):
48+
49+
tot_p = (len(s)-m)+1
50+
return len([1 for i in range(tot_p) if sum(s[i:i+m]) == d])
51+
52+
if __name__ == '__main__':
53+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
54+
55+
n = int(input().strip())
56+
57+
s = list(map(int, input().rstrip().split()))
58+
59+
dm = input().rstrip().split()
60+
61+
d = int(dm[0])
62+
63+
m = int(dm[1])
64+
65+
result = birthday(s, d, m)
66+
67+
fptr.write(str(result) + '\n')
68+
69+
fptr.close()

0 commit comments

Comments
 (0)