3 Replies - 298 Views - Last Post: 28 October 2017 - 01:38 PM Rate Topic: -----

#1 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-March 15

I think I managed to confuse myself

Posted 28 October 2017 - 12:41 PM

okay so I'm working on as assignment for my Introduction to programming class and I think I'm over-complicating the situation and thus screwing up my efforts at the solution. So the assignment is:

Quote

Given the lists list1 and list2, not necessarily of the same length, create a new list consisting of alternating elements of list1 and list2 (that is, the first element of list1 followed by the first element of list2 , followed by the second element of list1, followed by the second element of list2, and so on. Once the end of either list is reached, the remaining elements of the longer list is added to the end of the new list. For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6, 7, 8], then the new list should contain [1, 4, 2, 5, 3, 6, 7, 8]. Associate the new list with the variable list3.


Okay, so I have 2 lists that may or may not be the same size. in the previous exercise I started with the same thing, but the list3 would only go as far as the shorter of the two lists went. For that one I came up with the following:

list3 = []
range1 = len(list1)
range2 = len(list2)
if range1 < range2:
	full_range = range1
else:
	full_range = range2
for i in range(full_range):
	list3.append(list1[i])
	list3.append(list2[i])



And that worked out. Now I come to the next problem where instead of stopping at the end of the shorter list it continues until the end of the longer list.

So I'm figuring I need some sort of switch to look at the length of each list, see which one is shorter, and then use that to determine how far to go between each list, then after it reaches the end of the shorter list, use the end of the shorter lists length +1 to to start adding the remaining elements of the longer list until the end of the longer list.

Am I making sense?

Is This A Good Question/Topic? 0
  • +

Replies To: I think I managed to confuse myself

#2 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-March 15

Re: I think I managed to confuse myself

Posted 28 October 2017 - 12:56 PM

Okay I got it figured it out. Yes I was over-thinking it. What I came up with was:

list3 = [] 
for i in range(max(len(list1),len(list2))): 
	if i < len(list1): 
		list3.append(list1[i]) 
	if i < len(list2): 
		list3.append(list2[i])

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: I think I managed to confuse myself

Posted 28 October 2017 - 01:03 PM

Ok well you have full_range being the length of the longer list. So you are correct to be using that as the range to iterate on. Then check if "i" is less than or equal to the length of each array. If it is, take one from that list. The result is that one if statement will be still running for the longer list than the other.

for i in range(full_range):
   if i < range1:
      list3.append(list1[i])
   if i < range2:
      list3.append(list2[i])



Hope you get the idea :)
Was This Post Helpful? 0
  • +
  • -

#4 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-March 15

Re: I think I managed to confuse myself

Posted 28 October 2017 - 01:38 PM

Yep, I got it. thanks. Like I said, I was over thinking it. Making it more complicated then it needed to be.

This post has been edited by andrewsw: 28 October 2017 - 01:41 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1