Skip to content

Instantly share code, notes, and snippets.

@gatarelib
Created August 14, 2019 13:03
Show Gist options
  • Save gatarelib/50f828f1abce198d71bc4e700d249f4d to your computer and use it in GitHub Desktop.
Save gatarelib/50f828f1abce198d71bc4e700d249f4d to your computer and use it in GitHub Desktop.
Andela - Python D2 Software Developer
def minimum_swaps(ratings):
if sorted(ratings, reverse=True) == ratings:
return 0
swaps = 1
while sorted(ratings, reverse=True) != sorter(ratings):
swaps += 1
return swaps
def sorter(array):
for i in sorted(range(len(array)), reverse=True):
comp = array[:i]
if len(comp) > 0:
mini = min(comp)
if mini < array[i]:
index = array.index(mini)
array[i], array[index] = array[index], array[i]
return array
return array
import unittest
class Test(unittest.TestCase):
def test_minimum_swaps_should_handle_single_swap(self):
self.assertEqual(minimum_swaps([3,1,2]), 1)
def test_minimum_swaps_should_handle_multiple_swaps(self):
self.assertEqual(minimum_swaps([3,1,2,4]), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment