-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonal_code_check.py
More file actions
57 lines (46 loc) · 2.44 KB
/
personal_code_check.py
File metadata and controls
57 lines (46 loc) · 2.44 KB
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
49
50
51
52
53
54
55
56
57
from typing import *
def sort_array(arr):
"""
This function sorts an array of non-negative integers based on the number of ones in their binary representation.
If two numbers have the same number of ones, they are sorted by their decimal value.
"""
# Define a custom sorting key function
def sort_key(x):
# Count the number of 1s in the binary representation of x
# The bin() function converts the number to binary, and count('1') counts the number of '1's
return (bin(x).count('1'), x) # Return a tuple with the count of ones and the number itself
# Sort the array using the custom key
# The sorted() function sorts the array based on the values returned by sort_key
return sorted(arr, key=sort_key)
# Example usage:
# print(sort_array([1, 5, 2, 3, 4])) # Output: [1, 2, 3, 4, 5]
# print(sort_array([-2, -3, -4, -5, -6])) # Output: [-6, -5, -4, -3, -2]
# print(sort_array([1, 0, 2, 3, 4])) # Output: [0, 1, 2, 3, 4]
def check(candidate):
# Check some simple cases
assert True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate([1,5,2,3,4]) == [1, 2, 4, 3, 5]
assert candidate([-2,-3,-4,-5,-6]) == [-4, -2, -6, -5, -3]
assert candidate([1,0,2,3,4]) == [0, 1, 2, 4, 3]
assert candidate([]) == []
assert candidate([2,5,77,4,5,3,5,7,2,3,4]) == [2, 2, 4, 4, 3, 3, 5, 5, 5, 7, 77]
assert candidate([3,6,44,12,32,5]) == [32, 3, 5, 6, 12, 44]
assert candidate([2,4,8,16,32]) == [2, 4, 8, 16, 32]
assert candidate([2,4,8,16,32]) == [2, 4, 8, 16, 32]
# Check some edge cases that are easy to work out by hand.
assert True, "This prints if this assert fails 2 (also good for debugging!)"
check(sort_array)
def check(candidate):
# Check some simple cases
assert True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate([1,5,2,3,4]) == [1, 2, 4, 3, 5]
assert candidate([-2,-3,-4,-5,-6]) == [-4, -2, -6, -5, -3]
assert candidate([1,0,2,3,4]) == [0, 1, 2, 4, 3]
assert candidate([]) == []
assert candidate([2,5,77,4,5,3,5,7,2,3,4]) == [2, 2, 4, 4, 3, 3, 5, 5, 5, 7, 77]
assert candidate([3,6,44,12,32,5]) == [32, 3, 5, 6, 12, 44]
assert candidate([2,4,8,16,32]) == [2, 4, 8, 16, 32]
assert candidate([2,4,8,16,32]) == [2, 4, 8, 16, 32]
# Check some edge cases that are easy to work out by hand.
assert True, "This prints if this assert fails 2 (also good for debugging!)"
check(sort_array)