Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Given a list of N numbers, write a function to shift the numbers circularly by some integer k (where k < N).
- The function should take a list and k as a arguments and return the shifted list.
- """
- RIGHT = 1
- LEFT = 0
- def main():
- print("THe shifter function. Shits all elements in a array given a shift point and a direction")
- print("Populate the array!")
- print()
- numbers_list = []
- while True:
- number = int(input("Give a number: "))
- numbers_list.append(number)
- another = input("Another? ['y'/'n']: ")
- if another.lower() == 'n':
- break
- print("The array is: ", numbers_list)
- direction = input("Give the direction to switch the numbers: ")
- if direction.lower() == 'right':
- direction = RIGHT
- elif direction.lower() == 'left':
- direction = LEFT
- shifter_int = int(input("The shifter element: "))
- print(shifter(numbers_list, shifter_int, direction))
- def shifter(numbers, shift_integer, shift_direction):
- """
- :param numbers: it is the list of numbers generated by the user in the main function
- :param shift_integer: the integer to shift from - the index to be hold on place
- :param shift_direction: the direction to shift the number
- :return: the shifter list
- """
- # the complicate way - shifting to the right without list slicing
- # using a while loop and a container to hold the data - remove it and then append it at the end of the list
- # one could use slicing - easies way
- if shift_direction:
- while shift_integer >= 0:
- holder = numbers[shift_integer-1]
- numbers.remove(holder)
- numbers.append(holder)
- shift_integer -= 1
- return numbers
- # the easiest way - using the integer as a cut element and split the initial list accordingly
- else:
- numbers = numbers[shift_integer:][::-1] + numbers[:shift_integer]
- return numbers
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement