def f1(n):
	for i in range(0,n,3):
		yield i

def f2(n):
	for i in range(0,n,5):
		yield i

def top10(f,g):
	''' return the smallest 10 elements from generators f and g '''
	count=0

	# maintain a triple for each of f and g
	# (current_value, is_value_good, is_generator_exhausted)

	(f_cv, f_ivg, f_ige)=(None, False, False)
	(g_cv, g_ivg, g_ige)=(None, False, False)

	while count<10 and not (f_ige and g_ige):
		# advance f if the current vlaue is not good
		if (not f_ivg) and (not f_ige):
			try:
				f_cv=next(f)
				f_ivg=True
			except StopIteration as e:
				f_ige=True
				f_ivg=False

		# advance g if the current vlaue is not good
		if (not g_ivg) and (not g_ige):
			try:
				g_cv=next(g)
				g_ivg=True
			except StopIteration as e:
				g_ige=True
				g_ivg=False

		# yield whichever of f_cv and g_cv is smaller
		if f_ivg and g_ivg:
			if f_cv<g_cv:
				yield f_cv
				count+=1
				f_ivg=False
			else:
				yield g_cv
				count+=1
				g_ivg=False
		elif f_ivg:
			yield f_cv
			count+=1
			f_ivg=False
		elif g_ivg:
			yield g_cv
			count+=1
			g_ivg=False
		else: # no more vlaues in f,g
			return

	
for i in top10(f1(50), f2(50)):
	print(str(i))
