Skip to content

Instantly share code, notes, and snippets.

@pharshal
Created May 8, 2015 08:45
Show Gist options
  • Save pharshal/5d587793a9701179be98 to your computer and use it in GitHub Desktop.
Save pharshal/5d587793a9701179be98 to your computer and use it in GitHub Desktop.
Write a function that combines two lists by alternatingly taking elements. For example: given the two lists [a, b, c] and [1, 2, 3], the function should return [a, 1, b, 2, c, 3].
def new_list(list1, list2):
list = []
for item1, item2 in zip(list1, list2):
list.append(item1)
list.append(item2)
return list
@ravinat
Copy link

ravinat commented Jul 25, 2017

please share the full code in javascript.

@enamul1
Copy link

enamul1 commented Aug 20, 2017

let a = [5,10,15];
let b = ['A', 'B', 'C'];

var newList = function(a,b)
{
  let l = a.length+b.length;
  let r = [];
  let j=0,k=0;
  for(let i=0; i<l;i++) {
    if(i%2==0) {
      r[i]=a[j++];
    } else {
      r[i]=b[k++];
    }
  }
  return r;
}

console.log(newList(a,b));

@aminubarade
Copy link

function newList(alph, num) {
var list = [];
for (var i=0;i<alph.length;++i) {
list.push(alph[i]);
list.push(num[i]);
}
return list;
}
console.log(newList(['a','b','c'],[1,2,3]));

@cassandraC10
Copy link

please can you do the exact same in java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment