Skip to content
Snippets Groups Projects
Question 1.py 508 B
Newer Older
Narender Kumar's avatar
Narender Kumar committed
def reverse_list_or_string(input_list_or_string):
    if len(input_list_or_string) == 0:
        return input_list_or_string
    else:
        return reverse_list_or_string(input_list_or_string[1:]) + [input_list_or_string[0]]

# Example usage for reversing a list
my_list = [1, 2, 3, 4, 5]
print(reverse_list_or_string(my_list))  # Output: [5, 4, 3, 2, 1]

# Example usage for reversing a string
my_string = "Hello, World!"
print(''.join(reverse_list_or_string(list(my_string))))  # Output: "!dlroW ,olleH"