1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
amm1812
3 years ago
9

How to interchange first half of the array with second half of array in python?

Computers and Technology
1 answer:
damaskus [11]3 years ago
8 0

Answer:

Following are the code to this question:

l= []#defining an empty list

n=int(input("Enter total element you want to insert: "))#defining variable to input total element

for i in range(0,n):# defining loop to input value in list

   insert=int(input())#defining variable insert to input value

   l.append(insert) # adding the elemenents into the list

x=n//2#calculating midpoint  

l1 =l[:x]#using slicing to hold first-half value in l1 list

l2 =l[x:]#using slicing to hold second-half value in l2 list

print("Before interchange: ")#print message

print ("list : ",l)#print input list

l3=l2 + l1#add value in l3

print("After interchange: ")#print message

#print(l3)  #print list l3

print (str(l3)[1:-1])#print list l3

Output:

please find the attachment.

Explanation:

Description of the code:

  • In the above python program, an empty list l is declared, in the next line, variable n is defined, that input the total number of the list elements. In the next line, the for loop is declared, which uses the insert variable input value and used the append method to add value in the list.
  • In the next step, the variable x is declared, which finds the midpoint of the list, and defines the l1 and l2 lists, that use slicing to hold the first and second half values.
  • In the last step, the l3 list is declared, that adds the l1 and l2 lists and use the print method to print its values.

You might be interested in
A person gets 13 cards of a deck. Let us call for simplicity the types of cards by 1,2,3,4. In How many ways can we choose 13 ca
natima [27]

Answer:

There are 5,598,527,220 ways to choose <em>5</em> cards of type 1, <em>4 </em>cards<em> </em>of type 2, <em>2</em> cards of type 3 and <em>2</em> cards of type 4 from a set of 13 cards.

Explanation:

The <em>crucial point</em> of this problem is to understand the possible ways of choosing any type of card from the 13-card deck.

This is a problem of <em>combination</em> since the order of choosing them does not matter here, that is, the important fact is the number of cards of type 1, 2, 3 or 4 we can get, no matter the order that they appear after choosing them.

So, the question for each type of card that we need to answer here is, how many ways are there of choosing 5 cards of type 1, 4 cards of type 2, 2 cards of type 3 and 2 are of type 4 from the deck of 13 cards?

The mathematical formula for <em>combinations</em> is \\ \frac{n!}{(n-k)!k!}, where <em>n</em> is the total of elements available and <em>k </em>is the size of a selection of <em>k</em> elements  from which we can choose from the total <em>n</em>.

Then,

Choosing 5 cards of type 1 from a 13-card deck:

\frac{n!}{(n-k)!k!} = \frac{13!}{(13-5)!5!} = \frac{13*12*11*10*9*8!}{8!*5!} = \frac{13*12*11*10*9}{5*4*3*2*1} = 1,287, since \\ \frac{8!}{8!} = 1.

Choosing 4 cards of type 2 from a 13-card deck:

\\ \frac{n!}{(n-k)!k!} = \frac{13!}{(13-4)!4!} = \frac{13*12*11*10*9!}{9!4!} = \frac{13*12*11*10}{4!}= 715, since \\ \frac{9!}{9!} = 1.

Choosing 2 cards of type 3 from a 13-card deck:

\\ \frac{n!}{(n-k)!k!} =\frac{13!}{(13-2)!2!} = \frac{13*12*11!}{11!2!} = \frac{13*12}{2!} = 78, since \\ \frac{11!}{11!}=1.

Choosing 2 cards of type 4 from a 13-card deck:

It is the same answer of the previous result, since

\\ \frac{n!}{(n-k)!k!} = \frac{13!}{(13-2)!2!} = 78.

We still need to make use of the <em>Multiplication Principle</em> to get the final result, that is, the ways of having 5 cards of type 1, 4 cards of type 2, 2 cards of type 3 and 2 cards of type 4 is the multiplication of each case already obtained.

So, the answer about how many ways can we choose 13 cards so that there are 5 of type 1, there are 4 of type 2, there are 2 of type 3 and there are 2 of type 4 is:

1287 * 715 * 78 * 78 = 5,598,527,220 ways of doing that (or almost 6 thousand million ways).

In other words, there are 1287 ways of choosing 5 cards of type 1 from a set of 13 cards, 715 ways of choosing 4 cards of type 2 from a set of 13 cards and 78 ways of choosing 2 cards of type 3 and 2 cards of type 4, respectively, but having all these events at once is the <em>multiplication</em> of all them.

5 0
4 years ago
Word how to create a text box with lines 2013
Crank
U go on your keypad and where the numbers are at next to the 0 u see this ------------------------- so u can make lines with that


I hope this help*static*
4 0
4 years ago
Marijuana and hashish come from the hemp plant?
raketka [301]
Yes they come from the hemp plant
5 0
4 years ago
11 of the 25 people on the bus get off at the first stop. What percent best represents the portion of the bus that got off on th
miskamm [114]

Answer:

44%

Explanation: First, you have to divided 11 by 25. Which is 0.44. And then you multiply the quotient by 100 to get your percentage.

5 0
3 years ago
Which of the following is not a goal pursued in the game of economics?
Zanzabum

Answer:

I think B.Inflation

Explanation:

7 0
3 years ago
Other questions:
  • The key invention that enabled computers to go into every home and office is
    7·1 answer
  • If you feel that an OSHA inspection is needed to get hazards corrected at your workplace which is the best option
    9·1 answer
  • What is the primary limitation of free versions of antivirus applications
    14·1 answer
  • You support Richman Investments, a brokerage firm that employs 20 brokers. Each broker has his own client computer, and the firm
    15·1 answer
  • 4.3 Code Practice<br> edhisve
    13·1 answer
  • What is one advantage of top-down programming design?
    8·1 answer
  • 3.6 Code Practice Edhesive. (PYTHON LANGUAGE)
    13·1 answer
  • Define computer memory and write it's type ​
    6·1 answer
  • Edra kinsman is the sales manager for narbonne suites hotel chain, which has locations in washington, oregon, idaho, and montana
    14·1 answer
  • What is the significance of the scientific method?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!