본문 바로가기

HackerRank Algorithm27

[HackerRank] 27. Picking Numbers def pickingNumbers(a): high = 1 low = 1 maximum = 0 for n in a: high = a.count(n+1) + a.count(n) low = a.count(n-1) + a.count(n) maximum = max(maximum, high, low) return maximum if __name__ == '__main__': n = int(input().strip()) a = list(map(int, input().rstrip().split())) result = pickingNumbers(a) print(result) 2020. 4. 7.
[HackerRank] 26. forming a magic square def CreateMagicSquare(): # 3x3 magic square 모든 경우의 수 생성 num = set(range(1, 10)) group = [] magic_square = [] for a in range(1, 10): for b in range(1, 10): for c in range(1, 10): if (a + b + c == 15) & (a != b) & (a != c) & (b != c): temp = [a, b, c] group.append(temp) for i in range(len(group)): for j in range(len(group)): for k in range(len(group)): if set(group[i] + group[j] + group[k]) == num.. 2020. 4. 7.
[HackerRank] 25. Cats and Mouse # My code def catAndMouse(a, b, c): a_distance = abs(a - c) b_distance = abs(b - c) if a_distance > b_distance: return 'Cat B' elif a_distance < b_distance: return 'Cat A' else: return 'Mouse C' if __name__ == '__main__': q = int(input()) for q_itr in range(q): abc = input().split() a = int(abc[0]) b = int(abc[1]) c = int(abc[2]) result = catAndMouse(a, b, c) print(result) 2020. 4. 7.
[HackerRank] 24. Electronics shop # My code def getMoneySpent(keyboards, drives, b): temp = -1 for keyboard in keyboards: for drive in drives: if keyboard + drive 2020. 4. 7.
[HackerRank] 23. Apple and orange Implementation # 방법1 # Complete the countApplesAndOranges function below. def countApplesAndOranges(s, t, a, b, apples, oranges): # Computing distance apples = [a + apple for apple in apples] oranges = [b + orange for orange in oranges] # apples & oranges in the range in_apples = [apple for apple in apples if (apple >= s) and (apple = s) and (orange 2020. 4. 7.
[HackerRank] 22. Between Two Sets Between Two Sets You will be given two arrays of integers and asked to determine all integers that satisfy the following two conditions: The elements of the first array are all factors of the integer being considered The integer being considered is a factor of all elements of the second array 문제 요약 A의 요소로 나누어 떨어지고 B 요소를 나누어 떨어지게 하는 수의 개수 # 방법1 import math from functools import reduce # # Complet.. 2020. 4. 7.
[HackerRank] 21. Birthday cake candles # Complete the birthdayCakeCandles function below. def birthdayCakeCandles(ar): max_ = max(ar) survived_candles = [v for v in ar if v == max_] return len(survived_candles) if __name__ == '__main__': ar_count = int(input()) ar = list(map(int, input().rstrip().split())) result = birthdayCakeCandles(ar) print(result) def max_candles_count(num, candles_height): if num != len(candles_height): return .. 2020. 4. 1.
[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.