CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 3.3


  1. Check 3.3
    Write a function called everyThirdCharacter(s) which takes in a string s and returns a new string which only contains every third character from s. For example, everyThirdCharacter("abcdefghijk") should return "adgj". Note that there are several ways to solve this! Pick any of them!

    Hint: A useful programming pattern for strings is "building up a new string". Here's an example code snippet which uses that strategy. Think about how you can apply this kind of logic to the next few checks.
    def removeSpaces(s): newString = "" for c in s: if c != " ": newString += c return newString

    def everyThirdCharacter(s): return 42 print(everyThirdCharacter("Nokiabcd etc Fiji oO0b?%!")) 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 make_certificate(student_code_div_id, certificate_div_id): student_code = get_student_code(student_code_div_id) certificate = [] try: execCapture = captureIO() sys.stdout = execCapture sys.stderr = execCapture exec(student_code) for test in ['ZFY', 'RFlsw!AfXrEcbEeWvK&pB2h', 'HdHVm2WkObR5bf', 'N3iZqFG2NrdQxXe67aHs2Ism2sd', '$qYIZLFr91QEYTmw1sT', 'mGZn&2IeZTK&T!skgmm#!', 'kou5tWJSsYCr$YSBveR!kx&1mP2dL', 'vZLc#3!sNU7aLg', 'x5', 'rPMsz', 'GFgMuPiorhIr4GgZ', 'rrA3vMOdy$bp75MZP&Kah5o', '2hhBA8R9DwC', '8nC5bcxMSd&g71WkkavvX83BbxQ', 'qjEc7sN', 'uo2', 'skPwpjE!6!NAAMpmsmJETbHRbNRB', 'AP5aOp8MZV5x6vlX6m8HvVnsix', 'qoZPY', 'cU5keolvxGBR', '9acE', 'Sx5I9w!h1!gWjL#icGZOdtUV', 'HyFR3pZo', 'BWLF4J9FYxo4kx5K3eRkXFi3xf', '&2RLrSJswkoI!g', 'bMMpZKoU9ev!ZVP5rQI2W', 'Ko', 'zddXnlMn0xtPt#1X9E', 'Ber&ZN', '9fEQ#tyhPgRZ']: output = everyThirdCharacter(test) certificate.append((output, type(output))) except: pass set_certificate(certificate_div_id, str(certificate))

  2. Back to notes