Skip to content

Instantly share code, notes, and snippets.

@letroot
letroot / Square_root_recursive.py
Created June 13, 2015 10:01
I wrote a recursive function to compute the square root of a number using the Newton-Raphson method. I am using an inner function, is that a bad thing? How can I use just one function definition?
def r_sqrt(a):
a = float(a)
if a == 0.0:
return 0.0
x = a
def sqrt(x):
epsilon = 0.000000000000001
y = (x + a/x) / 2
@letroot
letroot / learntris.py
Last active August 29, 2015 14:24
author:daniel_adeyemo #lpmc
# this is learntris.py
import sys
def print_row():
row = '. ' * 10
return row.strip() + '\n'
def print_grid():
row_list = []
for i in range(22):
class Course:
def __init__(self, name, units, status, score):
# global get_gpa
self.name = name
self.units = units
self.status = status
self.score = score
self.gpa = self.get_gpa()
self.wgpa = self.gpa * self.units
@letroot
letroot / Course_
Last active August 29, 2015 14:24
class Course:
def __init__(self, name, units, status, score):
self.name = name # name of the course
self.units = units # number of units the course weighs
self.status = status # can assume C, R or E
self.score = score # the score achieved in the course
@property
def wgpa(self):
def multiply(x, y):
if y == 0:
return 0
z = multiply(x, y/2)
if y%2 == 0:
return 2*z
else:
return x + 2*z
>>> import sys
>>> def stuff():
... print("calling stuff!")
...
>>> def printer(frame, event, arg):
... print(frame, event, arg)
... return printer # return itself to keep tracing
...
>>> sys.settrace(printer) # register the tracing function
>>> stuff()
SOURCE_FILE = "data.txt"
DEST_FILE = "out.txt"
def stringify(s):
return('\"{}\"'.format(s.strip()))
def stringify_ls(string_ls):
return ','.join(list(map(stringify, string_ls.split(','))))
def stringify_file(source_file, dest_file):
@letroot
letroot / html2pdf.py
Created February 20, 2018 13:06
Batch download composingprogram.com pages as pdf
import pdfkit
import requests
from bs4 import BeautifulSoup
html = requests.get("http://www.composingprograms.com/")
soup = BeautifulSoup(html.text, 'html.parser')
res = []
for link in soup.find_all('a'):
linkstr = link.get('href')
import requests
from bs4 import BeautifulSoup
ROOT_URL = "https://talendexpert.com/page/{pg_num}"
# for page in range(2,193):
# html_doc = requests.get(ROOT_URL.format(pg_num=page)).text
# soup = BeautifulSoup(html_doc, 'html.parser')
# for link in soup.findAll("h1", attrs={'class':"product_title"}):
# print (link.a.name)
for i in range(1, 101):
pass
# this one goes from 1 to 100
for i in range(1, 100, -1):
print (i)