Coding 42

[python]소수점 자리수 round

round(값): 정수로 반올림round(값, 자리수n): 소수점 n자리수에서 반올림 # 프로그래머스 lv0 두 수의 나눗셈# 테스트 케이스 실패...def solution(num1, num2): return round((num1/num2)*1000) # 수정 후 return int((num1/num2)*1000) ※ int와 roundround: 반올림int: 정수로의 변환 - 소수점 이하 버림 ※ format, f-string"문자열 {1:.2f} ".format(실수1, 실수2) => 실수2를 소수점 둘째자리까지 출력f"문자열 {변수:.3f}" => 변수의 값을 소수점 셋째자리까지 출력 cf) https://blockdmask.tistory.com/534..

Coding/Python 2024.04.24

[python]리스트 합집합/교집합/차집합

set 활용하기!!! a_li = [a, b, c, d]b_li = [b, c, e, f] ※ 합집합list(set(a_li) | set(b_li))== list(set().union(a_li, b_li)) ※ 교집합 ※ 차집합list(set(a_li) - set(b_li)== list(set(a_li).difference(b_li))# 프로그래머스 lv0 글자 지우기# str에서 indices[0,2,6,...] 인덱스 글자들만 빼서 출력하기# 초반 풀이 - 테스트 케이스 에러..def solution(my_string, indices): li = list(set([i for i in range(len(my_string))]).difference(indices))..

Coding/Python 2024.04.24

[python]list comprehension, if-elif-else

※ list comprehension으로 다중 if문 쓰기for i in list:  if 조건A:     print("a")  elif 조건B:     print("b")  else:     print("c")≫ print(["a" if 조건A else "b" if 조건B else "c" for i in list]) # 프로그래머스 lv0 qr code# 초반 풀이def solution(q, r, code): return code[r-1::q] if r>0 else code[r::q-1] # ValueError: slice step cannot be zero # q=1, r=0일 때 오류! # 수정 후 return code[r::q] if r>0 else code if q..

Coding/Python 2024.04.24

[python]리스트 sort(key, reverse), sorted()

※ 리스트.sort()리스트 고유의 메서드, 기본 오름차순 정렬  # sort(reverse=False)가 default* 따로 리스트를 return하는 게 x, 해당 리스트를 정렬만 함≫ 리스트.sort(reverse=True) # 내림차순 정렬≫ sorted(리스트)  # 정렬된 리스트 return# 프로그래머스 lv0 접미사 배열def solution(s): return sorted([s[i:] for i in range(len(s))]) # 프로그래머스 lv0 중앙값 구하기# [7, 0, 5] => 5def solution(array): return sorted(array)[len(array)//2]  ≫ 리스트.sort(key=함수) / sorted(리스트, key=함수)함수 기..

Coding/Python 2024.04.21

[python]문자열 reverse, reversed

※ 리스트.reverse() 해당 리스트 역순으로 수정해줄 뿐 None return => list에서만 제공되는 함수!!! a_li = ['a', 'b', 'c', 'd'] b_li = a_li.reverse() print(b_li) # none print(a_li) # ['d', 'c', 'b', 'a'] ※ reversed(리스트/튜플/문자열) 역순으로 된 reversed 객체 return! list(reversed(a_li)) # ['d', 'c', 'b', 'a'] or ''.join(reversed(a_li)) # 'dcba' 처럼 활용가능! https://itholic.github.io/python-reverse-reversed/ ※ 문자열 뒤집기(역순 출력) 문자열은 슬라이싱(인덱스)으로 추..

Coding/Python 2024.04.19

[python]리스트 순열, 조합

# tuple 형식으로 return※ product(리스트, repeat=자리수)중복O, 모든 조합from itertools import product   # 중복순열''.join(i) for i in list(product('ABC', repeat = 2))     # AA AB AC BA BB BC CA CB CC ※ permutations(리스트, repeat=자리수)중복X, 순서Ofrom itertools import permutations   # 순열permutations('ABC', 2)       # AB AC BA BC CA CB ※ combinations(리스트, repeat=자리수)중복X, 순서Xfrom itertools import combinations    # 조합combinati..

Coding/Python 2024.04.18

[python]zip 활용 - dict, 리스트, 문자열, 반복문

※ for N in zip(iterable1, iterable2, ...)내장함수로, iterable 객체 여러개를 받아 tuple로 묶어주는 역할for z in zip("abc", [1,2,3]):      print(z)("a", 1)("b", 2)("c", 3) ※ for k,v in dict(zip(리스트1, 리스트2)key: 리스트1, value: 리스트2# 프로그래머스 수 조작하기1# control: 문자열 'wdsaaw...'def solution(n, control): w = control.count('w') s = control.count('s') d = control.count('d') a = control.count('a') r..

Coding/Python 2024.04.17