Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/Top-rank-per-group/JavaScript/top-rank-per-group-1.js
Normal file
58
Task/Top-rank-per-group/JavaScript/top-rank-per-group-1.js
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
var data = [
|
||||
{name: "Tyler Bennett", id: "E10297", salary: 32000, dept: "D101"},
|
||||
{name: "John Rappl", id: "E21437", salary: 47000, dept: "D050"},
|
||||
{name: "George Woltman", id: "E00127", salary: 53500, dept: "D101"},
|
||||
{name: "Adam Smith", id: "E63535", salary: 18000, dept: "D202"},
|
||||
{name: "Claire Buckman", id: "E39876", salary: 27800, dept: "D202"},
|
||||
{name: "David McClellan", id: "E04242", salary: 41500, dept: "D101"},
|
||||
{name: "Rich Holcomb", id: "E01234", salary: 49500, dept: "D202"},
|
||||
{name: "Nathan Adams", id: "E41298", salary: 21900, dept: "D050"},
|
||||
{name: "Richard Potter", id: "E43128", salary: 15900, dept: "D101"},
|
||||
{name: "David Motsinger", id: "E27002", salary: 19250, dept: "D202"},
|
||||
{name: "Tim Sampair", id: "E03033", salary: 27000, dept: "D101"},
|
||||
{name: "Kim Arlich", id: "E10001", salary: 57000, dept: "D190"},
|
||||
{name: "Timothy Grove", id: "E16398", salary: 29900, dept: "D190"},
|
||||
];
|
||||
|
||||
function top_rank(n) {
|
||||
var by_dept = group_by_dept(data);
|
||||
for (var dept in by_dept) {
|
||||
output(dept);
|
||||
for (var i = 0; i < n && i < by_dept[dept].length; i++) {
|
||||
var emp = by_dept[dept][i];
|
||||
output(emp.name + ", id=" + emp.id + ", salary=" + emp.salary);
|
||||
}
|
||||
output("");
|
||||
}
|
||||
}
|
||||
|
||||
// group by dept, and sort by salary
|
||||
function group_by_dept(data) {
|
||||
var by_dept = {};
|
||||
for (var idx in data) {
|
||||
var dept = data[idx].dept;
|
||||
if ( ! has_property(by_dept, dept)) {
|
||||
by_dept[dept] = new Array();
|
||||
}
|
||||
by_dept[dept].push(data[idx]);
|
||||
}
|
||||
for (var dept in by_dept) {
|
||||
// numeric sort
|
||||
by_dept[dept].sort(function (a,b){return b.salary - a.salary});
|
||||
}
|
||||
return by_dept;
|
||||
}
|
||||
|
||||
function has_property(obj, propname) {
|
||||
return typeof(obj[propname]) != "undefined";
|
||||
}
|
||||
|
||||
function output(str) {
|
||||
try {
|
||||
WScript.Echo(str); // WSH
|
||||
} catch(err) {
|
||||
print(str); // Rhino
|
||||
}
|
||||
}
|
||||
|
||||
top_rank(3);
|
||||
31
Task/Top-rank-per-group/JavaScript/top-rank-per-group-2.js
Normal file
31
Task/Top-rank-per-group/JavaScript/top-rank-per-group-2.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
var collectDept = function (arrOfObj) {
|
||||
var collect = arrOfObj.reduce(function (rtnObj, obj) {
|
||||
if (rtnObj[obj.dept] === undefined) {
|
||||
rtnObj[obj.dept] = [];
|
||||
}
|
||||
rtnObj[obj.dept].push(obj);
|
||||
return rtnObj;
|
||||
}, {});
|
||||
|
||||
return Object.keys(collect).map(function (key) {
|
||||
return collect[key];
|
||||
});
|
||||
};
|
||||
|
||||
var sortSalary = function (arrOfSalaryArrs) {
|
||||
return arrOfSalaryArrs.map(function (item) {
|
||||
return item.sort(function (a, b) {
|
||||
if (a.salary > b.salary) { return -1; }
|
||||
if (a.salary < b.salary) { return 1; }
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var getNTopSalariesByDept = function (n, data) {
|
||||
if (n < 0) { return; }
|
||||
|
||||
return sortSalary(collectDept(data)).map(function (list) {
|
||||
return list.slice(0,n);
|
||||
});
|
||||
};
|
||||
158
Task/Top-rank-per-group/JavaScript/top-rank-per-group-3.js
Normal file
158
Task/Top-rank-per-group/JavaScript/top-rank-per-group-3.js
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// topNSalariesPerDept :: Int -> [[String]] -> [String]
|
||||
const topNSalariesPerDept = (n, records) =>
|
||||
foldl(
|
||||
(a, k, i) => (a[toLower(k)] = x => x[i], a),
|
||||
this,
|
||||
head(records)
|
||||
) && map(intercalate(','),
|
||||
concatMap(take(n),
|
||||
reverse(
|
||||
groupBy(
|
||||
on(same, department),
|
||||
sortBy(
|
||||
flip(
|
||||
mappendComparing([
|
||||
department,
|
||||
salary
|
||||
])
|
||||
),
|
||||
tail(records)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// GENERIC FUNCTIONS -----------------------------------------------------
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
(x, y) => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) =>
|
||||
xs.length > 0 ? (() => {
|
||||
const unit = typeof xs[0] === 'string' ? '' : [];
|
||||
return unit.concat.apply(unit, xs.map(f));
|
||||
})() : [];
|
||||
|
||||
// curry :: Function -> Function
|
||||
const curry = (f, ...args) => {
|
||||
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
|
||||
function () {
|
||||
return go(xs.concat(Array.from(arguments)));
|
||||
};
|
||||
return go([].slice.call(args, 1));
|
||||
};
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f => (a, b) => f.apply(null, [b, a]);
|
||||
|
||||
// foldl :: (b -> a -> b) -> b -> [a] -> b
|
||||
const foldl = (f, a, xs) => xs.reduce(f, a);
|
||||
|
||||
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
|
||||
const groupBy = (f, xs) => {
|
||||
const dct = xs.slice(1)
|
||||
.reduce((a, x) => {
|
||||
const
|
||||
h = a.active.length > 0 ? a.active[0] : undefined,
|
||||
blnGroup = h !== undefined && f(h, x);
|
||||
return {
|
||||
active: blnGroup ? a.active.concat([x]) : [x],
|
||||
sofar: blnGroup ? a.sofar : a.sofar.concat([a.active])
|
||||
};
|
||||
}, {
|
||||
active: xs.length > 0 ? [xs[0]] : [],
|
||||
sofar: []
|
||||
});
|
||||
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
|
||||
};
|
||||
|
||||
// head :: [a] -> a
|
||||
const head = xs => xs.length ? xs[0] : undefined;
|
||||
|
||||
// intercalate :: String -> [a] -> String
|
||||
const intercalate = curry((s, xs) => xs.join(s));
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// mappendComparing :: [(a -> b)] -> (a -> a -> Ordering)
|
||||
const mappendComparing = fs => (x, y) =>
|
||||
fs.reduce((ord, f) => (ord !== 0) ? (
|
||||
ord
|
||||
) : (() => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : a > b ? 1 : 0
|
||||
})(), 0);
|
||||
|
||||
// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
|
||||
const on = (f, g) => (a, b) => f(g(a), g(b));
|
||||
|
||||
// reverse :: [a] -> [a]
|
||||
const reverse = xs =>
|
||||
typeof xs === 'string' ? (
|
||||
xs.split('')
|
||||
.reverse()
|
||||
.join('')
|
||||
) : xs.slice(0)
|
||||
.reverse();
|
||||
|
||||
// same :: a -> a -> Bool
|
||||
const same = (a, b) => a === b
|
||||
|
||||
// show :: Int -> a -> Indented String
|
||||
// show :: a -> String
|
||||
const show = (...x) =>
|
||||
JSON.stringify.apply(
|
||||
null, x.length > 1 ? [x[1], null, x[0]] : x
|
||||
);
|
||||
|
||||
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
|
||||
const sortBy = (f, xs) =>
|
||||
xs.slice()
|
||||
.sort(f);
|
||||
|
||||
// tail :: [a] -> [a]
|
||||
const tail = xs => xs.length ? xs.slice(1) : undefined;
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
const take = curry((n, xs) => xs.slice(0, n));
|
||||
|
||||
// toLower :: Text -> Text
|
||||
const toLower = s => s.toLowerCase();
|
||||
|
||||
// TEST ------------------------------------------------------------------
|
||||
|
||||
const xs = [
|
||||
["Employee Name", "Employee ID", "Salary", "Department"],
|
||||
["Tyler Bennett", "E10297", "32000", "D101"],
|
||||
["John Rappl", "E21437", "47000", "D050"],
|
||||
["George Woltman", "E00127", "53500", "D101"],
|
||||
["Adam Smith", "E63535", "18000", "D202"],
|
||||
["Claire Buckman", "E39876", "27800", "D202"],
|
||||
["David McClellan", "E04242", "41500", "D101"],
|
||||
["Rich Holcomb", "E01234", "49500", "D202"],
|
||||
["Nathan Adams", "E41298", "21900", "D050"],
|
||||
["Richard Potter", "E43128", "15900", "D101"],
|
||||
["David Motsinger", "E27002", "19250", "D202"],
|
||||
["Tim Sampair", "E03033", "27000", "D101"],
|
||||
["Kim Arlich", "E10001", "57000", "D190"],
|
||||
["Timothy Grove", "E16398", "29900", "D190"]
|
||||
];
|
||||
|
||||
return show(2,
|
||||
topNSalariesPerDept(3, xs)
|
||||
);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue