Skip to content

Commit c6d5c53

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent 4b90efd commit c6d5c53

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

output/cycle_a_list_of_values.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Kata - Cycle a list of values
2+
3+
completed at: 2024-04-14 16:32:28
4+
by: Jakub Červinka
5+
6+
## Prologue
7+
8+
You're part of a team porting MS Paint into the browser and currently working on a new UI component that allows user to control the canvas zoom level.
9+
10+
According to the wireframes delivered to you in PowerPoint format the user should be able to *cycle* through specified zoom levels by clicking a button in the UI repeatedly. The reverse direction should work with shift key held.
11+
12+
A new function is needed to support this behavior, so you alt-tab to Visual Studio and get to work.
13+
14+
---
15+
16+
## Instructions
17+
18+
Implement a function which when given the arguments
19+
20+
1. Direction to which to cycle the current value
21+
2. List of values
22+
3. Current value
23+
24+
returns the value next to current value in the specified direction.
25+
26+
The function should pick the next value from the other side of the list in case there are no values in the given direction.
27+
28+
## Examples
29+
30+
``` haskell
31+
cycleList R [1,2,3] 1 -- => Just 2
32+
cycleList L [1,2,3] 1 -- => Just 3
33+
cycleList R [1,2,3] 0 -- => Nothing
34+
cycleList L ["foo", "bar", "xyz"] "bar" -- => Just "foo"
35+
```
36+
``` javascript
37+
cycle(1, [1,2,3], 1) // => 2
38+
// Given the direction 1, returns the value next to 1 on the right
39+
40+
cycle(-1, [1,2,3], 1) // => 3
41+
// Given the direction -1 and value 1, wraps around list returning the last element
42+
43+
cycle(1, [1,2,3], 0) // => null
44+
// 0 does not exist in the list, returns null
45+
46+
cycle(1, [1,2,2,3], 2) // => 2
47+
// Corner case: multiple instances of given value, picks next relative to first occurrence
48+
```
49+
``` coffeescript
50+
# Given the direction 1, returns the value next to 1 on the right
51+
cycle(1, [1,2,3], 1) # => 2
52+
53+
# Given the direction -1 and value 1, wraps around list returning the last element
54+
cycle(-1, [1,2,3], 1) # => 3
55+
56+
# 0 does not exist in the list, returns null
57+
cycle(1, [1,2,3], 0) # => null
58+
59+
# Corner case: multiple instances of given value, picks next relative to first occurrence
60+
cycle(1, [1,2,2,3], 2) # => 2
61+
```
62+
``` python
63+
# Given the direction 1, returns the value next to 1 on the right
64+
cycle(1, [1,2,3], 1) # => 2
65+
66+
# Given the direction -1 and value 1, wraps around list returning the last element
67+
cycle(-1, [1,2,3], 1) # => 3
68+
69+
# 0 does not exist in the list, returns null
70+
cycle(1, [1,2,3], 0) # => null
71+
72+
# Corner case: multiple instances of given value, picks next relative to first occurrence
73+
cycle(1, [1,2,2,3], 2) # => 2
74+
```
75+
"""
76+
77+
def cycle(d, v, c):
78+
if c not in v:
79+
return None
80+
i = v.index(c)
81+
try:
82+
return v[i + d]
83+
except IndexError:
84+
return v[0]
85+

0 commit comments

Comments
 (0)