-
Notifications
You must be signed in to change notification settings - Fork 2
Python Basic coding QNA
Praveen Kumar Anwla edited this page Dec 18, 2023
·
6 revisions
Ans: To reverse a given list or array without creating another list or array (in-place reversal), you can use the following Python code:
def reverse_in_place(arr):
start_index = 0
end_index = len(arr) - 1
while start_index < end_index:
# Swap elements at start_index and end_index
arr[start_index], arr[end_index] = arr[end_index], arr[start_index]
# Move indices towards the center
start_index += 1
end_index -= 1
# Example usage:
my_list = [1, 2, 3, 4, 5]
reverse_in_place(my_list)
print("Reversed List:", my_list)
This code defines a function reverse_in_place
that takes a list as an argument and reverses it in-place using a two-pointer approach. The start_index
starts from the beginning of the list, and the end_index
starts from the end of the list. Elements at these indices are swapped, and the indices move towards the center until they meet.
Keep in mind that this modifies the original list, and if you want to preserve the original list, you may need to create a copy before applying the in-place reversal.
Ans:
# it has O(s) so basically linear running time complexity as far as the number
# of letters in the string is concerned
def is_palindrome(s):
original_string = s
# this is what we have implemented in the previous lecture in O(N)
reversed_string = reverse(s)
if original_string == reversed_string:
return True
return False
# O(N) linear running time where N is the number of letters in string s N=len(s)
def reverse(data):
# string into a list of characters
data = list(data)
# pointing to the first item
start_index = 0
# index pointing to the last item
end_index = len(data)-1
while end_index > start_index:
# keep swapping the items
data[start_index], data[end_index] = data[end_index], data[start_index]
start_index = start_index + 1
end_index = end_index - 1
# transform the list of letters into a string
return ''.join(data)
if __name__ == '__main__':
print(is_palindrome('Kevin'))
Ans:
def reverse_integer(n):
reversed_integer = 0
while n > 0:
remainder = n % 10
reversed_integer = reversed_integer*10 + remainder
n = n // 10
return reversed_integer
if __name__ == '__main__':
print(reverse_integer(12345678))