Check 5.4
Write the function flatten2D(lst) that takes a 2D list of integers (possibly ragged!) and returns a new 1D list with the same elements row-by-row.
For example, flatten2D([[1, 2, 3], [4, 5], [6, 7], [8]]) should return [1, 2, 3, 4, 5, 6, 7, 8].
We'll keep this simple and assume that every integer is nested inside of the inner lists, i.e. there are no elements such that type(lst[i])==int. There are also no empty lists.
def flatten2D(lst):
pass
def testFlatten2D():
print("Testing flatten2D...", end="")
a=[[1, 2], [4, 5]]
assert(flatten2D(a)==[1,2,4,5])
a=[[1, 2, 3], [110, 112, 122], [6, 7, 8]]
assert(flatten2D(a)==[1,2,3,110,112,122,6,7,8])
a=[[1, 2, 3], [4, 5], [6, 7], [8]]
assert(flatten2D(a)==[1,2,3,4,5,6,7,8])
print("Passed.")
testFlatten2D()
import sys
def set_certificate(certificate_div_id, certificate):
document[certificate_div_id].textContent = certificate
def get_student_code(student_code_div_id):
raw_student_code = document[student_code_div_id].textContent
return window.patchCodeToCheckTimeout(raw_student_code, 'check_timeout();');
class captureIO:
def __init__(self):
self.captured = []
def get_output(self):
out = ""
for c in self.captured:
out += str(c)
return out
def write(self, data):
self.captured.append(data)
def flush(self):
pass
def test(student_code):
exec(student_code)
result = []
cases = [[[11, 14, 15, 4], [20, 11, 2], [9, 18, 10]], [[2, 6, 12, 10], [12, 16, 7, 9], [9, 4, 5, 13, 3]], [[12, 0, 1], [4, 10], [16, 18]], [[4], [20, 16, 16, 9], [11, 18, 8]], [[12], [17, 8, 14, 7], [1, 12, 19]], [[13, 12, 2, 18], [3, 13], [20, 19]], [[18, 3, 6, 9], [9, 3, 9, 0, 18], [0, 10, 16, 10, 16]], [[7, 13, 4], [20, 8, 0, 10, 2], [7, 16]], [[15, 16, 1, 15, 16], [7, 8, 11], [8, 2, 9]], [[1, 16, 13, 19], [2], [11, 0, 5]], [[16], [19, 13, 19, 2, 16], [3, 20, 10, 0]], [[4, 0, 4], [9, 7, 8, 3, 17], [1, 11, 15]], [[13, 5, 11, 7], [2], [13, 14]], [[17, 1, 11], [5, 14, 7, 11], [14, 4, 9, 2, 19]], [[13, 4, 4, 6, 7], [20], [16, 4, 12]], [[13, 19, 8, 8, 2], [6, 14, 4], [0]], [[11], [11, 7, 15], [3, 13, 9]], [[2, 1, 1, 15, 11], [7], [5, 0, 1, 10, 2]], [[13, 18, 7, 3], [11, 12, 15], [9, 0, 8, 10]], [[0, 5, 17, 7], [13, 10, 13], [12, 8, 16, 11]]]
for lst in cases:
result.append(flatten2D(lst))
return result
def make_certificate(student_code_div_id, certificate_div_id):
student_code = get_student_code(student_code_div_id)
certificate = []
try:
capture = captureIO()
sys.stdout = capture
sys.stderr = capture
result = test(student_code)
certificate.append((result, type(result)))
set_certificate(certificate_div_id, str(certificate))
except:
set_certificate(certificate_div_id, "error")