Scala - Shift element pairs in a list -
i starting scala , want code function shifts element pairs in list so:
changepairs(list(1,2,3,4,5,6,7,8,9,10,11))
//> res62: list[int] = list(2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 11)
i thought spliting list in two, reverse values , join list again, can't reverse values, managed reverse whole list:
def changepairs(a: list[int]) = { val listaux = zip a.tail val listaux2 = listaux.reverse } //> res6: list[(int, int)] = list((10,11), (9,10), (8,9), (7,8), (6,7), (5,6), (4,5), (3,4), (2,3), (1,2))
does know how can manage solve problem?
here's 1 way it, using grouped:
list.grouped(2).flatmap(_.reverse).tolist
Comments
Post a Comment