Last active
March 12, 2020 14:16
-
-
Save imamdigmi/591e9d22f08cf4588cb2ee08c427bb4f to your computer and use it in GitHub Desktop.
Python Logic Cheatseet
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
| def fun_max(a): | |
| c = a[0] | |
| for i in a: | |
| if c < i: | |
| c = i | |
| return c | |
| def fun_min(a): | |
| c = a[0] | |
| for i in a: | |
| if c > i: | |
| c = i | |
| return c | |
| def fun_sort(array): | |
| """ | |
| Insertion method, like you're sorting a playing card | |
| """ | |
| for i in range(len(array)): | |
| position, cursor = i, array[i] | |
| while (position > 0) and (array[position - 1] > cursor): | |
| # Swap the number down in array | |
| array[position] = array[position - 1] | |
| position -= 1 | |
| # Break and do the final swap | |
| array[position] = cursor | |
| return array | |
| if __name__ == "__main__": | |
| array = [-15, 2, 1, 3, 10] | |
| print("Sorting: {}, Maximum: {}, Minimum: {}".format(fun_sort(array), fun_max(array), fun_min(array))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment