Skip to content

Instantly share code, notes, and snippets.

@takwas
Last active June 11, 2019 13:48
Show Gist options
  • Save takwas/c535306af6dc8e760ed4bdaaed7b1340 to your computer and use it in GitHub Desktop.
Save takwas/c535306af6dc8e760ed4bdaaed7b1340 to your computer and use it in GitHub Desktop.
Quizzes
# THE SOLUTION IS THE FUNCTION `generate_first_elements`
# write a function which takes an iterable of iterators as
# an argument and returns a generator of first elements of
# the input iterators
def generate_first_elements(iterable_arg):
# return a "generator object" that produces the first
# element of each iterator in `iterable_arg`
return (next(i) for i in iterable_arg)
def _build_test():
iterables = [
range(1, 4),
'some string',
('T', 'U', 'P', 'L', 'E')
]
# create a test input iterable (e.g. a list)
# containing iterators as members
iterator_iterable = [iter(iterable) for iterable in iterables]
print('\ninput iterable: %r' % iterator_iterable)
generator = generate_first_elements(iterator_iterable)
return iterables, generator
def test_generate_first_elements():
iterables, generator = _build_test()
feed_elements = [elem[0] for elem in iterables]
result_elements = list(generator)
print('return values: %r' % result_elements)
assert feed_elements == result_elements
def test_generate_first_elements_return_type():
from types import GeneratorType
iterables, generator = _build_test()
print('return type: %r' % type(generator))
assert isinstance(generator, GeneratorType)
if __name__ == '__main__':
test_generate_first_elements()
test_generate_first_elements_return_type()
# THE SOLUTION IS THE FUNCTION `find_most_frequent_byte_in_file`
# you have a path to 1TB file, please write a function
# that returns the byte with the largest number of
# occurrences in that file
def _generate_bytes(binary_file):
pos = 0
while True:
# read 1 byte at a time
byte_ = binary_file.read(1)
if byte_ == b'':
# EOF reached
raise StopIteration
yield byte_
pos += 1
pos = binary_file.seek(pos)
def _find_most_frequent_byte_in_file(binary_file):
occurrences = {} # store all occurrences found
# track the maximum occurring byte
max_frequency = {
'byte': None,
'count': 0
}
# for efficiency, generate file content rather
# than read at once
bytes_generator = _generate_bytes(binary_file)
while True:
try:
byte_ = next(bytes_generator)
except StopIteration:
break
occurrences[byte_] = count = occurrences.get(byte_, 0) + 1
if count > max_frequency['count']:
max_frequency['byte'] = byte_
max_frequency['count'] = count
return max_frequency['byte']
def find_most_frequent_byte_in_file(file_path):
with open(file_path, 'rb') as binary_file:
return _find_most_frequent_byte_in_file(binary_file)
if __name__ == '__main__':
def test_find_most_frequent_byte_in_file():
from io import BytesIO
file_content = bytes('content', 'utf-8')
mock_file = BytesIO(file_content)
assert _find_most_frequent_byte_in_file(mock_file) in [b'n', b't']
test_find_most_frequent_byte_in_file()
print('Result: %r' %
find_most_frequent_byte_in_file('/tmp/frequent_byte.txt'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment