본문 바로가기
728x90
320x100

💻 하나씩 차곡차곡/프로그래머스 (Python)93

[프로그래머스/python/Lv1] 숫자 문자열과 영단어 def solution(s): english = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] answer = 0 for i, v in enumerate(english) : s = s.replace(english[i], number[i]) return int(s) (+) 다른 사람 풀이 num_dic = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "ni.. 2023. 12. 20.
[프로그래머스/python/Lv1] 예산 def solution(d, budget): result = 0 answer = 0 for i in sorted(d): result += i if result > budget: return answer answer += 1 return answer 나는 더하는 방식으로 했는데 다른 분들은 예산에서 빼는 방식으로 했더라. (+) 다른 사람 풀이 def solution(d, budget): answer = 0 # 가능한 부서의 수 for i in sorted(d): budget -= i if budget < 0: break answer += 1 return answer 2023. 12. 19.
[프로그래머스/python/Lv1] 3진법 뒤집기 def solution(n): result='' while n : result += str(n%3) n=n//3 return int(result,3) int(result, 3) -> 3진법인 result을 10진법으로 바꿔줌. 2023. 12. 18.
[프로그래머스/python/Lv1] 부족한 금액 계산하기 def solution(price, money, count): answer = 0 for i in range(1, count+1): answer += price*i if answer 2023. 12. 17.
[프로그래머스/python/Lv1] 약수의 갯수와 덧셈 def solution(left, right): answer = 0 cnt = 0 list = [] for i in range(left, right+1): for j in range(1, i+1): if i % j == 0: cnt += 1 if cnt % 2 == 0: answer += i cnt = 0 else : answer -= i cnt = 0 return answerex) 20의 약수를 구할 땐 20 / 1~20까지를 나눴을 때 0 인 것들이 약수임. 맨날 까먹냐 진짜.  (+) 쬑금 개선def soluti.. 2023. 12. 16.
[프로그래머스/python/Lv1] 내적 def solution(a, b): answer = 0 for i, v in enumerate(a): answer += a[i]*b[i] return answer 이정도면 enumerate 러버... (+) 다른 사람 풀이 def solution(a, b): return sum([x*y for x, y in zip(a,b)]) zip() 이라는 내장함수를 사용했더라. 저렇게 하면 a,b를 나란히 출력할 수있음. 근데 만약에 a,b의 길이가 다르면 길이가 짧은 걸로 맞춰지니까 주의하길. 2023. 12. 15.
728x90
320x100