Algorithm 67

[프로그래머스]_Level 1_행렬의 덧셈_파이썬

2021.09.03 def solution(arr1, arr2): for i in range(len(arr1)): for j in range(len(arr1[i])): arr1[i][j] += arr2[i][j] return arr1 2개 중 0개 성공, TypeError : Object of type ndarray is not JSON serializable import numpy as np def solution(arr1, arr2): a = np.array(arr1, np.float) b = np.array(arr1, np.float) return a + b numpy 사용하면 쉬울 것 같았는데 TypeError..... 아래와 같이 행렬 arr1과 arr2는 중첩리스트로 구성되어 있다. arr1 ..

[프로그래머스]_Level 1_x만큼 간격이 있는 n개의 숫자_파이썬

2021.09.03 def solution(x, n): answer = [] for i in range(int(n)): answer.append((i+1) * int(x)) return answer NameError def sol(x, n): a = [a.append((i+1) * int(x)) for i in range(int(n))] return a 리스트를 만들어서 출력하고 싶었는데 list 본인은 list에 넣을 수 없었다. 본인을 넣을 수 없으면 그냥 바로 return 하면 되는걸...... def sol(x, n): return [(i+1) * int(x) for i in range(int(n))]

[프로그래머스]_Level 1_직사각형 별찍기_파이썬

2021.09.03 a, b = map(int, input().strip().split(' ')) print(('*'*a + '\n')* b) 2개 중 1개 성공, 실행한 결과값 ***이(가) 기댓값 *****와(과) 다릅니다. a, b = map(int, input().split()) for i in range(a): print('*'*b) a와 b를 거꾸로 생각했는데도 50%의 정답률이 나온건 정말......ㅎ....... map() map(f, iterable)은 함수(f)와 반복 가능한(iterable) 자료형을 입력으로 받는다. map은 입력받은 자료형의 각 요소를 함수 f가 수행한 결과를 묶어서 돌려주는 함수이다. input() 사용자 입력함수, 입력되는 모든 것을 문자열로 취급한다. 문자열 ..

[프로그래머스]_Level 1_완주하지 못한 선수_파이썬

2021.09.03 def solution(participant, completion): participant.sort() completion.sort() for i in range(len(completion)): if participant[i] != completion[i]: return participant[i] return participant[i+1] 문제에서 완주하지 못한 선수는 참여한 선수 중 한 명이고, 참가자 중 동명이인이 있을 수 있다는 조건이 주어졌다. 마라톤을 참여한 선수의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어졌을 때, sort()를 이용해 정렬하면 아래와 같은 결과가 나온다. participant = ["mislav",..

210830_[백준]_알고리즘 기초_9093번 : 단어 뒤집기_python

https://www.acmicpc.net/problem/9093 9093번: 단어 뒤집기 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 www.acmicpc.net n = int(input()) for i in range(n): sentence = list(input().split()) for j in sentence: print(j[::-1], end=' ')

Algorithm/Beakjoon 2021.08.30

210830_[백준]_알고리즘 기초_10828번 : 스택_python

https://www.acmicpc.net/problem/10828 10828번: 스택 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 www.acmicpc.net 시간 초과 코드 n = int(input()) stack = list() for i in range(n): word = list(map(str,input().split())) if word[0] == 'push': stack.append(int(word[1])) elif word[0] == 'pop': if len(stack) == 0: print(-1) # pop() : 지운 ..

Algorithm/Beakjoon 2021.08.30

210825_[백준]_정렬_python

1181번 # 단어 정렬 n = int(input()) words = [str(input()) for i in range(n)] words = list(set(words)) # lambda : 익명함수 # lambda 인자(words의 인자) : 표현식((글자수, 글자)) # key = 정렬을 목적으로 하는 함수를 값으로 넣음 words.sort(key = lambda x : (len(x), x)) print('\n'.join(words)) 10814번 # 나이순 정렬 n = int(input()) m = [] for i in range(n): age, name = map(str, input().split()) age = int(age) m.append((age, name)) m.sort(key = la..

Algorithm/Beakjoon 2021.08.25

210824_[백준]_정렬_2108번 : 통계학(?)_python

ValueError: invalid literal for int() with base 10: '' 이거 해결 될 때까지는 계속 시간초과 뜰 듯.... 쓸 수가 없는데 어떻게 하냐고...... import statistics from collections import Counter n = int(input()) n_list = [int(input()) for i in range(n)] # 산술평균 print(int(round(statistics.mean(n_list),0))) # 중앙값 print(statistics.median(n_list)) # 최빈값...보다 하나 작은 값 k = Counter(n_list).most_common() if len(n_list) > 1 : if k[0][1] == k[1..

Algorithm/Beakjoon 2021.08.24