본문 바로가기

Algorithm5

[HackerRank] 20. Birthday chocolate Birthday Chocolate 초콜렛 바는 연속되어야 한다. list 의 끝 인덱스가 넘어가면 마지막 인덱스까지 추출한다.\ sum, len을 반환하여 로직을 세운다. import math import os # Complete the birthday function below. def birthday(s, d, m): count = 0 for i in range(len(s)): temp = s[i:i+m] if sum(temp) == d: if len(temp) == m: count += 1 return count if __name__ == '__main__': n = int(input().strip()) s = list(map(int, input().rstrip().split())) dm = inp.. 2020. 4. 1.
[HackerRank] 19. Bon Appétit Bon Appétit import math # Complete the bonAppetit function below. def bonAppetit(bill, k, b): half_total = sum(bill) / 2 bill.pop(k) anna_actual = sum(bill) / 2 if b == anna_actual: print('Bon Appetit') else: print(int(half_total - anna_actual)) if __name__ == '__main__': nk = input().rstrip().split() n = int(nk[0]) k = int(nk[1]) bill = list(map(int, input().rstrip().split())) b = int(input().s.. 2020. 4. 1.
[HackerRank] 18. Breaking the Records Breaking the Records import math import os import random import re import sys # Complete the breakingRecords function below. def breakingRecords(scores): max_score = scores[0] min_score = scores[0] max_count = 0 min_count = 0 for score in scores: if max_score score: min_score = score min_count += 1 return max_count, min_count if __name__.. 2020. 4. 1.
[HackerRank] 17. Counting Valleys Counting Valleys def countingValleys(n, s): height = 0 pre_height = 0 result = 0 for i in s: # 이전 높이 계산 pre_height = height # 현재 높이 계산 if i =='D': height -= 1 else: height += 1 if height == 0 and pre_height < 0: result += 1 return result def main(): n = int(input()) s = input() result = countingValleys(n, s) print(result) if __name__ == '__main__': main() 2020. 4. 1.
[HackerRank] 16. Day of the programmer Day of the Programmer #!/bin/python3 import math import os import random import re import sys # Complete the dayOfProgrammer function below. def solve(year): if (year == 1918): return '26.09.1918' elif ((year 1918) & (year%400 == 0 or ((year%4 == 0) & (year%100 != 0)))): return '12.09.%s' %year else: return '13.09.%s' %year if __name__ == '__main__': year = int(input().strip()) result = dayOfPro.. 2020. 4. 1.