惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
L
LINUX DO - 最新话题
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
WordPress大学
WordPress大学
L
LINUX DO - 热门话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Visual Studio Blog
O
OpenAI News
AI
AI
Hacker News: Ask HN
Hacker News: Ask HN
V2EX - 技术
V2EX - 技术
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
Spread Privacy
Spread Privacy
Y
Y Combinator Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
T
The Blog of Author Tim Ferriss
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
N
News and Events Feed by Topic
Project Zero
Project Zero
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog
G
Google Developers Blog

Posts on Changkun's Blog

Why High-Output Systems Are Often the First to Stop Growing Dark Forest Theory: A Formal Derivation Agents (or Humans) in Goal-Directed and Goalless Environments: On Pipelines, Priors, and the Rhythm Between Exploration and Exploitation At the Boundary of Self-Reference: From Stable Structures in Artificial Intelligence to the Self as a Recursive Model in an Open Dissipative System 2023 Reading List 2022 Reading List 2021 Reading List Are PSS/USS and RSS Actually the Same Thing? Performance Differences from Page Faults vs. Prefetching 2020 Year-End Review Migration with Zero Downtime 2020 Reading List The All in Go Stack Pointers Might Not Be Ideal for Parameters Eliminating A Source of Measurement Errors in Benchmarks Setup Wordpress in 10 Minutes 我为什么不再写博客了? 2019 年终总结 2018-2019 读书清单 Ten years of blogging Rethinking the Reflections on Communications and Trusts 2018 年终总结 Go source code study is open source Go source study: unsafe Pattern Go source study: sync.Pool Go runtime programming A Million WebSocket and Go Designing Asynchronous RESTful APIs 分布式杂谈01:CAP 理论的误解 Issues of Human-Bot Interaction 压缩法与深度网络的泛化性 Go in 1 Hour UMSLT04: The Past and Present of SGD UMSLT03: A Gentle Start of Learning Theory UMSLT02: A Breif History of Neural Networks UMSLT01: A Breif History of Regularization 不笑不足以为道 论文笔记:Generalization in Deep Learning 2017 年终总结 2017 读书清单 深度学习的泛化理论简介 删除 GitHub 上已经提交的敏感信息 硕士生涯的第一年就这样告一段落了 人肉计算(10): 系统参与激励 人肉计算(9): 陷阱的解法 别聊,一聊你就暴露 人肉计算(8): 人肉计算与数据科学中的陷阱 人肉计算(7): 社会行为分析 Hexo + GitHub + Travis CI + VPS 自动部署 人肉计算(6): 预测市场 人肉计算(5): 信用风险评级模型 读书与回报 瞎扯: 对现代企业理论与当下IT企业的商业模式和信息产业链的规律性的思考 人肉计算(4): 输入数据聚合与PageRank 又一次打整了一下博客 人肉计算(3): 输入数据聚合与链路预测 人肉计算(2): 意图博弈 GWAPs 人肉计算(1): 众包与群众智慧 对后辈同学在计算机专业上的答疑与解惑 在德国的医疗及住院体验 这可能不是一个技术博客了 迅速更换了 DISQUS Electron 深度实践总结 良好的编码体验的三个方面 2016 年终总结 2016 读书清单 最近在着手写的文章 微信小程序文档极致总结 谈谈过去三个月在实验楼的实习经历 Built a Desktop Client for My Blog Guacamole 源码分析与 VNC 中 RFB 协议的坑 《高速上手 C++11/14》正式发布 Docker 极速入门教程02 - 镜像与容器管理 Docker 极速入门教程01 - 基本概念和操作 阶段性沉默 ELK+Redis 最佳实践 终于全面启用了 HTTPS 苹果开源了LZFSE无损压缩 Hash 碰撞的一种思路 记一次完整的 Kaldi-TIMIT 示例运行 Kaldi 上的 TIMIT 例子 Kaldi 安装与部署 从科研写作谈起 Swift API 设计指南 有趣的人类 所以其实论文并没有什么鬼用 Githug 通关记录及指南 小结一下这学期的收获 2015 读书清单 2015 年终总结 负能量爆表 转眼就快两个月了 博客迁移记录 大三总结 这个世界,终究不会是我们的。 Linux 内核分析 之六:Linux 内核创建进程的过程 小说「泽缘」 Linux 内核分析 之五:system_call中断处理过程的简要分析 大创项目的标题真是每年都在考验同学们的想象力啊 Linux 内核分析 之四:使用库函数API和嵌入汇编两种方式使用同一个系统调用
实验楼楼赛第3期-Python-题解
Changkun Ou · 2017-03-27 · via Posts on Changkun's Blog

Published at发布于:   |   PV/UV: /   |   Reading阅读: 3 min

因对 Python 感兴趣,于是早上吃早饭时看看。题目相对比较简单,最后一题代码写得比较随意。原本打算交上去测试一下,可惜海外党使用环境实在是卡得惨不忍睹,没办法写敲那么多代码上去。

题目:https://www.shiyanlou.com/contests/lou3/challenges

题目1: 存储学习记录

出现的问题:

  • sqlite url schema 错误
  • 外键设置错误
  • 外键重复设置

解决方法:

  • line 10: sqlite:// 改为 sqlite:///
  • line 22: backref='user' 改为 backref='course'
  • 表设计为一对多的外键,故注释掉 line 28 & line 30

题目2: 追踪课程更新

出现的问题:

  • 缺少 object 基类
  • 缺少子类初始化
  • @property 方法访问错误
  • typename 缺少参数
  • 浅拷贝错误

解决方法:

  • line 5: 改为 class Course(object)
  • line 21: 给 typename 传递参数 self
  • line 28: 替换为 super(BootcampCourse, self).__init__(name, True, lab_count)
  • line 37: 改为调用 self.lab_count
  • line 29: 改为调用 copy.deepcopy

题目3: 修复 Internal Server Error

出现的问题:

  • filter_by 用法错误
  • html 模板错误

解决方法:

  • app/main/views.py line 34: filter_by(id=id).first_or_404()
  • app/templates/todolist.html line 4: for('static', filename='js/todolist.js')
  • app/templates/todolist.html line 9: {{todolist.title|title}}

题目4: NoSQL 的艺术

潜在的问题:

  • 从 bash 中读入的值为字符串
  • mongodb 的 aggregate pipeline 操作
  • sort 结果搜索: mongodb 的 aggregate 操作找了半天实在没找到如何从操作结果的 cursor 拿到排名,只得做如下妥协处理,读者可继续探究
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# TODO: return the rank of user_id
user_id = int(user_id)    
results = contests.aggregate([
  {
    '$group': {
      '_id': '$user_id', 
      'sum_score': {'$sum': '$score'}, 
      'sum_time': {'$sum': '$submit_time'}
    }
  },
  {
    '$sort': {
      'sum_score': -1,
      'sum_time': -1
    }
  }
])
# CRISES: id_list may cause large memory consumption
id_list = [int(value['_id']) for value in results]
# INPROVEMENTS: could use binary search
try: return id_list.index(user_id)+1
except: return 0

题目5: 增加服务器管理功能

潜在的问题:

  • 异常处理方式: 异常处理的部分原题目似乎并未写指示清楚, 这里用直接抛出 ScriptError 异常做处理
  • csv 文件处理: 虽然其实并不是 csv 格式,但由于带空格的形式可以视为 csv 文件进行处理; 另一方面文件操作似乎并不支持单独一行的处理操作?
  • 重复添加操作判断: 重复添加的判断逻辑应为: 当 hostip 和 user 同时相同时,则不能添加,否则可添加。
  • 命令行参数处理: 最初决定使用 argparse 处理命令行参数,扫文档扫了半天没搞明白怎么添加 subparameter requirement,google 了一阵也很奇怪,最后还是决定直接暴力解包判断了
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import csv

class ScriptError(Exception):
  def __init__(self, value):
    self.value = value
  def __str__(self):
    return repr('ERROR:'+self.value)

def listinfo():
  with open('serverinfo', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    for row in reader:
      print ' '.join(row)

def addinfo(hostip, user, password):
  with open('serverinfo', 'r+') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    contents = []
    new = [hostip, user]
    for row in reader:
      contents.append([row[0], row[1]])
    if new not in contents:
      writer = csv.writer(csvfile, delimiter=' ')
      writer.writerow([hostip, user, password])
    else:
      raise ScriptError('user exists')

def deleteinfo(hostip):
  f = open('serverinfo', 'r')
  lines = f.readlines()
  f.close()
  f = open('serverinfo', 'w')
  for line in lines:
    if hostip not in line:
      f.write(line)
  f.close()

def main():
  if len(sys.argv) < 2:
    print """
    Usage:
    ======
    python server.py [add|delete|list]
    
    add: add info, required parameter below
         -h hostip
         -u user
         -p password
    delete: delete info, required parameter below
         -h hostip
    list:list all info
    """
  elif sys.argv[1] == 'add':
    if (sys.argv[2] == '-h' or sys.argv[2] == 'hostip')   and \
       (sys.argv[4] == '-u' or sys.argv[4] == 'user')     and \
       (sys.argv[6] == '-p' or sys.argv[6] == 'password') and \
       len(sys.argv) == 8:
      addinfo(sys.argv[3], sys.argv[5], sys.argv[7])
    else:
      raise ScriptError('argument error')
  elif sys.argv[1] == 'delete':
    if (sys.argv[2] == '-h' or sys.argv[2] == 'hostip') and len(sys.argv) == 4:
      deleteinfo(sys.argv[3])
    else:
      raise ScriptError('argument error')
  elif sys.argv[1] == 'list':
    listinfo()
  else: raise ScriptError('no such option')

if __name__ == '__main__':
  main()