闯关任务 Leetcode 383(笔记中提交代码与leetcode提交通过截图)
力扣383题:赎金信
https://leetcode.cn/problems/ransom-note/
我的解法:
使用Counter
分别计算字母出现次数,再依次比较每个字母的次数
from collections import Counter
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
ran = Counter(ransomNote)
mag = Counter(magazine)
for char, count in ran.items():
if not char in mag:
return False
if mag[char] < count:
return False
return True
通过截图:
闯关任务 Vscode连接InternStudio debug笔记
在我自己的Windows电脑上安装了VS Code并安装了Remote-SSH
插件,然后连接到了开发机。
创建了一个debug配置文件:
然后安装openai
包:
pip install openai
创建一个 try_debug.py
文件,存放本任务的原始代码。直接运行此文件的话,会报一个JSONDecodeError
错误:
在
res_json = json.loads(res)
这行前面添加一个断点,以debug模式再次运行,可在左侧面板看到res
的值:
res = '```json\n{\n "模型名字": "书生浦语InternLM2.5",\n "开发机构": "上海人工智能实验室",\n "提供参数版本": ["1.8B", "7B", "20B"],\n "上下文长度": "1M"\n}\n```'
可以看到,报错是因为字符串含有markdown格式中用来标示json格式的字符(```json),所以我们需要将其去掉再传入 json.loads()。
因此在代码中添加一段if
语句:
res = internlm_gen(prompt,client)
if "```json" in res:
res = res.replace("```json", "").replace("```", "").strip()
res_json = json.loads(res)
print(res_json)
再次运行,可以看到正确的结果:
解析出的json格式(以python字典表示)为:
{
'model_name': '书生浦语InternLM2.5',
'development_organization': '上海人工智能实验室',
'parameter_versions': ['1.8B', '7B', '20B'],
'context_length': '1M'
}
任务完成!