Exemplo n.º 1
0
def shuffle(data):
	data = deepcopy(data)
	length = len(data)
	for index in xrange(length):
		random_position = randint(0, index)
		swap(data, index, random_position)
	return data
Exemplo n.º 2
0
    def test_swap_empty(self):
        """Expects IndexError.

        :return:
        """
        with self.assertRaises(IndexError):
            sort.swap([], 0, 1)
Exemplo n.º 3
0
    def test_swap_empty(self):
        """Expects IndexError.

        :return:
        """
        with self.assertRaises(IndexError):
            sort.swap([], 0, 1)
Exemplo n.º 4
0
def _bsort(iterable):
    for offset in range(0, len(iterable) - 1):
        for index in range(0, len(iterable) - 1 - offset):
            if iterable[index] > iterable[index + 1]:
                swap(iterable, index, index + 1)

    return iterable
Exemplo n.º 5
0
    def test_swap_except(self):
        """

        :return:
        """
        l1 = [1, 2]
        with self.assertRaises(IndexError):
            sort.swap(l1, 0, 2)
Exemplo n.º 6
0
    def test_swap_except(self):
        """

        :return:
        """
        l1 = [1, 2]
        with self.assertRaises(IndexError):
            sort.swap(l1, 0, 2)
Exemplo n.º 7
0
def _gsort(iterable):
    for sorted_end in range(len(iterable) - 1):
        for index in range(sorted_end, -1, -1):
            if iterable[index] < iterable[index + 1]:
                break

            swap(iterable, index, index + 1)

    return iterable
Exemplo n.º 8
0
def _ssort(iterable):
    for offset, min_value in enumerate(iterable):
        min_index = offset

        for index, value in enumerate(iterable[offset:]):
            if value < min_value:
                min_index = offset + index
                min_value = value

        swap(iterable, min_index, offset)

    return iterable
Exemplo n.º 9
0
def partlo(ar,s,e):
#    p=randint(1,len(ar)-1)
    p=ar[int(math.floor((s+e)/2))]
    i=-1
    for k in ar:
        if k>p:
            pass
        elif k<=p:
            i+=1
            swap(i,ar.index(k),ar)
    swap(i,ar.index(p),ar)
    return ar.index(p)
Exemplo n.º 10
0
def init_byte_array(byte_array, key):

    my_byte_array = copy.deepcopy(byte_array)

    key_length = len(key)

    j = 0

    for i in range(0, 256):

        j = (j + my_byte_array[i] + ord(key[i % key_length])) % 256

        swap(my_byte_array, i, j)

    return my_byte_array
Exemplo n.º 11
0
def init_byte_array(byte_array, key):

    my_byte_array = copy.deepcopy(byte_array)

    key_length = len(key)

    j = 0

    for i in range(0, 256):

        j = (j + my_byte_array[i] + ord(key[i % key_length])) % 256

        swap(my_byte_array, i, j)

    return my_byte_array
Exemplo n.º 12
0
    def test_swap_same_index(self):
        """Swap same index gracefully.

        :return:
        """
        l1 = [5]
        l2 = sort.swap(l1, 0, 0)
        self.assertEqual(l1, l2, "swap [x], 0, 0 does not equal [x]")
Exemplo n.º 13
0
    def test_swap_legal(self):
        """

        :return:
        """
        l1 = [1, 2]
        l2 = sort.swap(l1, 0, 1)
        self.assertEqual(l1, [2, 1], "swap [x, y], 0, 1 does not equal [y, x]")
Exemplo n.º 14
0
    def test_swap_same_index(self):
        """Swap same index gracefully.

        :return:
        """
        l1 = [5]
        l2 = sort.swap(l1, 0, 0)
        self.assertEqual(l1, l2, "swap [x], 0, 0 does not equal [x]")
Exemplo n.º 15
0
    def test_swap_legal(self):
        """

        :return:
        """
        l1 = [1, 2]
        l2 = sort.swap(l1, 0, 1)
        self.assertEqual(l1, [2, 1],
                         "swap [x, y], 0, 1 does not equal [y, x]")
Exemplo n.º 16
0
def _partition(iterable, start, end):
    pivot, original_pivot_index = _pivot_choice(iterable, start, end)
    pivot_index = start

    for index in range(start, end + 1):
        if (index == original_pivot_index or iterable[index] > pivot):
            continue

        swap(iterable, pivot_index, index)

        if pivot_index == original_pivot_index:
            original_pivot_index = index

        pivot_index += 1

    swap(iterable, pivot_index, original_pivot_index)

    return pivot_index
Exemplo n.º 17
0
	def partition(a,lo, hi):
		i = lo
		j = hi+1
		while True:
			i += 1
			while a[i] < a[lo]:
				if i == hi:
					break
				i += 1

			j -= 1
			while a[lo] < a[j]:
				if j == lo:
					break
				j -= 1

			if i >= j:
				break
			swap(a, i, j)
			step(a)
		swap(a, lo, j)
		step(a)
		return j
Exemplo n.º 18
0
 def swap(self):
     self.assertEqual(sort.swap(self.before, 0, 1), [4, 5, 4, 3, 1, 2])
Exemplo n.º 19
0
 def testSwapOnNonEmptyListOfStrings(self):
     l = ["a", "b", "c"]
     sort.swap(l, 1, 2)
     self.assertEqual(l, ["a", "c", "b"])
Exemplo n.º 20
0
 def testSwapOnNonEmptyList(self):
     l = [1, 2, 3]
     sort.swap(l, 1, 2)
     self.assertEqual(l, [1, 3, 2])
Exemplo n.º 21
0
def test_swap():
    a = [1, 2, 3, 4, 5]
    sort.swap(a, 0)
    expected = [2, 1, 3, 4, 5]
    assert expected == a
Exemplo n.º 22
0
def test_swap_2():
    a = [1, 2, 3, 4, 5]
    sort.swap(a, 1)
    expected = [1, 3, 2, 4, 5]
    assert expected == a
Exemplo n.º 23
0
 def test_swap(self):
     numbers = [1, 2, 3, 4, 5, 6]
     numbers = sort.swap(numbers, 1, 3)
     self.assertEqual(numbers, [1, 4, 3, 2, 5, 6])