Git Product home page Git Product logo

algorithm's Introduction

멋쟁이사자처럼 중앙대학교 알고리즘 스터디

멋쟁이사자처럼 9기의 알고리즘 스터디입니다.
함께 공부하실 9기분들은 언제든 참여해주세요.

사용하는 언어

Python Java C++
이외 언어가 있다면 추가해주세요

📌 스터디 참여 방법

  1. 알고리즘 풀이 후 PUSH
  2. PR(Pull Request) 요청
  3. 스터디 참가자들 코드 리뷰
  4. MERGE

✨추천하는 Fork를 사용하여 참여하는 방법
GitHub 프로젝트에 기여하기

폴더 구조

알고리즘출처/카테고리명/문제명/풀이한 사람 이름

예시: Baekjoon/입출력과사칙연산/2557.HelloWorld/김승아.py

PR(Pull Request) 규칙

[알고리즘출처] 카테고리명 문제명 사용언어라벨

PR 본문

해당 문제를 풀면서 든 생각, 간단한 로직 설명을 써주세요.

algorithm's People

Contributors

leobang17 avatar seojisoosoo avatar seunguri avatar tarakyu avatar youngkwon02 avatar yunseonyeong avatar

algorithm's Issues

[Python3] About copy


🌟 Deep copy and Shallow copy

안녕하세요,
알고리즘 스터디 코드리뷰 중, 변수의 copy 관련해서 석진 준호님과 나눈 내용을 정리할겸,
정리한 내용을 멋사 여러분과 함께 공유하면 좋지 않을까 하여 글을 작성하게 되었습니다.

다음은 제가 알고있는 Programming Language에서 Copy에 관한 지식입니다.
(혹시 잘못된 부분이 있다면 피드백 부탁드려요~ 😆)


Deep copy (깊은 복사)

저희가 일반적으로 생각하는 변수의 복사입니다.
예를 들어, a라는 변수가 100이라는 정수형 값일 때, b = a와 같이 b에 a의 값을 복사하면,
이후 a와 b는 완전히 독립적인 변수가 되어, 서로의 연산 결과에 영향을 받지 않습니다.
이를 코드로 작성하면 다음과 같습니다.

// Written by Javascript
let a = 100;
let b = a;
b += 100;

console.log(a) // 100
console.log(b) // 200

Shallow copy (얕은 복사)

값을 복사하지만 새로운 변수에 대해 새로운 메모리를 할당하지 않습니다.
즉, 두 변수는 같은 Memory space를 공유하며, 변수의 값을 수졍하면 서로에게 영향을 미칩니다.
이를 코드로 작성하면 다음과 같습니다.

// Written by Javascript
let a = [1, 2, 3, 4, 5]
let b = a;
for(let i = 0; i < 5; i++){
  b[i] += 100; // b 배열의 모든 원소에 100을 더합니다.
}

console.log(a); // [101, 102, 103, 104, 105]
console.log(b); // [101, 102, 103, 104, 105]

- Data type / Language별 Copy 방식

스크린샷 2021-10-31 오전 2 29 05

위의 표에서 언급된 모든 언어에서, list type에 대해서는 Shallow copy가 발생합니다.
즉, 다음과 같이 작성된 python 코드에서 배열의 복사는 Shallow copy 방식으로 동작합니다.

# Written by Python
a = [1, 2, 3, 4, 5]
b = a
b[0] = 10

print(a) # [10, 2, 3, 4, 5]
print(b) # [10, 2, 3, 4, 5]

List type 변수에 대해 Deep copy를 적용하고 싶다면 다음과 같이 처리할 수 있습니다.

from copy import deepcopy

a = [1, 2, 3, 4, 5]
b = a[:] # 방법 1
c = list(a) # 방법 2
d = a.copy() # 방법 3
e = deepcopy(a) # 방법 4

글 마무리를 어떻게 해야할지 모르겠네요!
Deep & Shallow copy에 대해 한번쯤은 들어보셨을텐데,
내용이 헷갈리셨던 분들에게 아무쪼록 도움이 되셨다면 좋겠습니다☺️
감사합니다.

- 질문

혹시 직전 코드에서 방법 3과 4가 모두 Deep copy가 맞는거 같은데, 둘의 유의미한 차이가 있을까요?

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.