본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 103

반응형

Q>

- Oh. This new generation of robots is trying to do everything faster than is needed.

  (오. 이 새로운 세대의 로봇은 필요한 것보다 더 빨리 모든 것을하려고합니다.)

- Come on!! Gaffer! Can I just replace a string and that's all.

  (어서와! 두목! 문자열을 바꿀 수 있을까요?)

- No, young man. You can’t “just replace a string”.

  (아니, 젊은 남자. 당신은 "그냥 문자열을 대체 할 수 없다".)

  You should respect your memory cards, even though you have a lot of them.

  (당신은 당신의 카드가 많더라도 당신의 메모리 카드를 존중해야합니다.)

  Now sit and think a little bit.

  (이제 앉아서 조금 생각해보십시오.)

  How can this string can be replaced in a most efficient way?

  (어떻게이 문자열을 가장 효율적인 방법으로 대체 할 수 있습니까?)

- Ok ok. As you wish.

  (그래 그래. 원하면.)

  Now I should respect even my own hardware.

  (이제는 내 하드웨어도 존중해야합니다.)

  You are given two strings, line1 and line2.

  (line1과 line2라는 두 개의 문자열이 주어집니다.)

  Answer, what is the smallest number of operations you need to transform line1 to line2?

  (답변, line1을 line2로 변환하는 데 필요한 최소 작업 수는 얼마입니까?)

 

Operations are:

  • Delete one letter from one of strings
  • Insert one letter into one of strings
  • Replace one of letters from one of strings with another letter

운영 :

- 문자열 중 하나에서 한 문자 삭제

- 문자열 중 하나에 하나의 문자 삽입

- 문자열 중 하나의 문자를 다른 문자로 바꾸기 

 

Input: two arguments, two strings.

         (두 개의 인수, 두 개의 문자열)

 

Output: int, minimum number of operations.

           (int, 최소 작업 수입니다.)

 

Example:

steps_to_convert('line1', 'line1') == 0
steps_to_convert('line1', 'line2') == 1
steps_to_convert('ine', 'line2') == 2

 

Precondition: 0 <= len(line) < 100

 

A>

def steps_to_convert(line1, line2):
    if len(line1) * len(line2) == 0 : return max(len(line1), len(line2))

    # 첫번째 문자열이 같으면 건너띄기
    if line1[0] == line2[0]: return steps_to_convert(line1[1:], line2[1:])

    # 문자열이 'a'이면 삭제
    n1 = steps_to_convert(line1[1:], line2)

    # 'l'을 삽입
    n2 = steps_to_convert(line2[0] + line1, line2)

    # 'x'를 'l'로 바꿈
    n3 = steps_to_convert(line2[0] + line1[1:], line2)

    return min(n1, n2, n3) + 1

if __name__ == "__main__":
    # These "asserts" using only for self-checking and not necessary for auto-testing
    assert steps_to_convert('line1', 'line1') == 0, "eq"
    assert steps_to_convert('line1', 'line2') == 1, "2"
    assert steps_to_convert('line', 'line2') == 1, "none to 2"
    assert steps_to_convert('ine', 'line2') == 2, "need two more"
    assert steps_to_convert('line1', '1enil') == 4, "everything is opposite"
    assert steps_to_convert('', '') == 0, "two empty"
    assert steps_to_convert('l', '') == 1, "one side"
    assert steps_to_convert('', 'l') == 1, "another side"
    print("You are good to go!")

 

O>

You are good to go!

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글