Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save caot/17387aba8ee4b933342537d7a780f02d to your computer and use it in GitHub Desktop.
Save caot/17387aba8ee4b933342537d7a780f02d to your computer and use it in GitHub Desktop.
[Hackerrank] Solution of Birthday Chocolate in JavaScript
function birthday(s, d, m) {
let result = 0;
for(let i = 0; i < s.length - (m - 1); i++) {
if (s.slice(i, i + m).reduce((r, v) => r + v, 0) === d) {
result++;
}
}
return result;
}
@caot
Copy link
Author

caot commented Apr 14, 2022

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'birthday' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER_ARRAY s
#  2. INTEGER d
#  3. INTEGER m
#

def birthday(s, d, m):
    # Write your code here
    from functools import reduce

    result = 0
    for i in range(len(s) - (m - 1)):
        if reduce(lambda x, y: x+y, s[i: i + m]) == d:
            result += 1

    return result

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

    n = int(input().strip())

    s = list(map(int, input().rstrip().split()))

    first_multiple_input = input().rstrip().split()

    d = int(first_multiple_input[0])

    m = int(first_multiple_input[1])

    result = birthday(s, d, m)

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

    fptr.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment