파이썬에서 리스트를 뒤집는 세 가지 방법
·
Tips
리스트를 뒤집는 3가지 방법 [::-1] 리스트를 역순으로 슬라이싱 하는 방법이다. 새로운 리스트를 만들 필요 없이 기존의 리스트를 뒤집어 반환한다. 하지만 슬라이싱을 활용해 뒤집는 경우 리스트의 크기가 매우 크다면 메모리를 필요 이상으로 낭비하게 될 수 있다. reverse() 원본 리스트를 직접 뒤집기 때문에 메모리 사용량이 적다. 하지만 원본 리스트가 변경되기 때문에 원본 리스트를 유지하고 싶다면 사용하지 않아야 한다. reversed() 반환하는 값이 iterator이기 때문에 원본 리스트가 바뀌지 않으면서 메모리 사용량이 적다. 하지만 바로 사용할 수는 없어 list()를 통해 리스트 형태로 변환 후 사용하게 된다.결론 원본 리스트를 변경해도 상관 없는 경우 → reverse() 원본 리스트를 ..
[Python] Three Methods to Reverse a List
·
Tips
Three Methods to Reverse a List [::-1] This method reverses the list by slicing it in reverse order. It creates a new list, so if the list is very large, it can lead to unnecessary memory usage. reverse() This method directly reverses the original list, which means it uses less memory. However, since it modifies the original list, it should not be used if you want to keep the original list intac..
[Python] 파이썬의 전달 방식은 값 전달? 참조 전달?
·
Tips
파이썬의 전달 방식 정답부터 말하자면, 파이썬의 전달 방식은 call by assignment이다. 다르게는 call by object-reference 또는 call by sharing이라고 부르기도 한다. 모두 같은 의미로, 파이썬에서는 함수 호출 시 인자를 객체에 대한 참조로 전달한다는 것이다.  이해를 돕기 위해 예시를 들자면, int나 str 같은 데이터 타입의 경우 불변 객체이기 때문에 함수 내에서 값을 변경하려 해도 원래 객체에는 영향을 미치지 않게 된다. 반면, list나 set 같은 가변 객체의 경우 함수 내에서 변경을 시도하면 원래 객체도 변경된다. 즉, call by value와 call by reference가 혼합된 형태라고 이해하면 된다.객체 참조 전달 방식불변 객체 → call..
[Python] Call by Value, Call by Reference?
·
Tips
How? To begin with, the correct answer is that python's parameter passing mechanism is call by assignment. It is also referred to as call by object-reference or call by sharing, all of which mean the same thing. In Python, when a function is called, the argument is passed as a reference to the object.  To help understand this, consider that data types like int or str are immutable objects, so ev..
[Python] 백준 11718번: EOFError가 발생하지 않는다고?
·
Engineering Notes/CS & Algorithms
sys.stdin.readline()은 빈 문자열을 반환한다!import syswhile True: try: userInput = sys.stdin.readline().strip() print(userInput) except: break EOFError(End of File)는 파일의 끝, 즉 입력의 끝을 의미한다. input()은 파일의 끝에서 EOFError를 발생시키지만, sys.stdin.readline()은 빈 문자열을 반환하므로 EOFError를 발생시키지 않는다. 따라서 출력 초과로 인한 오답 처리가 되는 것이다.while True: try: print(input()) except: break 물론 이렇게 간..
[Python] BOJ 11718: No input values, No EOFError?
·
Engineering Notes/CS & Algorithms
sys.stdin.readline() returns an empty string!import syswhile True: try: userInput = sys.stdin.readline().strip() print(userInput) except: break EOFError(End of File) means the end of the file, or the end of input. The input() function raises an EOFError at the end of the file, but sys.stdin.readline() returns an empty str, so it does not raise an EOFError. This can lea..
문자열 결합 연산을 쓰면 안 되는 이유
·
Archive
시간 복잡도 프로그래밍 언어에서 시간복잡도란 입력의 크기(N)에 따라 알고리즘이 얼마나 많은 "시간"을 소모하는지를 나타내는 척도이다. 표기법은 다양하나, 일반적으로는 O(1), O(n), O(log n), O(n^2) 등의 표기법을 사용한다.실제 코드 시간 복잡도 분석import sysN = int(sys.stdin.readline())result = ""for i in range(N // 4): result += "long "result += "int"print(result) 백준 25314번 문항을 풀며 실제 내가 제출한 코드로, 입력값 N에 따라 "long "을 N // 4번 반복해서 결과 문자열에 추가한 후, 마지막에 "int"를 붙여서 출력하는 프로그램이다. 다음은 코드를 쪼개어 각 코드..
[Python] Time Complexity: No String Concatenation!
·
Archive
Time Complexity In programming languages, time complexity is a measure of how much 'time' an algorithm consumes based on the size of the input (N). There are various notations, but commonly used ones include O(1), O(n), O(log n), and O(n^2).Time Complexity Analysis through Examplesimport sysN = int(sys.stdin.readline())result = ""for i in range(N // 4): result += "long "result += "int"print(r..