-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABS Value Sort.py
48 lines (29 loc) · 952 Bytes
/
ABS Value Sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/python3
"""
Task: Sort a list in ascending order by the element's absolute value. If two or more elements share the same absolute value, then sort them with numeric #cluded
Ex.
Input: [0, -9, 3, 3, -1, -1, 0]
Output: [0, 0, -1, -1, 3, 3, -9]
"""
from collections import Counter
def bubblesort(arr):
# Keep track of many duplicates there are
a_dict = Counter(arr)
# Get absolute value of each element inside of list
arr = list(map(abs, (i for i in arr)))
# Sort list
arr.sort()
# Iterate list
for i in range(len(arr)):
curr_key = arr[i] * -1
# Check if key exist and has a value greater than 1
if a_dict[curr_key]:
# Flip sign
arr[i] = arr[i] * -1
# Decrement value
a_dict[curr_key] -= 1
return arr
if __name__ == "__main__":
arr = [0, -9, 3, 3, -1, -1, 0]
print(arr)
print(bubblesort(arr))