목록python/리스트 (46)
June is Combung
# 문제1) 숫자 9 3개를 data 에 랜덤위치에 저장 # 조건) 단 겹치면안된다. import random data = [0,0,0,0,0] check = [0,0,0,0,0] i = 0 while i < 3: r = random.randint(0,len(data)-1) if check[r] == 0: data[r] = 9 check[r] = 1 i += 1 print(data)
data = [0,0,9,9,0,0,9,0,9] # 1) 9칸의 리스트가 있다. # 2) 1칸씩 검사해서 숫자 0을 만나면 # 왼쪽 , 오른쪽 검사해서 9의 개수저장 # 결과 : data = [0,1,9,9,1,1,9,2,9] for i in range(len(data)-1): cnt = 0 if i != 0 and data[i] == 0: if data[i-1] == 9: cnt += 1 if data[i+1] == 9: cnt += 1 data[i] = cnt print(data)
# 중복안된 숫자 제거 test1 = [1, 2, 3, 2, 1] test2 = [1, 3, 4, 4, 2] test3 = [1, 1, 1, 2, 1] # 위 리스트에서 중복 안된 숫자를 제거하시오. # [1] [1, 2, 2, 1] # [2] [4, 4] # [3] [1, 1, 1, 1] temp1 = [] temp2 = [] temp3 = [] i = 0 while i < 5: cnt = 0 j = 0 while j < 5: if i != j and test1[i] == test1[j]: cnt = 1 j += 1 if cnt == 1: temp1.append(test1[i]) cnt = 0 j = 0 while j < 5: if i != j and test2[i] == test2[j]: cnt = ..
# 리스트 컨트롤러[2단계] : 함수 적용시 # 1. 추가 # . 값을 입력받아 순차적으로 추가 # 2. 삭제(인덱스) # . 인덱스를 입력받아 해당 위치의 값 삭제 # 3. 삭제(값) # . 값을 입력받아 삭제 # . 없는 값 입력 시 예외처리 # 4. 삽입 # . 인덱스와 값을 입력받아 삽입 score = [] while True: print(score) print("[리스트 컨트롤러]") print("[1]추가") print("[2]삭제(인덱스)") print("[3]삭제(값)") print("[4]삽입") print("[0]종료") choice = int(input("메뉴 선택 : ")) if choice == 1: num = int(input("입력: ")) score.append(num) el..
# 석차 출력 # 성적 순으로 이름 출력 name = ["홍길동", "김영", "자바킹", "민병철", "메가맨"] scores = [87, 42, 100, 11, 98] for i in range(len(scores)): max_score = scores[i] max_idx = i for j in range(i+1,len(scores)): if max_score < scores[j]: max_score = scores[j] max_idx = j temp = scores[i] scores[i] = scores[max_idx] scores[max_idx] = temp temp = name[i] name[i] = name[max_idx] name[max_idx] = temp print(name) print(..
# 정렬하기 # 1. 인덱스 0번이 나머지를 검사한다. # 2. 제일 큰 값을 찾아 교환한다. # 3. 인덱스 1증가한다. # 4. [1~3]을 끝까지 반복한다. # 예) # 10, 50, 30, 40, 80, 7 # 80, 50, 30, 40, 10, 7 # 80, 50, 30, 40, 10, 7 # 80, 50, 40, 30, 10, 7 scores = [10, 50, 30, 40, 80, 7] for i in range(len(scores)): max_score = scores[i] max_idx = i for j in range(i+1,len(scores)): if max_score < scores[j]: max_idx = j temp = scores[i] scores[i] = scores[max..
# ATM[4단계] : 전체 기능구현 # 1. 회원가입 # . id와 pw를 입력받아 가입 # . 가입 시 돈 1000원 부여 # . id 중복체크 # 2. 회원탈퇴 # . 로그인시에만 이용가능 # 3. 로그인 # . id와 pw를 입력받아 로그인 # . 로그아웃 상태에서만 이용가능 # 4. 로그아웃 # . 로그인시에만 이용가능 # 5. 입금 # . 로그인시에만 이용가능 # 6. 이체 # . 로그인시에만 이용가능 # 7. 잔액조회 # . 로그인시에만 이용가능 size = 5 ids = [0 for i in range(size)] pws = [0 for i in range(size)] moneys = [0 for i in range(size)] count = 0 log = -1 while True: pri..
# 1 to 50[3단계] : 1 to 18 # 1. 구글에서 1 to 50 이라고 검색한다. # 2. 1 to 50 순발력 게임을 선택해 게임을 실습해본다. # 3. 위 게임을 숫자범위를 좁혀 1 to 18로 직접 구현한다. # 4. 숫자 1~9는 front 배열에 저장하고, # 숫자 10~18은 back 배열에 저장한다. import random size = 9 front = [0 for i in range(size)] back = [0 for i in range(size)] print(front) print(back) i = 0 while i < size: front[i] = i + 1 back[i] = i + 10 i += 1 i = 0 while i < 1000: r1 = random.randi..