空飛ぶチンアナゴの統計解析日記

統計解析を嗜むチンアナゴのメモ帳です

Section Leaderは断念しました(前編)

セクションリーダーになることは断念。
最後の5分レクチャーの動画がどうしてもうまくできず断念しました。

あとは問題なく(というと若干語弊があるけど)できたんだけどねぇ……。

例題はどんなのが出たの?

https://codeinplace.stanford.edu/teach
実際のアプリケーションフォームから見てもらえるとわかるんだけど、アンケート数問と明らかにバグってるコードが置いてあってそれに対してどうアドバイスするのかという課題とMarsweight.pyを課題に「変数」について5分間ほどレクチャーするというもの。
今回は思ったよりも長くなりそうなのでまずは前編の『バグってるコードに対するアドバイスの課題』について書いていこうかと。

【前編】バグってるコードに対するアドバイスの課題

The next two questions are about the following buggy student code. You are allowed to run the code, but we encourage you to focus on the student's intention.

def main():
         dictionary = {}
         dictionary["learning"] = "awesome"
         dictionary["coding"] = "fun"
         # ... Fill with more data
         remove_keys_containing_string(dictionary, "learn")
 """
 This Python function takes in a dict and a string and removes all keys containing that string from the dict
 """
def remove_keys_containing_string(dictionary, remove):
    toRemove = None
    for key in dictionary:
        for i in range(len(key)):
            if key[i:i+1] == remove:
                toRemove.add(key)
    if toRemove != None:  
        for key in toRemove:
            del dictionary[key]

というコードが与えられて最初の課題として
「A) Identify the problem(s) in the student's code for remove_keys_containing_string *」
という問いが与えられる。これが全部のコードかはわからないがおそらく全部のコードだろうということで話を進めていきます。
実際にこれをコピペして適当なファイル名で保存して
python3 ファイル名.py
で動かしてみるとわかるのですがエラーが出ます。
その原因として

  1. “if __name__ == ‘__main__': main()が文末にないことでmain()がremove_keys_containing_stringを認識できない
  2. toRemoveがNoneだとそのあと何もできんよね
  3. key[i:i+1] だとremoveが2文字以上だと対応できんよね

ということから

a) If this script has not “if __name__ == ‘__main__': main()", the function main() can not recognize the function remove_keys_containing_string().
b) If the length of the argument ‘remove’ is longer than two letters, the function remove_keys_containing_string() doesn’t remove keys from the dictionary.
c) The local value toRemove should not be None. It should be a empty list.

とするのがよいかと思いました。

従ってB課題の「B) Write 3 sentences of advice to the student who wrote this code. Rather than point out the bugs, consider how to guide the student toward diagnosing and correcting the problems on their own. 」も

a) You need to consider how teach remove_keys_containing_string to main.
b) If the length of the argument ‘remove’ is longer than two letters, what kind of issue is occur?
c) None type is immutable, so what kind of type should toRemove is?

ちなみにこのコードは

def main():
         dictionary = {}
         dictionary["learning"] = "awesome"
         dictionary["coding"] = "fun"
         # ... Fill with more data
         remove_keys_containing_string(dictionary, "learn")
         print(dictionary)

def remove_keys_containing_string(dictionary, remove):
    toRemove = []
    for key in dictionary:
        for i in range(len(key)):
            if key[i:i+len(remove)] == remove:
                toRemove.append(key)
    if toRemove != []:  
        for key in toRemove:
            del dictionary[key]

if __name__ == '__main__':
    main()

と修正するのが個人的な正解かとおもいます。
もうちょっといい回答があればあとで教えていただけると勉強になります。はい。