By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

do looping from N to 1

P: 16
Suppose I've some user-defined lists:
Say,

[1,2,3,5,6,8]
[1,2,3,4,5,6,7,8,9,10]

Suppose I've to split the list using user defined number, say N =3 for the first list.
Then the output will be:

for N = 3 [1,2,3]
for N-1 = 2 [5,6]
for N -2 = 1 [8]

say N =4 for the second list.
Then the output will be:

for N = 4 [1,2,3,4]
for N-1 = 3 [5,6,7]
for N -2 = 2 [8,9]
for N-3 = 1 [10]

So looping from N to 1. How can I get the output.
Nov 5 '14 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 613
Just a hint, but this is enough info to figure out the logic. And you should use a for or while loop to do it.
Expand|Select|Wrap|Line Numbers
  1. x=[1,2,3,4,5,6,7,8,9,10]
  2. n=4
  3. group=n
  4. previous=0
  5. print x[previous:n]
  6. previous = n
  7. group -= 1
  8. n += group
  9. print x[previous:n] 
Nov 5 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.