Pusan National University power PNU string Spring 2019

  • Slides: 29
Download presentation
Pusan National University power PNU 세계로 미래로 문자열(string) 활용하기 Spring, 2019 School of CSE

Pusan National University power PNU 세계로 미래로 문자열(string) 활용하기 Spring, 2019 School of CSE Pusan National University http: //yeols. com/80072448118

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] len,

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] len, substring, right, left 등을 구하기 str 1 = "abc 123 XYZ" len(str 1) : 9 str 1[0] : a str 1[len(str 1)-1] : Z str 1[0: 0 + 1] : a str 1[4: 6 + 1] : 23 X str 1[4: 7 + 1] : 23 XY str 1[6: 8 + 1] : XYZ str 1[0: 2 + 1] : abc str 1[0: 4 + 1] : abc 12 str 1[len(str 1)-3: len(str 1)] XYZ # right 3 chars str 1[6: 8 + 1] XYZ # right 3 chars str 1[len(str 1)-5: len(str 1)] 23 XYZ #right 5 chars str 1[4: 8 + 1] 23 XYZ #right 5 chars Advanced Broadcasting & Communications Lab. 2

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] substring,

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] substring, right, left 등을 구하는 함수 def left_n (str 1, n): return (str 1[0: n]) def right_n (str 1, n): return (str 1[len(str 1)-n: len(str 1)]) def mid_st_num (str 1, st_pos, num_chars): return (str 1[st_pos: st_pos + num_chars]) def mid_st_end (str 1, st_pos, end_pos): return (str 1[st_pos: end_pos + 1]) Advanced Broadcasting & Communications Lab. 3

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] str

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] str 1 = "abc 123 XYZ" mid_st_num (str 1, 0, 1) : a mid_st_num (str 1, 4, 3) : 23 X mid_st_num (str 1, 4, 4) : 23 XY mid_st_num (str 1, 6, 3) : XYZ mid_st_end (str 1, 0, 0) : a mid_st_end (str 1, 4, 6) : 23 X mid_st_end (str 1, 4, 7) : 23 XY mid_st_end (str 1, 6, 8) : XYZ Advanced Broadcasting & Communications Lab. 4

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] str

세계로 미래로 power PNU len (str 1), substring str 1[n 1: n 2+1] str 1[0: 2 + 1] : abc str 1[0: 4 + 1] : abc 12 left_n[str 1, 3] : abc left_n[str 1, 5] : abc 12 str 1[6: 8 + 1] : XYZ str 1[4: 8 + 1] : 23 XYZ right_n(str 1, 3) : XYZ right_n(str 1, 5) : 23 XYZ Advanced Broadcasting & Communications Lab. 5

세계로 미래로 power PNU substring의 값을 바꾸기 str 2 = "abcdef"의 str 2 [2:

세계로 미래로 power PNU substring의 값을 바꾸기 str 2 = "abcdef"의 str 2 [2: 4]를 "xx"로 바꾸기 str 2 [2: 4] = "xx" # 안 됨 str 2 [2] = "x"; str 2 [3] = "x" # 안 됨 str 2 = str 2[: 2] + "xx" + str 2[4: ] str 3 = "abcdef"의 첫 글자 (a)와 마지막 글자(f) 맞바꾸기 결과: fbcdea strtmp=str 3[0]; str 3[0]=str 3[5]; str 3[5]=strtmp --> 안 됨. str 3=str 3[len(str 3)-1]+str 3[1: len(str 3)-1]+str 3[0] Advanced Broadcasting & Communications Lab. 6

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) palindrome 보기: aaa, abba, x, mum

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) palindrome 보기: aaa, abba, x, mum palindrome 이 아닌 보기: abc, abab, abcabc 주어진 문자열이 좌우 대칭인지 확인하기 종이와 연필로 하면 어떻게 할까? Advanced Broadcasting & Communications Lab. 7

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) 한 글자만 있을 때: 무조건 좌우

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) 한 글자만 있을 때: 무조건 좌우 대칭 글자가 없을 때: 좌우 대칭 판정을 할 수 없음 input_str = "abcabc" len 1 = len(input_str); num_iter = len 1 / 2 left_pos = 0; right_pos = len 1 - 1 palin. Q = True for j in range(0, num_iter): if (input_str[left_pos] <>input_str[right_pos]): palin. Q = False left_pos = left_pos + 1 right_pos = right_pos - 1 if (palin. Q): . . . else: . . . Advanced Broadcasting & Communications Lab. 9

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) input_str = "abcabc" len 1 =

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) input_str = "abcabc" len 1 = len(input_str); num_iter = len 1 / 2 left_pos = 0; right_pos = len 1 - 1 num_diff = 0 for j in range(1, num_iter +1): if (input_str[left_pos] <>input_str[right_pos]): num_diff = num_diff + 1 left_pos = left_pos + 1 right_pos = right_pos - 1 if (num_diff == 0): . . . else: . . . Advanced Broadcasting & Communications Lab. 11

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) input_str = "abccba" len 1 =

세계로 미래로 power PNU palindrome (좌우 대칭인 문자열) input_str = "abccba" len 1 = len(input_str); num_iter = len 1 / 2 left_pos = 0; right_pos = len 1 - 1 num_same = 0 for j in range(1, num_iter +1): if (input_str[left_pos] ==input_str[right_pos]): num_same = num_same + 1 left_pos = left_pos + 1 right_pos = right_pos - 1 if (num_same == num_iter): . . . else: . . . Advanced Broadcasting & Communications Lab. 13

세계로 미래로 power PNU chr ( ) 함수 0 ~ 255 값에 대응하여 글자

세계로 미래로 power PNU chr ( ) 함수 0 ~ 255 값에 대응하여 글자 한 자(char)를 되돌려 준다 ord ( ) 함수 글자 한 자(String)에 대응하여 0 ~ 255의 정수를 되돌려 준다 str 21 = chr (65) # "A": 65 (dec), 0 x 41 str 22 = chr (0 x 42) # "B": 0 x 42, 66 (dec) print str 21, str 22, chr(0 x 30), chr(57) 결과: A B 0 9 = ord("A") -> 0 x 41=65; ord("b")=0 x 62=98 ord("0") -> 0 x 30=48; ord("9")=0 x 39=57 Advanced Broadcasting & Communications Lab. 15

세계로 미래로 power PNU ASCII table ASCII american standard code for information interchange Advanced

세계로 미래로 power PNU ASCII table ASCII american standard code for information interchange Advanced Broadcasting & Communications Lab. 16

세계로 미래로 power PNU 문자열 다루기 주어진 char. str. 의 오른쪽부터 네 자씩 떼어냄

세계로 미래로 power PNU 문자열 다루기 주어진 char. str. 의 오른쪽부터 네 자씩 떼어냄 str 1 = "99123477778888" while (len(str 1) > 0): len 1 = len(str 1) if (len 1 >= 4): # 4/5/6/. . . chars print str 1[len 1 -4: len 1] str 1 = str 1[0: len 1 - 4] else: # 1/2/3 chars print str 1 = "" // 결과: 8888, 7777, 1234, 99가 차례대로 나옴 Advanced Broadcasting & Communications Lab. 17

세계로 미래로 power PNU 문자열을 정수로 바꾸기 십진 숫자 네 개인 char. str. 을

세계로 미래로 power PNU 문자열을 정수로 바꾸기 십진 숫자 네 개인 char. str. 을 정수로 바꾸기 보기: char. str. "2345" --> 정수 2345 • 과정: (((2 * 10) + 3 ) * 10 + 4) * 10 +5 =2345 # int(dec_str 1) 함수를 쓰지 않고 직접 해봄 dec_str 1 = "2048" num 1 = 0 for i in range(0, 3 + 1): char 1 = dec_str 1[i] digit 1 = ord (char 1) - ord("0") num 1 = num 1*10 + digit 1 Advanced Broadcasting & Communications Lab. 18

세계로 미래로 power PNU 정수를 문자열로 바꾸기 십진 수를 char. str. 으로 바꾸기 dec_dgt

세계로 미래로 power PNU 정수를 문자열로 바꾸기 십진 수를 char. str. 으로 바꾸기 dec_dgt = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] num 1 = 2748; dec_str 1 = "" # empty string while (num 1 > 0): rem 1 = num 1 % 10 # 10으로 나눈 나머지 dec_str 1 = dec_dgt[rem 1] + dec_str 1 num 1 = (num 1 - rem 1) / 10 print dec_str 1 Advanced Broadcasting & Communications Lab. 19

세계로 미래로 power PNU 정렬하기 (sort) 오름차순으로 간추린다. 원소 (1): (2) 견줌, (2): (3)

세계로 미래로 power PNU 정렬하기 (sort) 오름차순으로 간추린다. 원소 (1): (2) 견줌, (2): (3) 견줌, . . . (n-1): (n) 견줌 따라서 맨처음에 (n-1) 번 견줌 그리고 견줄 회수가 1씩 줄어듦 seongjeog = [0 for i in range(101)] # num_ints # 정수의 개수 # seongjeog에 num_ints 개의 성적이 원소 [1]. . [num_ints]에 있다고 가정. seongjeog[1]=10; seongjeog[2]=40; seongjeog[3]=35; seongjeog[4]=25; Advanced Broadcasting & Communications Lab. 21

세계로 미래로 power PNU 정렬하기 (sort) for num_cmps in range((num_ints-1), 0, -1): for i

세계로 미래로 power PNU 정렬하기 (sort) for num_cmps in range((num_ints-1), 0, -1): for i in range(1, num_cmps + 1): if (seongjeog[i] > seongjeog[i + 1]): tmp_val = seongjeog[i] = seongjeog[i + 1] = tmp_val Advanced Broadcasting & Communications Lab. 22

세계로 미래로 power PNU 정렬하기 (sort) num_cmps= 3 … 1 번째로 큰 수를 찾아서

세계로 미래로 power PNU 정렬하기 (sort) num_cmps= 3 … 1 번째로 큰 수를 찾아서 4 번째 원소에 넣는다 i: 1; sj[1]: 10, sj[2]: 40 X 맞바꾸지 않음 성적: 10 40 35 25 i: 2; sj[2]: 40, sj[3]: 35 O 맞바꿈 성적: 10 35 40 25 i: 3; sj[3]: 40, sj[4]: 25 O 맞바꿈 성적: 10 35 25 40 !! 1 번째로 가장 큰 값 : 40 Advanced Broadcasting & Communications Lab. 24

세계로 미래로 power PNU 연습문제 while_max_31_filenames. txt Advanced Broadcasting & Communications Lab. 27

세계로 미래로 power PNU 연습문제 while_max_31_filenames. txt Advanced Broadcasting & Communications Lab. 27