计算机科学(哈佛cs50)笔记汇总
Posted mask哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算机科学(哈佛cs50)笔记汇总相关的知识,希望对你有一定的参考价值。
1 c语言
-
程序编译系统构成:预处理器、编译器、汇编器、连接器
代码执行的过程:源代码->编译器->汇编代码->汇编器->目标代码->连接器->可执行程序 -
程序的构成:输入+算法+程序设计+输出
-
ascill (https://www.asciichart.com/)
-
Unicode 指定标准语言
-
Cs50 编程手册:https://manual.cs50.io/
-
Pseudo code 伪代码
-
程序调试的方法:
1.控制台打日志printf
2.debugger
3.rubber duck-头脑风暴与同伴讨论代码 -
动态内存分配技术,自动扩大或缩小阵列
-
提倡使用快捷键、shell命令编程,提高开发效率
-
c语言非零即真
2 算法:
- running time: 大O
- divide and conquer strategy:分而治之算法
- Linear search :线性搜索
- Binary search 二分查找 ,数组是有序的,从中间查找目标数字,左边数字或右边数字与目标数字比对。直到找打目标数据。
- Selectiton sort:选择排序
- merge sort :合并排序
- bubble sort: 冒泡排序
- 算法涉及对数
3 内存
-
指针
-
结构体:struct
-
strcmp:字符串比较
-
Dynamic Memory:动态内存分配
malloc :内存分配
Free :释放内存
-
值类型交换,变量值不变;引用类型交换,变量值改变。
-
Scanf(“%i”,&x) :从键盘输入
-
c语言编译 :make xx.c 执行 ./xx
4 数据结构
- struct
- Linked list:链表
#include <stdio.h>
#include <stdlib.h>
/**
*link list实现
*/
typedef struct node
int number;
struct node *next;
node;
int main(void)
// list of size
node *list = NULL;
// add a nuber to list
node *n = malloc(sizeof(node));
if (n == NULL)
return 1;
n->number = 1;
n->next = NULL;
// update list to point to new node
list = n;
// add a number to list
n = malloc(sizeof(node));
if (n == NULL)
free(list);
return 1;
n->number = 2;
n->next = NULL;
list->next = n;
// add a nuber to list
n = malloc(sizeof(node));
if (n == NULL)
free(list->next);
free(list);
return 1;
n->number = 3;
n->next = NULL;
list->next->next = n;
// print numbers
// for(node *tmp =list;tmp!=NULL;tmp=tmp->next)
// printf(“%i\\n”,tmp->next);
//
// free list
while (list != NULL)
node *tmp = list->next;
free(list);
list = tmp;
return 0;
- Tree
- Binary search tree 二叉搜索树;根节点大于左子节点,小于右子节点
typedef struct node
int number;
struct node *left;
struct node *right;
node;
void free_tree(node *root);
void print_tree(node *root);
int main(void)
// tree of the size 0
node *tree = NULL;
// add number to list
node *n = malloc(sizeof(node));
if (n == NULL)
// free the memory
return 1;
n->number = 1;
n->left = NULL;
tree = n;
// add number to list
n = malloc(sizeof(node));
if (n == NULL)
return 1;
n->number = 1;
n->left = NULL;
n->right = NULL;
tree->left = n;
// print tree
print_tree(tree);
// free tree
free_tree(tree);
void print_tree(node *root)
if (root == NULL)
return;
print_tree(root->left);
printf(“%i\\n”, root->number);
print_tree(root->right);
free_tree(root);
/**
*** @brief
** 二分查找树*
*/
bool search(node *tree, int number)
if (tree == NULL)
return 1;
else if (number < tree->number)
return search(tree->left, number);
else if (number > tree->number)
return search(tree->right, number);
else if (number == tree->number)
return 0;
-
AVL或红黑树
-
Hash table:哈希表=数组+链表
hash冲撞->散列函数 -
Tries :恒定的时间查找,对于海量数据集也是;就是多层哈希表;由树和数组组成。
-
Graph
-
Queues 队列 先进先出(FIFO)
Enqueue/dequeue -
Stack 后进先出 LIFO
push/pop -
Abstract data structures
5 python
-
python中定义的main函数,必须在程序最后一行调用。
-
docs.python.org
-
google & stackoverflow 检索问题
-
Pip python包安装管理,类似java中maven/gradle
pip install xx
-
命令行携带参数
from sys import argv
#命令行携带参数
if len(argv) == 2:
print(f"hello,argv[1]")
else:
print(“hello,world”)
执行运行结果:
-
with关键字使用
-
python 数据分析
-
python语音处理
-
python 人脸识别
face_recoginition模块 -
微服务springcloud/springcloudalibaba sidecar 调用python api
-
python生成二维码(QR)
-
python 代码范例:
agree.py
s = input(" Do you agree? ")
print("s:"+s)
s.lower()
if s == "Y" or s == "y":
print("Agree")
elif s == "N" or s == "n":
print("No agreement")
argv.py
from sys import argv
# 命令行携带参数
# if len(argv) == 2:
# print(f"hello,argv[1]")
# else:
# print("hello,world")
for arg in argv[2:]:
print(arg)
blur.py
from PIL import Image, ImageFilter
# 图片滤镜
before = Image.open("vscode.jpg")
# after = before.filter(ImageFilter.CONTOUR)
after = before.filter(ImageFilter.BoxBlur(10))
after.save("cp2vscode.jpg")
caculator.py
try:
x = int(input("x: "))
y = int(input("y: "))
print(x/y)
except ValueError:
print("that is not an int")
exit()
crackPwd.py
from string import ascii_letters, digits, punctuation
from itertools import product
# 暴力破解
for passcode in product(ascii_letters + digits + punctuation, repeat=8):
print(passcode)
dic.py
# 字典 hashtable set 去重
words = set()
def check(word):
if word.lower() in words:
return True
else:
return False
def load(dictionary):
file = open(dictionary, "r")
for line in file:
word = line.rstrip() # /n分割单词
words.add(word)
file.close()
return True
def size():
return len(words)
def unload(): # 释放内存
return True
edges.py
from PIL import Image, ImageFilter
# 滤镜处理
before = Image.open("vscode.jpg")
after = before.filter(ImageFilter.FIND_EDGES)
after.save("cp3vscode.jpg")
mario.py
print("?"*4)
for i in range(4):
print("?"+str(i), end="$") # 使用$进行分割字符串
meow.py
def main():
for i in range(3):
meow(8)
def meow(n):
print("meow"+str(n))
main() # python中必须调用定义的入口函数main
phonebook.py
# 数据结构hashtable&线性查找
people =
"mark": "110",
"steve": "112"
name = input("name: ")
if name in people:
num = people[name]
print(f"Number:num")
qrcode.py
import os
import qrcode
#二维码生成
img = qrcode.make(
"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png")
img.save("qr.png", "PNG")
os.system("open qr.png") # 调用系统命令open 打开二维码
scores.py
scores = [33, 55, 75]
# 求和
avg = sum(scores) / len(scores)
# print("avg"+str(avg))
print(f"avg:avg")
scores2.py
scores = []
for i in range(3):
score = int(input("score: "))
# scores.append(score)
scores += [score]
avg = sum(scores) / len(scores)
print(f"avg:avg")
6 SQL
- 事务保证数据的准确性(锁最小操作的原子sql语句)
7 html/css/javascript
- router
- tcp/ip
- domain
- DNS 域名解析
- http(GET/POST)
- curl -I -X GET http://baidu.com
- http常见状态码
8 Flask(python web开发框架)
-
Httpserver
-
安装flask: pip install flask
-
flask框架
from flask import Flask
-
用户注册功能代码:
-
flask项目启动: flask run
-
web开发必须有个应用框架模型,连接各种技术与业务在一起
-
http+json 接口格式数据
9 Emoji
-
代码质量评估=正确性+设计+代码风格
-
git
-
命令行工具:xcode
-
vscode
-
Host a web side (网站托管)
https://pages.github.com/
https://www.netlify.com/ -
Host a web app (web应用托管)
www.heroku.com/platform
-
推荐阅读网站博客
-
Balanced Binary search Tree平衡二叉树
-
Unicode->emoji(表情符号)
10 安全
- Brute force attack :暴力攻击
- Two-factor authentication :双子认证
- Information technology of trade offs :信息技术权衡
- End-to-end encryption: 端到端加密
- rotational algorithm:旋转算法或凯撒密码
总结:哈弗cs50 计算机科学课程相对于国内的传统授课方式,不局限于ppt内容,讲师的内容特别生动,并且对编程细节的许多内容讲解的透彻,涵盖编程的内容与实际的编程相关的趋近一线实际。
以上是关于计算机科学(哈佛cs50)笔记汇总的主要内容,如果未能解决你的问题,请参考以下文章