i have this 2d array, where each array is made up of 4 lots of 4 hex values:
list1=[['74 68 61 74', '73 20 6D 79', '20 6B 75 6E', '67 20 66 75'], ['C2 5B FC F1', 'B1 7B 91 88', '91 10 E4 E6', 'F6 30 82 93']]
And I would like to firstly split the array into multiple 1d arrays then combine the array into multiple single elements rather than each element having 4 values, like follows:
list2=[74, 68, 61, 74, 73, 20, 6D, 79, 20, 6B, 75, 6E, 67, 20, 66, 75]
list 3=[C2, 5B, FC, F1, B1, 7B, 91, 88, 91, 10, E4, E6, F6, 30, 82, 93]
Please let me know how this can be done. Thank you in advance.
What you essentially want to do is "flatten" the inner lists. There are several approaches to this. The example below borrows from here. Realize that the values in your lists are just strings (even though they could represent hex values) so you need to treat them like strings when splitting them up.
In [25]: list1
Out[25]:
[['74 68 61 74', '73 20 6D 79', '20 6B 75 6E', '67 20 66 75'],
['C2 5B FC F1', 'B1 7B 91 88', '91 10 E4 E6', 'F6 30 82 93']]
In [26]: list2 = [t for sublist in list1[0] for t in sublist.split(' ')]
In [27]: list3 = [t for sublist in list1[1] for t in sublist.split(' ')]
In [28]: list2
Out[28]:
['74',
'68',
'61',
'74',
'73',
'20',
'6D',
'79',
'20',
'6B',
'75',
'6E',
'67',
'20',
'66',
'75']
In [29]: list3
Out[29]:
['C2',
'5B',
'FC',
'F1',
'B1',
'7B',
'91',
'88',
'91',
'10',
'E4',
'E6',
'F6',
'30',
'82',
'93']
In [30]:
I've read multiple articles on how Selection Sort and Insertion sort work, and believe I understand their implementations. Selection sort iterates over the unsorted numbers in the inner loop, whereas insertion sort iterates over the sorted numbers in the inner loop. From what I understand, that's basically the only difference.
My question lies in the scenario where you're posed an input array, lets say it's this one:
Input Array: 30, 70, 40, 60, 50
Now, you're given a further list where the iterations are shown:
30, 70, 40, 60, 50
30, 40, 70, 60, 50
30, 40, 50, 60, 70
30, 40, 50, 60, 70
How is one meant to identify whether Insertion Sort or Selection sort has been used based PURELY on this? There is no code given, nor are we required to write any code. We are only required to choose which algorithm has been used from a multiple choice list. (Yes, both appear in the list).
To be clear, this is not an assignment question. However, this is assisting me with revision for an exam.
Think about what happens in each of the algorithms: selection sort always selects the minimum of the unsorted elements and adds it to the end of the sorted elements; insertion sort always takes the first of the unsorted elements and inserts it in the correct place in the sorted list.
Selection sort:
Sorted | Unsorted
| 30 70 40 60 50
30 | 70 40 60 50 # selects 30, the minimum unsorted element
30 40 | 70 60 50 # selects 40
30 40 50 | 70 60 # selects 50
30 40 50 60 | 70 # selects 60
30 40 50 60 70 | # selects 70
Insertion sort:
Sorted | Unsorted
| 30 70 40 60 50
30 | 70 40 60 50 # inserts 30, the first unsorted element
30 70 | 40 60 50 # inserts 70
30 40 70 | 60 50 # inserts 40
30 40 60 70 | 50 # inserts 60
30 40 50 60 70 | # inserts 50
The arrays listed in each iteration would be the concatenation of the sorted and unsorted portions of the array. It looks like these iterations show neither selection sort nor insertion sort.
After speaking with the lecturer via email, I have a solution to this question. This is indeed a Selection Sort, with the elements therefore being swapped in place. (See https://en.wikipedia.org/wiki/Selection_sort).
Now, for the explanation:
Selection Sort:
Input Array: 30, 70, 40, 60, 50
Sorting:
30, 70, 40, 60, 50 // 30 is already sorted.
30, 40, 70, 60, 50 // Swap 40 and 70.
30, 40, 50, 60, 70 // Swap 70 and 50.
30, 40, 50, 60, 70 // Array is sorted.
Here's what it looks like for an insertion sort:
Input Array: 30, 70, 40, 60, 50
Sorting:
30, 70, 40, 60, 50 // 30 is inserted.
30, 70, 40, 60, 50 // 70 is inserted.
30, 40, 70, 60, 50 // 40 is inserted.
30, 40, 60, 70, 50 // 60 is inserted.
30, 40, 50, 60, 70 // 50 is inserted.
Array is now sorted.
I hope this helps anybody else that may come across a similar problem in the future while undertaking an algorithms course at college or university.
i like to create an array and call the values. I stuck # starting :(
My Table (static)
Level | RPM 20 30 40 50 60 70 80 90 100 110 120 130
------------------------------------------------------
6 | 15 31 52 75 105 135 166 202 231 275 289
7 | 16 35 58 85 118 152 185 226 260 305 332
8 | 18 39 65 96 131 169 208 249 289 333 375
for example: # Level 6 and RPM >60<70 = 135
# Level 7 and RPM >50<60 = 118
And the bigger idea, can i interpolate between values so when i for example #65 RPM have the right values?
Is a array the right choice for this? How to build this array?
I find no example for this.
Is this the right way?
{
{
"Level6": "RPM20",
"Watt": 15,
}
{
"Level6": "RPM30",
"Watt": 31,
}
}
But in this way i type like hell!?
This is more interesting:
[
{
"Level X RPM": 6,
"20": 15,
"30": 31,
"40": 52,
"50": 69,
"60": 89,
"70": 106,
"80": 125,
"90": 143
},
{
"Level X RPM": 7,
"20": 16,
"30": 35,
"40": 52,
"50": 70,
"60": 88,
"70": 107,
"80": 124,
"90": 142
},
{
"Level X RPM": 8,
"20": 18,
"30": 39,
"40": 65,
"50": 87,
"60": 111,
"70": 135,
"80": 158,
"90": 180
}
]
I have changed and my List is now Python:
LEVEL_TO_POWER = {
1: [6,12,20,29,40,53,69,79,92,106,121],
2: [8,16,26,38,53,68,88,103,120,138,152],
3: [9,20,32,47,66,84,107,125,148,172,186],
4: [11,23,39,56,79,101,126,150,173,206,219],
5: [13,27,45,65,92,117,145,175,202,238,254],
6: [15,31,52,75,105,135,166,202,231,275,289],
7: [16,35,58,85,118,152,185,226,260,305,332],
8: [18,39,65,96,131,169,208,249,289,333,375],
9: [19,42,71,104,144,184,227,272,318,361,408],
10:[21,46,77,113,157,199,245,295,345,386,442],
11:[23,50,84,123,170,216,262,318,372,413,480],
12:[24,53,89,131,183,230,279,342,398,441,512],
13:[26,56,94,139,196,245,296,365,424,468,548],
14:[28,60,101,148,209,261,318,389,449,494,585],
15:[30,64,108,158,222,277,337,415,476,518,620],
16:[32,68,115,168,235,296,355,439,503,548,658],
17:[33,72,122,177,248,312,373,463,530,576,694],
18:[35,76,129,187,261,328,390,484,556,606,727],
19:[37,79,134,195,274,342,407,507,572,632,763],
20:[39,83,140,204,287,354,424,528,598,659,790],
21:[40,87,146,213,300,368,442,551,616,689,812],
22:[42,91,153,223,313,385,461,574,645,720,840],
23:[44,95,160,234,326,401,479,598,673,752,872],
24:[47,101,171,246,340,418,501,625,706,788,908],
}