Created
August 14, 2019 13:03
-
-
Save gatarelib/50f828f1abce198d71bc4e700d249f4d to your computer and use it in GitHub Desktop.
Andela - Python D2 Software Developer
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
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 |
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
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