This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The Three Laws of Recursion | |
| # 1. A recursive algorithm must have a base case. | |
| # 2. A recursive algorithm must change its state and move toward the base case. | |
| # 3. A recursive algorithm must call itself, recursively. | |
| import turtle | |
| myTurtle = turtle.Turtle() | |
| myWin = turtle.Screen() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node: | |
| # constructor | |
| def __init__(self, initdata): | |
| self.data = initdata | |
| self.next = None | |
| # get value from node | |
| def getData(self): | |
| return self.data | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node: | |
| # constructor | |
| def __init__(self, initdata): | |
| self.data = initdata | |
| self.next = None | |
| # get value from node | |
| def getData(self): | |
| return self.data | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Deque: | |
| def __init__(self): | |
| self.items = [] | |
| def isEmpty(self): | |
| return self.items == [] | |
| def addFront(self, item): | |
| self.items.append(item) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Queue: | |
| def __init__(self): | |
| self.items = [] | |
| def isEmpty(self): | |
| return self.items == [] | |
| def enqueue(self, item): | |
| self.items.insert(0, item) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Stack: | |
| def __init__(self): | |
| self.items = [] | |
| def isEmpty(self): | |
| return self.items == [] | |
| def push(self, item): | |
| self.items.append(item) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The Three Laws of Recursion | |
| # 1. A recursive algorithm must have a base case. | |
| # 2. A recursive algorithm must change its state and move toward the base case. | |
| # 3. A recursive algorithm must call itself, recursively. | |
| # Reverse String | |
| def reverse(s): | |
| if len(s) == 0 or len(s) == 1: # first law | |
| return s | |
| else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Euclid's algoithm | |
| def gcd(m, n): | |
| while m % n != 0: | |
| oldm = m | |
| oldn = n | |
| m = oldn | |
| n = oldm % oldn | |
| return n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LogicGate: | |
| def __init__(self,n): | |
| self.name = n | |
| self.output = None | |
| def getName(self): | |
| return self.name | |
| def getOutput(self): | |
| self.output = self.performGateLogic() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function count(arr, arrLength, target) { | |
| if (target === 0) | |
| return 1; | |
| if (target < 0 || (arrLength <= 0 && target >= 1)) | |
| return 0; | |
| return count(arr, arrLength - 1, target) + count(arr, arrLength, target-arr[arrLength - 1]); | |
| } |