Decrypt a string according to given rules | Function to return the original string after decryption ; Stores the decrypted string ; If length is odd ; Step counter ; Starting and ending index ; Iterate till all characters are decrypted ; Even step ; Odd step ; If length is even ; Step counter ; Starting and ending index ; Even step ; Odd step ; Reverse the decrypted string ; Driver Code
def decrypt ( s , l ) : NEW_LINE INDENT ans = " " NEW_LINE if ( l % 2 ) : NEW_LINE INDENT cnt = 0 NEW_LINE indl = 0 NEW_LINE indr = l - 1 NEW_LINE while ( len ( ans ) != l ) : NEW_LINE INDENT if ( cnt % 2 == 0 ) : NEW_LINE INDENT ans += s [ indl ] NEW_LINE indl += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += s [ indr ] NEW_LINE indr -= 1 NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT cnt = 0 NEW_LINE indl = 0 NEW_LINE indr = l - 1 NEW_LINE while ( len ( ans ) != l ) : NEW_LINE INDENT if ( cnt % 2 == 0 ) : NEW_LINE INDENT ans += s [ indr ] NEW_LINE indr -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += s [ indl ] NEW_LINE indl += 1 NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT DEDENT string = list ( ans ) NEW_LINE string . reverse ( ) NEW_LINE ans = ' ' . join ( string ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " segosegekfrek " NEW_LINE l = len ( s ) NEW_LINE print ( decrypt ( s , l ) ) NEW_LINE DEDENT