티스토리 뷰

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 1. 문자열 거꾸로 출력하는 4가지
 
text = 'lectopia'
 
# 1번 : range 사용
for i in range(len(text)-1,-1,-1):
    print(text[i],end=' ')
print()
# 2번 : while문
cnt = len(text)
while(cnt>0):
    cnt -=1
    print(text[cnt], end=' ')
print()
 
# 3번 : 슬라이싱
for i in text[::-1]:
    print(i,end=' ')
print()
 
# 4번 : reversed
for i in reversed(text):
    print(i, end= ' ')
print()
 
 
# 슬라이싱 한다는것은 원본을 복사한다는것,
 참조하는것이 아니라 원본을 복사해서 당겨오게됨 
그래서 슬라이싱은 다른 방법에 비해 성능이 다소 떨어짐
# 성능이 가장 좋은것은 1번 2번이다.
 
 
# 2. 리스트 거꾸로 뒤집기
= [0,1,2,3,4]
 
size = len(b)
for i in range(size//2):
    b[i],b[size-1-i] = b[size-1-i], i
print(b)
 
# 3. 선택 정렬로 정렬
 
def findMaxPos(ary, size):
    pos = 0
    for i in range(1,size):
        if ary[pos] < ary[i]:
            pos = i
    return pos
 
def selectionSort(ary, size):
    for i in range(size,1,-1):
        tmp = findMaxPos(ary,i)
        ary[tmp], ary[i-1= ary[i-1], ary[tmp]
 
selectionSort(b,5)
print(b)
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함