Git Product home page Git Product logo

algorithm's Introduction

문법 정리

파스칼: 조합 계수 계산하기

b = [1] * n
for i in range(1, n):
    b[i] = b[i - 1] * (n - i) / i

List에서 특정 element 갯수 세기

a=[1,2,3,1,1,2]
print(a.count(1)) # 3

dictionary 활용

체크용 / 누적용

  • 체크용 dic[key]=1, dic2[key]=0
  • 누적용 dic[key]=dic.get(x,0)+1

get()은 key값이 있으면 그 val값을, 없으면 0을 리턴하는 함수

for-else문

for문에서 break 없이 실행되었다면 else 실행

내장함수

  • isupper(): 대문자인지 확인
  • islower(): 소문자인지 확인
  • ord(): 아스키넘버로 변환
  • sorted(): 정렬된 리스트 반환 / x=sorted(x,reverse=True) reverse=True이면 내림차순, reverse=False면 오름차순
  • sort(): 리스트 자체를 정렬(메소드지만 비교) / x.sort(reverse=True) reverse=True이면 내림차순, reverse=False면 오름차순

enumerate 함수

이 함수는 (list, set, tuple, dictionary, string)을 입력으로 받아 인덱스 값을 포함하는 enumerate 객체를 리턴한다.

예시

data = enumerate((1, 2, 3))
print(data, type(data))

for i, value in data:
    print(i, ":", value)
print()

data = enumerate({1, 2, 3})
for i, value in data:
    print(i, ":", value)
print()

data = enumerate([1, 2, 3])
for i, value in data:
    print(i, ":", value)
print()

dict1 = {'이름': '뭉자', '나이': 5}
data = enumerate(dict1)
for i, key in data:
    print(i, ":", key, dict1[key])
print()

data = enumerate("안녕하세요")
for i, value in data:
    print(i, ":", value)
print()

map

  • map(함수, 리스트)

ex) map(int,['1','2','3'])

lambda

  • lambda 인자 : 표현식

ex) >>list(map(lambda x: x ** 2, range(5)))
[0, 1, 4, 9, 16]

전역변수와 지역변수

함수 안에서 전역 변수의 값을 변경하려면 global 키워드를 사용해야 한다.

def foo():

    global cnt
    cnt += 1
    
if __name__ == "__main__":
    foo()
    cnt = 0
    print(cnt) # 1

algorithm's People

Contributors

uyeonh avatar

Watchers

James Cloos avatar  avatar

algorithm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.