# Inflearn Algorithm Introduction

ghkd1330·2023년 9월 8일
0

목록 보기
1/3

# Sec0: Python Basic

## 01_Variable and Output Function

### 1) Variable Naming

1. 영문과 숫자, _로 구성

2. 대소문자를 구분

3. 문자나, _로 시작

4. 특수문자 사용불가(!, @ 등)

5. 키워드 사용불가(if, for 등)

### 2) Value Exchange

a, b = 10, 20
print(a, b) # 10 20
a, b = b, a
print(a, b) # 20 10

### 3) Output Type

print('number') # number
a, b, c = 1, 2, 3
print(a, b, c) # 1 2 3
print('number :', a, b, c) # number : 1 2 3
print(a, b, c, sep = ', ') # 1, 2, 3
print(a, b, c, sep = '') # 123
print(a, b, c, sep = '\n')
# 1
# 2
# 3

## 02_Variable Input and Operator

a = 11
b = 5
print(a // b) # 2
print(a % b) # 1
a = 4.3
b = 5
print(type(a + b)) # float > int
# <class 'float'>

## 03_Random Module

print(int(random() * 45) + 1, type(random())
# 31 <class 'float'>
print(randrange(0, 100, 3), type(randrange(0, 100, 3)))
# 51 <class 'int'>
print(randint(1, 45))
# 3

# Range(start(default:0), end + 1, step(optional))
print(range(1, 3), type(range(1, 3)), list(range(1, 3)))
# range(1, 3) <class 'range'> [1, 2]

## 04_Solve with Loop

### 1) Print Odd Nums from 1 to N

N = int(input('N : '))
inlist = list(range(1, N + 1))
outlist = []
for i in inlist:
if i % 2 == 0:
continue
outlist.append(i)
print(outlist)
N : 10
[1, 3, 5, 7, 9]
# Using Function
def Odd(N):
outlist = []
for i in range(1, N + 1):
if i % 2 == 0:
continue
outlist.append(i)
print(outlist)

Odd(int(input('N : ')))
N : 10
[1, 3, 5, 7, 9]

### 2) Sum from 1 to N

# My Sol
S = int(input('S : ')
k = 0
for i in range(1, S + 1):
k = k + i
print(k)
S : 10
55
# Lec Sol
S = int(input('S : ')
k = 0
for i in range(1, S + 1):
k = k + i
if i == S:
print(k)
S : 10
55
# Using Function
def Sum(S):
k = 0
for i in range(1, S + 1):
k = k + i
if i == S:
print(k)

Sum(int(input('S : ')))
S : 10
55
# GPT Sol
S = int(input('S : '))
k = 0
for i in range(1, S + 1):
k += i
print(k)
S : 0
55

### 3) Print Divisor of N

# Lec Sol
N = int(input('D : '))
divisors = []
for i in range(1, N + 1):
if N % i == 0:
divisors.append(i)
print(divisors)
D : 10
[1, 2, 5, 10]
# Using Function
def find_divisors(N):
divisors = []
for i in range(1, N + 1):
if N % i == 0:
divisors.append(i)
return divisors

find_divisors(int(input('N : ')))
N : 10
[1, 2, 5, 10]

## 05_Loop in Loop

### 1) Basic Structure

for i in range(5):
print('i:', i, sep='')
for j in range(5):
print('j:', j, sep='', end=' ')
print('\n')
i:0
j:0 j:1 j:2 j:3 j:4

i:1
j:0 j:1 j:2 j:3 j:4

i:2
j:0 j:1 j:2 j:3 j:4

i:3
j:0 j:1 j:2 j:3 j:4

i:4
j:0 j:1 j:2 j:3 j:4 

### 2) Print Stars#1

for i in range(5):
for j in range(i + 1):
print('*', end=' ')
print('')
*
* *
* * *
* * * *
* * * * * 

### 3) Print Stars#2

for i in range(5):
for j in range(5 - i):
print('*', end=' ')
print()
* * * * *
* * * *
* * *
* *
* 

## 06_String and Built-in Function

msg = 'abcdefghi'
for i in range(len(msg)):
print(i, ':', msg[i], end=' ')
print()
0 : a
1 : b
2 : c
3 : d
4 : e
5 : f
6 : g
7 : h
8 : i 
msg = 'abcdefghi'
for i in msg:
print(i, end = ' ')
print('\n')

for i in msg:
if i.islower():
print(i, end = ' ')
print('\n')

for i in msg:
if i.isalpha():
print(i, end = ' ')
print('\n')
a b c d e f g h i

a b c d e f g h i

a b c d e f g h i 
tmp = 'AZ'
for i in tmp:
print(ord(i), end=' ')
print('\n')

tmp = 'az'
for i in tmp:
print(ord(i), end=' ')
print('\n')

print(chr(65), chr(90), chr(97), chr(122))
65 90

97 122

A Z a z

## 07_List and Built-in Function

• enumrate(sequence) : (index, item) 형태의 Tuple Return
alist = [23, 12, 36, 53, 19]
for i in enumerate(alist):
print(i)

for index, value in enumerate(alist):
print(index, value)
(0, 23)
(1, 12)
(2, 36)
(3, 53)
(4, 19)
0 23
1 12
2 36
3 53
4 19
• all(iterable) : iterable의 Element들이 전부 True라면 True Return
• iterable Object : 반복 가능한 Object, ex. list, dict, set, str, bytes, tuple, range
print(all(['a', False])) # False
print(all([])) # True
alist = [True, True, False]
print(all(alist)) # False
alist = [23, 66, 36, 53, 19]
if all(60 > i for i in alist): # alist의 모든 원소가 60보다 작은가
print('Y')
else:
print('N')
# N

## 08_2D Array Creation and Access

a = [0] * 10 # 1차원
print(a)
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

aa = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # 2차원
print(aa)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

aaa = [[[1, 2, 1], [3, 4, 3]], [[5, 6, 5], [7, 8, 7]]] # 3차원
print(aaa)
# [[[1, 2, 1], [3, 4, 3]], [[5, 6, 5], [7, 8, 7]]]
print(aaa[0][1][1]) # 4

a = [[0] * 3 for i in range(3)] # [0] * 3 = [0, 0, 0] / 3번 반복한 걸 리스트로 -> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(a)
# [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

# Indexing
a[1][2] = 12
a[1][0] = 10
a[2][0] = 20
# a = [[0, 0, 0], [10, 0, 12], [20, 0, 0]]
for i in a: # 0 : [0, 0, 0] / 1 : [10, 0, 12] / 2 : [20, 0, 0]
for j in i: # i = [0, 0, 0], i = [10, 0, 12], i = [20, 0, 0]
print(j, end = ' ') # j(가로) -> 띄어쓰기로 처리
print() # 가로 작성 끝나면 다음 행으로 이동
# 0 0 0
# 10 0 12
# 20 0 0 

## 09_Function

### 1) isPrime

def isPrime(x):
for i in range(2, x):
if x % i == 0:
return False
return True

alist = [12, 13, 7, 9, 19]
for j in alist:
if isPrime(j):
print(j, end=' ')
# 13 7 19

### 2) Map Function

• map(function, iterable)
function : A function that will be applied to each element of the iterable.
iterable: An iterable (e.g., a list, tuple, or set) that contains the elements to be processed.
# define the function to be applied
def double(x):
return x * 2

# define the list of integers
my_list = [1, 2, 3, 4, 5]

# apply the function to each element of the list using map()
result = map(double, my_list)

# convert the map object to a list to see the results
print(result) # list로 형변환 필요
print(list(result))  # output: [2, 4, 6, 8, 10]

# <map object at 0x7f9e81571130>
# [2, 4, 6, 8, 10]

### 3) Split

• split() : 지정된 구분 기호를 기준으로 문자열을 분할 시에 사용
• ex) string.split(separator, maxsplit)
string : String you want to separate
separator : 구분 기호, Default는 띄어쓰기
maxsplit : Optional, 수행할 최대 분할 수를 지정하는 매개변수
string = "Hello, World!"
substring_list = string.split(",")
print(substring_list, type(substring_list))
# ['Hello', ' World!'] <class 'list'>

a, b = map(int, '111 222'.split())
print(a, b, type(a), type(b))
# 111 222 <class 'int'> <class 'int'>

a, b = (map(str, '333, 444'.split(', ')))
print(a, b, type(a), type(b))
# 333 444 <class 'str'> <class 'str'>

a, b = map(int, input('Input : ').split())
print(a, b, type(a), type(b))
# Input : (123 44444)
# 123 44444 <class 'int'> <class 'int'>

### 4) Lambda Function

def plus_one(x):
return x + 1

alist = [1, 2, 3]
print(list(map(plus_one, alist))) # 미리 정의해둔 Function을 alist에 적용하여 list로 변환
# [2, 3, 4]
print(list(map(lambda x : x + 100, alist))) # Lambda Function으로 간단한 수식을 즉시 작성하여 alist에 적용.
# [101, 102, 103]