Answer to Question #254373 in Python for Hazel

Question #254373

5. Groupings

by CodeChum Admin

Did you know that you can also reverse lists and group them according to your liking with proper code? Let's try it to believe it.


Instructions

  1. Create a variable that accepts a positive odd integer.
  2. Create an empty list. Then, using loops, add random integer values into the list one by one. The number of values to be stored is dependent on the inputted odd integer on the first instruction.
  3. Reverse the order of the list. Then, print out the list's new order in this manner:
  4. [first-half values]-[middle value]-[second-half values], just like that of the sample output. Make use of list slicing that you’ve learned from the past lessons to easily achieve this segment.

Input

The first line contains an odd positive integer.

The next lines contains an integer.

5
1
3
4
5
2

Output

A line containing a list.

[2,5]-[4]-[3,1]
1
Expert's answer
2021-10-23T03:04:08-0400
import random 
R=[]
N=int(input("Input an odd positive interger: "))
for x in range(N):
    R.append(random.randint(1,5))
print("Generated random list")
print(R)
Back_list=R[::-1]
print("[",Back_list[0],Back_list[1],"]","-","[",Back_list[2],"]","-","[",Back_list[3],Back_list[4],"]")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS