Exemplo n.º 1
0
def raw_group(divisors,messages,qh,rh,wh):
	'''
	purpose
		return a group with:
			name: 'crc_d%i_m%i_' with divisor, message lengths
			divisors: a list of divisors of the same length
			messages: a list of messages of the same length
			forward only: no divisor or dividend hotspots
			quotient hotspots: list of all qh-tuples of hotspots
			remainder hotspots: list of all rh-tuples of hotspots
			work hotspots: list of all rh-tuples of hotspots
	preconditions
		divisors: non-empty list of bitstrings of length > 1
			all divisors are the same length
		messages: non-empty list of bitstrings of length >= d
			all messages are the same length
		qh, rh, wh: legal hotspot counts
	'''
	# extract divisor, message lengths
	d_len = len(divisors[0])
	m_len = len(messages[0])

	# compute quotient, remainder, work lengths
	q_len = crc_util.quotient_len(d_len,m_len)
	r_len = crc_util.remainder_len(d_len,m_len)
	num_rows = crc_util.number_of_rows(d_len,m_len)
	row_width = crc_util.row_width(d_len,m_len)

	# compute quotient, remainder, work index lists; index pairs for work
	q_indexes = range(0,q_len)
	r_indexes = range(0,r_len)
	w_indexes = generate_util.cross_product \
		([range(num_rows),range(row_width)])

	# create the group, ready for cross product
	return [ 'crc_d%i_m%i_' % (d_len,m_len), [
		divisors, messages, [ ], [ ],
		generate_util.choose_k(q_indexes,qh),
		generate_util.choose_k(r_indexes,rh),
		generate_util.choose_k(w_indexes,wh),
	]]
# None count is equal to list length
actual = generate_util.generalize([1,2,3],3)
expected = [[None,None,None]]
if actual != expected:
	print 'Error:\n\tactual: %s\n\texpected: %s' % (str(actual),str(expected))

# None count is not equal to list length
actual = generate_util.generalize([1,2,3],1)
expected = [[None,2,3],[1,None,3],[1,2,None]]
if actual != expected:
	print 'Error:\n\tactual: %s\n\texpected: %s' % (str(actual),str(expected))

# ********** choose_k

cut = 'choose_k'
actual = generate_util.choose_k(range(3),2)
expected = [ [0,1], [0,2], [1,2] ]
message = '''Error in %s:
\tactual: %s
\texpected: %s
'''
if actual != expected:
	print message % (cut,str(actual),str(expected))

actual = generate_util.choose_k(range(3),1)
expected = [ [0], [1], [2] ]
message = '''Error in %s:
\tactual: %s
\texpected: %s
'''
if actual != expected:
def precedence(operator0,operator1):
	question = '''\
	Consider the binary operators <tt>$operator0</tt> and
	<tt>$operator1</tt>.
	Does <tt>$operator0</tt> have higher, equal, or lower precedence
	than <tt>$operator1</tt>?
	'''
	question = question.replace('$operator0',operator0[0])
	question = question.replace('$operator1',operator1[0])
	if operator0[1] < operator1[1]:
		correct_index = 0
	elif operator0[1] == operator1[1]:
		correct_index = 1
	else:
		correct_index = 2
	return [question, ['higher','equal','lower'], correct_index]

L = generate_util.choose_k(binary_operator_precedence,2)
L_swapped = [ [p[1],p[0]] for p in L ]

print 'expected number of multiple_choice_7_1_ questions:', \
	len(L)+len(L_swapped)

question_groups = [

['multiple_choice_7_1_'] + # structure and value of an expression -----
	[ precedence(p[0],p[1]) for p in (L + L_swapped) ]

]
Exemplo n.º 4
0
# None count is equal to list length
actual = generate_util.generalize([1, 2, 3], 3)
expected = [[None, None, None]]
if actual != expected:
    print 'Error:\n\tactual: %s\n\texpected: %s' % (str(actual), str(expected))

# None count is not equal to list length
actual = generate_util.generalize([1, 2, 3], 1)
expected = [[None, 2, 3], [1, None, 3], [1, 2, None]]
if actual != expected:
    print 'Error:\n\tactual: %s\n\texpected: %s' % (str(actual), str(expected))

# ********** choose_k

cut = 'choose_k'
actual = generate_util.choose_k(range(3), 2)
expected = [[0, 1], [0, 2], [1, 2]]
message = '''Error in %s:
\tactual: %s
\texpected: %s
'''
if actual != expected:
    print message % (cut, str(actual), str(expected))

actual = generate_util.choose_k(range(3), 1)
expected = [[0], [1], [2]]
message = '''Error in %s:
\tactual: %s
\texpected: %s
'''
if actual != expected: