Skip to content

Instantly share code, notes, and snippets.

View jesuscast's full-sized avatar

Andres Castaneda jesuscast

View GitHub Profile
@jesuscast
jesuscast / rmAll.py
Last active December 20, 2015 00:19
Removes all of the deleted files in git if there is any modified neither new files on stage
import subprocess
import re
def rmAll():
output = subprocess.check_output("git status", shell=True)
matches = re.findall('deleted: .*', output)
for n in matches:
clean = n.replace("deleted: ","")
subprocess.call("git rm "+clean, shell=True)
@jesuscast
jesuscast / gist:7961783
Last active December 31, 2015 08:39
Simple algorithm that calculates an approximation of pi. It is based on the idea that the area of a polygon with many sides circumscribed inside a unit circle approaches pi. In the example I test the algorithm with a polygon of 500 sides and the result of executing the code in my machine is 3.141592653589795Important Notes: The maximum length it…
import math
def calcSide(a):
side = math.sqrt(2)
H = math.sqrt(1/2)
for x in range(1,a):
b = side/2
x = 1 - H
side = math.sqrt(b**2+x**2)
H = math.sqrt(1-(side/2)**2)
r = {"side":side,"H":H}
@jesuscast
jesuscast / gist:7966634
Created December 14, 2013 23:47
AP Psy homework doer. Use along a OCR like "http://www.onlineocr.net/" Ex. do("your questions")
import requests
import json
import wikipedia
def retrieveQuestions(dataStr):
dataStr = dataStr.lower()
questions = []
another_question = False
w_s = ["what","why","when","how","have","can","could"]
w_c = ""
pos_i = 0
@jesuscast
jesuscast / simpsons_rule.py
Last active August 29, 2015 13:56
Calculates simpson's sum, receives the interval and the the number of intervals. An extra y function needs to be created separetely. Uses numpy. This function takes the results of the y function
from numpy import *
def simpsonSum(xRange,n):
tot = 0
width = 0
width = (xRange[1]-xRange[0])/n
x = [ interval for interval in arange(xRange[0],xRange[1]+width, width) ]
yRange = [ y(n) for n in x ]
tot += yRange[0]+yRange[n]
tot += sum([ 2*yRange[i] if (i%2==0) else 4*yRange[i] for i in range(1,n) ])
tot = tot*width/3
@jesuscast
jesuscast / trapezoidalSum.py
Last active August 29, 2015 13:56
Calculate the trapezoidal sum of a function over an interval. An external y function needs to be created. Uses numpy
def trapezoidalSum(xRange,n):
tot = 0
width = 0
width = (xRange[1]-xRange[0])/n
x = [ interval for interval in arange(xRange[0],xRange[1]+width, width) ]
yRange = [ y(n) for n in x ]
tot += yRange[0]+yRange[n]
tot += sum([ 2*yRange[i] for i in range(1,n) ])
tot = tot*width/2
return tot
@jesuscast
jesuscast / Pattern_Counter.py
Last active August 29, 2015 14:02
Counts the number of times a pattern appears on a list
def count_pattern(pattern, lst):
lenP = len(pattern)
#print("lenP",lenP)
lenL = len(lst)
#print("lenL",lenL)
total = 0
if(lenL >= lenP):
for i in range(0, lenL):
#print("repetition #:"+str(i))
match = False
@jesuscast
jesuscast / expression_depth.py
Last active August 29, 2015 14:02
Finds the depth of a mathematical expression. It follows the syntax expr = ('operator','term1','term2',...,'termN')
def depth(expr):
depthLevel = 0
if(isinstance(expr,(list,tuple))==False):
return 0
else:
depthLevel += 1
if( isinstance(expr[1],(list,tuple)) ):
biggestDepth = 0
for i in range(1,len(expr)):
depthOfCurrentTerm = depth(expr[i])
@jesuscast
jesuscast / tree_branches.py
Last active August 29, 2015 14:02
Tree branches. Defines a tree as a tuple and then access the branches with 0 based index, the nesting is defined as another tuple.
import math
def tree_ref(tree, index):
if(isinstance(index,(list,tuple)) and (len(index)>1)):
#print tree
#print index
#print "--1---"
newTree = tree[index[0]]
#print newTree
newIndex = tuple([index[k] for k in range(1,len(index))])
#print newIndex
@jesuscast
jesuscast / polynomialdivision.php
Last active August 29, 2015 14:02
Calculates de division of two polynomials. ENTRY an array where the index i equals the term with degree i. EX array(1,4,3) = 3x^2+4x+1
<?php
function degree($polynomial){
for($i = (count($polynomial)-1); $i>=0; $i--){
if($polynomial[$i]!=0){
return $i;
}
}
return 0;
}
function formatPolynomial($poly){
@jesuscast
jesuscast / highestCommonFactor.php
Last active August 29, 2015 14:02
Calculates the highest common factor in an array
<?php
function highestCommonFactor($array){
$hcf = 1;
$totalItems = count($array);
$smaller = $array[$totalItems-1];
for($i = 0; $i<$totalItems; $i++){
$temp = $array[$i];
if($array[$i]<0) {
$temp = -1*$temp;