You can use a dict that maps each list item to its sorted index:
mapping = { k: i for i, k in enumerate(sorted(s, reverse = True)) } print([mapping[i] for i in s ])
This outputs:
[3, 2, 1, 4, 0]
s = [20, 30, 45, 14, 59] ss = sorted(s, reverse = True) print([s.index(i) for i in ss]) # # gives[4, 2, 1, 0, 3] print([ss.index(i) for i in s]) # # gives[3, 2, 1, 4, 0]
Last Updated : 31 Jul, 2022
Input: [2, 3, 1, 4, 5]
Output: [2, 0, 1, 3, 4]
After sorting list becomes[1, 2, 3, 4, 5]
and their index as[2, 0, 1, 3, 4]
Input: [6, 4, 7, 8, 1]
Output: [4, 1, 0, 2, 3]
After sorting the list becomes[1, 4, 6, 7, 8]
and their index as[4, 1, 0, 2, 3].
Output :
[2, 0, 1, 3, 4]
Output:
[2, 0, 1, 3, 4]
Given an array X[] of size n, we need to find the maximum and minimum elements present in the array. Our algorithm should make the minimum number of comparisons.,If (X[i] < min): We have found a value smaller than minimum till ith index. So update min with X[i] i.e min = X[i].,One idea is : Pick elements in pairs and try to update the minimum and maximum. Suppose till (i - 1)th index, maximum and minimum have been updated in max and min variables. Now we are considering a pair of ith and (i + 1)th index in the next iteration. ,Combine part: Now we find the overall maximum and minimum by comparing the min and max of both halves. For this, we need to perform two comparisons only.
int[] findMinMax(int X[], int n) {
int max = X[0]
int min = X[0]
for (int i = 1; i < n; i = i + 1) {
if (X[i] > max)
max = X[i]
else if (X[i] < min)
min = X[i]
}
int maxMin[2] = {
max,
min
}
return maxMin
}
if (l == r) {
max = X[l]
min = X[l]
} else if (l + 1 == r) {
if (X[l] < X[r]) {
max = X[r]
min = X[l]
} else {
max = X[l]
min = X[r]
}
}
if (leftMinMax[0] > rightMinMax[0])
max = leftMinMax[0]
else
max = rightMinMax[0]
if (leftMinMax[1] < rightMinMax[1])
min = leftMinMax[1]
else
min = rightMinMax[1]
int[] findMinMax(int X[], int l, int r) {
int max, min
if (l == r) {
max = X[l]
min = X[l]
} else if (l + 1 == r) {
if (X[l] < X[r]) {
max = X[r]
min = X[l]
} else {
max = X[l]
min = X[r]
}
} else {
int mid = l + (r - l) / 2
int leftMinMax[2] = findMinMax(X, l, mid)
int rightMinMax[2] = findMinMax(X, mid + 1, r)
if (leftMinMax[0] > rightMinMax[0])
max = leftMinMax[0]
else
max = rightMinMax[0]
if (leftMinMax[1] < rightMinMax[1])
min = leftMinMax[1]
else
min = rightMinMax[1]
}
int maxMin[2] = {
max,
min
}
return maxMin
}
if (n % 2 != 0) {
max = X[0]
min = X[0]
i = 1
} else {
if (X[0] < X[1]) {
max = X[1]
min = X[0]
} else {
max = X[0]
min = X[1]
}
i = 2
}
while (i < n) {
if (X[i] < X[i + 1]) {
if (X[i] < min)
min = X[i]
if (X[i + 1] > max)
max = X[i + 1]
} else {
if (X[i] > max)
max = X[i]
if (X[i + 1] < min)
min = X[i + 1]
}
i = i + 2
}
The database system performs all operations on index-organized tables by manipulating the B-tree index structure. Table 3-4 summarizes the differences between index-organized tables and heap-organized tables.,A B-tree index leaf block for this heap-organized table contains the following entries, where the first value is the primary key and the second is the rowid:,If a row overflow area is specified, then the database can divide a row in an index-organized table into the following parts:,If a heap-organized table has no indexes, then the database must perform a full table scan to find a value. For example, without an index, a query of location 2700 in the hr.departments table requires the database to search every row in every table block for this value. This approach does not scale well as data volumes increase.
For example, suppose an application frequently queries the last_name
, job_id
, and salary
columns in the employees
table. Also assume that last_name
has high cardinality, which means that the number of distinct values is large compared to the number of table rows. You create an index with the following column order:
CREATE INDEX employees_ix ON employees(last_name, job_id, salary);
Multiple indexes can exist for the same table if the permutation of columns differs for each index. You can create multiple indexes using the same columns if you specify distinctly different permutations of the columns. For example, the following SQL statements specify valid permutations:
CREATE INDEX employee_idx1 ON employees(last_name, job_id); CREATE INDEX employee_idx2 ON employees(job_id, last_name);
Suppose that an application runs the following query:
SELECT department_id, last_name, salary
FROM employees
WHERE salary > 5000
ORDER BY department_id, last_name;
For example, the full scan could read the index entries as follows:
50, Atkinson, 2800, rowid 60, Austin, 4800, rowid 70, Baer, 10000, rowid 80, Abel, 11000, rowid 80, Ande, 6400, rowid 110, Austin, 7200, rowid . . .
For example, an application issues the following query, which does not include an ORDER BY
clause:
SELECT last_name, salary
FROM employees;
The last_name
column has a not null constraint. If the last name and salary are a composite key in an index, then a fast full index scan can read the index entries to obtain the requested information:
Baida, 2900, rowid Zlotkey, 10500, rowid Austin, 7200, rowid Baer, 10000, rowid Atkinson, 2800, rowid Austin, 4800, rowid . . .
For example, a user queries employees whose last names begin with A
. Assume that the last_name
column is indexed, with entries as follows:
Abel, rowid Ande, rowid Atkinson, rowid Austin, rowid Austin, rowid Baer, rowid . . .
As an illustration, suppose that a user runs the following query:
SELECT * FROM employees WHERE employee_id = 5;
Assume that the employee_id
column is the primary key and is indexed with entries as follows:
1, rowid 2, rowid 4, rowid 5, rowid 6, rowid . . .
Last modified: Aug 14, 2022, by MDN contributors
// Functionless
sort()
// Arrow function
sort((a, b) => {
/* … */ })
// Compare function
sort(compareFn)
// Inline compare function
sort(function compareFn(a, b) {
/* … */ })
function compareFn(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
function compareNumbers(a, b) {
return a - b;
}
const numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
// OR
const numbers2 = [4, 2, 5, 1, 3];
numbers2.sort((a, b) => a - b);
console.log(numbers2);
// [1, 2, 3, 4, 5]
const items = [{
name: 'Edward',
value: 21
},
{
name: 'Sharpe',
value: 37
},
{
name: 'And',
value: 45
},
{
name: 'The',
value: -12
},
{
name: 'Magnetic',
value: 13
},
{
name: 'Zeros',
value: 37
}
];
// sort by value
items.sort((a, b) => a.value - b.value);
// sort by name
items.sort((a, b) => {
const nameA = a.name.toUpperCase(); // ignore upper and lowercase
const nameB = b.name.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
const stringArray = ['Blue', 'Humpback', 'Beluga'];
const numberArray = [40, 1, 5, 200];
const numericStringArray = ['80', '9', '700'];
const mixedNumericArray = ['80', '9', '700', 40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
stringArray.join(); // 'Blue,Humpback,Beluga'
stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']
numberArray.join(); // '40,1,5,200'
numberArray.sort(); // [1, 200, 40, 5]
numberArray.sort(compareNumbers); // [1, 5, 40, 200]
numericStringArray.join(); // '80,9,700'
numericStringArray.sort(); // ['700', '80', '9']
numericStringArray.sort(compareNumbers); // ['9', '80', '700']
mixedNumericArray.join(); // '80,9,700,40,1,5,200'
mixedNumericArray.sort(); // [1, 200, 40, 5, '700', '80', '9']
mixedNumericArray.sort(compareNumbers); // [1, 5, '9', 40, '80', 200, '700']
There are no built-in functions for finding the max or min value in an array.,However, after you have sorted an array, you can use the index to obtain the highest and lowest values.,Even if objects have properties of different data types, the sort() method can be used to sort the array. ,Sorting a whole array is a very inefficient method if you only want to find the highest (or lowest) value.
const fruits = ["Banana", "Orange", "Apple", "Kiwi"];;
60. Write a Python program to find a tuple, the smallest second index value from a list of tuples. Go to the editor Click me to see the sample solution,27. Write a Python program to find the second smallest number in a list. Go to the editor Click me to see the sample solution,67. Write a Python program to find all the values in a list are greater than a specified number. Go to the editor Click me to see the sample solution,4. Write a Python program to get the smallest number from a list. Go to the editor Click me to see the sample solution
The Any() Function:
>>> arrival_hours = {
'Mon': 8.5,
'Tue': 8.75,
'Wed': 9,
'Thu': 8.5,
'Fri': 8.5
} >>>
arrival_checks = [x > 8.75
for x in arrival_hours.values()
] >>>
any(arrival_checks)
True