Reputation: 71
I have the following data:
for example:
Group 1:
|Width|Meters|
--------------
|144 |500 |
--------------
|142 | 450 |
Group 2:
|Width|Meters|
--------------
|140 |500 |
--------------
|156 | 450 |
Now i need to create a sorting algorithm which will first pick the Group with greater sum of meters and then sort the width in descending I am clueless how to store this data ? should it be a dictionary or a 3d array ? or three lists ? and with the right data structure how can i actually sort it. I am not asking for the full code even if you guide me towards the right path i will find my way .Thanks
Upvotes: 1
Views: 480
Reputation: 9806
Using numpy:
# Width Meters
data = np.array([[[144, 500], # Group 1
[142, 450]],
[[140, 500], # Group 2
[156, 460]]])
# Pick the group with the largest sum of Meters
group_index = np.argmax(data.sum(1)[:,1])
print(f'Group with the largest sum of Meters: Group {group_index + 1}')
result = data[group_index]
# sort by Width in descending order
result = result[np.argsort(result[:,0])[::-1]]
Result:
array([[156, 460],
[140, 500]])
EDIT:
data = np.array([[[144, 500],
[142, 450],
[150, 300]],
[[140, 500],
[156, 460],
[145, 300]],
[[170, 500],
[180, 455],
[160, 300]]])
# Sort Groups by the sum of Meters
group_inds = np.argsort(data.sum(1)[:, 1])[::-1]
result = data[group_inds]
# Sort by Width in each group
result = result[np.arange(data.shape[0])[:, None],
np.argsort(result[..., 0], axis=1)[:,::-1]]
Result:
array([[[156, 460],
[145, 300],
[140, 500]],
[[180, 455],
[170, 500],
[160, 300]],
[[150, 300],
[144, 500],
[142, 450]]])
Upvotes: 1
Reputation: 224
The sorting algorithm can be simply created with a divide et impera approach.
In fact sorting the groups and sorting the (width, meters) pairs are different problems that we can solve independently.
The choice of the data structure is up to you and depends on what you need; a group could be a list a namedtuple(width, meter) for example or if you need a bit more efficiency a 2d numpy array Nx2.
The container of the various groups could be a list (or a numpy array) if you don't need to name the groups, a list of (name, data) tuple or a dictionary name->data; there's no strict rule and there are advantages and disadvantages for every possibility (for example in a list of (name, data) could be difficult to find efficiently the data knowing the name, but it is very easy, 1 LOC, to sort. While a dictionary provide an excellent way to retrieve data but could be a little more messy to have it sorted).
Here an example of configuration where I use a dictionary name->data for storing the various groups (which won't be sorted!); a list which will contain only the name of the groups in a sorted way; lists of namedtuple for the data.
In this way I can retrieve data easily and if I want to look a the order I can access the list for names and then the dictionary for the data
from collections import namedtuple
GroupData = namedtuple("GroupData", ["Width", "Meters"])
# Container of groups:
groups = {"Group 1": [GroupData(144, 500), GroupData(142, 650)],
"Group 2": [GroupData(140, 800), GroupData(156, 450)]}
# Sorted list of names of groups
sorted_groups = sorted([name for name in groups],
key=lambda name:sum(data.Meters for data in groups[name]),
reverse=True)
# Sort each group
for group_data in groups.values():
group_data.sort(key=lambda data:data.Width, reverse=True)
print(groups)
print(sorted_groups)
Upvotes: 1