Coding/Basic Skills(Python)

[Programmers, Level 0] Pushing String

xeohyuni 2023. 5. 13. 21:30

Description

In the string "hello", push each character one space to the right and move the last character forward to become "ohell". If you define this as pushing a string, complete the solution function so that given the strings A and B as parameters, you have to push A to return the minimum number of times that you have to push B if you can, and push B to return -1 if you can't.

 

Restrictions

  • 0 < length of A = length of B < 100\
  • A and B are made up of lowercase letters.

Input/Output

A B result
"hello" "ohell" 1
"apple" "elppa" -1
"atat" "tata" 1
"abc" "abc" 0

Code

def solution(A, B):
    answer = 0
    while answer != len(A):
        if A == B:
            return answer
        else:
            A = A[-1] + A[:-1]
            answer += 1
    return -1

Discussion

The key point of this question is to determine the location and the transformation of the string well. It was important to know the slicing.

EX: a = 'hello world'

       a[0:5]

result >> 'hello'

In string, couting each alphabets always starts with 0 not 1.