While I can’t disclose too many details about the course or the quiz due to school policies and other related jazz, here’s the task at hand:

I have to hand in a quiz half of which is true / false questions – this is the part that I can’t get right, having tried reading the material and attempting the quiz one too many times I resort to the good old art of scripting.

Approach:

Write a python script to generate files, one for every combination of the true / false values .

Write a bash script to upload each solution file, the marking system counts only the entirely correct solution file so I don’t have to worry about uploading incorrect solutions.

There are 7 true and false questions in all, (sadly) even after going through the elimination process I am sure about only 2 of them, that leaves me with 2 ^ 5 possible combinations. Because of the boolean nature of the problem I am inclined to use binary numbers to represent these combinations.

If the correct combination contained True for each of the 5 questions, the binary number we’re looking at would be 11111 this corresponds to 31 (decimal number) , this is the maximum in our range.

The Python Stuff:

Generate the lists of all possible combinations

Initially I was using python’s built-in functions “int” and “bin” to convert between decimal and binary like so:

#convert 31 (dec) to binary bin(31) > 0b11111 #convert 0b11111 to dec int(str(11111),2) # here the first parameter, string function merely returns a string, the second parameter 2 tells the int function that the number we're passing it is of base 2 ("it is binary") > 31

However this would involve using the “zfill” function to fill in the zeros each time, a more elegant solution was to use “format” to handle both things.

<a href=”http://stackoverflow.com/questions/3252528/converting-a-number-to-binary-with-a-fixed-length”>more on this </a>

def generate_combinations(num_bits): # in our case this is 5 combinations = list() max_dec_num = pow(2,num_bits) for num in range(0,max_dec_num): combinations.append('{0:05b}'.format(num)) return combinations ''' Creating the solutions files ''' # Converting the list of binary numbers to T/F lists def convert(list_bin_nums): #pre: takes in a list of binary numbers and returns a list of t/f strings #post: returns a list of T, F s tf_list = list() for number in list_bin_nums: tf_string = list() for digit in number: if digit == '0': tf_string.append('F') if digit == '1': tf_string.append('T') tf_list.append(tf_string) return tf_list # Generating the text and parsing the combinations: def generate_content(*args): counter = 6 #I want to start from Question '6 file_contents_B="" for val in args[0]: file_contents_B = file_contents_B + "Question "+ str(counter) + ": " + str(val) + "

" counter = counter + 1 file_contents_A ='some text .... start of quiz ....

' file_contents_C ='some more text .... end of quiz, thank you for taking this annoying quiz

' file_contents = file_contents_A + file_contents_B + file_contents_C return file_contents_A + file_contents_B + file_contents_C

Note: the python function “range” works like so: range(a,b) => returns ‘a , a+1 ,.., b-1 (b is excluded).

Running this stuff in python shell, it would be nicer if I had wrapped this in a function:

>>> from scriptQuiz import * >>> combinations = generate_combinations(5) >>> combinations ['00000', '00001', '00010', '00011', '00100', '00101', '00110', '00111', '01000', '01001', '01010', '01011', '01100', '01101', '01110', '01111', '10000', '10001', '10010', '10011', '10100', '10101', '10110', '10111', '11000', '11001', '11010', '11011', '11100', '11101', '11110', '11111'] >>>len(combinations == 32) True >>> tf_list = convert(combinations) >>> tf_list [['F', 'F', 'F', 'F', 'F'], ['F', 'F', 'F', 'F', 'T'], ['F', 'F', 'F', 'T', 'F'], ['F', 'F', 'F', 'T', 'T'], ['F', 'F', 'T', 'F', 'F'], ['F', 'F', 'T', 'F', 'T'], ['F', 'F', 'T', 'T', 'F'], ['F', 'F', 'T', 'T', 'T'], ['F', 'T', 'F', 'F', 'F'], ['F', 'T', 'F', 'F', 'T'], ['F', 'T', 'F', 'T', 'F'], ['F', 'T', 'F', 'T', 'T'], ['F', 'T', 'T', 'F', 'F'], ['F', 'T','T', 'F', 'T'], ['F', 'T', 'T', 'T', 'F'], ['F', 'T', 'T', 'T', 'T'], ['T', 'F', 'F', 'F', 'F'], ['T', 'F', 'F', 'F', 'T'], ['T', 'F', 'F', 'T', 'F'], ['T', 'F', 'F', 'T', 'T'], ['T', 'F', 'T', 'F', 'F'], ['T', 'F', 'T', 'F', 'T'], ['T', 'F', 'T', 'T', 'F'], ['T', 'F', 'T', 'T', 'T'], ['T', 'T', 'F', 'F', 'F'], ['T', 'T', 'F','F', 'T'], ['T', 'T', 'F', 'T', 'F'], ['T', 'T', 'F', 'T', 'T'], ['T', 'T', 'T', 'F', 'F'], ['T', 'T', 'T', 'F', 'T'], ['T', 'T', 'T', 'T', 'F'], ['T', 'T', 'T', 'T', 'T']] >>> counter = 0 >>> for li in tf_list: ... files_stuff = generate_content(li) ... f_name = file_name + str(counter) + ".txt" ... buf = open(f_name,"wb") ... buf.write(files_stuff) ... buf.close() ... counter = counter + 1