def test_inversion_counter(self): self.assertEqual(2, inv.count_inversions([3, 1, 2, 4, 5, 6, 7, 8])[1], "Expected 2 inversions") self.assertEqual(2, inv.count_inversions([3, 1, 2])[1], "Expected 2 inversions") self.assertEqual( 3, inv.count_inversions([3, 1, 2, 4, 5, 6, 7, 8, 10, 9])[1], "Expected 3 inversions") self.assertEqual( 45, inv.count_inversions( [1000, 900, 800, 700, 600, 50, 40, 30, 2, 0.1])[1], "Expected 45 inversions")
# -*- coding: utf-8 -*- from inversions import count_inversions __author__ = 'Taras Drapalyuk <*****@*****.**>' __date__ = '20.03.2017' if __name__ == '__main__': with open('IntegerArray.txt') as f: array = [int(l) for l in f.readlines()] _, inversions = count_inversions(array) print(inversions)
import inversions as inv A = [] with open('./inversions.txt') as f: for line in f: A.append(int(line)) len = int(len(A)) print(A) print("Length of A", len) print(inv.count_inversions(A)[1])
def test_inversions(self): a = self.read_input('../data/IntegerArray.txt') count = inversions.count_inversions(a) self.assertEqual(2407905288, count)
def run_and_plot(test_cases): merge_times = [] selection_times = [] times = [] initial_inversions = [] merge_inversions_ratios = [] select_inversions_ratios = [] diff_inversions = [] for i in test_cases: B = [] mergearray = [] selectarray = [] for j in range(0, i): B.append(random.randint(0, 1000)) [numinversions, trash] = inversions.count_inversions(B) initial_inversions.append(numinversions) [x, y, mergearray, selectarray] = runtime.test_runtime(B) merge_inversions_ratios.append( inversions.count_inversions(mergearray)[0] / numinversions) select_inversions_ratios.append( float(inversions.count_inversions(selectarray)[0]) / numinversions) diff_inversions.append(numinversions - select_inversions_ratios[-1]) merge_times.append(x) selection_times.append(y) times.append(i) layout = go.Layout( title='Selection Sort Versus Merge Sort', xaxis=dict(title='size of array', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=dict(title='time in seconds', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f'))) trace0 = go.Scatter(x=times, y=merge_times, mode='markers', name='merge') trace1 = go.Scatter(x=times, y=selection_times, mode='markers', name='selection') trace2 = go.Scatter(x=select_inversions_ratios, y=selection_times, mode='markers', name='inversions ratio versus run time') trace3 = go.Scatter(x=initial_inversions, y=selection_times, mode='markers', name='inversions number versus run time') trace4 = go.Scatter(x=initial_inversions, y=select_inversions_ratios, mode='markers', name='inversions number versus run time') trace5 = go.Scatter(x=diff_inversions, y=selection_times, mode='markers', name='differences') py.offline.plot([trace5], filename='differences.html') data = [trace0, trace1] fig = go.Figure(data=data, layout=layout) py.offline.plot(fig, filename='selectionvsmerge.html') layout = go.Layout( title='Inversions Ratios versus time for Selection Sort', xaxis=dict(title='Inversions Ratio', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=dict(title='time in seconds', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f'))) fig = go.Figure(data=[trace2], layout=layout) py.offline.plot(fig, filename='inversionsratios.html') layout = go.Layout( title='Initial Inversions number versus the time for Selection Sort', xaxis=dict(title='Number of initial Inversions in Array', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=dict(title='time in seconds', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f'))) fig = go.Figure(data=[trace3], layout=layout) py.offline.plot(fig, filename='inversions.html') layout = go.Layout( title= 'Number of inversions versus the inversion ratio for Selection Sort', xaxis=dict(title='Number of initial inversions', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=dict(title='inversions ratio', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f'))) fig = go.Figure(data=[trace4], layout=layout) py.offline.plot(fig, filename='numvratio')
def test_count_inversions(array, count): assert count_inversions(array) == count