Условие задачи если кому-то интересно:
Given a sequence of items and a specific item in that sequence, return the item immediately following the item specified. If the item occurs more than once in a sequence, return the item after the first occurence. This should work for a sequence of any type. When the item isn't present or nothing follows it, the function should return nil in Clojure and Elixir, Nothing in Haskell, undefined in JavaScript, None in Python.
Я написал код который находит элемент в массиве и выводит следующий за ним, но он не работает с последним условием тестов ниже, как это можно исправить, чтобы он работал и с последним условием:
def next_item(xs, item):
lenght , i = len (list(xs)) , 0
while i < lenght:
if item == xs [i]:
if i+1 < lenght:
return xs [i+1]
i += 1
Вот код тестов:
print (next_item ([1, 2, 3, 4, 5, 6, 7, 8], 5), 6)
print (next_item (['a', 'b', 'c'], 'd'), None)
print (next_item (['a', 'b', 'c'], 'c'), None)
print (next_item ('testing', 't'), 'e')
print (next_item (iter(range(1, 30000)), 12), 13)
p.s: Можно еще описать в ответе, что делает iter() в 5 тесте
что делает iter() в 5 тесте
Вероятнее всего намекает, что на вход может подаваться не только последовательность (в терминах python), но и, например, бесконечный генератор, что не позволит вам преобразовать его в список, как это сделано сейчас.