This file contains 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
@myArray = [1,3,5,[7,9,11],12,[15,16,[17,18,19],[20,21,22],[23,24],[25,26]]] | |
@outArray = Array.new | |
def flatten(inArray) | |
inArray.each do |a| | |
if a.is_a?(Array) | |
flatten(a) | |
else | |
@outArray.push(a) | |
end |
This file contains 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 output is as follows: | |
# word followed by "total: " with the total amount next to it | |
# 2:1 The sentence that the word appears in and the number of times it appears. | |
# 2:1 means that the word appears in the second sentence one time. | |
class Concordance | |
shash = Array.new | |
total_hash = Hash.new |
This file contains 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 output is as follows: | |
# word followed by "total: " with the total amount next to it | |
# 2:1 The sentence that the word appears in and the number of times it appears. | |
# 2:1 means that the word appears in the second sentence one time. | |
class Concordance | |
shash = Array.new | |
total_hash = Hash.new |
This file contains 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
def binary_search(target_array, target, first, last): | |
#If the array is empty return -1 | |
if not len(target_array): | |
return -1 | |
#if the array has only one element in it, check that element. | |
elif len(target_array) == 1: | |
if target_array[0] == target: | |
return 0 | |
else: |
This file contains 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
import Queue | |
def matching_parens(in_list): | |
paren_q = Queue.Queue() | |
for item in in_list: | |
if item in ('(','['): | |
paren_q.put(item) | |
elif item == ')': | |
check = paren_q.get() | |
if check == '(': |
This file contains 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
def find_1d_peak(the_list, first, last): | |
if first == last: | |
return first | |
if last == first + 1 and the_list[first] >= the_list[last]: | |
return first | |
if last == first + 1 and the_list[first] < the_list[last]: | |
return last |
This file contains 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
def find_1d_peak(arr, low, high, n): | |
mid = low + (high - low) / 2 | |
if ((mid == 0 or arr[mid - 1] <= arr[mid]) and | |
(mid == n - 1 or arr[mid + 1] <= arr[mid])): | |
return mid | |
elif (mid > 0 and arr[mid - 1] > arr[mid]): | |
return find_1d_peak(arr, low, (mid - 1), n) | |
else: | |
return find_1d_peak(arr, (mid + 1), high, n) |
This file contains 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 to initialize the node object | |
def __init__(self, data, neighbors=[]): | |
self.data = data | |
self.neighbors = [] | |
class Graph: | |
def __init__(self): | |
self.head = None |
This file contains 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 to initialize the node object | |
def __init__(self, data, neighbors=[]): | |
self.data = data | |
self.neighbors = [] | |
class Graph: | |
def __init__(self): | |
self.head = None |
This file contains 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
def main(): | |
print("test1") | |
main() |
OlderNewer