Answer to Question #276925 in Python for hazel lofranco

Question #276925

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-12-08T03:06:19-0500
from random import randint
odd  = int(input('Enter odd integer: '))
L = list()
for _ in range(odd):
	L.append(str(randint(1, 9)))
L = L[::-1]
half = odd//2
print('[' + ','.join(L[:half]) + ']-[' + L[half] + ']-[' + ','.join(L[half+1:]) + ']')

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