Skip to content

Commit 5ecd2fb

Browse files
authored
Add files via upload
1 parent 1eac3df commit 5ecd2fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+53166
-0
lines changed

Harvard-CS50x/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# harvard-cs50
2+
Problem sets and projects for Harvard CS50: Introduction to Computer Science
3+
4+
To complete the following psets, I studied all of the CS50 lectures, walkthroughs, sections, and shorts on edX.
5+
6+
<h3>psets:</h3>
7+
8+
<ul>
9+
<li><a href="http://docs.cs50.net/2016/fall/psets/1/pset1.html">Problem Set 1: C</a></li>
10+
<li><a href="http://docs.cs50.net/2016/fall/psets/2/pset2.html">Problem Set 2: Crypto</a></li>
11+
<li><a href="http://docs.cs50.net/2016/fall/psets/3/pset3.html">Problem Set 3: Game of Fifteen</a></li>
12+
<li><a href="http://docs.cs50.net/2016/fall/psets/4/pset4.html">Problem Set 4: Forensics</a></li>
13+
<li><a href="http://docs.cs50.net/2016/fall/psets/5/pset5.html">Problem Set 5: Mispellings</a></li>
14+
<li><a href="http://docs.cs50.net/2016/fall/psets/6/pset6.html">Problem Set 6: Web Server</a></li>
15+
<li><a href="http://docs.cs50.net/2016/fall/psets/7/pset7.html">Problem Set 7: C$50 Finance</a></li>
16+
<li><a href="http://docs.cs50.net/2016/fall/psets/8/pset8.html">Problem Set 8: Mashup</a></li>
17+
</ul>
18+
19+
20+
<h3><a href="http://docs.cs50.net/2016/fall/syllabus/cs50.html">syllabus:</a></h3>
21+
<p>#####Week 0
22+
Binary. ASCII. Algorithms. Pseudocode. Source code. Compiler. Object code. Scratch. Statements. Boolean expressions. Conditions. Loops. Variables. Functions. Arrays. Threads. Events.</p>
23+
<p>#####Week 1
24+
Linux. C. Compiling. Libraries. Types. Standard output.</p>
25+
<p>#####Week 2
26+
Casting. Imprecision. Switches. Scope. Strings. Arrays. Cryptography.</p>
27+
<p>#####Week 3
28+
Command-line arguments. Searching. Sorting. Bubble sort. Selection sort. Insertion sort. O. Ω .Θ. Recursion. Merge Sort.</p>
29+
<p>#####Week 4
30+
Stack. Debugging. File I/O. Hexadecimal. Strings. Pointers. Dynamic memory allocation.</p>
31+
<p>#####Week 5
32+
Heap. Buffer overflow. Linked lists. Hash tables. Tries. Trees. Stacks. Queues.</p>
33+
<p>#####Week 6
34+
TCP/IP. HTTP.</p>
35+
<p>#####Week 7
36+
HTML. CSS. PHP.</p>
37+
<p>#####Week 8
38+
MVC. SQL.</p>
39+
<p>#####Week 9
40+
JavaScript. Ajax.</p>
41+
<p>#####Week 10
42+
Security. Artificial intelligence.</p>
43+
<p>#####Week 11
44+
Artificial intelligence, continued.</p>
45+
<p>#####Week 12
46+
Exciting conclusion.</p>

Harvard-CS50x/final-project/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
![Finale Project](http://i.imgur.com/xJDidjR.png)
3+
menu
4+
![Finale Project](http://i.imgur.com/S3MePpa.png)
5+
guess the country
6+
![Finale Project](http://i.imgur.com/SRQiUW3.png)
7+
random questions
8+
![Finale Project](http://i.imgur.com/j9BcGL6.png)
9+
![Finale Project](http://i.imgur.com/8gU9uDh.png)
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
from cs50 import SQL
2+
from flask import Flask, flash, redirect, render_template, request, session, url_for
3+
from flask_session import Session
4+
from passlib.apps import custom_app_context as pwd_context
5+
from tempfile import mkdtemp
6+
from passlib.context import CryptContext
7+
from helpers import *
8+
import helpers
9+
10+
# configure application
11+
app = Flask(__name__)
12+
13+
# ensure responses aren't cached
14+
if app.config["DEBUG"]:
15+
@app.after_request
16+
def after_request(response):
17+
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
18+
response.headers["Expires"] = 0
19+
response.headers["Pragma"] = "no-cache"
20+
return response
21+
22+
# custom filter
23+
app.jinja_env.filters["usd"] = usd
24+
25+
# configure session to use filesystem (instead of signed cookies)
26+
app.config["SESSION_FILE_DIR"] = mkdtemp()
27+
app.config["SESSION_PERMANENT"] = False
28+
app.config["SESSION_TYPE"] = "filesystem"
29+
Session(app)
30+
31+
# configure CS50 Library to use SQLite database
32+
db = SQL("sqlite:///finance.db")
33+
34+
@app.route("/")
35+
@login_required
36+
def index():
37+
38+
return render_template("index.html")
39+
40+
@app.route("/regs")
41+
def regs():
42+
rows = db.execute("SELECT * from users")
43+
return render_template("registrants.html", registrants=rows)
44+
45+
46+
47+
@app.route("/unregister", methods=["GET", "POST"])
48+
def unregister():
49+
if request.method == "GET":
50+
rows = db.execute("SELECT * from users")
51+
return render_template("unregister.html", registrants=rows)
52+
elif request.method == "POST":
53+
if request.form["id"]:
54+
id=request.form.get("id")
55+
db.execute("DELETE FROM users WHERE id=:id;", id=id)
56+
return redirect(url_for("regs"))
57+
58+
59+
@app.route("/play")
60+
@login_required
61+
def play():
62+
"""Show history of transactions."""
63+
return render_template("play.html")
64+
65+
@app.route("/leaderboard")
66+
@login_required
67+
def play2():
68+
"""Show history of transactions."""
69+
return render_template("play2.html")
70+
71+
72+
@app.route("/login", methods=["GET", "POST"])
73+
def login():
74+
"""Log user in."""
75+
76+
# forget any user_id
77+
session.clear()
78+
79+
# if user reached route via POST (as by submitting a form via POST)
80+
if request.method == "POST":
81+
82+
# ensure username was submitted
83+
if not request.form.get("username"):
84+
return apology("must provide username")
85+
86+
# ensure password was submitted
87+
elif not request.form.get("password"):
88+
return apology("must provide password")
89+
90+
# query database for username
91+
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
92+
93+
# ensure username exists and password is correct
94+
if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]):
95+
return apology("invalid username and/or password")
96+
97+
# query database for username
98+
db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
99+
# remember which user has logged in
100+
session["user_id"] = rows[0]["id"]
101+
102+
flash('Welcome back!')
103+
# redirect user to home page
104+
return redirect(url_for("index"))
105+
106+
# else if user reached route via GET (as by clicking a link or via redirect)
107+
else:
108+
return render_template("login.html")
109+
110+
@app.route("/logout")
111+
def logout():
112+
"""Log user out."""
113+
114+
# forget any user_id
115+
session.clear()
116+
117+
# redirect user to login form
118+
return redirect(url_for("login"))
119+
120+
121+
@app.route("/register", methods=["GET", "POST"])
122+
def register():
123+
# forget any user_id
124+
session.clear()
125+
# if user reached route via POST (as by submitting a form via POST)
126+
if request.method == "POST":
127+
# ensure username was submitted
128+
if not request.form.get("username"):
129+
return apology("must provide username")
130+
# ensure password was submitted
131+
elif not request.form.get("password"):
132+
return apology("must provide password")
133+
# ensure password was submitted
134+
elif not request.form.get("password2"):
135+
return apology("must provide password")
136+
# ensure password was submitted
137+
if request.form.get("password") != request.form.get("password2"):
138+
return apology("passwords should be the same!")
139+
# query database for username
140+
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
141+
# ensure username exists and password is correct
142+
if len(rows) == 1:
143+
return apology("username exists")
144+
# ensure doesn't exist
145+
if len(rows) != 1:
146+
#encrypt pass and save them to db
147+
encrypted=pwd_context.encrypt(request.form.get("password"))
148+
db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",username=request.form.get("username"),hash=encrypted)
149+
# query database for username
150+
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
151+
# remember which user has logged in
152+
session["user_id"] = rows[0]["id"]
153+
# redirect user to home page
154+
return redirect(url_for("index"))
155+
# else if user reached route via GET (as by clicking a link or via redirect)
156+
else:
157+
return render_template("register.html")
158+
159+
@app.route("/change", methods=["GET", "POST"])
160+
@login_required
161+
def change():
162+
#return apology("TODO")
163+
id = session["user_id"]
164+
# if user reached route via POST (as by submitting a form via POST)
165+
if request.method == "POST":
166+
167+
# ensure password was submitted
168+
if not request.form.get("password"):
169+
return apology("must provide the old password")
170+
171+
# query database for username
172+
rows = db.execute("SELECT * FROM users WHERE id = :id", id=id)
173+
174+
# ensure username exists and password is correct
175+
if len(rows) != 1 or not pwd_context.verify(request.form.get('password'), rows[0]['hash']):
176+
return apology("old password invalid")
177+
178+
# ensure password2 was submitted
179+
elif not request.form.get("password2"):
180+
return apology("must provide the new password")
181+
182+
encrypted2=pwd_context.encrypt(request.form.get("password2"))
183+
184+
185+
db.execute("UPDATE users SET hash=:hashes WHERE id=:id",id=id, hashes=encrypted2)
186+
187+
return redirect(url_for('logout'))
188+
189+
# else if user reached route via GET (as by clicking a link or via redirect)
190+
else:
191+
return render_template("change.html")
192+
193+
194+
195+
@app.route("/uchange", methods=["GET", "POST"])
196+
@login_required
197+
def uchange():
198+
#return apology("TODO")
199+
id = session["user_id"]
200+
201+
if request.method == "POST":
202+
203+
# ensure password was submitted
204+
if not request.form.get("name"):
205+
return apology("must provide the old name")
206+
207+
# query database for username
208+
rows = db.execute("SELECT * FROM users WHERE id = :id", id=id)
209+
210+
# ensure username exists and password is correct
211+
if len(rows) != 1 or not request.form.get('name') == rows[0]['username']:
212+
return apology("old username invalid")
213+
214+
# ensure password2 was submitted
215+
elif not request.form.get("name2"):
216+
return apology("must provide the new username")
217+
218+
# query database for username
219+
rowss = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("name2"))
220+
# ensure username exists and password is correct
221+
if len(rowss) == 1:
222+
return apology("username already taken")
223+
224+
else:
225+
db.execute("UPDATE users SET username=:username WHERE id=:id",id=id, username=request.form.get('name2'))
226+
227+
return redirect(url_for('logout'))
228+
229+
# else if user reached route via GET (as by clicking a link or via redirect)
230+
else:
231+
return render_template("uchange.html")
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import csv
2+
import urllib.request
3+
4+
from flask import redirect, render_template, request, session, url_for
5+
from functools import wraps
6+
7+
def apology(top="", bottom=""):
8+
"""Renders message as an apology to user."""
9+
def escape(s):
10+
"""
11+
Escape special characters.
12+
13+
https://github.com/jacebrowning/memegen#special-characters
14+
"""
15+
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"),
16+
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]:
17+
s = s.replace(old, new)
18+
return s
19+
return render_template("apology.html", top=escape(top), bottom=escape(bottom))
20+
21+
def login_required(f):
22+
"""
23+
Decorate routes to require login.
24+
25+
http://flask.pocoo.org/docs/0.11/patterns/viewdecorators/
26+
"""
27+
@wraps(f)
28+
def decorated_function(*args, **kwargs):
29+
if session.get("user_id") is None:
30+
return redirect(url_for("login", next=request.url))
31+
return f(*args, **kwargs)
32+
return decorated_function
33+
34+
def lookup(symbol):
35+
"""Look up quote for symbol."""
36+
37+
# reject symbol if it starts with caret
38+
if symbol.startswith("^"):
39+
return None
40+
41+
# reject symbol if it contains comma
42+
if "," in symbol:
43+
return None
44+
45+
# query Yahoo for quote
46+
# http://stackoverflow.com/a/21351911
47+
try:
48+
url = "http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={}".format(symbol)
49+
webpage = urllib.request.urlopen(url)
50+
datareader = csv.reader(webpage.read().decode("utf-8").splitlines())
51+
row = next(datareader)
52+
except:
53+
return None
54+
55+
# ensure stock exists
56+
try:
57+
price = float(row[2])
58+
except:
59+
return None
60+
61+
# return stock's name (as a str), price (as a float), and (uppercased) symbol (as a str)
62+
return {
63+
"name": row[1],
64+
"price": price,
65+
"symbol": row[0].upper()
66+
}
67+
68+
def usd(value):
69+
"""Formats value as USD."""
70+
return "${:,.2f}".format(value)
71+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)