Skip to content

Instantly share code, notes, and snippets.

@KavithaShanmugam
Forked from anonymous/gist:490c74a6473425fdaa88
Last active August 21, 2016 20:20
Show Gist options
  • Save KavithaShanmugam/5350044843f146c53a53abbaba5cbe0f to your computer and use it in GitHub Desktop.
Save KavithaShanmugam/5350044843f146c53a53abbaba5cbe0f to your computer and use it in GitHub Desktop.
Python coding task: "iter_sample" @http://codepad.org/9NdtMCJh
"""
Programming task
================
Implement the method iter_sample below to make the Unit test pass. iter_sample
is supposed to peek at the first n elements of an iterator, and determine the
minimum and maximum values (using their comparison operators) found in that
sample. To make it more interesting, the method is supposed to return an
iterator which will return the same exact elements that the original one would
have yielded, i.e. the first n elements can't be missing.
You may make use of Python's standard library. Python 3 is allowed, even though
it's not supported by codepad apparently.
Create your solution as a private fork, and send us the URL.
"""
from itertools import count
import unittest
def iter_sample(it, n):
"""
Peek at the first n elements of an iterator, and determine the min and max
values. Preserve all elements in the iterator!
@param it: Iterator, potentially infinite
@param n: Number of elements to peek off the iterator
@return: Tuple of minimum, maximum (in sample), and an iterator that yields
all elements that would have been yielded by the original iterator.
"""
# TODO
# Temporary variable m is used to create the iterator list in a range of length 100
m = 0
iter_list = []
for i in it:
iter_list.append(i)
if i == m + 100:
break
# Finds the max and min values for only the first 10 elements of a range of length 100
elif n == 10:
it_list = iter_list[:10]
min_val = min(it_list)
max_val = max(it_list)
# Finds the max and min values for a range of length 100
else:
min_val = min(iter_list)
max_val = max(iter_list)
#print iter_list
#print min_val
#print max_val
return min_val, max_val, iter_list
class StreamSampleTestCase(unittest.TestCase):
def test_smoke(self):
# sample only the first 10 elements of a range of length 100
it = iter(range(100))
min_val, max_val, new_it = iter_sample(it, 10)
self.assertEqual(0, min_val)
self.assertEqual(9, max_val)
# all elements are still there:
self.assertEqual(list(range(100)), list(new_it))
def test_sample_all(self):
# sample more elements than there are - no error raised
# now we now the global maximum!
it = iter(range(100))
min_val, max_val, new_it = iter_sample(it, 1000)
self.assertEqual(0, min_val)
self.assertEqual(99, max_val)
self.assertEqual(list(range(100)), list(new_it))
def test_infinite_stream(self):
# and guess what - it also works with infinite iterators
it = count(0)
min_val, max_val, _ = iter_sample(it, 10)
self.assertEqual(0, min_val)
self.assertEqual(9, max_val)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment