From 8aadbc0da6025452b86d71f8587ad1bd4eeb18de Mon Sep 17 00:00:00 2001 From: Narender Kumar <narender.kumar@stud.th-deg.de> Date: Sun, 15 Oct 2023 18:13:19 +0200 Subject: [PATCH] 22103962 --- 2.7 Exercises.py | 18 ++++++++++++++++-- 4_9_Exercises/Question 1.py | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 4_9_Exercises/Question 1.py diff --git a/2.7 Exercises.py b/2.7 Exercises.py index 96485ae..f9ca9f9 100644 --- a/2.7 Exercises.py +++ b/2.7 Exercises.py @@ -3,6 +3,21 @@ def rotateR(val): return val[-1] + val[:-1] if len(val) > 0 else None +# question 2 +def rotateL(val): + return val[:-1] + val[0] if len(val) > 0 else None + + +# question 3 +""" +The provided functions rotateR and rotateL are designed to work with both strings and lists. +There's no issue with their functionality for either data type. +example rotateR using strings and lists -- rotateR("abc") == "cab" and rotateR([1,2,3,4]) == [4,1,2,3] +example rotateL using strings and lists -- rotateL("abc") == "bca" and rotateL([1,2,3,4 ]) == [2,3,4,1] + +""" + + # question 4 def rotateRx(val): val[:] = [val[-1]] + val[:-1] @@ -16,9 +31,8 @@ def rotateRx_modified(val): # So the only way to achieve this would be to return a value and assign it back. pass -# question 6 - +# question 6 def rotateR2(val): return rotateR(rotateR(val)) diff --git a/4_9_Exercises/Question 1.py b/4_9_Exercises/Question 1.py new file mode 100644 index 0000000..dbc1d56 --- /dev/null +++ b/4_9_Exercises/Question 1.py @@ -0,0 +1,13 @@ +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" -- GitLab