Question

Use Python 3.7 to write the following program: Given a list of n numbers, write a...

Use Python 3.7 to write the following program: Given a list of n numbers, write a function to shift the numbers circularly by some integer k (k < n). The function should take a list, the amount to shift k, and an argument specifying the direction to shift (left or right). Suggestion: use the % operation and also you need a number between 0 inclusive and the length of the list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Python code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

def Rotate(lists, k, direction):
   if direction == 0:   #left rotation
      output_list = []
      for item in range(k, len(lists)):
         output_list.append(lists[item]) #add items from index k to end
      for item in range(0, k):
         output_list.append(lists[item]) #add items from index 0 to k-1
      return output_list
   else: #right rotation
      output_list = []
      for item in range(len(lists) - k, len(lists)): #add items from index end-k to end
         output_list.append(lists[item])
      for item in range(0, len(lists) - k): #add items from index 0 to end-k-1
         output_list.append(lists[item])
      return output_list
    
k = 3
lst = [1, 2, 3, 4, 5, 6,7,8,9,10]
print(Rotate(lst, k, 0))
print(Rotate(lst, k, 1))

test.py - C\WindowslSystem32test.py (3.5.0) File Edit Format Run Options Window Help def Rotate (lists, k, direction) if dire

Below is the screenshot of output

Python 3.5.0 Shell File Edit Shell Debug Options Window Help Python 3.5.0 (v3.5.0: 374f501f4567, Sep 13 2015, 02:16:59) [MSC

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
Use Python 3.7 to write the following program: Given a list of n numbers, write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • Can someone do this python program? Write a function that accepts a list of five numbers...

    Can someone do this python program? Write a function that accepts a list of five numbers from a user and prints the length, sum, average, and largest number of the list.

  • python In a program, write a function named roll that accepts an integer argument number_of_throws. The...

    python In a program, write a function named roll that accepts an integer argument number_of_throws. The function should generate and return a sorted list of number_of_throws random numbers between 1 and 6. The program should prompt the user to enter a positive integer that is sent to the function, and then print the returned list.

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Python: Write a func that will create a list of random numbers. The function will take...

    Python: Write a func that will create a list of random numbers. The function will take in two parameters. The first parameter specifies how many values to generate and the second parameter is the highest random # (2 is the minimum) that the func generates. To get the random numbers, you may use this line of code: y = random.randint (2,max), where max is the highest possible random #, INCLUSIVE.

  • 6 Generate Triangle Numbers: You must use Python 3 Write a function called generateTriangleNumbers(). This function...

    6 Generate Triangle Numbers: You must use Python 3 Write a function called generateTriangleNumbers(). This function will take in an integer x and will return a list of integers containing the first x triangle numbers. The nth triangle number is the sum of 1 + 2 + 3 + 4...(n − 1) + n. Some example input-output pairs: 3 -> [1, 3, 6] 1 -> [1] 7 -> [1, 3, 6, 10, 15, 21, 28]

  • Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers,...

    Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers, multiplies them together, and prints out the sentence '____ times ____ is ____'. Ask the user for two numbers, and call multiply_and_print with their two numbers as arguments. 2.Write a function called roll_dice that rolls two six-sided dice and prints out the result of each die. Call it three times. (Remember, you can import the random module with the statement import random, and then...

  • Can you help me write a Python 3.7 code for this question? Write a program using...

    Can you help me write a Python 3.7 code for this question? Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The...

  • #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function...

    #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function named roll that #accepts an integer argument number_of_throws. The #function should generate and return a sorted #list of number_of_throws random numbers between #1 and 6. The program should prompt the user to #enter a positive integer that is sent to the function, #and then print the returned list. How would you do this?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT