Question

May I please know how to program this question in python step by step? Thank you

You are to write a function called arrange. Your function will reorder a given list, where even indexed elements are arranged by descending order (based on index order) from 0 to n/2 of the list and odd indexed elements arranged in ascending order from (n/2) + 1 to n 1. Your function does not need to return the list but to change the existing list passed as an argument. Please remember, if you reassign the argument variable, you no longer have access to the original list contents. n refers to the number of elements in the list Your function will modify and rearrange the list passed to the function and the order will be checked How the list is reordered: Unchanged list Index: 0 1 2 3 45 6 Data: [3, 5, 2, 8, 7, 9, e] Rearranged list Index: 6 4 2 0, 1, 3,5 Data: [e, 7, 2, 3, 5, 8, 9] 6, 4, 2, e: Descending order from to n/2 1, 3, 5: Ascending order from n/2+ 1 to n-1 Example 1 Input:[1, 2, 3, 4, 5] Rearranged: [5, 3, 1, 2, 4]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
[0, 7, 2, 3, 5, 8, 9] 15, 3, 1, 2, 4, 5, 3, 1, 2, 4, 6] [0, 7, 2, 3, 5, 8, 9] 1 def arrange(lst): result # for knowing last o
def arrange(lst):
result = []
  
# for knowing last odd index
first = len(lst)-1
if first%2 == 1:
first = first - 1
  
# having first half of the result array
for i in range(first, -1, -2):
result.append(lst[i])
  
# having second half of the result array
for i in range(1, len(lst), 2):
result.append(lst[i])
  
# printing output
lst = result
print(lst)

# sample runs
arrange([3,5,2,8,7,9,0])
arrange([1,2,3,4,5])
arrange([1,2,3,4,5,6])
arrange([3,5,2,8,7,9,0])
Add a comment
Know the answer?
Add Answer to:
May I please know how to program this question in python step by step? Thank you...
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
  • write a code in python for COUNTING SORT. check the runtime for the algorithms for varying...

    write a code in python for COUNTING SORT. check the runtime for the algorithms for varying input sizes. Input size n (size of the list) the following list sizes 1. 10,000 2. 100,000 3. 1000,000 For each of these sizes we are going to have two types of input: a. Sorted list (ascending order) of elements from range (1:n) b. Sorted list (descending order) of elements from range (n:1)

  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Please use Haskell, thank you! For this question, you will implement (under specific constraints) a recursive...

    Please use Haskell, thank you! For this question, you will implement (under specific constraints) a recursive function in Haskell that takes a list of characters as an argument and returns a list that contains only every third element from the argument list in the same order. As a clarifying example, this function, when passed the argument list “ABCDEFGHIJKLMNO”, should return the list “ CFILO”. It should also be noted that your function must work (i.e., not terminate with an error)...

  • Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

    Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That...

  • 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...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • IN PYTHON Develop the program that will allow you to obtain an ordered list of whole...

    IN PYTHON Develop the program that will allow you to obtain an ordered list of whole numbers from least to greatest from two lists of whole numbers. The individual values ​​of the input lists are captured by the user one by one and then both lists will be joined and then sorted in ascending order (from lowest to highest value). Your program should allow the capture of the entire values ​​of the lists without making use of messages (legends) to...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

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
ADVERTISEMENT