분류 전체보기
-
Implement Stack using QueuesAlgorithm/Queue and Stack 2021. 11. 22. 20:55
https://leetcode.com/problems/implement-stack-using-queues/ Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x) Pushes element x to the top of the stack. int pop() Removes the element on the top of the stack and returns it. int top() Re..
-
Python LibraryComputer Science/Basic Programming with Python 2021. 11. 20. 20:07
Python에는 built-in functions이외에도, 설치할 때 함께 설치되는 library를 이용해서 다양한 함수를 쓸 수 있다. 예를들어, array와 관련된 functions들을 쓰기 위해 numpy library를 이용해서 코딩할 수 있고, 시간과 관련된 함수 사용을 위해 time 모듈을 사용할 수 있다. time 시간과 관련된 코딩을 쉽게 할 수 있는 time 모듈을 보자. import time time.time() 1637404592.245365 위와같이 time을 import 한 후, 1970년 1월 1일 0시 0분 0초를 기준으로 지금까지 지난 시간을 초단위로 받을 수 있고, print(0) time.sleep(5) print(5) sleep(number) 함수를 이용해서 몇초간 대기..
-
Python Useful FunctionsComputer Science/Basic Programming with Python 2021. 11. 20. 19:00
Python에서 다른 library를 이용하지 않고도 사용할 수 있는 유용한 함수들이 있다. abs는 절대값을 계산해준다. abs(-2.371) 2.371 최소값과 최대값을 반환하는 min, max함수도 있고, min([-1, 1, 2, 3, 4]) -1 max([-1, 1, 2, 3, 4]) 4 정렬 후 결과를 주는 sorted함수도 있다. sorted([4, -1, 5, 0, 3]) [-1, 0, 3, 4, 5] type이 무엇인지 알려주는 type함수도 있고, print(type(1)) print(type(0.23)) print(type('hello')) print(type([1, 2, 3, 4])) print(type((1, 2, 3, 4))) print(type({1, 2, 3, 4})) pri..
-
LambdaComputer Science/Basic Programming with Python 2021. 11. 20. 10:20
이름없는, anonymous function, lambda가 있다. 어떠한 숫자 또는 type을 arguments로 받을 수 있지만 단 하나의 expression만 할 수 있다. x = lambda a : a + 10 print(x(7)) 17 위에처럼 람다를 정의해서 x라는 함수로 만들어둔 후 사용도 가능하고, def run(func, x): print(func(x)) run(lambda x : x + 1, 5) 6 function의 parameter로도 받을 수 있다.
-
Nested FunctionComputer Science/Basic Programming with Python 2021. 11. 19. 20:12
함수 안에 또다른 함수를 정의할 수 있다. def func(a): def func2(): nonlocal a a = a + 1 return a return func2() nonlocal이라는 키워드는 nested functions에서 쓰는 용어인데, local에 없는 값을 가져올 수 있다. 위 상황에서 a는 func에 있는 값을 의미해서 func에 입력된 값을 가져다가 쓸 수 있다. a = 3 def func(): def func2(): global a a = a + 1 return a return func2() 위와같이 global값도 또한 쓸 수 있다. func() 4
-
Recursive FunctionComputer Science/Basic Programming with Python 2021. 11. 19. 20:02
함수내부에서 자기 자신을 부르는 식으로 코딩을 하는 방법이 있는데, 바로 Recursive function이다. def function(count): if count > 0: print(count, 'now') function(count - 1) print('result:', count) 위와같이 function안에 function을 불러서 다른 조건으로 처리하는식으로 코딩할 수 있다. 만약 5를 집어넣으면 function(5) 5 now 4 now 3 now 2 now 1 now result: 0 result: 1 result: 2 result: 3 result: 4 result: 5 loop을 쓰는것과 마찬가지로 어떤 동작을 반복적으로 하는것인데, 이러한 recurisve function을 쓸 때 주..
-
Python VariableComputer Science/Basic Programming with Python 2021. 11. 18. 05:08
Parameters parameter이란, 아래같은 예제에서 num1, num2와 같은 inputs을 의미한다. def add(num1, num2) return num1 + num2 여기서, default값을 지정해줄수도 있다. def add(num1 = 1, num2 = 2) return num1 + num2 물론, 위의 코드는 의미없는 코드이지만, 값을 입력하지 않을때, default값을 정해줄수 있다. add(4, 5) 이것은 9를 리턴하고, add(7) 위는 a의 값만 준것으로 인식되어 default값인 b = 2를 사용, 9가 리턴된다. add(b = 1) 위와같이 b의값을 주면, a는 default인 1을 이용, 2를 리턴한다. * parameter 갯수가 몇개인지 모를때에는 *를 사용할 수 ..
-
Python FunctionComputer Science/Basic Programming with Python 2021. 11. 14. 13:34
What is Function? 함수란, 어떤 값을 받아서 어떤값을 내보내거나 변형시키는 것이다. 또는 출력을 하기도 한다. 프로그래밍에서는 입력값이 있을수도 없을수도 있고, input, output이 숫자가 아니라 string, list, dictionary등 다양한 Type이 될수도 있다. 또는 같은 함수인데, input갯수가 다른 함수가 여러개 일수도 있다. output이 여러개일 경우, list, tuple, dictionary등으로도 나올수 있다. Python에서 함수는 다음과 같이 작성할 수 있다. def function_name(x1, x2, x3): code1 code2 return value 위의 예시를 보면, input인 어떠한 값, x1, x2, x3를 받아서 어떠한 과정 이후, va..