1 alternating([5]) True 2 alternating([1,2,1]) True 3 alternating([3,2]) True 4 alternating([3,2,3,3]) False

Define a Python function alternating(l) that returns True if the values in the input list alternately go up and down PROGRAM

25th Feb 2018, 8:57 AM
kamalesh vra
kamalesh vra - avatar
1 Answer
Have you tried something? You cannot request for someone who will did your homework... but we could help if you have specific problem/question at any step of your development ^^ First track: you need to iterate over the list to determine if contiguous elements taken 2 by 2 have a difference with sign strictly different from the previous difference (or the next: the two must be true, but testing both would be half-redundant when testing the next pair). With the special case difference equals zero, wich must be considered as if both sign(0)==sign(-1) and sign(0)==sign(1) even if obviously sign(-1)!=sign(1)... (the sign() method isn't a python built-in, it's just here to shorter my explanation ;))
2nd Apr 2020, 9:08 PM
visph - avatar