Created
April 10, 2018 10:48
-
-
Save samidarko/5f308cc56ec5277e091f71ab4a835399 to your computer and use it in GitHub Desktop.
counting sort tryout
This file contains hidden or 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
#!/bin/env python3 | |
from random import randint | |
def count(src, dest): | |
for i in src: | |
dest[i] += 1 | |
def get_int(): | |
return randint(0, 200) | |
def median(n, arr): | |
temp = sorted(arr) | |
mid = n // 2 | |
if n % 2 == 0: | |
return (temp[mid-1] + temp[mid]) / 2 | |
else: | |
return temp[mid] | |
def median2(n, arr): | |
mid = n // 2 | |
if n % 2 == 0: | |
temp = None | |
for i, v in enumerate(arr): | |
mid -= v | |
if mid < 0 and v >= 2: | |
print("mid < 0 and v >= 2", str(i)) | |
return i | |
if mid < 0 and temp is not None: | |
print("mid < 0 and temp is not None", str(i)) | |
return (i + temp) / 2 | |
if mid == 0: | |
print("mid == 0", str(i)) | |
temp = i | |
else: | |
mid += 1 | |
for i, v in enumerate(arr): | |
mid -= v | |
if mid <= 0: | |
return i | |
def test_odd_array(): | |
for _ in range(0, 10): | |
odd_array = [get_int() for _ in range(0, 21)] | |
median(len(odd_array), odd_array) | |
odd_array_count = [0] * 201 | |
count(odd_array, odd_array_count) | |
m1 = median(len(odd_array), odd_array) | |
m2 = median2(len(odd_array), odd_array_count) | |
if m1 == m2: | |
print("ok") | |
else: | |
print("boom") | |
def test_even_array(): | |
for _ in range(0, 10): | |
even_array = [get_int() for _ in range(0, 20)] | |
median(len(even_array), even_array) | |
even_array_count = [0] * 201 | |
count(even_array, even_array_count) | |
m1 = median(len(even_array), even_array) | |
m2 = median2(len(even_array), even_array_count) | |
if m1 == m2: | |
print("ok") | |
else: | |
print("boom") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment