-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataFilesMaker.py
161 lines (127 loc) · 5.74 KB
/
DataFilesMaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
This module will get two lists of some user-specified size from the list maker module.
One will be sorted and one will not be. It will then colllect data from the search and
modules.
Student Name: Michael Fourie
Student Number: 20102203
"""
import ListMaker
import Searches
import Sorts
# Run this function to start the program
def getLists():
"""
This module gets the lists based on the users input
Input: integer from suer
Output: two lists, one sorted one unsorted
"""
# The following code will get a user submitted length for the lists, being atleast 100 elements long.
listLength = 0
while listLength < 100:
listLength = int(input("Please indicate the length of the list, being atleast 100 elements long: "))
if listLength < 100:
print('Enter a value of atleast 100 elements.')
# This code will get two lists, one sorted and one unsorted, of the length indicated by the user
minValue = -10 * listLength # This code will get an appropriate min range value based on what the user had entered
maxValue = 10 * listLength # This code will get an appropriate max range value based on what the user had entered
unsortedList = ListMaker.unorderdIntegers(minValue, maxValue, listLength)
sortedList = ListMaker.orderdIntegersAscending(minValue, maxValue, listLength)
# run searchData to get data from search algorithms
searchData(unsortedList, sortedList)
# run sortData to get data from the sorting algorithms
sortData(unsortedList)
def searchData(unsortedList, sortedList):
"""
This function runs the two searching algorithms and tracks there data.
Input: Sorted and unsorted list
Output: text file of data
"""
# Begin by testing the binary search
binarySearchData = []
linearSearchData = []
# the range of testing the list begins at zero
testRange = 0
# get a value that is known not to be in the list
target = 0
while target in sortedList:
target += 1
for i in range(len(sortedList) + 1):
binarySearchCount = Searches.binarySearch(sortedList[:testRange], target)
binarySearchData.append(binarySearchCount[1])
linearSearchCount = Searches.linearSearch(sortedList[:testRange], target)
linearSearchData.append(linearSearchCount[1])
testRange += 1
# Wrtie the search data to text file
writeSearchDataToText(binarySearchData, linearSearchData)
def sortData(unsortedList):
"""
This function will run the sorting algortihms, and track their data.
Input: unsorted list
Output: data from the sorting algorithms
"""
# We will begin with the data from the bubble sort
bubbleSortData = []
bubbleSortOptData = []
selectionSortData = []
insertionSortData = []
# The range of testing will begin at zero
testRange = 0
for i in range(len(unsortedList) + 1):
bubbleSortCount = Sorts.bubbleSort(unsortedList[:testRange])
bubbleSortData.append(bubbleSortCount)
bubbleSortOptCount = Sorts.bubbleSortOpt(unsortedList[:testRange])
bubbleSortOptData.append(bubbleSortOptCount)
selectionSortCount = Sorts.selectionSort(unsortedList[:testRange])
selectionSortData.append(selectionSortCount)
insertionSortCount = Sorts.insertionSort(unsortedList[:testRange])
insertionSortData.append(insertionSortCount)
testRange += 1
# Write the sort data to text file
writeSortDataToText(bubbleSortData, bubbleSortOptData, selectionSortData, insertionSortData)
def writeSearchDataToText(binarySearchData, linearSearchData):
"""
This function will write the data colected from the previosu function
and write it onto a text file
Input: linear and binary search data
Output: two text files
"""
# Write data from binary search
binarySearchFile = open('binary_search.txt', 'w')
for i in range(len(binarySearchData)):
binarySearchFile.write(str(binarySearchData[i]))
binarySearchFile.write('\n')
# Write data from linear search
linearSearchFile = open('linear_search.txt', 'w')
for i in range(len(linearSearchData)):
linearSearchFile.write(str(linearSearchData[i]))
linearSearchFile.write('\n')
def writeSortDataToText(bubbleSortData, bubbleSortOptData, selectionSortData, insertionSortData):
"""
This module will write the data from the sorting algorithms to text files
Input: Data from each sorting algorithm
Output: text files written, holding the data
"""
# Write data from bubble sort
bubbleSortFile = open('bubble_sort.txt', 'w')
for i in range(len(bubbleSortData)):
bubbleSortFile.write(str(bubbleSortData[i]))
bubbleSortFile.write('\n')
# Write data from bubble sort opt
bubbleSortOptFile = open('optimized_bubble_sort.txt', 'w')
for i in range(len(bubbleSortOptData)):
bubbleSortOptFile.write(str(bubbleSortOptData[i]))
bubbleSortOptFile.write('\n')
# Write data from selection sort
selectionSortFile = open('selection_sort.txt', 'w')
for i in range(len(selectionSortData)):
selectionSortFile.write(str(selectionSortData[i]))
selectionSortFile.write('\n')
# Write data from insertion sort
insertionSortFile = open('insertion_sort.txt', 'w')
for i in range(len(insertionSortData)):
insertionSortFile.write(str(insertionSortData[i]))
insertionSortFile.write('\n')
if __name__ == '__main__':
getLists()
print('This function runs all the subsequent functions below it.')
print('This will create all text files to be used, and stored on the local disk.')