Coder Social home page Coder Social logo

codeplus-sw-competency's People

Contributors

kwakcena avatar

Watchers

 avatar

codeplus-sw-competency's Issues

C++] vector의 원소를 출력하는 방법 세 가지

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vec(5, 1);     // 1로 초기화된 5개의 원소
    
    // 방법1
    for (int i = 0 ; i < vec.size(); i++)
    	cout << vec[i] << endl;
    
    
    // 방법2    
    vector<int>::iterator it;
    for (it = vec.begin(); it != vec.end(); it++)
        cout << *it << endl;
    
    
    // 방법3
    for (auto it = vec.begin(); it != vec.end(); it++)
        cout << *it << endl;
}

재귀를 사용하는 코드가 런타임 에러를 발생시키면 sys.setrecursionlimit(limit)

파이썬의 재귀 깊이

파이썬 인터프리터 소스 코드(C 언어)에는 최대 재귀 깊이가 1,000으로 정의되어 있으며 최대 재귀 깊이는 sys 모듈의 getrecursionlimit 함수로 확인할 수 있습니다.

import sys
sys.getrecursionlimit()
1000

만약 최대 재귀 깊이를 늘리려면 sys 모듈의 setrecursionlimit 함수를 사용하면 됩니다.

sys.setrecursionlimit(10**8)    # 최대 재귀호출 횟수를 10의 8승으로 늘림

next_permutation과 prev_permutation

next_permutation과 perv_permutation

어떤 리스트에 들어있는 순열의 다음 순열이나 이전 순열을 구하는 코드

방법

1. A[i - 1] < A[i] 를 만족하는 가장 큰 i를 찾는다.
2. j >= i 이면서 A[j] > A[i - 1] 를 만족하는 가장 큰 j를 찾는다.
3. A[i - 1] 과 A[j] 를 swap 한다
4. A[i] 부터 순열을 뒤집는다.

코드

numbers = [7, 2, 3, 6, 5, 4, 1]


def next_permutation(a):
    # 1. A[i - 1] < A[i] 를 만족하는 가장 큰 i 를 찾는다.
    # 7, 2, 3, 6, 5, 4, 1 (i = 6)
    #          i
    i = len(a) - 1
    while i > 0 and a[i - 1] >= a[i]:
        i -= 1
    # 만약 i가 0보다 작거나 같으면 다음 순열이 없다는 것.
    if i <= 0:
        return False

    # 2. j >= i 이면서 A[j] > A[i - 1]를 만족하는 가장 큰 j를 찾는다.
    # 7, 2, 3, 6, 5, 4, 1 (j = 6)
    #          i        j
    # 6 ~ 4 사이 숫자 중 A[j] > 3 보다 큰 숫자의 위치인 j를 찾는다.
    j = len(a) - 1
    while a[j] <= a[i - 1]:
        j -= 1

    # 3. A[i - 1] 인 3과 A[j] 인 4의 위치를 바꾼다.
    # 7, 2, 4, 6, 5, 3, 1
    a[i - 1], a[j] = a[j], a[i - 1]

    # 4. A[i] 부터 순열을 뒤집는다.
    # i = 3, j = 6
    j = len(a) - 1
    while i < j:
        a[i], a[j] = a[j], a[i]
        i += 1
        j -= 1

def prev_permutation(a):
    i = len(a) - 1
    while i > 0 and a[i - 1] <= a[i]:
        i -= 1
    if i <= 0:
        return False
    j = len(a) - 1
    while a[j] >= a[i - 1]:
        j -= 1

    a[i - 1], a[j] = a[j], a[i - 1]

    j = len(a) - 1
    while i < j:
        a[i], a[j] = a[j], a[i]
        i += 1
        j -= 1

    return True

리스트의 원소에서 중복을 제거하는 방법 (join 사용)

리스트의 원소 중복 제거 (join 사용하기)

from collections import OrderedDict

strings = ["ABC", "ABDDDD", "XYZ"]
numbers = [1, 1, 1, 1, 1, 2, 2, 3, 3]

# 리스트의 원소를 한 줄로 이어 붙인다.
string = ''.join(strings)           # ABCABDDDDXYZ
num = ''.join(map(str, numbers))    # 111112233

# set을 이용하여 중복을 제거 후 리스트로 바꾼다.
print(list(set(''.join(strings))))
print(list(set(''.join(map(str, numbers)))))

# 문자열을 순서대로 정렬하고 싶다면 OrderedDict를 이용한다.
print(''.join(OrderedDict.fromkeys(string)))
print(''.join(OrderedDict.fromkeys(num)))

사용 문제

#74 단어 수학 - 백준 1339

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.