I was doing some work with python iterable parameters.
I was making a function something like this :
def once_in_a_row(iterable):
pass
This function should take any iterable: It should produce every value in the iterable, but does not produce the same value twice in a row: if the current value is the same as the last one yielded, it skips yielding the current value.
Example :
for i in once_in_a_row('abbcccaadd'):
print(i,end=' ')
It produces the values 'a', 'b', 'c', 'a', and 'd'.
What can be best simple way to do it ? I am having a hide(iterable) definition too.
def hide(iterable):
for v in iterable:
yield v
This function is called to ensure that code works on general iterable parameters (not just a string, tuple, list, etc.). For example, although we can call len(string)
we cannot call len(hide(string))
, so the function once_in_a_row should not call len
on their parameters