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

extend list according to its elements

P: 51
i'm looking for a way to extend a list of 2 elements that represent boundaries, to the entire range.


for example: input list = [1 ,5,10,15]
output list would be = [1, 2, 3, 4 ,5 , 10 , 11 , 12 ,13 ,14 ,15]


i can make it using the following semi-pseudo code, but perhaps there is an easier way, more elegant...

Expand|Select|Wrap|Line Numbers
  1. $i = 0; 
  2. while $i != length(list) 
  3.     for ($j = list($i); j++; j<list($i+1)) {
  4.           push(@out_list,$j); 
  5.       }; 
  6.      $i += 2; 
  7. };
Apr 22 '08 #1
Share this Question
Share on Google+
1 Reply


nithinpes
Expert 100+
P: 410
i'm looking for a way to extend a list of 2 elements that represent boundaries, to the entire range.


for example: input list = [1 ,5,10,15]
output list would be = [1, 2, 3, 4 ,5 , 10 , 11 , 12 ,13 ,14 ,15]


i can make it using the following semi-pseudo code, but perhaps there is an easier way, more elegant...

$i = 0;
while $i != length(list)
for ($j = list($i); j++; j<list($i+1)) {
push(@out_list,$j);
};
$i += 2;
};
Though logic remains the same, you may reduce the number of lines of code by using this:
Expand|Select|Wrap|Line Numbers
  1. for($i=0;$i<@list;$i+=2) {
  2. push @out_list,($list[$i]..$list[$i+1]);
  3. }
  4.  
Apr 22 '08 #2

Post your reply

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