Skip to content

Instantly share code, notes, and snippets.

View basarat's full-sized avatar
🌹
youtube.com/basaratali

Basarat Ali Syed basarat

🌹
youtube.com/basaratali
View GitHub Profile
@basarat
basarat / gist:1086093
Created July 16, 2011 07:15
A sample of combinations in python
seq = [1,2,3]
n=len(seq)
for i in range(n):
for j in range(n):
print "%s * %s = %s"%(seq[i],seq[j],seq[i]*seq[j])
seq = [1,2,3]
n=len(seq)
for i in range(n):
for j in range(i,n):
print "%s * %s = %s"%(seq[i],seq[j],seq[i]*seq[j])
seq = [1,2,3]
n=len(seq)
for i in range(n):
for j in range(i+1,n):
print "%s * %s = %s"%(seq[i],seq[j],seq[i]*seq[j])
seq = [1,2,3]
n=len(seq)
for i in range(n-1):
for j in range(i+1,n):
print "%s * %s = %s"%(seq[i],seq[j],seq[i]*seq[j])
#the wordlist file is the SCOWL wordlist downloaded from : http://wordlist.sourceforge.net/
file = open("./SCOWL/wordlist.txt")
words =[line[:-1] for line in file.readlines()] #remove the newline character.
file.close()
#basic anagram comparion algorithm
def AreSameCharacters(word1,word2):
word1 = list(word1)
def agent(env, alpha, epsilon, initialQ, gamma, numberEp, alg): #the agent function
n_s = env.numStates
n_a = env.numActions
Q = initialQ * np.ones([n_s, n_a]) #initialize Q-table
epLength = [] #stores the episode length for plotting purposes
eps = 0
while eps < numberEp: #run numberEp episodes
s = env.initState
@basarat
basarat / insertionsort.py
Created July 31, 2012 13:01
Insertion sort in python
def insertionsort(A):
#we start loop at second element (index 1) since the first item is already sorted
for j in range(1,len(A)):
key = A[j] #The next item we are going to insert into the sorted section of the array
i = j-1 #the last item we are going to compare to
#now we keep moving the key back as long as it is smaller than the last item in the array
while (i > -1) and key < A[i]: #if i == -1 means that this key belongs at the start
A[i+1]=A[i] #move the last object compared one step ahead to make room for key
i=i-1 #observe the next item for next time.
@basarat
basarat / cardinaldirections.js
Last active November 14, 2022 17:44
Snapping an angle to the nearest cardinal direction in javascript
/**
* Given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW"
*/
export function getCardinal(angle) {
/**
* Customize by changing the number of directions you have
* We have 8
*/
const degreePerDirection = 360 / 8;
$(function(){
// init code
});
var app = angular.module("myApp",["Directives","Controllers","Services"];