From ab0c5b573522da9c2a2831ea5298d87188b65baa Mon Sep 17 00:00:00 2001 From: feilong Date: Mon, 22 Nov 2021 00:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B20=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../4.\347\261\273/key_value_set.json" | 20 +- .../4.\347\261\273/key_value_set.md" | 224 ++++++++++++++++++ .../4.\347\261\273/key_value_set.py" | 53 +++-- 3 files changed, 261 insertions(+), 36 deletions(-) create mode 100644 "data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.md" diff --git "a/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.json" "b/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.json" index 7d47738..b88e79c 100644 --- "a/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.json" +++ "b/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.json" @@ -1,23 +1,5 @@ { - "one_line": { - "self.dict.get(key)": [ - "self.dict[key]" - ], - "is not None": [ - "is None" - ], - "len(self.dict.keys())": [ - "self.dict.keys().length()" - ], - "value = kv.get(key)": [ - "value = kv[key]" - ], - "if ret == value:": [ - "if ret = value:", - "if ret != value:" - ] - }, - "source": "key_value_set.py", + "source": "key_value_set.md", "depends": [], "exercise_id": 167, "type": "code_options" diff --git "a/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.md" "b/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.md" new file mode 100644 index 0000000..559a4ed --- /dev/null +++ "b/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.md" @@ -0,0 +1,224 @@ +# Python 类的基本使用(1) + +下面的类封装了一组 `set`/`get`/`keys` 接口 + +```python +class KeyValueSet: + def __init__(self) -> None: + self.dict = {} + + def set(self, key, value): + self.dict[key] = value + + def get(self, key): + return self.dict.get(key) + + def keys(self): + return self.dict.keys() +``` + +请你使用类 `KeyValueSet` 完成一个交互式命令行`你想我猜`游戏。支持: + +* 装载N个句子对 +* `你猜我想` 闯关,输出上半句,等待用户猜测下半句 +* 如果猜中就累加1分,否则扣1分 +* 全部结束输出用户本次得分 + +我们编写一个新的class,内部通过组合`KeyValueSet`来支持上述功能,程序框架如下: + +```python +class GuessSentenceGame: + def __init__(self): + self.kv = KeyValueSet() + self.score = 0 + + def setup(self, sentences): + # TODO(You): 请在此编写装载逻辑 + + def guess(self, first_word): + # TODO(You): 请在此编写猜测结果,返回 err, value + + def run(self): + self.score = 0 + for first_word in self.kv.keys(): + ret = input("猜一猜下半句是什么? {} -> :".format(first_word)) + err, value = self.guess(first_word) + if err==0: + print('你太厉害了,这都能猜得到!+10分!') + self.score += 10 + else: + self.score -= 2 + print('哈哈,肯定猜不到得啦:{}->{},扣除2分!'.format(first_word, value)) + print('游戏结束,你本次游戏得分:', self.score) + + +if __name__ == '__main__': + + sentences = [ + "hello world", + 'monkey king', + 'tomorrow is another day', + "good bye" + ] + + game = GuessSentenceGame() + game.setup(sentences) + game.run() +``` + +一个示例输出是: + +```bash +猜一猜下半句是什么? hello -> :world +你太厉害了,这都能猜得到!+10分! +猜一猜下半句是什么? monkey -> :king +你太厉害了,这都能猜得到!+10分! +猜一猜下半句是什么? tomorrow -> :is another day +你太厉害了,这都能猜得到!+10分! +猜一猜下半句是什么? good -> :a +哈哈,肯定猜不到得啦:good->bye,扣除2分! +游戏结束,你本次游戏得分: 28 +``` + +请选出下面的实现代码中,错误 的选项。 + +## template + +```python +class KeyValueSet: + def __init__(self) -> None: + self.dict = {} + + def set(self, key, value): + self.dict[key] = value + + def get(self, key): + return self.dict.get(key) + + def exist(self, key): + return self.dict.get(key) is not None + + def keys(self): + return self.dict.keys() + + +class GuessSentenceGame: + def __init__(self): + self.kv = KeyValueSet() + self.score = 0 + + def setup(self, sentences): + for sentence in sentences: + self.append(sentence) + + def append(self, sentence): + cut_pos = sentence.find(' ') + first_word, rest = sentence[0:cut_pos], sentence[cut_pos+1:].strip() + self.kv.set(first_word, rest) + + def run(self): + self.score = 0 + for first_word in self.kv.keys(): + ret = input("猜一猜下半句是什么? {} -> :".format(first_word)) + value = self.kv.get(first_word) + if ret == value: + print('你太厉害了,这都能猜得到!+10分!') + self.score += 10 + else: + self.score -= 2 + print('哈哈,肯定猜不到得啦:{}->{},扣除2分!'.format(first_word, value)) + print('游戏结束,你本次游戏得分:', self.score) + + +if __name__ == '__main__': + + sentences = [ + "hello world", + 'monkey king', + 'tomorrow is another day', + "good bye!" + ] + + game = GuessSentenceGame() + game.setup(sentences) + game.run() +``` + +## 答案 + +```python +class GuessSentenceGame: + ... + + def setup(sentences): + for sentence in sentences: + cut_pos = sentence.find(' ') + self.kv.set(sentence[0:cut_pos], sentence[cut_pos+1:].strip()) + + def guess(first_word): + self.kv.get(first_word) + return 0, value if value else 1, None +``` + +## 选项 + +### A + +```python +class GuessSentenceGame: + ... + + def setup(self, sentences): + for sentence in sentences: + first_word, rest = self.parse_sentence(sentence) + self.kv.set(first_word, rest) + + def parse_sentence(self, sentence): + cut_pos = sentence.find(' ') + return sentence[0:cut_pos], sentence[cut_pos+1:].strip() + + def guess(self, first_word): + value = self.kv.get(first_word) + return 0, value if value else 1, None +``` + +### B + +```python +class GuessSentenceGame: + ... + + def setup(self, sentences): + for sentence in sentences: + cut_pos = sentence.find(' ') + first_word, rest = sentence[0:cut_pos], sentence[cut_pos+1:].strip() + self.kv.set(first_word, rest) + + def guess(self, first_word): + value = self.kv.get(first_word) + err = 0 if value else 1 + return err, value +``` + +### C + +```python +class GuessSentenceGame: + ... + + def setup(self, sentences): + for sentence in sentences: + self.append(sentence) + + def append(self, sentence): + cut_pos = sentence.find(' ') + first_word, rest = sentence[0:cut_pos], sentence[cut_pos+1:].strip() + self.kv.set(first_word, rest) + + def guess(self, first_word): + ret = self.kv.get(first_word) + if ret is None: + return 1, None + else: + return 0, ret +``` diff --git "a/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.py" "b/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.py" index 52d1e84..b1b9c13 100644 --- "a/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.py" +++ "b/data/1.python\345\210\235\351\230\266/2.\345\237\272\347\241\200\350\257\255\346\263\225/4.\347\261\273/key_value_set.py" @@ -19,25 +19,44 @@ class KeyValueSet: def keys(self): return self.dict.keys() - def count(self): - return len(self.dict.keys()) +class GuessSentenceGame: + def __init__(self): + self.kv = KeyValueSet() + self.score = 0 + + def setup(self, sentences): + for sentence in sentences: + self.append(sentence) + + def append(self, sentence): + cut_pos = sentence.find(' ') + first_word, rest = sentence[0:cut_pos], sentence[cut_pos+1:].strip() + self.kv.set(first_word, rest) + + def run(self): + self.score = 0 + for first_word in self.kv.keys(): + ret = input("猜一猜下半句是什么? {} -> :".format(first_word)) + value = self.kv.get(first_word) + if ret == value: + print('你太厉害了,这都能猜得到!+10分!') + self.score += 10 + else: + self.score -= 2 + print('哈哈,肯定猜不到得啦:{}->{},扣除2分!'.format(first_word, value)) + print('游戏结束,你本次游戏得分:', self.score) -def test(): - kv = KeyValueSet() - kv.set('hello', 'world!') - kv.set('monkey', 'king!') - kv.set('tomorrow', 'is another day') - kv.set('good', 'bye!') - for key in kv.keys(): - ret = input("猜一猜下半句是什么? {} -> :".format(key)) - value = kv.get(key) - if ret == value: - print('你太厉害了,这都能猜得到!') - else: - print('哈哈,肯定猜不到得啦:{}->{}'.format(key, value)) +if __name__ == '__main__': + sentences = [ + "hello world", + 'monkey king', + 'tomorrow is another day', + "good bye!" + ] -if __name__ == '__main__': - test() + game = GuessSentenceGame() + game.setup(sentences) + game.run() -- GitLab