# Task 2: Reverse Prefix # Submitted by: Mohammad Sajid Anwar # # You are given a string, $str and a character in the given string, $char. # Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. # # Example 1 # Input: $str = "programming", $char = "g" # Output: "gorpmming" # # Reverse of prefix "prog" is "gorp". # # Example 2 # Input: $str = "hello", $char = "h" # Output: "hello" # # Example 3 # Input: $str = "abcdefghij", $char = "h" # Output: "hgfedcbaj" # # Example 4 # Input: $str = "reverse", $char = "s" # Output: "srevere" # # Example 5 # Input: $str = "perl", $char = "r" # Output: "repl" def reverse_prefix(str, char): strings = list(str) new_str = '' for n in (range(len(str))): letter = strings.pop(0); new_str += letter if letter == char: new_str = new_str[::-1] break new_str += ''.join( strings ) print("'%s' '%s' -> '%s'" % (str, char, new_str) ) if __name__ == "__main__": str = "programming" char = "g" reverse_prefix( str, char ) str = "hello" char = "h" reverse_prefix( str, char ) str = "abcdefghij" char = "h" reverse_prefix( str, char ) str = "reverse" char = "s" reverse_prefix( str, char ) str = "perl" char = "r" reverse_prefix( str, char )