repl.it
@m1s5ed/

the project at hand

Python

No description

fork
loading
Files
  • main.py
  • test_run.txt
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
def max_num(L):
  biggest = -100000
  for I in L:
    if I > biggest:
      big_number = I
      biggest = big_number
  return big_number
    

def min_num(L):
  smallest = 0
  for I in L:
    if I < smallest:
      small_num = I
      smallest = small_num
    elif I > smallest:
      smallest += 1

  return small_num


def sum_elems(L):
  alln = 0
  for I in L:
    alln += I
  return alln


def biggerist_num(a,b):
  if a > b:
    return a
  else:
    return b


def greatest_sum(L):
  Max = 0 
  for x in L: 
    Sum = 0 
    for y in x: 
      Sum += y      
      Max = biggerist_num(Sum, Max)  
  return Max


def dot_product(L1,L2):
  total = 0
  if len(L1) != len(L2):
    raise ValueError('Lists are not of the same length')
  for i in range(len(L2)):
    total += L1[i] * L2[i]
  return total


def mean(L):
  tot_num = 0
  num_add = 0
  for i in L:
    tot_num += 1
    num_add += i
  Mean = num_add / tot_num
  return int(Mean)


def mode(L):
  mode = 1
  mode_list = []
  for elem in L:
    if L.count(elem) > mode:
      mode += L.count(elem)
      mode_list = []
    elif L.count(elem) == mode:
      if elem not in mode_list:
        mode_list.append(elem)
  return mode_list

def perimeter(L):
  #does not work 
  pass

def prime_list(L):
  #does not work
  pass

def prime_factorization(n):
  factors = []
  a = 2
  while n != 1:
    if n % a == 0:
      n //= a
      factors.append(a)
      a = 2
    else:
      a += 1
  return factors

def flatten(L):
  flat = []
  for I in L:
    for elem in I:
      flat.append(elem)
  return flat

def move(L, i1, i2):
  L[i1], L[i2] = L[i2], L[i1]

def exchange(L, i1, i2):
  L[i1], L[i2] = L[i2], L[i1]

def elim_dups(L):
  #does not work 
  for elem in L:
    if L.count(elem) > 1:
      L.remove(elem)

def bubble_sort(L):
  sorted = False
  while not sorted:
    change = False
    for i in range(len(L)-1):
      if L[i] > L[i+1]:
        exchange(L, i, i+1)
        change = True
    if change == False:
      break

def bubble_sort_count(L):
  count = 0
  sorted = False
  while not sorted:
    change = False
    for i in range(len(L)-1):
      if L[i] > L[i+1]:
        exchange(L, i, i+1)
        count += 1
        change = True
    if change == False:
      break
  return count

def min_index(L):
  min = L[0]
  min_i = 0
  for i in range(1,len(L)):
    if L[i] < min:
      min = L[i]
      min_i = i
  return min_i

def selection_sort(L):
  sorted = False
  while not sorted:
    change = False
    for i in range(len(L)):
      min_i = i + min_index(L[i:])
      if min_i != i:
        elem = L.pop(min_i)
        L.insert(i, elem)
        change = True
    if change == False:
      break

def selection_sort_count(L):
  sorted = False
  count = 0
  while not sorted:
    change = False
    for i in range(len(L)):
      min_i = i + min_index(L[i:])
      if min_i != i:
        elem = L.pop(min_i)
        L.insert(i, elem)
        change = True
        count += 1
    if change == False:
      break
  return count
  

def disorder_ratio(L):
  return len(L) / bubble_sort_count(L)

def median(L):
  I = L[:]
  selection_sort(I)
  if len(I) % 2 != 0:
    return(I[len(I)//2])
  return (I[len(I)//2] + I[len(I)//2-1]) / 2

# Lists and Tuples Unit Test
# 1/15/20 @ 12:50 pm

#Helper Functions
import operator as op

eq_comp = op.__eq__
is_comp = op.is_

def elems_comp(a_list, b_list):
    return sorted(a_list) == sorted(b_list)

def order_comp(a_list, b_list):
    for i in range(len(a_list)):
        try:
            if a_list[i] != b_list[i]:
                return False
        except:
            return False
    return True

def eq_not_is(an_obj, another_obj):
    return (an_obj == another_obj) and not(an_obj is another_obj)

def approx_eq(expected, actual):
    if isinstance(actual, (int, float, complex)) and not isinstance(actual, bool):
        return abs(expected - actual) < 0.000001
    return False

def build_arg_str(args, is_poly):
    arg_str = ""
    if is_poly:
        for arg in args:
            if callable(arg):
                arg_str = arg_str + arg.__name__ + " "
            else:
                if isinstance(arg, str):
                    arg_str = arg_str + "'" + arg + "'" + " "
                else:
                    arg_str = arg_str + str(arg) + " "
    else:
        if callable(args):
            arg_str = args.__name__
        else:
            if isinstance(args, str):
                arg_str = "'" + args + "'"
            else:
                arg_str = str(args)
    return arg_str.strip()

class Unit():

    def __init__(self, func, name, cases, is_polyadic, is_mutator, test_type):
        # If the function is polyadic, we need to * the input so that it unpacks.
        # If the function is a mutator, we don't ask for its return value.
        # Instead we compare the input after the function is called to the correct mutated value.
        # Six test types are required:
        # 1. Verify equality of content: eq_comp
        # 2. Verify identity: is_comp
        # 3. Verify content equality and non-identity: eq_not_is
        # 4. Verify that two lists have the same elements, order irrelevant: elems_comp
        # 5. Verify that two lists have same elements in same order: order_comp
        # 6. Verify that two floats are close enough: approx_eq
        self.func = func
        self.name = name
        self.cases = cases
        self.is_polyadic = is_polyadic
        self.is_mutator = is_mutator
        self.test_type = test_type
        self.score = 0

    def add_test(self, new_test):
        self.cases.append(new_test)

class UnitTest():

    def __init__(self, name, units):
        self.name = name
        self.units = units
        self.score = 0
        self.num_tests = 0

    def run_test(self):
        test_file = open("test_run.txt", "w")
        test_file.write(self.name + "\n")
        total_score = 0
        for unit in self.units:
            test_file.write("\nFunction: " + unit.name + "\n")
            for case in unit.cases:
                self.num_tests += 1
                arg = case[0]
                expected_value = case[1]
                arg_str = build_arg_str(arg, unit.is_polyadic)
                test_file.write("Argument(s): " + arg_str + "  ")
                try:
                    if unit.is_polyadic:
                        actual_value = unit.func(*arg)
                    else:
                        actual_value = unit.func(arg) 
                except:
                    test_file.write("Crash! No point!\n")
                else:
                    if not unit.is_mutator:
                        if unit.test_type(expected_value, actual_value):
                            test_file.write("Return value correct! +1\n")
                            unit.score += 1
                        else:
                            test_file.write("Incorrect return value! No point!\n")
                    else:
                        if unit.is_polyadic:
                            mutated_value = arg[0]
                        else:
                            mutated_value = arg
                        if unit.test_type(expected_value, mutated_value):
                            test_file.write("Mutated value correct! +1\n")
                            unit.score += 1
                        else:
                            test_file.write("Incorrect mutated value! No point!\n")

            total_score += unit.score

        self.score = total_score
        test_file.write("\nFinal Score: " + str(self.score) + "/" + str(self.num_tests))

# Functions
# max_num, min_num, sum_elems, greatest_sum, dot_product, mean, mode
# perimeter, prime_list, prime_factorization, flatten, move, exchange
# elim_dups, bubble_sort, selection_sort, median

# Test Cases (tc)
# Format: a list of tuples where each tuple
# gives an argument (or tuple of arguments)
# and the expected return for that argument.
max_num_tc = [([0, 10**99, (-10)**99], 10**99),
              ([-1], -1)]
min_num_tc = [([0, 10**99, (-10)**99], (-10)**99),
              ([-1], -1)]
sum_elems_tc = [([1**2, 2**2, 3**2, 4**2], 30),
                ([-1], -1),
                ([], 0)]
greatest_sum_tc = [([[1], [2, 3, 4], [5, 6], [7]], 11),
                   ([[]], 0),
                   ([], 0)]
dot_product_tc = [(([1, 2, 3], [4, 5, 6]), 32),
                  ([[12], [12]], 144),
                  ([[], []], 0)]
mean_tc = [([1, 2, 3, 4, 5], 3),
           ([0], 0)]
mode_tc = [([1, 2, 2, 3, 3, 3, 2, 1], [2, 3]),
           ([1], [1]),
           ([], [])]
perimeter_tc = [(((1, 2), (1, 12), (-2, 0), (-12, 0)), 45.522263314818886),
                (((1, 1), ), 0),
                ((), 0)]
prime_list_tc = [((1, 100), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])]
prime_factorization_tc = [(24, [2, 2, 2, 3]),
                          (23, [23])]
flatten_tc = [([[1, 2], [2, 3, 4], [4]], [1, 2, 2, 3, 4, 4]),
              ([[]], []),
              ([], [])]
L, I = [1, 2, 3], [1]
move_tc = [((L, 1, 2), [1, 3, 2]),
           ((I, 0, 0), [1])]
L, I = [1, 2, 3, 4], [1]
exchange_tc = [((L, 1, 2), [1, 3, 2, 4]),
               ((I, 0, 0), [1])]
elim_dups_tc = [([3, 2, 1, 2, 1, 4, 3], [3, 2, 1, 4]),
                ([3, 2, 1], [3, 2, 1]),
                ([], [])]
L = [24, 54, 39, 71, 42, 13, 9, 72, 19, 52, 95, 68, 88, 96, 61, 64, 69, 93, 83, 52, 53, 32, 1, 16, 97, 28, 61, 78, 30, 86, 11, 1, 22, 20, 8, 98, 6, 82, 91, 70, 31, 31, 40, 89, 41, 18, 18, 8, 87, 3, 45, 39, 72, 15, 19, 82, 97, 7, 12, 91, 9, 59, 44, 91, 37, 92, 90, 47, 31, 34, 2, 79, 19, 77, 30, 50, 32, 77, 5, 50, 4, 3, 98, 73, 44, 43, 21, 47, 70, 14, 100, 35, 23, 27, 40, 4, 47, 41, 93, 15]
I = [1, 1, 2, 3, 3, 4, 4, 5, 6, 7, 8, 8, 9, 9, 11, 12, 13, 14, 15, 15, 16, 18, 18, 19, 19, 19, 20, 21, 22, 23, 24, 27, 28, 30, 30, 31, 31, 31, 32, 32, 34, 35, 37, 39, 39, 40, 40, 41, 41, 42, 43, 44, 44, 45, 47, 47, 47, 50, 50, 52, 52, 53, 54, 59, 61, 61, 64, 68, 69, 70, 70, 71, 72, 72, 73, 77, 77, 78, 79, 82, 82, 83, 86, 87, 88, 89, 90, 91, 91, 91, 92, 93, 93, 95, 96, 97, 97, 98, 98, 100]
S = []
bubble_sort_tc = [(L, I), (S, S)]
L = [5, 8, 0, 2, 1]
I = [24, 54, 39, 71, 42, 13, 9, 72, 19, 52, 95, 68, 88, 96, 61, 64, 69, 93, 83, 52, 53, 32, 1, 16, 97, 28, 61, 78, 30, 86, 11, 1, 22, 20, 8, 98, 6, 82, 91, 70, 31, 31, 40, 89, 41, 18, 18, 8, 87, 3, 45, 39, 72, 15, 19, 82, 97, 7, 12, 91, 9, 59, 44, 91, 37, 92, 90, 47, 31, 34, 2, 79, 19, 77, 30, 50, 32, 77, 5, 50, 4, 3, 98, 73, 44, 43, 21, 47, 70, 14, 100, 35, 23, 27, 40, 4, 47, 41, 93, 15]
bubble_sort_count_tc = [(L, 7), (I, 2616)]
L = [24, 54, 39, 71, 42, 13, 9, 72, 19, 52, 95, 68, 88, 96, 61, 64, 69, 93, 83, 52, 53, 32, 1, 16, 97, 28, 61, 78, 30, 86, 11, 1, 22, 20, 8, 98, 6, 82, 91, 70, 31, 31, 40, 89, 41, 18, 18, 8, 87, 3, 45, 39, 72, 15, 19, 82, 97, 7, 12, 91, 9, 59, 44, 91, 37, 92, 90, 47, 31, 34, 2, 79, 19, 77, 30, 50, 32, 77, 5, 50, 4, 3, 98, 73, 44, 43, 21, 47, 70, 14, 100, 35, 23, 27, 40, 4, 47, 41, 93, 15]
I = [1, 1, 2, 3, 3, 4, 4, 5, 6, 7, 8, 8, 9, 9, 11, 12, 13, 14, 15, 15, 16, 18, 18, 19, 19, 19, 20, 21, 22, 23, 24, 27, 28, 30, 30, 31, 31, 31, 32, 32, 34, 35, 37, 39, 39, 40, 40, 41, 41, 42, 43, 44, 44, 45, 47, 47, 47, 50, 50, 52, 52, 53, 54, 59, 61, 61, 64, 68, 69, 70, 70, 71, 72, 72, 73, 77, 77, 78, 79, 82, 82, 83, 86, 87, 88, 89, 90, 91, 91, 91, 92, 93, 93, 95, 96, 97, 97, 98, 98, 100]
selection_sort_tc = [(L, I), (S, S)]
L = [5, 8, 0, 2, 1]
I = [24, 54, 39, 71, 42, 13, 9, 72, 19, 52, 95, 68, 88, 96, 61, 64, 69, 93, 83, 52, 53, 32, 1, 16, 97, 28, 61, 78, 30, 86, 11, 1, 22, 20, 8, 98, 6, 82, 91, 70, 31, 31, 40, 89, 41, 18, 18, 8, 87, 3, 45, 39, 72, 15, 19, 82, 97, 7, 12, 91, 9, 59, 44, 91, 37, 92, 90, 47, 31, 34, 2, 79, 19, 77, 30, 50, 32, 77, 5, 50, 4, 3, 98, 73, 44, 43, 21, 47, 70, 14, 100, 35, 23, 27, 40, 4, 47, 41, 93, 15]
selection_sort_count_tc = [(L, 7), (I, 2616)]
L = [24, 54, 39, 71, 42, 13, 9, 72, 19, 52, 95, 68, 88, 96, 61, 64, 69, 93, 83, 52, 53, 32, 1, 16, 97, 28, 61, 78, 30, 86, 11, 1, 22, 20, 8, 98, 6, 82, 91, 70, 31, 31, 40, 89, 41, 18, 18, 8, 87, 3, 45, 39, 72, 15, 19, 82, 97, 7, 12, 91, 9, 59, 44, 91, 37, 92, 90, 47, 31, 34, 2, 79, 19, 77, 30, 50, 32, 77, 5, 50, 4, 3, 98, 73, 44, 43, 21, 47, 70, 14, 100, 35, 23, 27, 40, 4, 47, 41, 93, 15]
disorder_ratio_tc = [(L, 0.0382262996941896)]
L = [24, 54, 39, 71, 42, 13, 9, 72, 19, 52, 95, 68, 88, 96, 61, 64, 69, 93, 83, 52, 53, 32, 1, 16, 97, 28, 61, 78, 30, 86, 11, 1, 22, 20, 8, 98, 6, 82, 91, 70, 31, 31, 40, 89, 41, 18, 18, 8, 87, 3, 45, 39, 72, 15, 19, 82, 97, 7, 12, 91, 9, 59, 44, 91, 37, 92, 90, 47, 31, 34, 2, 79, 19, 77, 30, 50, 32, 77, 5, 50, 4, 3, 98, 73, 44, 43, 21, 47, 70, 14, 100, 35, 23, 27, 40, 4, 47, 41, 93, 15]
I = [1]
S = [1, 2]
T = [1, 2, 3]
median_tc = [(L, 42.5), (I, 1), (S, 1.5), (T, 2)]

# Create Units
# This block will cause a crash if you haven't created all of the required functions.
# Format: Unit(func, name, cases, is_polyadic, is_mutator, test_type)
# Test types: eq_comp, is_comp, eq_not_is, elems_comp
max_num_unit = Unit(max_num, "max_num", max_num_tc, False, False, eq_comp)
min_num_unit = Unit(min_num, "min_num", min_num_tc, False, False, eq_comp)
sum_elems_unit = Unit(sum_elems, "sum_elems", sum_elems_tc, False, False, eq_comp)
greatest_sum_unit = Unit(greatest_sum, "greatest_sum", greatest_sum_tc, False, False, eq_comp)
dot_product_unit = Unit(dot_product, "dot_product", dot_product_tc, True, False, eq_comp)
mean_unit = Unit(mean, "mean", mean_tc, False, False, eq_comp)
mode_unit = Unit(mode, "mode", mode_tc, False, False, eq_comp)
perimeter_unit = Unit(perimeter, "perimeter", perimeter_tc, False, False, approx_eq)
prime_list_unit = Unit(prime_list, "prime_list", prime_list_tc, True, False, eq_comp)
prime_factorization_unit = Unit(prime_factorization, "prime_factorization", prime_factorization_tc, False, False, eq_comp)
flatten_unit = Unit(flatten, "flatten", flatten_tc, False, False, eq_comp)
move_unit = Unit(move, "move", move_tc, True, True, eq_comp)
exchange_unit = Unit(exchange, "exchange", exchange_tc, True, True, eq_comp)
elim_dups_unit = Unit(elim_dups, "elim_dups", elim_dups_tc, False, True, eq_comp)
bubble_sort_unit = Unit(bubble_sort, "bubble_sort", bubble_sort_tc, False, True, eq_comp)
bubble_sort_count_unit = Unit(bubble_sort_count, "bubble_sort_count", bubble_sort_count_tc, False, False, eq_comp)
selection_sort_unit = Unit(selection_sort, "selection_sort", selection_sort_tc, False, True, eq_comp)
selection_sort_count_unit = Unit(selection_sort_count, "selection_sort_count", selection_sort_count_tc, False, False, eq_comp)
disorder_ratio_unit = Unit(disorder_ratio, "disorder_ratio", disorder_ratio_tc, False, False, approx_eq)
median_unit = Unit(median, "median", median_tc, False, False, eq_comp)

# Unit Test List
units = [max_num_unit, min_num_unit, sum_elems_unit, greatest_sum_unit, dot_product_unit, mean_unit,
         mode_unit, perimeter_unit, prime_list_unit, prime_factorization_unit, flatten_unit, move_unit,
         exchange_unit, elim_dups_unit, bubble_sort_unit, bubble_sort_count_unit, selection_sort_unit,
         selection_sort_count_unit, disorder_ratio_unit, median_unit]

# Create Unit Test
unit_test = UnitTest("Lists and Tuples Unit Test", units)

# Run Test!
unit_test.run_test()
Fetching token
?