Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 827 Bytes

alternate.md

File metadata and controls

24 lines (17 loc) · 827 Bytes
Story
  • Given an integer n and two other values, build an array of size n filled with these two values alternating.

Examples (input -> output):

5, true, false     -->  [true, false, true, false, true]
10, "blue", "red"  -->  ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
0, "one", "two"    -->  []

Solution:

def alternate(n, first_value, second_value):
    return [first_value if i % 2 == 0 else second_value for i in range(n)]

print(alternate(5, True, False))     # [True, False, True, False, True]
print(alternate(10, "blue", "red"))  # ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
print(alternate(0, "one", "two"))    # []