Coding 42

[python]리스트 값 추가, 삭제/요소 곱 - reduce, prod)

a_li = [1,2,3]※ 리스트.append(값)마지막에 값 추가a_li.append(4)   # [1,2,3,4] ※ 리스트.insert(인덱스, 값)a_li.insert(1, 5)   # [1,5,2,3,4] ※ del 리스트[인덱스]인덱스 위치의 요소 삭제del a_li[2]   # [1,5,3,4] ※ 리스트.remove(값)리스트 내의 값 삭제 >> 없으면 ValueErrora_li.remove(5)   # [1,3,4]del a_li[a_li.index(3)]   # [1,4]# 프로그래머스 마지막 두 원소def solution(li): li.append(li[-1]-li[-2] if li[-1] > li[-2] else 2*li[-1]) return li ※ reduce(함수,..

Coding/Python 2024.04.17

[python]문자열 인덱싱

※ 콜론[:]을 활용한 문자열 인덱싱text = 'abcdef'text[0] = 'a'text[0:2] = 'ab'text[-2:0] = 'ef'text[:-3] = 'abc'text[::2] = 'ace'  # 0번째 문자부터 끝까지 2개씩 건너뛰기# 프로그래머스 lv0 코드 처리하기def solution(code): mode = False # 0 ret = "" for i in range(len(code)): if code[i] == "1": # 숫자 1이 아닌 문자열 1이라는 것 놓치지 않기! mode = not mode elif (mode == False and i%2 == 0) or (mode == True and i%2 == ..

Coding/Python 2024.04.17

[python]조건문

if-else문 관련 문제 풀이 # 프로그래머스 조건 문자열 def solution(ineq, eq, n, m): if ineq == '>': if eq == '=': return int(n >= m) else: return int(n > m) else: if eq == '=': return int(n 오히려 runtime 오래 걸려서 시간 초과됨... # 프로그래머스 콜라츠 수열 # 초반 풀이 - 시간 초과ㅠㅡㅠ def solution(n): answer = [] while n > 1: answer.append(n/2 if n%2 == 0 else 3*n + 1) print(answer) answer.append(1) return answer # 수정 후 answer = [] while n > 1: a..

Coding/Python 2024.04.17

[python]join 함수

※ '구분자'.join(리스트) 리스트 → 문자열 li = ['apple', 'banana', 'kiwi'] print(''.join(li))# applebananakiwi print(' '.join(li))# apple banana kiwi # 프로그래머스 문자열 돌리기 # input(): "abcde" print('_'.join(x for x in input()))# a_b_c_d_e print('\n'.join(x for x in input().strip()))# print('\n'.join(input())) # 프로그래머스 문자열 섞기# aaaaa, bbbbb => ababababab def solution(str1, str2): return ''.join(x+y for x,y in zip(str..

Coding/Python 2024.04.17

[python]대소문자 함수-upper, lower, swapcase, isupper

기본 내장함수str.lower() => 소문자로 변환str.upper() => 대문자로 변환str.swapcase() => 대소문자 반대로 or join 활용방법 기억하기!#프로그래머스 lv0 문자열 대소문자 변환str = input() # AbC deFprint(str.swapcase()) # aBc DEfprint(''.join(x.upper() if x == x.lower() else x.lower() for x in input())) # 다른 사람 풀이 참고#cfprint(str.lower()) # abc defprint(str.upper()) # ABC DEFprint(str.capitalize()) # Abc def # 첫 글자만 대문자print(st..

Coding/Python 2024.04.16

[python]bool 자료형/함수, 논리연산자

※ boolean bool 자료형 True, False ※ bool 함수 bool(값이 있는 변수) == True int(bool(True))) == 1 # 백준 2753 a = int(input()) if a % 4 == 0: if a % 100 == 0: if a % 400 == 0: print(1) else: print(0) else: print(1) else: print(0) # bool a = int(input()) print(int((not a % 4) and (a % 100 or not a % 400))) # 프로그래머스 n의 배수 def solution(num, n): return 1 if num % n == 0 else 0 # 다른 풀이 # return int(num % n == 0) #..

Coding/Python 2024.03.14

[python]sum

기본 함수!! 쓰는 거 기억하기 ※ sum() 각 요인의 합 도출 # 백준 11382 a, b, c = map(int, input().split()) print(a + b + c) # sum print(sum(int(i) for i in input().split())) # 프로그래머스 홀짝 합 def solution(n): # 홀수면 n 이하 홀수의 합, 짝수면 n 이하 짝수 제곱의 합 return sum(x*x for x in range(0, n+1, 2)) if n%2 == 0 else sum(range(1, n+1, 2)) # 다른 풀이 # if n%2: # return sum(range(1,n+1,2)) # return sum([i*i for i in range(2,n+1,2)])

Coding/Python 2024.03.14

[python]문자열 format, raw string

f'문자열 {변수}' # 백준 10926 print(input() + "??!") # f-string a = input() print(f"{a}??!") #프로그래머스 a, b = map(int, input().strip().split(' ')) print(f"a = {a}\nb = {b}") print(f"{a} + {b} = {a+b}") print("{} + {} = {}".format(a, b, a+b)) # 더 크게 합치기 def solution(a, b): # return max(int(str(a) + str(b)), int(str(b) + str(a))) return max(int(f"{a}{b}"), int(f"{b}{a}")) "%d %s" % (값, 값) # 백준 2525 h, m = ..

Coding/Python 2024.03.14