study with Q - 파이썬
-
72 - (파이썬) 구문오류와 예외study with Q - 파이썬 2024. 8. 8. 19:08
# 예외 (Exception): 오류의 일종 # 오류(Error): 프로그램의 이상을 일으키는 썸띵 - Syntax Error[Compile Error] : 프로그램의 실행 전에 발생하는 오류 - Runtime Error[Exception] : 프로그램 실행 중에 발생하는 오류 #구문 오류print("#프로그램이 시작되었습니다.")print("구문 오류를 발생시킵니다.">>>...SyntaxError: '(' was never closed # 예외 (Exception) eg) 리스트가 없는데 어떻게 첫 번째 요소를 불러와요...print("#프로그램이 시작되었습니다.")리스트[1]>>>#프로그램이 시작되었습니다.Traceback (most recent call last): File "/Users/k..
-
70 - (파이썬) 하노이탑study with Q - 파이썬 2024. 8. 1. 15:57
# 하노이의 탑: 하노이의 탑(Tower of Hanoi)은 퍼즐의 일종이다. 세 개의 기둥과 이 기둥에 꽂을 수 있는 크기가 다양한 원판들이 있고, 퍼즐을 시작하기 전에는 한 기둥에 원판들이 작은 것이 위에 있도록 순서대로 쌓여 있다.게임의 목적은 다음 세 가지 조건을 만족시키면서, 한 기둥에 꽂힌 원판들을 그 순서 그대로 다른 기둥으로 옮겨서 다시 쌓는 것이다.한 번에 한개의 원판만 옮길 수 있다.가장 위에 있는 원판만 이동할 수 있다.큰 원판이 작은 원판 위에 있어서는 안 된다.# 유래더보기"인도 베나레스에 있는 한 사원에는 세상의 중심을 나타내는 큰 돔이 있고 그 안에 세 개의 다이아몬드 바늘이 동판 위에 세워져 있습니다. 바늘의 높이는 1 큐빗이고 굵기는 벌의 몸통만 합니다. 바늘 가운데 하나에..
-
69 - (파이썬) 확인문제, 메모화 추가study with Q - 파이썬 2024. 8. 1. 14:54
전체사람수 = 6counter = 0#이전에 몇 명을 앉혔는지 이전 화살표에 적힌 숫자를 만들어주고def graph(nod,preArrowNumber): if nod == 0: global counter counter += 1 #2와 이전 화살표에 적힌 숫자 중 큰 것을 선택하는 조건을 만들어준다 for i in range(max(2,preArrowNumber), min(nod, 10)+1): #매개변수는 i를 전달해서 말해준다 graph(nod-i, i)#이전 화살표의 항등원은 0graph(전체사람수,0)print(counter)>>>4 여기에 메모화를 추가해보자. 위 함수는 값을 가지지 않기 때문에 (return 값을 가지지 않으므로) 메모화가 불가능하다. 따라서 어떠한 r..
-
68 - (파이썬) 확인문제: 기본 형태study with Q - 파이썬 2024. 7. 29. 22:31
# 여러 개의 테이블에 나누어 앉을 때, 한 사람만 앉는 테이블이 없도록 그룹을 지어보자. (단, 인원 수를 나누는 패턴만 구한다.)eg) 6명일 때, 다음 네 가지 경우를 갖는다.2명+2명+2명2명+4명3명+3명6명 #한 테이블에 앉을 수 있는 최대 인원은 10명일 때, - step 1) 화살표 만들기전체사람수 = 6#그래프를 나타내는 함수에 노드에 적힌 수를 매개변수로 지정한다def graph(nod): #화살표를 2명부터 6명까지 뽑아주기 위해 for 반복문을 이용한다 #한 테이블에 앉을 수 있는 최대 인원은 10명이므로 nod와 10명 중 작은 것을 골라서 돌리게 한다 for i in range(2, min(nod, 10)+1): print(i)graph(전체사람수)>>>23456 -..
-
67 - (파이썬) 가독성과 유지보수성study with Q - 파이썬 2024. 7. 15. 19:25
# 현대적 관점의 개발은 함수함수함수함수함수함수다. : 가독성, 유지보수적의 측면에서 이런 것까지? 하는 부분까지 함수로 만드는 것이 좋다. eg) 원주율이 주어졌을 때, 원의 둘레와 넓이를 구하는 방법을 구하는 프로그램을 만든다고 할 때, 🧐 요래 작성하면 되는 거 아닌가?#숫자 입력number = input("Enter a number: ")radius = float(number)#둘레와 넓이print(2*3.14*radius)print(3.14*radius*radius) 주석은 코드를 수정하면서 바로 같이 수정되지 않으므로 누락( → 휴먼 에러 발생)의 가능성이 있다.그래서 아예 함수로 요래요래 변환해버림def num_input(): number = input("Enter a number: ..
-
66 - (파이썬) 이터러블, 이터레이터, 제너레이터 함수, 제너레이터 표현식study with Q - 파이썬 2024. 7. 9. 11:01
제너레이터 (generator): 파이썬의 특수한 문법구조. 이터레이터를 직접 만들 때 사용하는 코드. 함수 내부에 yield 키워드를 사용하면 해당 함수는 제너레이터 함수가 되며 일반 함수와 다르게 함수를 호출해도 함수 내부의 코드가 실행되지 않는다. 참고)더보기이터러블: 반복할 수 있는 것 / aka 반복문 뒤에 넣을 수 있는 것Iterate + ableeg) 리스트, 튜플, 딕셔너리 등 이터레이터 : 반복하는 것 / 이터러블을 만드는 방법 중 하나Iterate + or이터레이터를 만드는 방법- 제너레이터 표현식 사용- 제너레이터 함수 사용- 이터레이터 클래스 (투머치 고급)기억나니.... #리스트 내포[ #표현식 표현식 #반복문 for 요소 in 반복할수있는것 #조건문 if 조건문] ..
-
65 - (파이썬) CSV 파일 읽고 쓰기study with Q - 파이썬 2024. 7. 8. 17:28
텍스트를 사용해 데이터를 구조적으로 표현할 수 있는 방법으로는 CSV, XML, JSON🤍등이 있다. # CSV:(comma-separated values)는 몇 가지 필드를 쉼표(,)로 구분한 텍스트 데이터 및 텍스트 파일을 뜻한다.(인공지능 개발이나 데이터 분석에서 쓰이는 데이터 형식이 CSV이다.) 참고더보기XML(eXtensible Markup Language)은 데이터를 정의하는 규칙을 제공하는 마크업 언어입니다. JSON(JavaScript Object Notation)은 Javascript 객체 문법으로 구조화된 데이터를 표현하기 위한 문자 기반의 표준 포맷입니다. 웹 어플리케이션에서 데이터를 전송할 때 일반적으로 사용합니다(서버에서 클라이언트로 데이터를 전송하여 표현하려거나 반대의 경우..
-
64 - (파이썬) 기본 파일 처리study with Q - 파이썬 2024. 7. 8. 15:30
파일은 크게 텍스트 파일과 바이너리 파일로 나뉘는데, 여기서는 텍스트 파일만 다룸. 우선 Replit에 test.txt 파일을 만들고 다음과 같이 작성한다.안녕 세계! 오키. 이제 이거 가져다 쓰자. # 파일 처리: 읽기 처리(r) / 쓰기 처리(w,a) #(1) 스트림 연결testflie = open("test.txt", "r")# 모드 종류 : w 쓰기 모드 / a 추가해서 쓰기 모드 / r 읽기 모드 #(2) 스트림을 통한 데이터 통신teststr = testflie.read()print(teststr)#(3) 스트림 해제testflie.close()>>>안녕 세계! 그런데 프로그램이 길어지면 open()함수와 close()함수 사이에 내용이 길어지고 열고 닫는 것을 잊어버리기 쉬워진다. 이런 실수..