Coding/Basic Skills(Python)

[Programmers, Level 1] Password Just for Two of us

xeohyuni 2023. 6. 8. 20:13

Description:

Given two strings, s and skip, and a natural number index, you want to create a string according to the following rule. The rules of the password are as follows.

Replace each letter of the string s with the alphabet after the index.
If the alphabet after the index exceeds z, it returns to a.
Skip except for alphabets in skip.
For example, if s = "auks", skip = "wbqd", index = 5, the letters a to 5 are f, but in [b, c, d, e, f], 'b' and 'd' are included in the skip, so they are not counted. So except for 'b' and 'd', the alphabet after 'a' by 5 is 'h' in the order [c, e, f, g, h]. If you change the rest of the "ukks" to the above rule, it becomes "appy" and the result is "happy."

Complete the solution function to return the result of converting s according to the above rule when two strings s and skip, and the natural number index, are given as parameters.

Restriction:

  • Length of 5 ≤ s ≤ 50
  • 1 ≤ skip length ≤ 10
  • The s and skip are made up of lowercase alphabetic characters.
    • The alphabet included in skip is not included in s.
  • 1 ≤ index ≤ 20

Input/Output:

s skip index result
"aukks" "wbqd" 5 "happy"

Code:

def solution(s, skip, index):
    answer = ""
    
    alpha = "abcdefghijklmnopqrstuvwxyz"  
    
    for ch in skip: 
        if ch in alpha:
            alpha = alpha.replace(ch, "") 
    
    for i in s:
        char_index = alpha.index(i)
        new_index = (char_index + index) % len(alpha)
        change = alpha[new_index] 
        answer += change
    
    return answer

Discussion/Explanation:

Code Explanation - The provided code aims to transform a given string s based on a given index and a set of characters to skip. It utilizes the alpha string, which represents the full alphabet sequence, as a reference for finding the shifted characters. The code first removes the skip characters from the alpha string by iterating through each character in the skip string and using the replace method. This ensures that the skip characters are excluded from the available alphabet for transformation. Then, for each character i in the original string s, the code calculates the char_index as the index of i in the modified alphabet. It determines the new_index by adding the char_index and the given index, modulo len(alpha) to wrap it within the range of the alphabet sequence. This determines the transformed character change by retrieving the character at the new_index position in the modified alphabet. The transformed character is appended to the answer string. Finally, the answer string, representing the transformed string, is returned as the output.

 

Important to know - Character indexing: The code relies on the concept of indexing, where each character in a string has a corresponding index. Understanding how indexing works, such as obtaining the index of a character in a string (alpha.index(i)) or accessing a character at a specific index (alpha[new_index]), is crucial for correctly implementing the transformation logic.

Modulo operation: The modulo operator (%) is used to wrap the calculated index within the range of the alphabet sequence (len(alpha)). This ensures that the index remains within a valid range and enables the cyclic behavior required when shifting beyond the end of the alphabet.

String manipulation: The code utilizes string manipulation methods, such as replace, to remove skip characters from the alphabet string. Being familiar with string manipulation operations and their usage can help ensure the correct modification of strings.

Looping and iteration: The code utilizes loops to iterate over characters in the input strings. Understanding how loops work, iterating over strings, and accessing individual characters are essential for correctly processing each character and performing the necessary transformations.

Problem constraints: Pay attention to the given constraints, such as the length of the input strings and the range of the index. Ensure that the code handles these constraints appropriately and that the algorithm is efficient enough to handle inputs within the specified limits.