Last active
August 29, 2015 14:21
-
-
Save lmasikl/328588429b5302a69b6e to your computer and use it in GitHub Desktop.
This file contains 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
# coding=utf-8 | |
from sys import argv | |
from random import randint | |
def create_array(n=10): | |
""" | |
Создает массив неупорядоченных чисел | |
>>> len(create_array()) | |
10 | |
""" | |
return [randint(0, n) for x in xrange(n)] | |
def insertion_sort(array): | |
""" | |
Сортировка вставками | |
>>> insertion_sort([2, 1]) | |
[1, 2] | |
""" | |
for x in xrange(1, len(array)): | |
key = array[x] | |
i = x - 1 | |
while i >= 0 and array[i] > key: | |
array[i + 1] = array[i] | |
i = i - 1 | |
array[i + 1] = key | |
return array | |
def clean_array(array): | |
""" | |
Очистка от дубликатов | |
>>> clean_array([1, 1]) | |
[1] | |
""" | |
current = array[0] | |
for x in array[1:]: | |
if x == current: | |
array.pop(array.index(x)) | |
else: | |
current = x | |
return array | |
def shift_clean_array(array): | |
""" | |
Очистка от дубликатов | |
>>> shift_clean_array([0, 3, 3, 3, 3, 4, 4, 5, 5, 8]) | |
[0, 3, 4, 5, 8] | |
""" | |
j = 1 | |
for current in array: | |
if j >= len(array): | |
break | |
is_equal = current == array[j] | |
if is_equal: | |
array = array[:j] + array[j + 1:] | |
else: | |
j += 1 | |
return array | |
if __name__ == '__main__': | |
if argv[1] == '-v': | |
import doctest | |
doctest.testmod() | |
else: | |
array = create_array() | |
print(array) | |
insertion_sort(array) | |
print(array) | |
clean_array(array) | |
print(array) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment