Python

Suninatas [1번]

beginner29 2019. 8. 6. 19:28

Q. 

<%
    str = Request("str")

    If not str = "" Then
        result = Replace(str,"a","aad")
        result = Replace(result,"i","in")
        result1 = Mid(result,2,2)
        result2 = Mid(result,4,6)
        result = result1 & result2
        Response.write result
        If result = "admin" Then
            pw = "????????"
        End if
    End if
%>

 

 

문제풀이)

 

1. <% 태그로 봐서 ASP 나 JSP라고 생각할 수 있음

 

2. if not str = "" Then~
: str이 공백이 아니면 if문을 실행시켜라


3. if문안의 replace 를 통해 문자열 str 변수안에 "a" 는 "aad" 로 변경시켜 result 변수에 대입하고
result 변수 안에 "i" 는 "in" 으로 변경시켜서 다시 result에 대입하라

4. 그 후 Mid()함수를 사용하여 문자열 추출해냄

5. result = result1 & result2
즉, result1 과 result2를 연결 시킴 
ex) result1 = "he" , result2 ="llo" -> hello

6. if문 안에 if문이 있으니, 즉 연결시킨 result가 admin이면 pw 출력 


ex) Mid 함수 : 변수의 값을 잘라낼 문자열의 시작과 잘라낼 문자열의 개수를 정하여 사용합니다.

'사용법 : Mid(변수, 시작, 잘라낼 개수)
rst = Mid(tmpNm, 1, 3)
response.write "Mid함수 사용 결과 : " & rst

'1번째 자리부터 3개를 잘라낸 결과는 Hel 이 됩니다.
출처: https://h5bak.tistory.com/6 [이준빈은 호박머리]

 

 


 

간단한 python 코드로 변환

 



str_1 = input("input the result: ")

if not str_1 == "":
result = str_1.replace("a","aad")
result = result.replace("i","in")
result1 = result[1:3]
result2 = result[3:]

result = result1 + result2 
if result == "admin":
print("flag = ~~~")
else:
print("result not admin. ")

 

 

python replace 사용법.

 

result = 문자열.replace("찾을값","바꿀값",[바꿀횟수])

- 단, 좌측부터 값 변경.

- python은 [0]번 부터 시작 

ex) hello -> 0 = h , 1 = e , ..