import random
def argsort(x, reverse=False):
tuples = [(element, index) for index, element in enumerate(x, 0)]
tuples = sorted(tuples, reverse=reverse)
indices = [item[1] for item in tuples]
return indices
list1 = [76, 81, 30, 47, 50, 18, 23, 49]
print(argsort(list1))
list2 = ['Chad', 'Beth', 'Emma', 'Alex', 'Drew', 'Fred']
print(argsort(list2, True))
def process_line(line,schema,sep):
tokens = line.split(sep)
result = []
for i in range(len(tokens)):
t = tokens[i]
dt = schema[i]
result.append(dt(t))
return result
def main():
test_line = '6,174,Blah,Hello World,7.37'
test_schema = [int,int,str,str,float]
print(process_line(test_line,test_schema,','))
if __name__== "__main__":
main()
def process_lines(lines,j,schema,sep):
index=0
lst=[]
for element in lines[j].split(sep):
if schema[index]==str:
lst.append(str(element))
elif schema[index]==int:
lst.append(int(element))
else:
lst.append(float(element))
index=index+1
return lst
def read_file_to_list(path,schema ,sep):
f=open(path, "r")
contents =f.read()
lines = contents.split('\n')
data=[]
header = lines[0].split(sep)
data.append(header)
for j in range(1,len(lines)):
lst = process_lines(lines,j,schema,sep)
data.append(lst)
return(data)
my_schema=[int,int,str,str,int,float]
data = read_file_to_list("titanic_partial.txt",my_schema,sep='\t')
for i in data:
print(i)
my_schema=[float,str,str,str,int]
data = read_file_to_list("diamonds_partial.txt",my_schema,',')
for i in data:
print(i)
def recursive_product(x):
if len(x) == 1:
return x[0]
else:
temp = recursive_product(x[1:])
return temp * x[0]
factors = [11, 8, 17, 9, 18, 10]
print(recursive_product(factors))
import random
def gcd(m, n):
if m < n:
return gcd(n, m)
elif n == 0:
return m
else:
return gcd(n, m % n)
print(gcd(72, 300))
print(gcd(180, 210))
print(gcd(20, 400))
print(gcd(30, 77))
def flatten(nestedList):
if nestedList == []:
return nestedList
if isinstance(nestedList[0], list):
return flatten(*nestedList[:1]) + flatten(nestedList[1:])
return nestedList[:1] + flatten(nestedList[1:])
nestedList = [1, [8, [5, 6, [4, 9]]], [7, 2, 3]]
print(flatten(nestedList))
def flatten(x):
flat_list = []
for i in x:
if type(i) is list:
flat_list = flat_list + flatten(i)
else:
flat_list.append(i)
return flat_list
nested_list = [1, [8, [5, 6, [4, 9]]], [7, 2, 3]]
a = [1,[2],[3]]
b = [1,[2],[[4,5],6]]
print(flatten(nested_list))
print(flatten(a))
print(flatten(b))
def binary_search(x, item):
if item < x[0] or item > x[len(x)-1]:
return False
mid = int(len(x) / 2)
if item == x[mid]:
return True
elif item < x[mid]:
return binary_search(x[0:mid], item)
elif item > x[mid]:
return binary_search(x[mid+1:], item)
random.seed(1)
random_list_1 = []
random_list_1 = sorted(random.choices(range(100), k=100))
print(random_list_1)
for i in range(0, 10):
print(i, '-', binary_search(random_list_1, i))
def quicksort(x):
if len(x) <=1:
return x
pivot = x[0]
mid = [pivot]
low=[]
high=[]
for i in range(1,len(x)):
if x[i]<mid[0]:
low.append(x[i])
else:
high.append(x[i])
sorted_low =quicksort(low)
sorted_high = quicksort(high)
return sorted_low+mid+sorted_high
random.seed(2)
random_list_2 =random.choices(range(100),k=20)
print(random_list_2)
print(quicksort(random_list_2))
random.seed(3)
random_list_3 =random.choices(range(100),k=20)
print(random_list_3)
print(quicksort(random_list_3))