Top rank per group: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(91 intermediate revisions by 38 users not shown) | |||
Line 22:
</pre>
<br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V data = [(‘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’)]
DefaultDict[String, [(String, String, Int, String)]] departments
L(rec) data
departments[rec[3]].append(rec)
V n = 3
L(department, recs) sorted(departments.items())
print(‘Department #.’.format(department))
print(‘ #<15 #<15 #<15 #<15 ’.format(‘Employee Name’, ‘Employee ID’, ‘Salary’, ‘Department’))
L(rec) sorted(recs, key' rec -> rec[2], reverse' 1B)[0 .< n]
print(‘ #<15 #<15 #<15 #<15 ’.format(rec[0], rec[1], rec[2], rec[3]))
print()</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll>
Department D050
Employee Name Employee ID Salary Department
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
Department D101
Employee Name Employee ID Salary Department
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
Tyler Bennett E10297 32000 D101
Department D190
Employee Name Employee ID Salary Department
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
Department D202
Employee Name Employee ID Salary Department
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
David Motsinger E27002 19250 D202
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
DEFINE ENTRY_SIZE="8"
TYPE Employee=[
PTR name,id,dep ;CHAR ARRAY
CARD salary]
BYTE ARRAY data(200)
BYTE count=[0]
PTR FUNC GetItemAddr(INT index)
PTR addr
addr=data+index*ENTRY_SIZE
RETURN (addr)
PROC Append(CHAR ARRAY n,i CARD s CHAR ARRAY d)
Employee POINTER dst
dst=GetItemAddr(count)
dst.name=n
dst.id=i
dst.dep=d
dst.salary=s
count==+1
RETURN
PROC InitData()
Append("Tyler Bennett","E10297",32000,"D101")
Append("John Rappl","E21437",47000,"D050")
Append("George Woltman","E00127",53500,"D101")
Append("Adam Smith","E63535",18000,"D202")
Append("Claire Buckman","E39876",27800,"D202")
Append("David McClellan","E04242",41500,"D101")
Append("Rich Holcomb","E01234",49500,"D202")
Append("Nathan Adams","E41298",21900,"D050")
Append("Richard Potter","E43128",15900,"D101")
Append("David Motsinger","E27002",19250,"D202")
Append("Tim Sampair","E03033",27000,"D101")
Append("Kim Arlich","E10001",57000,"D190")
Append("Timothy Grove","E16398",29900,"D190")
RETURN
PROC Swap(Employee POINTER e1,e2)
PTR tmp
tmp=e1.name e1.name=e2.name e2.name=tmp
tmp=e1.id e1.id=e2.id e2.id=tmp
tmp=e1.dep e1.dep=e2.dep e2.dep=tmp
tmp=e1.salary e1.salary=e2.salary e2.salary=tmp
RETURN
PROC Sort()
INT i,j,minpos,comp
Employee POINTER e1,e2
FOR i=0 TO count-2
DO
minpos=i
FOR j=i+1 TO count-1
DO
e1=GetItemAddr(minpos)
e2=GetItemAddr(j)
comp=SCompare(e1.dep,e2.dep)
IF comp>0 OR comp=0 AND e1.salary<e2.salary THEN
minpos=j
FI
OD
IF minpos#i THEN
e1=GetItemAddr(minpos)
e2=GetItemAddr(i)
Swap(e1,e2)
FI
OD
RETURN
PROC TopRank(BYTE n)
BYTE i,c
CHAR ARRAY d
Employee POINTER e
i=0
WHILE i<count
DO
e=GetItemAddr(i)
IF i=0 OR SCompare(e.dep,d)#0 THEN
d=e.dep c=0
IF i>0 THEN PutE() FI
PrintF("Department %S:%E",d)
c==+1
PrintF(" %U %S %S%E",e.salary,e.id,e.name)
ELSEIF c<n THEN
c==+1
PrintF(" %U %S %S%E",e.salary,e.id,e.name)
FI
i==+1
OD
RETURN
PROC Main()
InitData()
Sort()
TopRank(3)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Top_rank_per_group.png Screenshot from Atari 8-bit computer]
<pre>
Department D050:
47000 E21437 John Rappl
21900 E41298 Nathan Adams
Department D101:
53500 E00127 George Woltman
41500 E04242 David McClellan
32000 E10297 Tyler Bennett
Department D190:
57000 E10001 Kim Arlich
29900 E16398 Timothy Grove
Department D202:
49500 E01234 Rich Holcomb
27800 E39876 Claire Buckman
19250 E27002 David Motsinger
</pre>
=={{header|Ada}}==
top.adb:
<
with Ada.Text_IO;
Line 95 ⟶ 277:
end;
end loop;
end Top;</
<pre style="height:30ex;overflow:scroll">Department D050
John Rappl | E21437 | 47000
Line 112 ⟶ 294:
=={{header|Aime}}==
<syntaxhighlight lang="aime">Add_Employee(record employees, text name, id, integer salary, text department)
{
}
collect(record top, employees)
{
for (, list l in employees) {
top.v_index(l[3]).v_list(l[2]).link(-1, l);
}
for (text department, index x in top) {
list t;
x.ucall(l_ucall, 0, l_append, 1, t);
t.erase(N, -1);
}
top[department] = t;
}
}
print_department(text department, list employees)
{
o_form(" ~ | ~ | ~\n", l[0], l[1], l[2]);
}
}
main(void)
{
Line 204 ⟶ 344:
collect(top, employees);
}</
<pre>aime rcs/top_rank_per_group c N 5</pre>{{out}}
<pre style="height:30ex;overflow:scroll">Department D050
Line 226 ⟶ 366:
David Motsinger | E27002 | 19250
Adam Smith | E63535 | 18000</pre>
=={{header|ALGOL 68}}==
Using code from the [[Sort using a custom comparator#ALGOL 68|ALGOL 68 sample in the Sort using a custom comparator task]].
<syntaxhighlight lang="algol68">
BEGIN # show the top rank salaries per department #
# NODE to hold the employee data - will be the MODE that is sorted #
MODE SITEM = STRUCT( STRING employee name
, STRING employee id
, INT salary
, STRING department
);
# ---- begin code from the quicksort using a custom comparator task ----- #
#--- Swap function ---#
PROC swap = (REF[]SITEM array, INT first, INT second) VOID:
(
SITEM temp := array[first];
array[first] := array[second];
array[second]:= temp
);
#--- Quick sort partition arg function with custom comparision function ---#
PROC quick = (REF[]SITEM array, INT first, INT last, PROC(SITEM,SITEM)INT compare) VOID:
(
INT smaller := first + 1,
larger := last;
SITEM pivot := array[first];
WHILE smaller <= larger DO
WHILE compare(array[smaller], pivot) < 0 AND smaller < last DO
smaller +:= 1
OD;
WHILE compare( array[larger], pivot) > 0 AND larger > first DO
larger -:= 1
OD;
IF smaller < larger THEN
swap(array, smaller, larger);
smaller +:= 1;
larger -:= 1
ELSE
smaller +:= 1
FI
OD;
swap(array, first, larger);
IF first < larger-1 THEN
quick(array, first, larger-1, compare)
FI;
IF last > larger +1 THEN
quick(array, larger+1, last, compare)
FI
);
#--- Quick sort array function with custom comparison function ---#
PROC quicksort = (REF[]SITEM array, PROC(SITEM,SITEM)INT compare) VOID:
(
IF UPB array > LWB array THEN
quick(array, LWB array, UPB array, compare)
FI
);
# ==== end code from the quicksort using a custom comparator task ===== #
# sort comparison routine - sort ascending department, descending salary #
PROC sort by department then salary = ( SITEM a, SITEM b )INT:
IF department OF a < department OF b THEN -1
ELIF department OF a > department OF b THEN 1
ELIF salary OF a > salary OF b THEN -1
ELIF salary OF a < salary OF b THEN 1
ELSE 0
FI # sort by department then salary # ;
# returns s blank padded on the right to at least w characters #
PROC pad right = ( STRING s, INT w )STRING:
IF INT len = ( UPB s - LWB s ) + 1; len >= w THEN s ELSE s + ( ( w - len ) * " " ) FI;
# shows the top n ranked salaries in each department #
PROC show top rank = ( []SITEM data, INT n )VOID:
BEGIN
# copy the data and sort it #
[ LWB data : UPB data ]SITEM sorted data := data;
quicksort( sorted data, sort by department then salary );
# show the top salaries per department #
INT d pos := LWB sorted data;
WHILE d pos <= UPB sorted data DO
STRING curr department := department OF sorted data[ d pos ];
print( ( "Department: ", curr department, newline ) );
INT e count := 0;
WHILE IF e count < n THEN
print( ( " "
, employee id OF sorted data[ d pos ]
, " "
, pad right( employee name
OF sorted data[ d pos ], 24 )
, " "
, whole( salary OF sorted data[ d pos ], -6 )
, newline
)
)
FI;
e count +:= 1;
d pos +:= 1;
IF d pos > UPB sorted data
THEN FALSE
ELSE curr department = department OF sorted data[ d pos ]
FI
DO SKIP OD;
print( ( newline ) )
OD
END # show top rank # ;
# employee data #
[]SITEM employees = ( ( "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" )
);
# show the top two salaries by department #
show top rank( employees, 2 )
END
</syntaxhighlight>
{{out}}
<pre>
Department: D050
E21437 John Rappl 47000
E41298 Nathan Adams 21900
Department: D101
E00127 George Woltman 53500
E04242 David McClellan 41500
Department: D190
E10001 Kim Arlich 57000
E16398 Timothy Grove 29900
Department: D202
E01234 Rich Holcomb 49500
E39876 Claire Buckman 27800
</pre>
=={{header|AppleScript}}==
It looks as if this task's requirements have changed — possibly more than once — since it was first set and named. As at the end of May 2021, it specifies the return of the top N ''salaries'' in each department and makes no mention at all of any of the other employee details. This makes it easier to decide how to handle the other thing it doesn't mention: what to return when two or more people in the same department are on the same pay. The interpretation here is "top N ''discrete'' salary values" (or of course fewer if a department doesn't have that many).
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
on topNSalariesPerDepartment(employeeRecords, n)
set output to {}
set employeeCount to (count employeeRecords)
if ((employeeCount > 0) and (n > 0)) then
-- Sort a copy of the employee record list by department
-- with descending subsorts on salary.
copy employeeRecords to employeeRecords
script comparer
on isGreater(a, b)
return ((a's department > b's department) or ¬
((a's department = b's department) and (a's salary < b's salary)))
end isGreater
end script
considering numeric strings
tell sorter to sort(employeeRecords, 1, employeeCount, {comparer:comparer})
end considering
-- Initialise the output with data from the first record in the sorted list,
-- then work through the rest of the list.
set {department:previousDepartment, salary:previousSalary} to beginning of employeeRecords
set {mv, topSalaries} to {"-", {previousSalary}}
set end of output to {department:previousDepartment, salaries:topSalaries}
repeat with i from 2 to employeeCount
set {department:thisDepartment, salary:thisSalary} to item i of employeeRecords
if (thisDepartment = previousDepartment) then
if ((thisSalary < previousSalary) and ((count topSalaries) < n)) then
set end of topSalaries to thisSalary
set previousSalary to thisSalary
end if
else
-- First record of the next department.
-- Pad out the previous department's salary list if it has fewer than n entries.
repeat (n - (count topSalaries)) times
set end of topSalaries to mv
end repeat
-- Start a result record for the new department and add it to the output.
set topSalaries to {thisSalary}
set end of output to {department:thisDepartment, salaries:topSalaries}
set previousDepartment to thisDepartment
set previousSalary to thisSalary
end if
end repeat
-- Pad the last department's salary list if necessary.
repeat (n - (count topSalaries)) times
set end of topSalaries to mv
end repeat
end if
return output
end topNSalariesPerDepartment
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
on task()
set employeeRecords to {¬
{|name|:"Tyler Bennett", |ID|:"E10297", salary:32000, department:"D101"}, ¬
{|name|:"John Rappl", |ID|:"E21437", salary:47000, department:"D050"}, ¬
{|name|:"George Woltman", |ID|:"E00127", salary:53500, department:"D101"}, ¬
{|name|:"Adam Smith", |ID|:"E63535", salary:18000, department:"D202"}, ¬
{|name|:"Claire Buckman", |ID|:"E39876", salary:27800, department:"D202"}, ¬
{|name|:"David McClellan", |ID|:"E04242", salary:41500, department:"D101"}, ¬
{|name|:"Rich Holcomb", |ID|:"E01234", salary:49500, department:"D202"}, ¬
{|name|:"Nathan Adams", |ID|:"E41298", salary:21900, department:"D050"}, ¬
{|name|:"Richard Potter", |ID|:"E43128", salary:15900, department:"D101"}, ¬
{|name|:"David Motsinger", |ID|:"E27002", salary:19250, department:"D202"}, ¬
{|name|:"Tim Sampair", |ID|:"E03033", salary:27000, department:"D101"}, ¬
{|name|:"Kim Arlich", |ID|:"E10001", salary:57000, department:"D190"}, ¬
{|name|:"Timothy Grove", |ID|:"E16398", salary:29900, department:"D190"}, ¬
{|name|:"Simila Pey", |ID|:"E16399", salary:29900, department:"D190"} ¬
}
set n to 4
set topSalaryRecords to topNSalariesPerDepartment(employeeRecords, n)
-- Derive a text report from the result.
set report to {"Top " & n & " salaries per department:"}
repeat with thisRecord in topSalaryRecords
set end of report to thisRecord's department & ": " & join(thisRecord's salaries, " ")
end repeat
return join(report, linefeed)
end task
task()</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">"Top 4 salaries per department:
D050: 47000 21900 - -
D101: 53500 41500 32000 27000
D190: 57000 29900 - -
D202: 49500 27800 19250 18000"</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">printTopEmployees: function [count, entries][
data: read.csv.withHeaders entries
departments: sort unique map data 'row -> row\Department
loop departments 'd [
print "----------------------"
print ["department:" d]
print "----------------------"
loop first.n: count
sort.descending.by:"Salary"
select data 'row [row\Department = d] 'employee ->
print [get employee "Employee Name" "->" employee\Salary]
print ""
]
]
printTopEmployees 2 {
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
}</syntaxhighlight>
{{out}}
<pre>----------------------
department: D050
----------------------
John Rappl -> 47000
Nathan Adams -> 21900
----------------------
department: D101
----------------------
George Woltman -> 53500
David McClellan -> 41500
----------------------
department: D190
----------------------
Kim Arlich -> 57000
Timothy Grove -> 29900
----------------------
department: D202
----------------------
Rich Holcomb -> 49500
Claire Buckman -> 27800</pre>
=={{header|AutoHotkey}}==
<
StringSplit, Department_, Departments, `,, %A_Space%
Line 283 ⟶ 727:
Employee_%m%_Salary := Salary
Employee_%m%_Dept := Department
}</
<pre style="height:30ex;overflow:scroll">Department: D050
---------------------------
Line 305 ⟶ 749:
Claire Buckman E39876 27800 D202
David Motsinger E27002 19250 D202</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f TOP_RANK_PER_GROUP.AWK [n]
#
Line 352 ⟶ 797:
exit(0)
}
</syntaxhighlight>
<p>Sample command and output:</p>
<pre>
Line 376 ⟶ 821:
=={{header|Bracmat}}==
Bracmat has no dedicated sorting functions. However, when evaluating algebraic expressions, Bracmat sorts factors in products and terms in sums. Moreover, Bracmat simplifies products by, amongst other transformations, collecting exponents of the same base into a single exponent, which is the sum of the collected exponents: <code>a^b*a^c</code> → <code>a^(b+c)</code>. This built-in behaviour is made use of in this solution.
<
(John Rappl,E21437,47000,D050)
(George Woltman,E00127,53500,D101)
Line 415 ⟶ 860:
)
& toprank$(!employees.3)
& ;</
<pre style="height:30ex;overflow:scroll">Top 3 salaries per department.
Department D050:
Line 433 ⟶ 878:
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
typedef struct {
} person;
person ppl[] = {
};
int pcmp(const void *a, const void *b)
{
}
Line 469 ⟶ 914:
void top(int n)
{
}
}
}
int main()
{
}</
<pre>D050 47000: John Rappl
D050 21900: Nathan Adams
Line 499 ⟶ 944:
D202 49500: Rich Holcomb
D202 27800: Claire Buckman</pre>
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
class Employee
{
public Employee(string name, string id, int salary, string department)
{
Name = name;
Id = id;
Salary = salary;
Department = department;
}
public string Name { get; private set; }
public string Id { get; private set; }
public int Salary { get; private set; }
public string Department { get; private set; }
public override string ToString()
{
return String.Format("{0, -25}\t{1}\t{2}", Name, Id, Salary);
}
}
private static void Main(string[] args)
{
var employees = new List<Employee>
{
new Employee("Tyler Bennett", "E10297", 32000, "D101"),
new Employee("John Rappl", "E21437", 47000, "D050"),
new Employee("George Woltman", "E21437", 53500, "D101"),
new Employee("Adam Smith", "E21437", 18000, "D202"),
new Employee("Claire Buckman", "E39876", 27800, "D202"),
new Employee("David McClellan", "E04242", 41500, "D101"),
new Employee("Rich Holcomb", "E01234", 49500, "D202"),
new Employee("Nathan Adams", "E41298", 21900, "D050"),
new Employee("Richard Potter", "E43128", 15900, "D101"),
new Employee("David Motsinger", "E27002", 19250, "D202"),
new Employee("Tim Sampair", "E03033", 27000, "D101"),
new Employee("Kim Arlich", "E10001", 57000, "D190"),
new Employee("Timothy Grove", "E16398", 29900, "D190")
};
DisplayTopNPerDepartment(employees, 2);
}
static void DisplayTopNPerDepartment(IEnumerable<Employee> employees, int n)
{
var topSalariesByDepartment =
from employee in employees
group employee by employee.Department
into g
select new
{
Department = g.Key,
TopEmployeesBySalary = g.OrderByDescending(e => e.Salary).Take(n)
};
foreach (var x in topSalariesByDepartment)
{
Console.WriteLine("Department: " + x.Department);
foreach (var employee in x.TopEmployeesBySalary)
Console.WriteLine(employee);
Console.WriteLine("----------------------------");
}
}
}</syntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
Department: D101
George Woltman E21437 53500
David McClellan E04242 41500
----------------------------
Department: D050
John Rappl E21437 47000
Nathan Adams E41298 21900
----------------------------
Department: D202
Rich Holcomb E01234 49500
Claire Buckman E39876 27800
----------------------------
Department: D190
Kim Arlich E10001 57000
Timothy Grove E16398 29900
----------------------------</pre>
Online demo: http://ideone.com/95TxAV
=={{header|C++}}==
<
#include <set>
#include <list>
Line 510 ⟶ 1,047:
struct Employee
{
{
}
};
Line 527 ⟶ 1,064:
struct CompareEarners
{
{
}
};
Line 544 ⟶ 1,081:
void initialize(EMPLOYEELIST& Employees)
{
}
void group(EMPLOYEELIST& Employees, DEPARTMENTLIST& Departments)
{
{
}
}
void present(DEPARTMENTLIST& Departments, unsigned int N)
{
{
{
}
}
}
int main(int argc, char* argv[])
{
}</
<pre style="height:30ex;overflow:scroll">In department D050
Name ID Salary Department
Line 637 ⟶ 1,174:
David Motsinger E27002 19250 D202</pre>
=={{header|
<syntaxhighlight lang="ceylon">class Employee(name, id, salary, dept) {
shared String name;
shared String id;
shared Integer salary;
shared String dept;
string => "``name`` ``id`` $``salary``.00 ``dept``";
}
Employee[] employees = [
Employee("Tyler Bennett", "E10297", 32000, "D101"),
Employee("John Rappl", "E21437", 47000, "D050"),
Employee("George Woltman", "E00127", 53500, "D101"),
Employee("Claire Buckman", "E39876", 27800, "D202"),
Employee("David McClellan", "E04242", 41500, "D101"),
Employee("Rich Holcomb", "E01234", 49500, "D202"),
Employee("Nathan Adams", "E41298", 21900, "D050"),
Employee("Richard Potter", "E43128", 15900, "D101"),
Employee("David Motsinger", "E27002", 19250, "D202"),
Employee("Tim Sampair", "E03033", 27000, "D101"),
Employee("Kim Arlich", "E10001", 57000, "D190"),
Employee("Timothy Grove", "E16398", 29900, "D190")
];
"This is the main function."
shared void run() {
value topRanked = topSalaries(employees, 3);
for (dept -> staff in topRanked) {
print(dept);
for (employee in staff) {
}
}
}
Map<String, {Employee*}> topSalaries({Employee*} employees, Integer n) => map {
for (dept -> staff in employees.group(Employee.dept))
dept -> staff.sort(byDecreasing(Employee.salary)).take(n)
};</syntaxhighlight>
=={{header|Clojure}}==
<
(defstruct employee :Name :ID :Salary :Department)
Line 756 ⟶ 1,245:
(doseq [e (take 3 (reverse (sort-by :Salary emps)))]
(println e)))
</
<pre style="height:30ex;overflow:scroll">Department: D050
{:Name John Rappl, :ID E21437, :Salary 47000, :Department D050}
Line 774 ⟶ 1,263:
=={{header|Common Lisp}}==
<
(let ((not-pred (complement predicate))
(group-data (make-hash-table :test group-test)))
Line 807 ⟶ 1,296:
(second entry) (insert datum (second entry))))))
(dolist (datum data group-data)
(update-entry (entry (funcall group-key datum)) datum)))))</
Example
<
'(("Tyler Bennett" E10297 32000 D101)
("John Rappl" E21437 47000 D050)
Line 836 ⟶ 1,325:
D050 (2 (("Nathan Adams" E41298 21900 D050) ("John Rappl" E21437 47000 D050)))
D202 (3 (("David Motsinger" E27002 19250 D202) ("Claire Buckman" E39876 27800 D202) ("Rich Holcomb" E01234 49500 D202)))
D190 (2 (("Timothy Grove" E16398 29900 D190) ("Kim Arlich" E10001 57000 D190)))</
=={{header|D}}==
<
struct Employee {
Line 873 ⟶ 1,362:
writefln("Department %s\n %(%s\n %)\n", dep, recs.take(n));
}
}</
<pre style="height:30ex;overflow:scroll">Department D202
Employee("Rich Holcomb", "E01234", 49500, "D202")
Line 891 ⟶ 1,380:
Employee("John Rappl", "E21437", 47000, "D050")
Employee("Nathan Adams", "E41298", 21900, "D050")</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
type TEmployeeInfo = record
Name,ID: string;
Salary: double;
Dept: string;
end;
var EmployeeInfo: array [0..12] of TEmployeeInfo = (
(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 SalarySort(Item1, Item2: Pointer): Integer;
var Info1,Info2: TEmployeeInfo;
begin
Info1:=TEmployeeInfo(Item1^);
Info2:=TEmployeeInfo(Item2^);
Result:=AnsiCompareStr(Info1.Dept,Info2.Dept);
If Result=0 then Result:=Trunc(Info1.Salary-Info2.Salary);
end;
procedure ShowTopSalaries(Memo: TMemo);
var List: TList;
var Info: TEmployeeInfo;
var I: integer;
var S,OldDept: string;
procedure NewDepartment(Name: string);
begin
Memo.Lines.Add('');
Memo.Lines.Add('Department: '+Name);
Memo.Lines.Add('Employee Name Employee ID Salary Department');
OldDept:=Name;
end;
begin
List:=TList.Create;
try
for I:=0 to High(EmployeeInfo) do
List.Add(@EmployeeInfo[I]);
List.Sort(SalarySort);
OldDept:='';
for I:=0 to List.Count-1 do
begin
Info:=TEmployeeInfo(List[I]^);
if OldDept<>Info.Dept then NewDepartment(Info.Dept);
S:=Format('%-18S %9S %11.0m %8S',[Info.Name,Info.ID,Info.Salary,Info.Dept]);
Memo.Lines.Add(S);
end;
finally List.Free; end;
end;
</syntaxhighlight>
{{out}}
<pre>
Department: D050
Employee Name Employee ID Salary Dept
Nathan Adams E41298 $21,900 D050
John Rappl E21437 $47,000 D050
Department: D101
Employee Name Employee ID Salary Dept
Richard Potter E43128 $15,900 D101
Tim Sampair E03033 $27,000 D101
Tyler Bennett E10297 $32,000 D101
David McClellan E04242 $41,500 D101
George Woltman E00127 $53,500 D101
Department: D190
Employee Name Employee ID Salary Dept
Timothy Grove E16398 $29,900 D190
Kim Arlich E10001 $57,000 D190
Department: D202
Employee Name Employee ID Salary Dept
Adam Smith E63535 $18,000 D202
David Motsinger E27002 $19,250 D202
Claire Buckman E39876 $27,800 D202
Rich Holcomb E01234 $49,500 D202
Elapsed Time: 35.284 ms.
</pre>
=={{header|Dyalect}}==
<syntaxhighlight lang="dyalect">type Employee(name,id,salary,department) with Lookup
func Employee.ToString() {
"$\(this.salary) (name: \(this.name), id: \(this.id), department: \(this.department)"
}
let employees = [
Employee("Tyler Bennett","E10297",32000,"D101"),
Employee("John Rappl","E21437",47000,"D050"),
Employee("George Woltman","E00127",53500,"D101"),
Employee("Adam Smith","E63535",18000,"D202"),
Employee("Claire Buckman","E39876",27800,"D202"),
Employee("David McClellan","E04242",41500,"D101"),
Employee("Rich Holcomb","E01234",49500,"D202"),
Employee("Nathan Adams","E41298",21900,"D050"),
Employee("Richard Potter","E43128",15900,"D101"),
Employee("David Motsinger","E27002",19250,"D202"),
Employee("Tim Sampair","E03033",27000,"D101"),
Employee("Kim Arlich","E10001",57000,"D190"),
Employee("Timothy Grove","E16398",29900,"D190")
]
func topNSalaries(n) {
//We sort employees based on salary
employees.Sort((x,y) => y.salary - x.salary)
let max =
if n > employees.Length() - 1 {
employees.Length() - 1
} else {
n
}
for i in 0..max {
yield employees[i]
}
}
var seq = topNSalaries(5)
for e in seq {
print(e)
}</syntaxhighlight>
{{out}}
<pre>$57000 (name: Kim Arlich, id: E10001, department: D190
$53500 (name: George Woltman, id: E00127, department: D101
$49500 (name: Rich Holcomb, id: E01234, department: D202
$47000 (name: John Rappl, id: E21437, department: D050
$41500 (name: David McClellan, id: E04242, department: D101
$32000 (name: Tyler Bennett, id: E10297, department: D101</pre>
=={{header|E}}==
<
def addKeys(keys, rows) {
def res := [].diverge()
Line 931 ⟶ 1,577:
out.println()
}
}</
Note: This uses an append-and-then-sort to maintain the list of top N; a sorted insert or a proper [[wp: selection algorithm|selection algorithm]] would be more efficient. As long as <var>N</var> is small, this does not matter much; the algorithm is O(n) with respect to the data set.{{out}}
Line 959 ⟶ 1,605:
=={{header|EchoLisp}}==
We use the '''sql.lib''' to select, sort and group records from the table 'emps'. The output is appended in a new table, 'high'.
<
(lib 'struct) ;; tables are based upon structures
(lib 'sql) ;; sql-select function
Line 974 ⟶ 1,620:
group-by emp.dept
order-by emp.salary desc limit N into high))
</syntaxhighlight>
{{out}}
<
(define emps_file
'(("Tyler Bennett" E10297 32000 D101)
Line 1,016 ⟶ 1,662:
[3] D202 Rich Holcomb 49500
[4] D666 Simon Gallubert 42
</syntaxhighlight>
=={{header|Elena}}==
ELENA 6.x :
import extensions'text;
class Employee
{
string Name : prop;
string ID : prop;
int Salary : prop;
string Department : prop;
string toPrintable()
= new StringWriter()
.writePaddingRight(Salary.toPrintable(), 12)
}
extension reportOp
{
topNPerDepartment
= self
^ new {
Department = x
Employees
= x
}
}
public program()
{
var employees := new Employee[]
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
new Employee{
employees
console
info
console
console
}</syntaxhighlight>
{{out}}
<pre>
Line 1,123 ⟶ 1,753:
=={{header|Elixir}}==
Quick implementation using piping and anonymous functions.
<
def per_groupe(data, n) do
String.split(data, ~r/(\n|\r\n|\r)/, trim: true)
Line 1,158 ⟶ 1,788:
Timothy Grove,E16398,29900,D190
"""
TopRank.per_groupe(data, 3)</
{{out}}
Line 1,179 ⟶ 1,809:
=={{header|Erlang}}==
<
-export( [employees/0, employees_in_department/2, highest_payed/2, task/1] ).
Line 1,186 ⟶ 1,816:
employees() ->
employees_in_department( Department, Employees ) -> [X || #employee{department=D}=X <- Employees, D =:= Department].
highest_payed( N, Employees ) ->
task( N ) ->
task_write( Highest_payeds ) ->
</
<pre><14> top_rank_per_group:task(2).
"D050" 53500: "George Woltman"
Line 1,233 ⟶ 1,863:
=={{header|F Sharp|F#}}==
<
[
"Tyler Bennett", "E10297", 32000, "D101";
Line 1,252 ⟶ 1,882:
let topRank n =
Seq.groupBy (fun (_, _, _, d) -> d) data
|> Seq.map (snd >> Seq.sortBy (fun (_, _, s, _) -> -s) >> Seq.take n)</
=={{header|Factor}}==
<
sorting ;
IN: top-rank
Line 1,296 ⟶ 1,926:
] first-n-each
nl
] assoc-each ;</
<pre style="height:30ex;overflow:scroll">
Department D101:
Line 1,318 ⟶ 1,948:
=={{header|Forth}}==
<syntaxhighlight lang="forth">
\ Written in ANS-Forth; tested under VFX.
\ Requires the novice package: http://www.forth.org/novice.html
Line 1,448 ⟶ 2,078:
test-data kill-employee
</
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 1,483 ⟶ 2,113:
=={{header|Fortran}}==
The example data can easily be declared via DATA statements, as with <
1 GRIST("Tyler Bennett","E10297",32000,"D101"),
2 GRIST("John Rappl","E21437",47000,"D050"),
3 GRIST("George Woltman","E00127",53500,"D101")/
</syntaxhighlight>
(just showing the first three), however this does not allow the preparation of the header line, because <code>GRIST("Employee Name","Employee ID","Salary","Department")</code> would be invalid since the "salary" entry is a floating-point number in the data aggregate, not text. The header line could be discarded, but this is in violation of the ideas of properly-described data files. So, the plan is to read the data from an input file containing the full example input, with its header line given special treatment. No attempt is made to detect or report improperly-formatted data. The READ statements could refer to an internally-defined block of text, but reading an actual disc file is more normal. Ad-hoc decisions are made for the size of the CHARACTER variables, that are "surely big enough" for the example data, likewise with the size of the array to hold all the records.
Line 1,498 ⟶ 2,128:
If the requirement had been to report the highest-ranked salary, the deed could have been done via the F90 function MAXLOC, which locates the array index of the (first-encountered?) maximum value of an array of values; further, it allows an additional MASK parameter which could be used to select only those entries having a particular "department" code. However, aside from the multiple scans that would be required for multiple departmental codes, there is the requirement for the top N salaries, and anyway, there would still be the need to identify the different departmental codes once only. Accordingly, the way ahead is the classic: sort the data and then work through the sorted sequence. Since standard Fortran provides no "sort" function in its standard libraries nor does it offer a pre-processor that might include a sort generator and the like to generate the specific code needed for a particular data aggregate and sort key specification, one must engage in some repetition. For this purpose, the "comb" sort is convenient with its code requiring just one comparison and one "swap" action, so in-line code is not troublesome. On the other hand, when dealing with compound sort keys, a three-way IF statement (or equivalent) is necessary whereby the first field of the sort key is compared with appropriate action for the < and > cases, but for the = case the comparison moves on to the second key. ''Three'' different consequences from the one comparison. Despite the deprecations of the modernisers this is still possible for numerical comparisons, but alas, character comparisons are not allowed in an arithmetic-IF statement - though one could struggle with ICHAR action. Thus, two comparisons are made where only one should suffice.
Finally, standard Fortran does not offer a means of receiving parameters as might be supplied when a code file is activated in a command-line environment. There may be special library routines with names such as GETARG, but they're not standard. So, a READ statement is employed. Or, one could rewrite this routine as a subroutine having one parameter so as to approximate the form of the specification more closely. Extra statements... <
CHARACTER*28 HEADING(4)
TYPE GRIST
CHARACTER*28 NAME
CHARACTER*6 ID
REAL*8 SALARY
CHARACTER*6 DEPARTMENT
END TYPE GRIST
INTEGER HORDE
PARAMETER (HORDE = 66)
TYPE(GRIST) EMPLOYEE(HORDE),HIC
LOGICAL CURSE
INTEGER I,N,H
INTEGER II,R
INTEGER KBD,MSG,IN
KBD = 5
MSG = 6
IN = 10
WRITE (MSG,1)
1 FORMAT ("Reads a set of employee information from TopRank.csv"/
1"Then for each department code, shows the N highest paid.")
OPEN (IN,NAME = "TopRank.csv",FORM = "FORMATTED")
READ (IN,*) HEADING
Chug through the input.
N = 0
10 READ (IN,*,END = 20) EMPLOYEE(N + 1)
N = N + 1
IF (N.GT.HORDE) STOP "Too many employee records!"
GO TO 10
Collate the collection.
20 CLOSE(IN)
Crank up a comb sort, which requires only one comparison statement. Especially good for compound fields.
H = N - 1
21 H = MAX(1,H*10/13)
CURSE = .FALSE.
DO I = N - H,1,-1
IF (EMPLOYEE(I).DEPARTMENT.LT.EMPLOYEE(I + H).DEPARTMENT) CYCLE
IF (EMPLOYEE(I).DEPARTMENT.EQ.EMPLOYEE(I + H).DEPARTMENT
* .AND. EMPLOYEE(I).SALARY.GT.EMPLOYEE(I + H).SALARY) CYCLE
CURSE = .TRUE.
HIC = EMPLOYEE(I)
EMPLOYEE(I) = EMPLOYEE(I + H)
EMPLOYEE(I + H) = HIC
END DO
IF (CURSE .OR. H.GT.1) GO TO 21
Cast forth results.
30 WRITE (6,31) N
31 FORMAT (I0," employees. How many per dept? ",$)
READ (KBD,*) R
IF (R.LE.0) STOP
WRITE (MSG,32) "Rank",HEADING
32 FORMAT (/,A6,1X,A28,2X,A12,1X,A10,A)
HIC.DEPARTMENT = "...Not this"
DO I = 1,N
IF (HIC.DEPARTMENT.EQ.EMPLOYEE(I).DEPARTMENT) THEN
II = II + 1
ELSE
II = 1
HIC.DEPARTMENT = EMPLOYEE(I).DEPARTMENT
WRITE (MSG,*) "For ",HIC.DEPARTMENT
END IF
IF (II.LE.R) WRITE (MSG,33) II,EMPLOYEE(I)
33 FORMAT (I6,1X,A28,1X,A12,F11.2,1X,A)
END DO
END
</syntaxhighlight>
As ever, some tricky sizing of fields in the FORMAT statements, and a collection of ad-hoc constants. Cross-linking the integers via PARAMETER statements and the like would add a whole lot of blather, so if field sizes are changed, a fair amount of fiddling would follow. A more accomplished data processing routine would include this.
Line 1,591 ⟶ 2,221:
2 Claire Buckman E39876 27800.00 D202
3 David Motsinger E27002 19250.00 D202
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define N 3 'show the top three employees of each rank
'here is all the data to be read in
data "Tyler Bennett","E10297",32000,"D101"
data "John Rappl","E21437",47000,"D050"
data "George Woltman","E00127",53500,"D101"
data "Adam Smith","E63535",18000,"D202"
data "Claire Buckman","E39876",27800,"D202"
data "David McClellan","E04242",41500,"D101"
data "Rich Holcomb","E01234",49500,"D202"
data "Nathan Adams","E41298",21900,"D050"
data "Richard Potter","E43128",15900,"D101"
data "David Motsinger","E27002",19250,"D202"
data "Tim Sampair","E03033",27000,"D101"
data "Kim Arlich","E10001",57000,"D190"
data "Timothy Grove","E16398",29900,"D190"
type employee
'define a data type for employees
nm as string*32 'name
en as string*6 'employee number
sl as uinteger 'salary
dp as string*4 'department
fl as boolean 'a flag
end type
dim as employee emp(1 to 13)
dim as uinteger e, d, x, ce, cs
dim as string*4 dept(1 to 4) = {"D050", "D101", "D190", "D202"}
for e = 1 to 13
'put all the employee data into an array
read emp(e).nm
read emp(e).en
read emp(e).sl
read emp(e).dp
emp(e).fl = false
next e
for d = 1 to 4 'look at each department
print "Department ";dept(d);":"
for x = 1 to N 'top N employees
cs = 0
ce = 0
for e = 1 to 13 'look through employees
if emp(e).dp = dept(d) andalso emp(e).fl = false andalso emp(e).sl > cs then
emp(ce).fl = false 'unflag the previous champion so they can be found on the next pass
ce = e
cs = emp(e).sl
emp(e).fl = true 'flag this employee so that on the next pass we can get the next richest
endif
next e
if ce>0 then print emp(ce).nm;" ";emp(ce).en;" ";emp(ce).sl;" ";emp(ce).dp
next x
print
next d</syntaxhighlight>
{{out}}<pre>
Department D050:
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
Department D101:
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
Tyler Bennett E10297 32000 D101
Department D190:
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
Department D202:
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
David Motsinger E27002 19250 D202
</pre>
=={{header|FunL}}==
<
employees = [
Line 1,620 ⟶ 2,329:
printf( " %-16s %6s %7d\n", e.name, e.id, e.salary )
println()</
{{out}}
Line 1,643 ⟶ 2,352:
=={{header|Go}}==
<
import (
)
// language-native data description
type Employee struct {
}
Line 1,660 ⟶ 2,369:
var data = EmployeeList{
}
Line 1,689 ⟶ 2,398:
type By func(e1, e2 *Employee) bool
type employeeSorter struct {
}
Line 1,708 ⟶ 2,417:
// nth salary (callers that don't care about ties could just trim more.)
func (el EmployeeList) TopSalariesByDept(n int) []EmployeeList {
}
}
}
}
}
}
}
}
func main() {
}
}
}
}</
{{out|Output (with additional data demonstrating ties)}}
<pre style="height:30ex;overflow:scroll">
Line 1,786 ⟶ 2,495:
=={{header|Groovy}}==
<
employees.groupBy { it.Department }.sort().each { department, staff ->
println "Department $department"
Line 1,813 ⟶ 2,522:
[Name: 'Timothy Grove', ID: 'E16398', Salary: 29900, Department: 'D190']
]
displayRank(employees, 3)</
<pre style="height:30ex;overflow:scroll">Department D050
Name ID Salary
Line 1,837 ⟶ 2,546:
=={{header|Haskell}}==
{{Works with|GHC|7.10.3}}
<syntaxhighlight lang="haskell">import Data.List (sortBy, groupBy)
import Text.Printf (printf)
import Data.Ord (comparing)
import Data.Function (on)
type ID = Int
type DEP = String
type NAME = String
type SALARY = Double
data Employee = Employee
{ nr :: ID
, dep :: DEP
, name :: NAME
, sal :: SALARY
}
employees :: [Employee]
employees =
fmap
(\(i, d, n, s) -> Employee i d n s)
, (1106, "CC", "Boon T.J.",
, (1107, "CC", "Beloop L.O.", 31000)
, (1009, "CD", "Janszoon A.", 38665)
, (1026, "CD", "Janszen H.P.", 41000)
, (1011, "CC", "de Goeij J.", 39000)
, (106, "KA", "Pragtweik J.M.V.", 42300)
, (111, "KA", "Bakeuro S.", 31000)
, (105, "KA", "Clubdrager C.", 39800)
, (104, "KA", "Karendijk F.", 23000)
, (107, "KA", "Centjes R.M.", 34000)
, (119, "KA", "Tegenstroom H.L.", 39000)
, (1111, "CD", "Telmans R.M.", 55500)
, (1093, "AB", "de Slegte S.", 46987)
, (1199, "CC", "Uitlaat G.A.S.", 44500)
]
-> [[Employee]]
firstN n o1 o2 x =
fmap
(take n . sortBy (comparingDown o2))
(groupBy (groupingOn o1) (sortBy (comparing o1) x))
groupingOn
:: Eq a1
=>
groupingOn = ((==) `on`)
comparingDown
:: Ord a
=> (b -> a) -> b -> b -> Ordering
comparingDown = flip . comparing
main :: IO ()
main = do
printf "%-16s %3s %10s\n" "NAME" "DEP" "TIP"
putStrLn $ replicate 31 '='
mapM_ (traverse ((printf "%-16s %3s %10.2g\n" . name) <*> dep <*> sal)) $
firstN 3 dep sal employees</syntaxhighlight>
{{out}}
<pre>NAME DEP TIP
===============================
de Bont C.A. AB 65000.00
Line 1,896 ⟶ 2,641:
Ezelbips P.J. KA 52000.00
't Woud B. KA 45000.00
Pragtweik J.M.V. KA 42300.00</pre>
====Data.Map====
{{Works with|GHC|7.10.3}}
Alternatively, if we store the data in key-value maps, rather than in a cons list, we can use Map.lookup and Map.filter, Map.keys and Map.elems, to pull out the data and shape reports fairly flexibly.
<syntaxhighlight lang="haskell">import Data.List (intercalate, nub, sort, sortBy, transpose)
import qualified Data.Map as M
import Data.Maybe (fromJust)
import Data.Ord (comparing)
main :: IO ()
main =
(putStrLn . unlines) $
table
" "
( (reportLines <*> highSalaryKeys 3)
=<< (sort . nub) (M.elems mapDepts)
)
reportLines :: String -> [(Int, Int)] -> [[String]]
reportLines dept =
fmap
( \(k, n) ->
[ fromJust $ M.lookup k mapNames,
dept,
show n
]
)
highSalaryKeys :: Int -> String -> [(Int, Int)]
highSalaryKeys n dept =
take n $
sortBy ((flip . comparing) snd) $
(,) <*> (fromJust . flip M.lookup mapSalaries)
<$> M.keys (M.filter (== dept) mapDepts)
mapNames, mapDepts :: M.Map Int String
[mapNames, mapDepts] =
(readPairs <$> [nameKV, deptKV]) <*> [xs]
mapSalaries :: M.Map Int Int
mapSalaries = readPairs salaryKV xs
xs :: [(Int, String, String, Int)]
xs =
[ (1001, "AB", "Janssen A.H.", 41000),
(101, "KA", "'t Woud B.", 45000),
(1013, "AB", "de Bont C.A.", 65000),
(1101, "CC", "Modaal A.M.J.", 30000),
(1203, "AB", "Anders H.", 50000),
(100, "KA", "Ezelbips P.J.", 52000),
(1102, "CC", "Zagt A.", 33000),
(1103, "CC", "Ternood T.R.", 21000),
(1104, "CC", "Lageln M.", 23000),
(1105, "CC", "Amperwat A.", 19000),
(1106, "CC", "Boon T.J.", 25000),
(1107, "CC", "Beloop L.O.", 31000),
(1009, "CD", "Janszoon A.", 38665),
(1026, "CD", "Janszen H.P.", 41000),
(1011, "CC", "de Goeij J.", 39000),
(106, "KA", "Pragtweik J.M.V.", 42300),
(111, "KA", "Bakeuro S.", 31000),
(105, "KA", "Clubdrager C.", 39800),
(104, "KA", "Karendijk F.", 23000),
(107, "KA", "Centjes R.M.", 34000),
(119, "KA", "Tegenstroom H.L.", 39000),
(1111, "CD", "Telmans R.M.", 55500),
(1093, "AB", "de Slegte S.", 46987),
(1199, "CC", "Uitlaat G.A.S.", 44500)
]
readPairs ::
Ord k =>
(a1 -> (k, a)) ->
[a1] ->
M.Map k a
readPairs f xs = M.fromList (f <$> xs)
nameKV,
deptKV ::
(Int, String, String, Int) -> (Int, String)
nameKV (k, _, name, _) = (k, name)
deptKV (k, dept, _, _) = (k, dept)
salaryKV :: (Int, String, String, Int) -> (Int, Int)
salaryKV (k, _, _, salary) = (k, salary)
table :: String -> [[String]] -> [String]
table delim rows =
let justifyLeft c n s = take n (s <> replicate n c)
justifyRight c n s =
drop
(length s)
(replicate n c <> s)
in intercalate delim
<$> transpose
( ( fmap
=<< justifyLeft ' '
. maximum
. fmap length
)
<$> transpose rows
)</syntaxhighlight>
{{Out}}
<pre>de Bont C.A. AB 65000
Anders H. AB 50000
de Slegte S. AB 46987
Uitlaat G.A.S. CC 44500
de Goeij J. CC 39000
Zagt A. CC 33000
Telmans R.M. CD 55500
Janszen H.P. CD 41000
Janszoon A. CD 38665
Ezelbips P.J. KA 52000
't Woud B. KA 45000
Pragtweik J.M.V. KA 42300</pre>
=={{header|HicEst}}==
<
CHARACTER name*20, employee_ID*10, department*10, temp*10
REAL :: idx(1), N_top_salaries=3
Line 1,929 ⟶ 2,790:
ENDDO
END</
<pre style="height:30ex;overflow:scroll">John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
Line 1,947 ⟶ 2,808:
=={{header|Icon}} and {{header|Unicon}}==
<
procedure getEmployees ()
Line 1,990 ⟶ 2,851:
every show_employee (!employeesInGroup[1:(1+min(N,*employeesInGroup))])
}
end</
<pre style="height:30ex;overflow:scroll">
$ ./top-rank-by-group 2
Line 2,023 ⟶ 2,884:
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190</pre>
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "Employee.bas"(N)
110 LET NR=100:LET X=0
120 STRING NAME$(1 TO NR)*20,ID$(1 TO NR)*6,DEPT$(1 TO NR)*4
130 NUMERIC SALARY(1 TO NR)
140 DO UNTIL N>0 AND N<=NR
150 INPUT PROMPT "Enter the number of ranks: ":N$
160 LET N=VAL(N$)
170 LOOP
180 CALL READDATA
190 CALL SORT
200 CALL LST
210 END
220 DEF READDATA
230 LET EOF=0
240 OPEN #1:"Employee.dat"
250 WHEN EXCEPTION USE IOERROR
260 DO
270 LET X=X+1
280 INPUT #1:NAME$(X),ID$(X),SALARY(X),DEPT$(X)
290 LOOP UNTIL EOF OR X=NR
300 END WHEN
310 HANDLER IOERROR
320 IF EXTYPE<>8001 THEN PRINT EXSTRING$(EXTYPE)
330 CLOSE #1
340 LET X=X-1:LET EOF=-1
350 IF X=0 THEN PRINT "No data.":STOP
360 CONTINUE
370 END HANDLER
380 END DEF
390 DEF SORT
400 LET GAP=X:LET SW=1
410 DO WHILE GAP>1 OR SW
420 LET GAP=MAX(INT(GAP/1.3),1):LET SW=0
430 FOR I=1 TO X-GAP
440 IF DEPT$(I)>DEPT$(I+GAP) OR DEPT$(I)=DEPT$(I+GAP) AND SALARY(I)<SALARY(I+GAP) THEN
450 LET T$=NAME$(I):LET NAME$(I)=NAME$(I+GAP):LET NAME$(I+GAP)=T$
460 LET T$=DEPT$(I):LET DEPT$(I)=DEPT$(I+GAP):LET DEPT$(I+GAP)=T$
470 LET T$=ID$(I):LET ID$(I)=ID$(I+GAP):LET ID$(I+GAP)=T$
480 LET T=SALARY(I):LET SALARY(I)=SALARY(I+GAP):LET SALARY(I+GAP)=T
490 LET SW=1
500 END IF
510 NEXT
520 LOOP
530 END DEF
540 DEF LST
550 LET J=1:LET PREV$=""
560 FOR I=1 TO X
570 IF DEPT$(I)<>PREV$ THEN PRINT "Department ";DEPT$(I):LET PREV$=DEPT$(I):LET J=1
580 IF J<=N THEN PRINT " ";NAME$(I);TAB(23);ID$(I),SALARY(I):LET J=J+1
590 NEXT
600 END DEF</syntaxhighlight>
=={{header|J}}==
J has a rich set of primitive functions, which combine the power of an imperative language with the expressiveness of a declarative, SQL-like language:
<
('`',,;:^:_1: N=:{.Employees) =:, (_&{"1)`'' ([^:(_ -: ])L:0)"0 _~ i.# E =: {: Employees
NB. Show top six ranked employees in each dept
N , (<@:>"1@:|:@:((6 <. #) {. ] \: SALARY)/.~ DEPT) |: <"1&> E</
<pre style="height:30ex;overflow:scroll">
+-----+-----+-----------------+------+
Line 2,091 ⟶ 3,005:
Named as a function where the (maximum) number of employees in each department is a parameter:
<
<pre style="height:30ex;overflow:scroll">
reportTopSalaries 2
Line 2,112 ⟶ 3,026:
=={{header|Java}}==
{{works with|Java|8}}
<
import java.util.*;
Line 2,160 ⟶ 3,074:
});
}
}</
<pre style="height:30ex;overflow:scroll">Department D050
Line 2,179 ⟶ 3,093:
E39876 Claire Buckman 27800 D202
E27002 David Motsinger 19250 D202</pre>
=={{header|JavaScript}}==
===Iterative Solution===
<
{name: "Tyler Bennett", id: "E10297", salary: 32000, dept: "D101"},
{name: "John Rappl", id: "E21437", salary: 47000, dept: "D050"},
Line 2,242 ⟶ 3,155:
top_rank(3);
</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">D101
Line 2,264 ⟶ 3,177:
===
====ES5====
<syntaxhighlight lang="javascript">var collectDept = function (arrOfObj) {
var collect = arrOfObj.reduce(function (rtnObj, obj) {
if (rtnObj[obj.dept] === undefined) {
Line 2,296 ⟶ 3,209:
return list.slice(0,n);
});
};</syntaxhighlight>
====ES6====
By composition of generic functions:
<syntaxhighlight lang="javascript">(() => {
'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)
);
})();</syntaxhighlight>
{{Out}}
<pre>[
"John Rappl,E21437,47000,D050",
"Nathan Adams,E41298,21900,D050",
"George Woltman,E00127,53500,D101",
"David McClellan,E04242,41500,D101",
"Tyler Bennett,E10297,32000,D101",
"Kim Arlich,E10001,57000,D190",
"Timothy Grove,E16398,29900,D190",
"Rich Holcomb,E01234,49500,D202",
"Claire Buckman,E39876,27800,D202",
"David Motsinger,E27002,19250,D202"
]</pre>
=={{header|jq}}==
The task description invites use of the "language-native" data structure, which for jq is JSON, and so the following assumes that the file data.json contains an array of objects, each having as keys the strings on the header line. Thus, the first object in the array looks like this:
<
"Employee Name": "Tyler Bennett",
"Employee ID": "E10297",
"Salary": "32000",
"Department": "D101"
}</
===Program===
<
group_by(.Department)
| reduce .[] as $dept
Line 2,318 ⟶ 3,403:
| ($dept[0] | .Department) as $dept
| . + [ { "Department": $dept, "top_salaries": $max } ] );
</syntaxhighlight>
===Example===
With the above program, the top two salaries in each dapartment can be found as shown in the following transcript:<
$ jq 'top_rank_per_department(2) data.json
[
Line 2,352 ⟶ 3,437:
]
}
]</
=={{header|Jsish}}==
Based on Javascript, imperative solution
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Top rank per group, in Jsish */
function top_rank(n) {
var by_dept = group_by_dept(data);
for (var dept in by_dept) {
puts(dept);
for (var i = 0; i < n && i < by_dept[dept].length; i++) {
var emp = by_dept[dept][i];
puts(emp.name + ", id=" + emp.id + ", salary=" + emp.salary);
}
puts("");
}
}
// 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 ( !by_dept.hasOwnProperty(dept)) {
by_dept[dept] = new Array();
}
by_dept[dept].push(data[idx]);
}
for (var dept in by_dept) {
by_dept[dept].sort(function (a,b) { return b.salary - a.salary; });
}
return by_dept;
}
if (Interp.conf('unitTest')) {
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"}
];
top_rank(3);
}
/*
=!EXPECTSTART!=
D050
John Rappl, id=E21437, salary=47000
Nathan Adams, id=E41298, salary=21900
D101
George Woltman, id=E00127, salary=53500
David McClellan, id=E04242, salary=41500
Tyler Bennett, id=E10297, salary=32000
D190
Kim Arlich, id=E10001, salary=57000
Timothy Grove, id=E16398, salary=29900
D202
Rich Holcomb, id=E01234, salary=49500
Claire Buckman, id=E39876, salary=27800
David Motsinger, id=E27002, salary=19250
=!EXPECTEND!=
*/</syntaxhighlight>
{{out}}
<pre>prompt$ jsish --U topRankPerGroup.jsi
D050
John Rappl, id=E21437, salary=47000
Nathan Adams, id=E41298, salary=21900
D101
George Woltman, id=E00127, salary=53500
David McClellan, id=E04242, salary=41500
Tyler Bennett, id=E10297, salary=32000
D190
Kim Arlich, id=E10001, salary=57000
Timothy Grove, id=E16398, salary=29900
D202
Rich Holcomb, id=E01234, salary=49500
Claire Buckman, id=E39876, salary=27800
David Motsinger, id=E27002, salary=19250
prompt$ jsish -u topRankPerGroup.jsi
[PASS] topRankPerGroup.jsi</pre>
=={{header|Julia}}==
<syntaxhighlight lang="julia"># v0.6.0
using DataFrames
df = DataFrame(
EmployeeName=["Tyler Bennett", "John Rappl", "George Woltman", "Adam Smith",
"Claire Buckman", "David McClellan", "Rich Holcomb", "Nathan Adams",
"Richard Potter", "David Motsinger", "Tim Sampair", "Kim Arlich", "Timothy Grove"],
EmployeeID = ["E10297", "E21437", "E00127", "E63535", "E39876", "E04242",
"E01234", "E41298", "E43128", "E27002", "E03033", "E10001", "E16398"],
Salary = [32000, 47000, 53500, 18000, 27800, 41500, 49500, 21900, 15900, 19250,
27000, 57000, 29900],
Department = ["D101", "D050", "D101", "D202", "D202", "D101", "D202", "D050",
"D101", "D202", "D101", "D190", "D190"])
# To get only values
function firstnby(n::Int, y::Array, by::Array)
# Check that each value belong to one and one only class
if length(y) != length(by); error("y and by must have the same length"); end
# Initialize resulting dictionary
rst = Dict{eltype(by), Array{eltype(y)}}()
# For each class...
for cl in unique(by)
# ...select the values of that class...
i = find(x -> x == cl, by)
# ...sort them and store them in result...
rst[cl] = sort(y[i]; rev=true)
# ...if length is greater than n select only first n elements
if length(i) > n
rst[cl] = rst[cl][1:n]
end
end
return rst
end
for (cl, val) in firstnby(3, Array(df[:Salary]), Array(df[:Department]))
println("$cl => $val")
end
# To get the full row...
function firstnby(n::Int, df::DataFrame, y::Symbol, by::Symbol)
rst = Dict{eltype(df[by]), DataFrame}()
for cl in unique(df[by])
i = find(x -> x == cl, df[by])
rst[cl] = sort(df[i, :]; cols=order(y; rev=true))
if length(i) > n
rst[cl] = rst[cl][1:n, :]
end
end
return rst
end
for (cl, data) in firstnby(3, df, :Salary, :Department)
println("\n$cl:\n$data")
end
</syntaxhighlight>
{{out}}
<pre>D202 => [49500, 27800, 19250]
D101 => [53500, 41500, 32000]
D050 => [47000, 21900]
D190 => [57000, 29900]
D202:
3×4 DataFrames.DataFrame
│ Row │ EmployeeName │ EmployeeID │ Salary │ Department │
├─────┼───────────────────┼────────────┼────────┼────────────┤
│ 1 │ "Rich Holcomb" │ "E01234" │ 49500 │ "D202" │
│ 2 │ "Claire Buckman" │ "E39876" │ 27800 │ "D202" │
│ 3 │ "David Motsinger" │ "E27002" │ 19250 │ "D202" │
D101:
3×4 DataFrames.DataFrame
│ Row │ EmployeeName │ EmployeeID │ Salary │ Department │
├─────┼───────────────────┼────────────┼────────┼────────────┤
│ 1 │ "George Woltman" │ "E00127" │ 53500 │ "D101" │
│ 2 │ "David McClellan" │ "E04242" │ 41500 │ "D101" │
│ 3 │ "Tyler Bennett" │ "E10297" │ 32000 │ "D101" │
D050:
2×4 DataFrames.DataFrame
│ Row │ EmployeeName │ EmployeeID │ Salary │ Department │
├─────┼────────────────┼────────────┼────────┼────────────┤
│ 1 │ "John Rappl" │ "E21437" │ 47000 │ "D050" │
│ 2 │ "Nathan Adams" │ "E41298" │ 21900 │ "D050" │
D190:
2×4 DataFrames.DataFrame
│ Row │ EmployeeName │ EmployeeID │ Salary │ Department │
├─────┼─────────────────┼────────────┼────────┼────────────┤
│ 1 │ "Kim Arlich" │ "E10001" │ 57000 │ "D190" │
│ 2 │ "Timothy Grove" │ "E16398" │ 29900 │ "D190" │
</pre>
=={{header|Kotlin}}==
<
data class Employee(val name: String, val id: String, val salary: Int, val dept: String)
Line 2,385 ⟶ 3,666:
println()
}
}</
{{out}}
Line 2,407 ⟶ 3,688:
Claire Buckman E39876 27800
</pre>
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
exec 2> /tmp/Top_rank_per_group.err
# Top rank per group
# # Variables:
#
integer TOP_NUM=2
typeset -T Empsal_t=(
typeset -h 'Employee Name' ename=''
typeset -h 'Employee ID' eid=''
typeset -i -h 'Employee Salary' esalary
typeset -h 'Emplyee Department' edept=''
function init_employee {
typeset buff ; buff="$1"
typeset oldIFS ; oldIFS="$IFS"
typeset arr ; typeset -a arr
IFS=\,
arr=( ${buff} )
_.ename="${arr[0]}"
_.eid="${arr[1]}"
_.esalary=${arr[2]}
_.edept="${arr[3]}"
IFS="${oldIFS}"
}
)
edata='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'
######
# main #
######
# # Employee data into array of Types
#
typeset -a empsal_t # array of Type variables (objects)
integer j i=0
echo "${edata}" | while read; do
Empsal_t empsal_t[i] # Create Type (object)
empsal_t[i++].init_employee "$REPLY" # Initialize Type (object)
done
# # Sort the array of Type variables
#
set -a -s -A empsal_t -K edept,esalary:n:r,ename
# # BUG work around! duplicate the now sorted Type array and use it for output
#
sorted=$(typeset -p empsal_t) && sorted=${sorted/empsal_t/sorted} && eval ${sorted}
for ((i=0; i<${#sorted[*]}; i++)); do
if [[ ${sorted[i].edept} != ${prevdept} ]] || (( j < TOP_NUM )); then
[[ ${sorted[i].edept} != ${prevdept} ]] && j=0
print "${sorted[i].edept} ${sorted[i].esalary} ${sorted[i].eid} ${sorted[i].ename}"
prevdept=${sorted[i].edept}
(( j++ ))
fi
done</syntaxhighlight>
{{out}}<pre>
D050 47000 E21437 John Rappl
D050 21900 E41298 Nathan Adams
D101 53500 E00127 George Woltman
D101 41500 E04242 David McClellan
D190 57000 E10001 Kim Arlich
D190 29900 E16398 Timothy Grove
D202 49500 E01234 Rich Holcomb
D202 27800 E39876 Claire Buckman</pre>
=={{header|Lua}}==
<
lst = { { "Tyler Bennett","E10297",32000,"D101" },
}
Line 2,429 ⟶ 3,797:
for i = 1, #lst do
if dep[ lst[i][4] ] == nil then
else
end
end
Line 2,438 ⟶ 3,806:
for i, _ in pairs( dep ) do
table.sort( dep[i], function (a,b) return a[3] > b[3] end )
print( "Department:", dep[i][1][4] )
for l = 1, math.min( N, #dep[i] ) do
end
print ""
end</
<pre style="height:30ex;overflow:scroll">Department:
Department:
Department:
Department: D101
George Woltman E00127 53500
David McClellan E04242 41500</pre>
=== Object-Oriented ===
<syntaxhighlight lang="lua">--Employee Class
local EmployeeMethods = {
}
local EmployeeMetamethods = {
__index=EmployeeMethods,
__tostring=function(self)
return ("%s %s %s %s"):format(self.Name,self.EmployeeId,self.Salary,self.DepartmentName)
end,
__metatable="Locked",
}
local EmployeeBase = {}
EmployeeBase.new = function(Name,EmployeeId,Salary,DepartmentName)
return setmetatable({
Name=Name,
EmployeeId=EmployeeId,
Salary=Salary,
DepartmentName=DepartmentName,
},EmployeeMetamethods)
end
--Department Class
local DepartmentMethods = {
NewEmployee=function(self,Employee)
table.insert(self.__Employees,Employee)
end,
CalculateHighestSalaries=function(self,Amount)
local Highest = {}
local EL = #self.__Employees
table.sort(self.__Employees,function(a,b)
return a.Salary > b.Salary
end)
for i=1,Amount do
if i>EL then
break
end
table.insert(Highest,self.__Employees[i])
end
return Highest
end,
}
local DepartmentMetamethods = {
__index=DepartmentMethods,
__tostring=function(self)
return ("Department %s"):format(self.Name)
end,
__metatable="Locked",
}
local DepartmentBase = {
__Departments={},
}
DepartmentBase.new = function(Name)
local Department = DepartmentBase.__Departments[Name]
if Department then return Department end
Department = setmetatable({
__Employees={},
Name=Name,
},DepartmentMetamethods)
DepartmentBase.__Departments[Name] = Department
return Department
end
--Main Program
local Employees = {
EmployeeBase.new("Tyler Bennett","E10297",32000,"D101"),
EmployeeBase.new("John Rappl","E21437",47000,"D050"),
EmployeeBase.new("George Woltman","E00127",53500,"D101"),
EmployeeBase.new("Adam Smith","E63535",18000,"D202"),
EmployeeBase.new("Claire Buckman","E39876",27800,"D202"),
EmployeeBase.new("David McClellan","E04242",41500,"D101"),
EmployeeBase.new("Rich Holcomb","E01234",49500,"D202"),
EmployeeBase.new("Nathan Adams","E41298",21900,"D050"),
EmployeeBase.new("Richard Potter","E43128",15900,"D101"),
EmployeeBase.new("David Motsinger","E27002",19250,"D202"),
EmployeeBase.new("Tim Sampair","E03033",27000,"D101"),
EmployeeBase.new("Kim Arlich","E10001",57000,"D190"),
EmployeeBase.new("Timothy Grove","E16398",29900,"D190"),
}
for _,Employee in next,Employees do
local Department = DepartmentBase.new(Employee.DepartmentName)
Department:NewEmployee(Employee)
end
for _,Department in next,DepartmentBase.__Departments do
local Highest = Department:CalculateHighestSalaries(2)
print(Department)
for _,Employee in next,Highest do
print("\t"..tostring(Employee))
end
end</syntaxhighlight>
{{out}}
<pre>
Department D050
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
Department D101
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
Department D190
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
Department D202
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
' erase stack of values, so we can add data
Flush
Input "N=",N
Enum Departments {D050,D101,D190,D202}
\\ Inventory Department need unique keys
Inventory Department
\\ each item in this inventory should be an inventory too
Class Empl {
name$, id$, salary
Class:
Module Empl(.name$, .id$, .salary) {}
}
Data "Tyler Bennett","E10297",32000,D101
Data "John Rappl","E21437",47000,D050
Data "George Woltman","E00127",53500,D101
Data "Adam Smith","E63535",18000,D202
Data "Claire Buckman","E39876",27800,D202
Data "David McClellan","E04242",41500,D101
Data "Rich Holcomb","E01234",49500,D202
Data "Nathan Adams","E41298",21900,D050
Data "Richard Potter","E43128",15900,D101
Data "David Motsinger","E27002",19250,D202
Data "Tim Sampair","E03033",27000,D101
Data "Kim Arlich","E10001",57000,D190
Data "Timothy Grove","E16398",29900,D190
Data ""
Read name$
While name$<>"" {
Read id$, salary, dep
Rem : Print name$, id$, salary, dep
If Exist(Department, dep) Then {
z=Eval(Department) ' get pointer to inventory
AppendOne()
} Else {
z=queue
AppendDep()
AppendOne()
}
Read name$
}
Sort Department as number
i=each(Department)
\\ make depname as type of Departments
depname=D050
Print "Dep. Employee Name Emp. ID Salary"
While i {
\\ when we pass a number to a enum variable
\\ if the number exist, get that enum item else raise error
depname=val(eval$(i, i^))
\\ z is a pointer to inventory
z=Eval(i)
Sort descending z as number
k=each(z,1,N)
While k {
Empl=Eval(k)
For Empl {
\\ eval$(depname) return the name of enum variable (like D050)
Print Format$("{0:6}{1:20}{2:8}{3::-8}",Eval$(depname), .name$, .id$, .salary)
}
}
}
Print "Done"
Sub AppendDep()
Append Department, dep:=z
End Sub
Sub AppendOne()
Append z, salary:=Empl(name$, id$, salary)
End Sub
}
Checkit
</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
N=1
Dep. Employee Name Emp. ID Salary
D050 John Rappl E21437 47000
D101 George Woltman E00127 53500
D190 Kim Arlich E10001 57000
D202 Rich Holcomb E01234 49500
Done
N=2
Dep. Employee Name Emp. ID Salary
D050 John Rappl E21437 47000
D050 Nathan Adams E41298 21900
D101 George Woltman E00127 53500
D101 David McClellan E04242 41500
D190 Kim Arlich E10001 57000
D190 Timothy Grove E16398 29900
D202 Rich Holcomb E01234 49500
D202 Claire Buckman E39876 27800
Done
</pre >
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{"John Rappl","E21437",47000,"D050"},{"George Woltman","E00127",53500,"D101"},
{"Adam Smith","E63535",18000,"D202"},{"Claire Buckman","E39876",27800,"D202"},
Line 2,475 ⟶ 4,052:
Scan[((Print["Department ",#[[1,4]],"\n","Employee","\t","Id","\t","Salary"]&[#])&[#];
(Scan[Print[#[[1]],"\t",#[[2]],"\t",#[[3]]]&,#] )& [#])&,TrimmedList]</
{{out}}
<pre style="height:30ex;overflow:scroll"><pre>Department D101
Employee
George Woltman
David McClellan
Tyler Bennett
Department D050
Employee
John Rappl
Nathan Adams
Department D202
Employee
Rich Holcomb
Claire Buckman
David Motsinger
Department D190
Employee
Kim Arlich
Timothy Grove
=={{header|Nim}}==
{{trans|C}}
<
type Record = tuple[name, id: string
var people
("
("
("
("
("
("
("
("
("
("
("
proc pcmp(a, b: Record): int =
result = cmp(a.department, b.department)
if result
result = cmp(b.salary, a.salary)
proc printTop(people: openArray[Record]; n: Positive) =
var people = sorted(people, pcmp)
var rank = 0
for i, p in people:
if i > 0 and p.department != people[i-1].department:
rank = 0
echo
if rank < n:
echo p.department, " ", p.salary, " ", p.name
inc rank
people.printTop(2)</syntaxhighlight>
{{out}}
<pre>D050 47000 John Rappl
D050 21900 Nathan Adams
Line 2,550 ⟶ 4,125:
=={{header|OCaml}}==
<
let to_string (name,_,s,_) = (Printf.sprintf "%s (%d)" name s)
Line 2,599 ⟶ 4,174:
let () =
toprank data 3;
;;</
<pre style="height:30ex;overflow:scroll">Department: D190
Kim Arlich (57000), Timothy Grove (29900)
Line 2,620 ⟶ 4,195:
Then sort each department by salaries and keep only the first n elements.
<
Employee method: initialize := dep := salary := id := name ;
Line 2,646 ⟶ 4,221:
#dep employees sortBy groupWith( #dep )
map(#[ sortBy(#[ salary neg ]) left(n) ]) apply(#println) ; </
{{out}}
Line 2,658 ⟶ 4,233:
=={{header|Oz}}==
<
%% Create a list of employee records.
Data = {Map
Line 2,682 ⟶ 4,257:
{Record.map {GroupBy Employees department}
fun {$ Employees}
end}
end
Line 2,703 ⟶ 4,278:
end
in
{Inspect {TopEarners Data 3}}</
=={{header|PARI/GP}}==
{{PARI/GP select}}
<
["John Rappl","E21437",47000,"D050"],
["George Woltman","E00127",53500,"D101"],
Line 2,734 ⟶ 4,309:
};
top(2,V)</
=={{header|Pascal}}==
{{works with|Free_Pascal}}
{{libheader|Classes}} {{libheader|Math}}
<
uses
Line 2,747 ⟶ 4,322:
TData = record
name: string;
PTData = ^TData;
Line 2,808 ⟶ 4,383:
end;
end.
</
<pre style="height:30ex;overflow:scroll">% ./TopRankPerGroup
Enter the number of ranks: 3
Line 2,831 ⟶ 4,406:
=={{header|Perl}}==
<
my @a = @{shift()};
my @b = @{shift()};
Line 2,863 ⟶ 4,438:
EOF
my $N = shift || 3;
foreach my $d (
print "$d\n";
my @es =
Line 2,878 ⟶ 4,452:
}
print "\n";
}</
{{out}}
<pre>D050
John Rappl | E21437 | 47000
Nathan Adams | E41298 | 21900
D101
George Woltman | E00127 | 53500
David McClellan | E04242 | 41500
Tyler Bennett | E10297 | 32000
D190
Kim Arlich | E10001 | 57000
Timothy Grove | E16398 | 29900
D202
Rich Holcomb | E01234 | 49500
Claire Buckman | E39876 | 27800
David Motsinger | E27002 | 19250</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span>
<span style="color: #000080;font-style:italic;">-- Employee Name,Employee ID,Salary,Department</span>
<span style="color: #008080;">enum</span> <span style="color: #000080;font-style:italic;">/*NAME,*/ /*ID,*/</span> <span style="color: #000000;">SAL</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DEPT</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">employees</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"Tyler Bennett"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E10297"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"John Rappl"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E21437"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">47000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D050"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"George Woltman"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E00127"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">53500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Adam Smith"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E63535"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Claire Buckman"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E39876"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">27800</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"David McClellan"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"E04242"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">41500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Rich Holcomb"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E01234"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Nathan Adams"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E41298"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21900</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D050"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Richard Potter"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E43128"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15900</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"David Motsinger"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"E27002"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19250</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Tim Sampair"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E03033"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">27000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Kim Arlich"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E10001"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">57000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D190"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Timothy Grove"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E16398"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29900</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D190"</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">by_dept_sal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]&-</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">SAL</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]&-</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">SAL</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">by_dept_sal</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">)))</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">lastdep</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dcount</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Top %d salaries by department\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">N</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">emp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">emp</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">lastdep</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">lastdep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">emp</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">dcount</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">dcount</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dcount</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">N</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">emp</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
Line 2,993 ⟶ 4,537:
=={{header|PHP}}==
<
);
function top_sal($num){
}
}
}
}
}
}
}
top_sal(3);</
<pre style="height:30ex;overflow:scroll">D050
Name
John Rappl
Nathan Adams
D101
Name
George Woltman
David McClellan
Tyler Bennett
D190
Name
Kim Arlich
Timothy Grove
D202
Name
Rich Holcomb
Claire Buckman
David Motsinger
</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
Emp = [
% 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"]
],
print_top_ranks(Emp,3),
nl.
print_top_ranks(Emp,N) =>
printf("Top %d ranks per department:\n", N),
foreach(Dept in [Dept : [_,_,_,Dept] in Emp].sort_remove_dups())
printf("Department %s\n", Dept),
Emp2 = sort_down([[Salary,Name] : [Name,_,Salary,D] in Emp, D = Dept]),
foreach({[Salary,Name],E} in zip(Emp2,1..Emp2.length), E <= N)
printf("%-20s %-10s\n", Name, Salary)
end,
nl
end.</syntaxhighlight>
{{out}}
<pre>Top 3 ranks per department:
Department D050
John Rappl 47000
Nathan Adams 21900
Department D101
George Woltman 53500
David McClellan 41500
Tyler Bennett 32000
Department D190
Kim Arlich 57000
Timothy Grove 29900
Department D202
Rich Holcomb 49500
Claire Buckman 27800
David Motsinger 19250</pre>
===Using data as facts===
<syntaxhighlight lang="picat">go2 =>
Emp = findall([Name,ID,Salary,Dept],emp(Name,ID,Salary,Dept)),
print_top_ranks(Emp,3),
nl.
emp("Tyler Bennett","E10297","32000","D101").
emp("John Rappl","E21437","47000","D050").
emp("George Woltman","E00127","53500","D101").
emp("Adam Smith","E63535","18000","D202").
emp("Claire Buckman","E39876","27800","D202").
emp("David McClellan","E04242","41500","D101").
emp("Rich Holcomb","E01234","49500","D202").
emp("Nathan Adams","E41298","21900","D050").
emp("Richard Potter","E43128","15900","D101").
emp("David Motsinger","E27002","19250","D202").
emp("Tim Sampair","E03033","27000","D101").
emp("Kim Arlich","E10001","57000","D190").
emp("Timothy Grove","E16398","29900","D190").</syntaxhighlight>
=={{header|PicoLisp}}==
<
(de *Employees
("Tyler Bennett" E10297 32000 D101)
Line 3,091 ⟶ 4,707:
(prinl) ) ) )
(topEmployees 3)</
<pre style="height:30ex;overflow:scroll">Department D101:
Name ID Salary
Line 3,115 ⟶ 4,731:
=={{header|PL/I}}==
<
rank: procedure options (main); /* 10 November 2013 */
Line 3,197 ⟶ 4,813:
end;
put skip list ('FINISHED');
end rank;</
<pre style="height:30ex;overflow:scroll">How many highest-paid employees do you want?
Line 3,233 ⟶ 4,849:
=={{header|PL/SQL}}==
<
cursor CSR_EMP(TOP_N pls_integer) is
select case LINE
Line 3,340 ⟶ 4,956:
DBMS_OUTPUT.PUT_LINE(v_emp."Top rank per group");
end loop;
end;</
=={{header|PowerShell}}==
<
New-Object PSObject `
| Add-Member -PassThru NoteProperty EmployeeName $Name `
Line 3,373 ⟶ 4,990:
} `
| Format-Table -GroupBy Department
}</
<pre style="height:30ex;overflow:scroll">PS> Get-TopRank 2
Line 3,405 ⟶ 5,022:
=={{header|Prolog}}==
<
emp('Tyler Bennett','E10297',32000,'D101').
emp('John Rappl','E21437',47000,'D050').
Line 3,457 ⟶ 5,074:
writef(' ID: %w\t%w\tSalary: %w\n', [Id,Name,Sal]),
fail.
topDeps(_).</
<pre style="height:30ex;overflow:scroll">?- topDeps(2).
Department: D101
ID: E00127
ID: E04242
Department: D050
ID: E21437
ID: E41298
Department: D202
ID: E01234
ID: E39876
Department: D190
ID: E10001
ID: E16398
true.
</pre>
=={{header|PureBasic}}==
<
Name$
ID$
Line 3,532 ⟶ 5,149:
NewList MyEmployees.Employees()
displayTopEarners(MyEmployees(), 3)</
'''Save this as 'DataFile.txt and let the program open this file'''<pre>
Line 3,582 ⟶ 5,199:
=={{header|Python}}==
Python 2.7/3.x compatible.
<
from heapq import nlargest
Line 3,611 ⟶ 5,228:
for rec in nlargest(N, recs, key=lambda rec: rec[-2]):
print (format % rec)
print('')</
<pre style="height:30ex;overflow:scroll">Department D050
Employee Name Employee ID Salary Department
Line 3,639 ⟶ 5,256:
Uses namedtuples for database records, and groupby builtin to group records by Department:
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">from collections import namedtuple
from itertools import groupby
Line 3,661 ⟶ 5,279:
rows = db.split('\n')
DBRecord = namedtuple('DBRecord', rows[0].replace(' ', '_'))
records = [DBRecord(*row.split(',')) for row in rows[1:]]
records.sort(key=lambda record: (record.Department, -float(record.Salary)))
print('\n\n'.join(
'\n '.join([dpt] + [str(g) for g in grp][:N])
for dpt, grp in groupby(
records,
lambda record: record.Department
)
))</syntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">D050
DBRecord(Employee_Name='John Rappl', Employee_ID='E21437', Salary='47000', Department='D050')
Line 3,684 ⟶ 5,309:
=={{header|R}}==
First, read in the data.
<
"Employee Name,Employee ID,Salary,Department
Tyler Bennett,E10297,32000,D101
Line 3,698 ⟶ 5,323:
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190")); close(tc)</
To just return the top salary, it's very simple using tapply.
<
To return N salaries, we replace max with our own function.
<
{
with(dfr, tapply(Salary, Department,
Line 3,714 ⟶ 5,339:
}
get.top.N.salaries(3)</
$D050
[1] 47000 21900
Line 3,728 ⟶ 5,353:
To return the whole record for each of the top salaries, a different tack is required.
<
{
#Sort data frame by Department, then by Salary
Line 3,742 ⟶ 5,367:
})
}
get.top.N.salaries2(3)</
<pre style="height:30ex;overflow:scroll"> $D050
Employee.Name Employee.ID Salary Department
Line 3,764 ⟶ 5,389:
5 Claire Buckman E39876 27800 D202
10 David Motsinger E27002 19250 D202</pre>
=== With dplyr ===
With the dplyr package we can use group_by and top_n to select the top n records in each group.
<syntaxhighlight lang="r">
library(dplyr)
dfr %>%
group_by(Department) %>%
top_n(2, Salary)
</syntaxhighlight>
Provides:
<pre style="height:30ex;overflow:scroll">
Employee.Name Employee.ID Salary Department
<fct> <fct> <int> <fct>
John Rappl E21437 47000 D050
George Woltman E00127 53500 D101
Claire Buckman E39876 27800 D202
David McClellan E04242 41500 D101
Rich Holcomb E01234 49500 D202
Nathan Adams E41298 21900 D050
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
</pre>
=={{header|Racket}}==
<
(struct employee (name id salary dept))
Line 3,797 ⟶ 5,445:
(employee-name e)
(employee-id e))))
</
<pre style="height:30ex;overflow:scroll">Department D101:
$53500: George Woltman (E00127)
Line 3,810 ⟶ 5,458:
$57000: Kim Arlich (E10001)
$29900: Timothy Grove (E16398)</pre>
=={{header|Raku}}==
(formerly Perl 6)
We use whitespace-separated fields here from a heredoc; <tt>q:to/---/</tt> begins the heredoc. The <tt>Z=></tt> operator zips two lists into a list of pairs.
In <tt>MAIN</tt>, the <tt>classify</tt> method generates pairs where each key is a different department, and each value all the entries in that department. We then sort the pairs and process each department separately. Within each department, we sort on salary (negated to reverse the order). The last statement is essentially a list comprehension that uses a slice subscript with the <tt>^</tt> "up to" operator to take the first N elements of the sorted employee list. The <tt>:v</tt> modifier returns only valid values. The <tt>.<Name></tt> form is a slice hash subscript with literals strings. That in turn is just the subscript form of the <tt><...></tt> ("quote words") form, which is more familar to Perl 5 programmers as
<tt>qw/.../</tt>. We used that form earlier to label the initial data set.
This program also makes heavy use of method calls that start with dot. In Raku this means a method call on the current topic, <tt>$_</tt>, which is automatically set by any <tt>for</tt> or <tt>map</tt> construct that doesn't declare an explicit formal parameter on its closure.
<syntaxhighlight lang="raku" line>my @data = do for q:to/---/.lines -> $line {
E10297 32000 D101 Tyler Bennett
E21437 47000 D050 John Rappl
E00127 53500 D101 George Woltman
E63535 18000 D202 Adam Smith
E39876 27800 D202 Claire Buckman
E04242 41500 D101 David McClellan
E01234 49500 D202 Rich Holcomb
E41298 21900 D050 Nathan Adams
E43128 15900 D101 Richard Potter
E27002 19250 D202 David Motsinger
E03033 27000 D101 Tim Sampair
E10001 57000 D190 Kim Arlich
E16398 29900 D190 Timothy Grove
---
$%( < Id Salary Dept Name >
Z=>
$line.split(/ \s\s+ /)
)
}
sub MAIN(Int $N = 3) {
for @data.classify({ .<Dept> }).sort».value {
my @es = .sort: { -.<Salary> }
say '' if (state $bline)++;
say .< Dept Id Salary Name > for @es[^$N]:v;
}
}</syntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
D050 E21437 47000 John Rappl
D050 E41298 21900 Nathan Adams
D101 E00127 53500 George Woltman
D101 E04242 41500 David McClellan
D101 E10297 32000 Tyler Bennett
D190 E10001 57000 Kim Arlich
D190 E16398 29900 Timothy Grove
D202 E01234 49500 Rich Holcomb
D202 E39876 27800 Claire Buckman
D202 E27002 19250 David Motsinger</pre>
=={{header|REXX}}==
===version 1===
<
parse arg topN . /*get optional # for the top N salaries*/
if topN=='' | topN=="," then topN=
say 'Finding the top ' topN " salaries in each department."; say
@.= /*════════ employee name ID salary dept. ═══════ */
@.1 = "Tyler Bennett ,E10297, 32000, D101"
@.2 = "John Rappl ,E21437, 47000, D050"
@.3 = "George Woltman ,E00127, 53500, D101"
@.4 = "Adam Smith ,E63535, 18000, D202"
@.5 = "Claire Buckman ,E39876, 27800, D202"
@.6 = "David McClellan ,E04242, 41500, D101"
@.7 = "Rich Holcomb ,E01234, 49500, D202"
@.8 = "Nathan Adams ,E41298, 21900, D050"
@.9 = "Richard Potter ,E43128, 15900, D101"
@.10 = "David Motsinger ,E27002, 19250, D202"
@.11 = "Tim Sampair ,E03033, 27000, D101"
@.12 = "Kim Arlich ,E10001, 57000, D190"
@.13 = "Timothy Grove ,E16398, 29900, D190"
depts= /*build people database from @ array.*/
if
employees= j-1
say 'There are ' employees "employees, " words(depts) 'departments: ' depts
say
if h==0 then iterate
say 'department: ' dept.h " $" || sal.h+0 id.h space(name.h)
dept.h=
end /*topN*/
end
{{out|output|text= when using the input: <tt> 2 </tt>}}
<pre>
Finding the top 2 salaries in each department.
Line 3,872 ⟶ 5,569:
department: D190 $29900 E16398 Timothy Grove
</pre>
{{out|output|text= when using the input: <tt> 100 </tt>}}
<pre>
Finding the top 100 salaries in each department.
Line 3,898 ⟶ 5,596:
===version 2===
<
* 12.02.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 3,972 ⟶ 5,670:
exit: Say arg(1)
help: Say 'Syntax: rexx topsal [topn]'
Exit</
'''output'''
<pre>13 employees, 4 departments: D101 D050 D202 D190
Line 3,989 ⟶ 5,687:
department: D190 $57000 E10001 Kim Arlich
department: D190 $29900 E16398 Timothy Grove</pre>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Top rank per group
load "stdlib.ring"
salary = "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"
temp = substr(salary, ",", nl)
temp = str2list(temp)
depsal = newlist(13,4)
for n = 1 to len(temp)
n1 = ceil(n/4)
n2 = n%4
if n2 = 0
n2 = 4
ok
depsal[n1][n2] = temp[n]
next
for n = 1 to len(depsal)-1
for m = n+1 to len(depsal)
if strcmp(depsal[m][4], depsal[n][4]) < 0
tmp = depsal[n]
depsal[n] = depsal[m]
depsal[m] = tmp
ok
next
next
for n = 1 to len(depsal)-1
for m = n+1 to len(depsal)
if (depsal[m][4] = depsal[n][4]) and (depsal[m][3] > depsal[n][3])
tmp = depsal[n]
depsal[n] = depsal[m]
depsal[m] = tmp
ok
next
next
see "Department : " + depsal[1][4] + nl
see "Name " + "Id " + "Salary" + nl + nl
see "" + depsal[1][1] + " " + depsal[1][2] + " " + depsal[1][3]+ nl
for n = 1 to len(depsal)-1
if (depsal[n+1][4] != depsal[n][4])
see nl
see "Department : " + depsal[n+1][4] + nl
see "Name " + "Id " + "Salary" + nl + nl
see "" + depsal[n+1][1] + " " + depsal[n+1][2] + " " + depsal[n+1][3]+ nl
else
see "" + depsal[n+1][1] + " " + depsal[n+1][2] + " " + depsal[n+1][3]+ nl
ok
next
</syntaxhighlight>
Output:
<pre>
Department : D050
Name Id Salary
John Rappl E21437 47000
Nathan Adams E41298 21900
Department : D101
Name Id Salary
George Woltman E00127 53500
David McClellan E04242 41500
Tyler Bennett E10297 32000
Tim Sampair E03033 27000
Richard Potter E43128 15900
Department : D190
Name Id Salary
Kim Arlich E10001 57000
Timothy Grove E16398 29900
Department : D202
Name Id Salary
Rich Holcomb E01234 49500
Claire Buckman E39876 27800
David Motsinger E27002 19250
Adam Smith E63535 18000
</pre>
=={{header|Ruby}}==
Without much thought to report formatting:
{{works with|Ruby|2.2+}}
<
data = <<EOS
Line 4,025 ⟶ 5,817:
end
show_top_salaries_per_group(data, 3)</
{{out}}
Line 4,049 ⟶ 5,841:
=={{header|Run BASIC}}==
<
John Rappl,E21437,47000,D050;
George Woltman,E00127,53500,D101
Line 4,087 ⟶ 5,879:
end if
print word$(depSal$(i),1,",");chr$(9);word$(depSal$(i),2,",");chr$(9);word$(depSal$(i),3,",")
next i</
<pre style="height:30ex;overflow:scroll">Employee Name
Department:D050
Nathan Adams
Department:D050;
John Rappl
Department:D101
Richard Potter
Tim Sampair
Tyler Bennett
David McClellan
George Woltman
Department:D190
Timothy Grove
Kim Arlich
Department:D202
David Motsinger
Claire Buckman
Rich Holcomb
Department:D202;
Adam Smith
=={{header|Rust}}==
The implementation counts with ties.
<syntaxhighlight lang="rust">#[derive(Debug)]
struct Employee<S> {
// Allow S to be any suitable string representation
id: S,
name: S,
department: S,
salary: u32,
}
impl<S> Employee<S> {
fn new(name: S, id: S, salary: u32, department: S) -> Self {
Self {
id,
name,
department,
salary,
}
}
}
#[rustfmt::skip]
fn load_data() -> Vec<Employee<&'static str>> {
vec![
Employee::new("Tyler Bennett", "E10297", 32000, "D101"),
Employee::new("John Rappl", "E21437", 47000, "D050"),
Employee::new("George Woltman", "E00127", 53500, "D101"),
Employee::new("Adam Smith", "E63535", 18000, "D202"),
Employee::new("Claire Buckman", "E39876", 27800, "D202"),
Employee::new("David McClellan", "E04242", 41500, "D101"),
Employee::new("Rich Holcomb", "E01234", 49500, "D202"),
Employee::new("Nathan Adams", "E41298", 21900, "D050"),
Employee::new("Richard Potter", "E43128", 15900, "D101"),
Employee::new("David Motsinger", "E27002", 19250, "D202"),
Employee::new("Tim Sampair", "E03033", 27000, "D101"),
Employee::new("Kim Arlich", "E10001", 57000, "D190"),
Employee::new("Timothy Grove", "E16398", 29900, "D190"),
// Added to demonstrate various tie situations
Employee::new("Kim Tie", "E16400", 57000, "D190"),
Employee::new("Timothy Tie", "E16401", 29900, "D190"),
Employee::new("Timothy Kim", "E16401", 19900, "D190"),
]
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let n = {
println!("How many top salaries to list? ");
let mut buf = String::new();
std::io::stdin().read_line(&mut buf)?;
buf.trim().parse::<u32>()?
};
let mut employees = load_data();
// Reverse order, then just pick top N employees
employees.sort_by(|a, b| b.salary.cmp(&a.salary));
let sorted = employees
.into_iter()
.fold(std::collections::BTreeMap::new(), |mut acc, next| {
// We store the number of unique salaries as well to handle
// ties (and list always all employees with the same salary)
let mut bucket = acc
.entry(next.department)
.or_insert_with(|| (0, Vec::<Employee<_>>::new()));
match bucket.1.last().map(|e| e.salary) {
Some(last_salary) if last_salary == next.salary => {
if bucket.0 <= n {
bucket.1.push(next);
}
}
_ => {
if bucket.0 < n {
bucket.0 += 1; // Next unique salary
bucket.1.push(next);
}
}
}
acc
});
for (department, (_, employees)) in sorted {
println!("{}", department);
employees
.iter()
.for_each(|employee| println!(" {:?}", employee));
}
Ok(())
}</syntaxhighlight>
{{out}}
<pre>How many top salaries to list?
3
D050
Employee { id: "E21437", name: "John Rappl", department: "D050", salary: 47000 }
Employee { id: "E41298", name: "Nathan Adams", department: "D050", salary: 21900 }
D101
Employee { id: "E00127", name: "George Woltman", department: "D101", salary: 53500 }
Employee { id: "E04242", name: "David McClellan", department: "D101", salary: 41500 }
Employee { id: "E10297", name: "Tyler Bennett", department: "D101", salary: 32000 }
D190
Employee { id: "E10001", name: "Kim Arlich", department: "D190", salary: 57000 }
Employee { id: "E16400", name: "Kim Tie", department: "D190", salary: 57000 }
Employee { id: "E16398", name: "Timothy Grove", department: "D190", salary: 29900 }
Employee { id: "E16401", name: "Timothy Tie", department: "D190", salary: 29900 }
Employee { id: "E16401", name: "Timothy Kim", department: "D190", salary: 19900 }
D202
Employee { id: "E01234", name: "Rich Holcomb", department: "D202", salary: 49500 }
Employee { id: "E39876", name: "Claire Buckman", department: "D202", salary: 27800 }
Employee { id: "E27002", name: "David Motsinger", department: "D202", salary: 19250 }
</pre>
=={{header|Scala}}==
===Application idiomatic version===
<
import scala.language.implicitConversions
import scala.language.reflectiveCalls
Line 4,126 ⟶ 6,037:
val rawData = """Employee Name;Employee ID;Salary;Department
class Employee(name: String, id: String,
Line 4,174 ⟶ 6,085:
.map(_.toString).mkString("\t", "\n\t", ""))
}
}</
<pre style="height:30ex;overflow:scroll">Reporting top 3 salaries in each department.
Line 4,181 ⟶ 6,092:
Department: D190 pop: 2 avg: 43450,00
Department: D050 pop: 2 avg: 34450,00
Department: D101 pop: 5 avg: 33980,00
Department: D202 pop: 4 avg: 28637,50
</pre>
===using SLICK ORM===
{{libheader|H2 Database Engine}} Version 1.3.1
<
import scala.slick.lifted.ProvenShape
Line 4,348 ⟶ 6,259:
plainQuery.foreach(println(_))
} // session
} // TopNrankSLICK</
{{out}}
<pre style="height:30ex;overflow:scroll">Tot.15 Employees in 4 deps.Avg salary: 33336.67
Line 4,380 ⟶ 6,291:
E27002 David Motsinger 19250.00 3
</pre>
=={{header|Scheme}}==
{{works with|Gauche Scheme}}
<
;; This will let us treat a list as though it is a structure (record).
Line 4,440 ⟶ 6,350:
(for-each
(pa$ print-record col-widths)
(take* matches n))))))</
<b>Testing:</b>
<pre>
Line 4,485 ⟶ 6,395:
=={{header|Sidef}}==
{{trans|Perl}}
<
Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
Line 4,511 ⟶ 6,421:
}
print "\n"
}</
{{out}}
<pre>
Line 4,537 ⟶ 6,447:
The following [[SMEQL]] example returns the top 6 earners in each department based on this table schema:
<
----------------
empID
dept
empName
salary</
Source Code:
<
top = group(srt, [(dept) dept2, max(order) order])
join(srt, top, a.dept=b.dept2 and b.order - a.order < 6)</
Note: Since SMEQL is a query language, it has no built-in I/O capability to prompt the user for the threshold number ("6" in this example). However, it would be possible to retrieve the threshold from another table, but would make the example a bit confusing. In practice, an application language would most likely pass the value to it as a parameter.
=={{header|SQL}}==
<
(
EMP_ID varchar2(6 char),
Line 4,589 ⟶ 6,499:
insert into EMP (EMP_ID, EMP_NAME, DEPT_ID, SALARY)
values ('E16400','Timothy Grive','D190',29900);
COMMIT;</
<
when 10 then
'Tot.' || LPAD(POPULATION, 2) || ' Employees in ' || TIE_COUNT ||
Line 4,683 ⟶ 6,593:
where EMP4.DEPT_ID = EMP3.DEPT_ID
and EMP4.SALARY >= EMP3.SALARY))
order by DEPT_ID ,LINE ,SALARY desc, EMP_ID;</
<pre style="height:30ex;overflow:scroll">Tot.15 employees in 4 deps.Avg salary: 33336.67
-
Line 4,714 ⟶ 6,624:
E27002 David Motsinger 19250.00 3</pre>
Note the T2 tie ranking aka ex aequo.
Since there is no requirement for the layout - using the data from the table above
<syntaxhighlight lang="sql">WITH ranked_emp AS (
SELECT emp_name
,dept_id
,salary
,DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary ) ranking
FROM emp
)
SELECT dept_id
,ranking
,emp_name
,salary
FROM ranked_emp
WHERE ranking <= 2
ORDER BY dept_id, ranking;</syntaxhighlight>
<pre style="height:30ex;overflow:scroll">
DEPT RANKING EMP_NAME SALARY
---- ---------- -------------------- ----------
D050 1 Nathan Adams 21900
D050 2 John Rappl 47000
D101 1 Richard Potter 15900
D101 2 Tim Sampair 27000
D190 1 Timothy Grove 29900
D190 1 Timothy Grave 29900
D190 1 Timothy Grive 29900
D190 2 Kim Arlich 57000
D202 1 Adam Smith 18000
D202 2 David Motsinger 19250</pre>
=={{header|Stata}}==
The following shows the top k salaries. If there are less than k employees in a department, this will print all salaries.
<syntaxhighlight lang="stata">import delimited employees.csv
local k 2
bysort department (salary): list salary if _N-_n<`k'</syntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="swift">struct Employee {
var name: String
var id: String
var salary: Int
var department: String
}
let employees = [
Employee(name: "Tyler Bennett", id: "E10297", salary: 32000, department: "D101"),
Employee(name: "John Rappl", id: "E21437", salary: 47000, department: "D050"),
Employee(name: "George Woltman", id: "E00127", salary: 53500, department: "D101"),
Employee(name: "Adam Smith", id: "E63535", salary: 18000, department: "D202"),
Employee(name: "Claire Buckman", id: "E39876", salary: 27800, department: "D202"),
Employee(name: "David McClellan", id: "E04242", salary: 41500, department: "D101"),
Employee(name: "Rich Holcomb", id: "E01234", salary: 49500, department: "D202"),
Employee(name: "Nathan Adams", id: "E41298", salary: 21900, department: "D050"),
Employee(name: "Richard Potter", id: "E43128", salary: 15900, department: "D101"),
Employee(name: "David Motsinger", id: "E27002", salary: 19250, department: "D202"),
Employee(name: "Tim Sampair", id: "E03033", salary: 27000, department: "D101"),
Employee(name: "Kim Arlich", id: "E10001", salary: 57000, department: "D190"),
Employee(name: "Timothy Grove", id: "E16398", salary: 29900, department: "D190")
]
func highestSalaries(employees: [Employee], n: Int = 1) -> [String: [Employee]] {
return employees.reduce(into: [:], {acc, employee in
guard var cur = acc[employee.department] else {
acc[employee.department] = [employee]
return
}
if cur.count < n {
cur.append(employee)
} else if cur.last!.salary < employee.salary {
cur[n - 1] = employee
}
acc[employee.department] = cur.sorted(by: { $0.salary > $1.salary })
})
}
for (dept, employees) in highestSalaries(employees: employees, n: 3) {
let employeeString = employees.map({ "\($0.name): \($0.salary)" }).joined(separator: "\n\t")
print("\(dept)'s highest paid employees are: \n\t\(employeeString)")
}</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">D050's highest paid employees are:
John Rappl: 47000
Nathan Adams: 21900
D101's highest paid employees are:
George Woltman: 53500
David McClellan: 41500
Tyler Bennett: 32000
D202's highest paid employees are:
Rich Holcomb: 49500
Claire Buckman: 27800
David Motsinger: 19250
D190's highest paid employees are:
Kim Arlich: 57000
Timothy Grove: 29900</pre>
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<
set text {Tyler Bennett,E10297,32000,D101
Line 4,749 ⟶ 6,762:
}
top_n_salaries 3 $data</
<pre style="height:30ex;overflow:scroll">Department D101
George Woltman E00127 53500
Line 4,767 ⟶ 6,780:
Kim Arlich E10001 57000
Timothy Grove E16398 29900</pre>
=={{header|Transd}}==
Version using built-in database-like functionality.
<syntaxhighlight lang="scheme">#lang transd
MainModule: {
tbl : String(
`EmployeeName,EmployeeID,Salary:Int,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`),
N: 2,
_start: (λ
(with tabl Table()
(load-table tabl tbl)
(build-index tabl "Department")
(with rows (tsd-query tabl
select: ["Department"]
as: [[String()]] :distinct sortby: "Department" )
(for row in rows do
(with recs (tsd-query tabl
select: all
as: [[String(), String(), Int(), String()]]
satisfying: (lambda Department String()
(eq Department (get row 0)))
sortby: "Salary" :desc
limit: N)
(for rec in recs do (textout rec "\n")))))))
}</syntaxhighlight>{{out}}
<pre>
[D050, E21437, John Rappl, 47000]
[D050, E41298, Nathan Adams, 21900]
[D101, E00127, George Woltman, 53500]
[D101, E04242, David McClellan, 41500]
[D190, E10001, Kim Arlich, 57000]
[D190, E16398, Timothy Grove, 29900]
[D202, E01234, Rich Holcomb, 49500]
[D202, E39876, Claire Buckman, 27800]
</pre>
Version using untyped vectors.
<syntaxhighlight lang="scheme">
#lang transd
MainModule: {
tbl : String(
`"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"`),
Record : typealias(Tuple<String String Int String>()),
N: 2,
curN: 0,
dep: String(),
deps: Index<String Int>(),
_start: (λ
(with recs Vector<Record>( untypedTable tbl "," "\n" )
res Vector<Record>()
(sort recs Range(-1 0)
(lambda l Record() r Record() -> Bool()
(ret (less (get l 2) (get r 2)))))
(for el in recs do
(= dep (get el 3))
(= curN (snd (get-s deps dep 0)))
(if (< curN N) (add res el) (set deps dep (+ curN 1)))
)
(sort res
(lambda l Record() r Record() -> Bool()
(ret (less (get l 3) (get r 3)))))
(for i in res do (textout i "\n"))
))
}
</syntaxhighlight>{{out}}
<pre>
[[John Rappl, E21437, 47000, D050]]
[[Nathan Adams, E41298, 21900, D050]]
[[George Woltman, E00127, 53500, D101]]
[[David McClellan, E04242, 41500, D101]]
[[Kim Arlich, E10001, 57000, D190]]
[[Timothy Grove, E16398, 29900, D190]]
[[Rich Holcomb, E01234, 49500, D202]]
[[Claire Buckman, E39876, 27800, D202]]
</pre>
=={{header|TUSCRIPT}}==
<
MODE DATA
$$ SET dates=*
Line 4,815 ⟶ 6,939:
PRINT " "
ENDLOOP
ENDCOMPILE</
<pre style='height:30ex;overflow:scroll'>
Department D050
Line 4,844 ⟶ 6,968:
Claire Buckman E39876 27800
David Motsinger E27002 19250
Adam Smith E63535 18000
</pre>
Line 4,855 ⟶ 6,979:
The data is in a file, exactly as given in the problem. Parameter N is accepted from command line.
<
@{n-param}
@(next "top-rank-per-group.dat")
Line 4,877 ⟶ 7,001:
@ (end)
@ (end)
@(end)</
<pre style="height:30ex;overflow:scroll">Department: D101
George Woltman (E00127) $ 53500
Line 4,897 ⟶ 7,021:
Descend into argument list:
<syntaxhighlight lang
Collect first argument as <code>n-param</code> variable:
<syntaxhighlight lang
Drill into data file:
<
Match header exactly:
<
Now iterate over the data, requiring a variable called <code>record</code> to be bound in each iteration, and suppress all other variables from emerging. In the body of the collect, bind four variables. Then use these four variables to create a four-element list which is bound to the variable <code>record</code>. The <code>int-str</code> function converts the textual variable <code>salary</code> to an integer:
<
@name,@id,@salary,@dept
@(bind record (@(int-str salary) dept name id))
@(end)
</syntaxhighlight>
Next, we bind five variables to the output of some TXR Lisp code, which will return five lists:
<
@(let* ((n (int-str n-param))
(dept-hash [group-by second record :equal-based])
Line 4,927 ⟶ 7,051:
(ranked (collect-each ((rec (hash-values dept-hash)))
[apply mapcar list [[sort rec > first] 0..n]])))
(cons dept [apply mapcar list ranked])))</
This code binds some successive variables. <code>n</code> is an integer conversion of the command line argument.
Line 4,938 ⟶ 7,062:
The reason for these transpositions is to convert the data into individual nested lists, once for each field. This is the format needed by the TXR <code>@(output)</code> clause:
<
@ (repeat)
Department: @dept
Line 4,945 ⟶ 7,069:
@ (end)
@ (end)
@(end)</
Here, all these variables are individual lists. The <code>dept</code> variable is a flat list; one nesting of <code>@(repeat)</code> iterates over it. The other variables are nested lists; a nested repeat drills into these.
Line 4,955 ⟶ 7,079:
The output is identical to the previous version.
<
@{n-param}
@(next "top-rank-per-group.dat")
Line 4,972 ⟶ 7,096:
(put-line `Department: @d`)
(each ((r dept-recs))
(put-line ` @{r[2] 15} (@{r[3]}) $@{r[0] -6}`)))))</
=={{header|Ursala}}==
Line 4,978 ⟶ 7,102:
sort each partition descending by the second to last, take the first
n strings in each partition, and display the list of them in a reasonably understandable form.
<
#import nat
Line 5,003 ⟶ 7,127:
#show+
main = top3 data</
<pre style="height:30ex;overflow:scroll">D190
Kim Arlich,E10001,57000
Line 5,021 ⟶ 7,145:
John Rappl,E21437,47000
Nathan Adams,E41298,21900</pre>
=={{header|VBA}}==
Creating an Excel pivot table to display the top n salaried employees per department. Data is read from a file.
<syntaxhighlight lang="vb">Private Sub top_rank(filename As String, n As Integer)
Workbooks.OpenText filename:=filename, Comma:=True
Dim ws As Worksheet
Set ws = Sheets.Add: ws.Name = "output"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"data!R1C1:R14C4", Version:=6).CreatePivotTable TableDestination:= _
"output!R3C1", TableName:="TableName", DefaultVersion:=6
With Sheets("output").PivotTables("TableName")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
.AddDataField Sheets("output").PivotTables("TableName"). _
PivotFields("Salary"), "Top rank", xlSum
.PivotFields("Department").Orientation = xlRowField
.PivotFields("Department").Position = 1
.PivotFields("Salary").Orientation = xlRowField
.PivotFields("Salary").Position = 2
.PivotFields("Employee Name").Orientation = xlRowField
.PivotFields("Employee Name").Position = 3
.PivotFields("Employee ID").Orientation = xlRowField
.PivotFields("Employee ID").Position = 4
.PivotFields("Salary").PivotFilters.Add2 Type:=xlTopCount, _
DataField:=Sheets("output").PivotTables("TableName"). _
PivotFields("Top rank"), Value1:=n
.PivotFields("Salary").Subtotals = Array(False, False, False, False, _
False, False, False, False, False, False, False, False)
.PivotFields("Employee Name").Subtotals = Array(False, False, False, _
False, False, False, False, False, False, False, False, False)
.PivotFields("Department").Subtotals = Array(False, False, False, False, _
False, False, False, False, False, False, False, False)
.ColumnGrand = False
.PivotFields("Salary").AutoSort xlDescending, "Salary"
End With
End Sub
Public Sub main()
top_rank filename:="D:\data.txt", n:=3
End Sub</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-sort}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./dynamic" for Tuple
import "./sort" for Sort, Cmp
import "./seq" for Lst
import "./fmt" for Fmt
var Employee = Tuple.create("Employee", ["name", "id", "salary", "dept"])
var N = 2 // say
var employees = [
Employee.new("Tyler Bennett", "E10297", 32000, "D101"),
Employee.new("John Rappl", "E21437", 47000, "D050"),
Employee.new("George Woltman" , "E00127", 53500, "D101"),
Employee.new("Adam Smith", "E63535", 18000, "D202"),
Employee.new("Claire Buckman", "E39876", 27800, "D202"),
Employee.new("David McClellan", "E04242", 41500, "D101"),
Employee.new("Rich Holcomb", "E01234", 49500, "D202"),
Employee.new("Nathan Adams", "E41298", 21900, "D050"),
Employee.new("Richard Potter", "E43128", 15900, "D101"),
Employee.new("David Motsinger", "E27002", 19250, "D202"),
Employee.new("Tim Sampair", "E03033", 27000, "D101"),
Employee.new("Kim Arlich", "E10001", 57000, "D190"),
Employee.new("Timothy Grove", "E16398", 29900, "D190")
]
var cmpByDept = Fn.new { |employee1, employee2| Cmp.string.call(employee1.dept, employee2.dept) }
Sort.insertion(employees, cmpByDept)
var groupsByDept = Lst.groups(employees) { |e| e.dept }
System.print("Highest %(N) salaries by department:\n")
for (group in groupsByDept) {
var dept = group[0]
var groupEmployees = group[1].map { |i| i[0] }.toList
var cmpBySalary = Fn.new { |employee1, employee2| Cmp.numDesc.call(employee1.salary, employee2.salary) }
Sort.insertion(groupEmployees, cmpBySalary)
var topRanked = groupEmployees.take(N)
System.print("Dept %(dept) => ")
topRanked.each { |e| Fmt.print("$-15s $s $d", e.name, e.id, e.salary) }
System.print()
}</syntaxhighlight>
{{out}}
<pre>
Highest 2 salaries by department:
Dept D050 =>
John Rappl E21437 47000
Nathan Adams E41298 21900
Dept D101 =>
George Woltman E00127 53500
David McClellan E04242 41500
Dept D190 =>
Kim Arlich E10001 57000
Timothy Grove E16398 29900
Dept D202 =>
Rich Holcomb E01234 49500
Claire Buckman E39876 27800
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc Sort(Array, Field, Size); \Sort Array in descending order by Field
int Array, Field, Size, I, J, T;
[for J:= Size-1 downto 0 do
for I:= 0 to J-1 do
if Array(I,Field) < Array(I+1,Field) then
[T:= Array(I); Array(I):= Array(I+1); Array(I+1):= T];
];
int Data, I, I0, Dept, N, Cnt;
[Data:=[["Tyler Bennett", "E10297", 32000, 101],
["John Rappl", "E21437", 47000, 050],
["George Woltman", "E00127", 53500, 101],
["Adam Smith", "E63535", 18000, 202],
["Claire Buckman", "E39876", 27800, 202],
["David McClellan", "E04242", 41500, 101],
["Rich Holcomb", "E01234", 49500, 202],
["Nathan Adams", "E41298", 21900, 050],
["Richard Potter", "E43128", 15900, 101],
["David Motsinger", "E27002", 19250, 202],
["Tim Sampair", "E03033", 27000, 101],
["Kim Arlich", "E10001", 57000, 190],
["Timothy Grove", "E16398", 29900, 190],
[0, 0, 0, 000]]; \sentinel
I:= 0; \find number of employees = Data size
while Data(I,0) # 0 do I:= I+1;
Sort(Data, 3, I); \sort by department field (3)
I:= 0; \sort by salary within each department
while Data(I,0) do
[Dept:= Data(I,3);
I0:= I;
repeat I:= I+1 until Data(I,3) # Dept;
Sort(@Data(I0), 2, I-I0);
];
N:= IntIn(0); \get parameter
I:= 0;
loop [Dept:= Data(I,3); \for each department
Text(0, "Department D");
if Dept < 100 then ChOut(0, ^0); IntOut(0, Dept); Text(0, ":^m^j");
Cnt:= 0;
loop [if Cnt >= N then
while Data(I,3) = Dept do I:= I+1; \skip any remaining
if Data(I,3) # Dept then quit;
IntOut(0, Data(I,2)); Text(0, " "); \salary
Text(0, Data(I,1)); Text(0, " "); \ID
Text(0, Data(I,0)); CrLf(0); \name
Cnt:= Cnt+1;
I:= I+1;
];
if Data(I,0) = 0 then quit;
];
]</syntaxhighlight>
{{out}}
<pre>
3
Department D202:
49500 E01234 Rich Holcomb
27800 E39876 Claire Buckman
19250 E27002 David Motsinger
Department D190:
57000 E10001 Kim Arlich
29900 E16398 Timothy Grove
Department D101:
53500 E00127 George Woltman
41500 E04242 David McClellan
32000 E10297 Tyler Bennett
Department D050:
47000 E21437 John Rappl
21900 E41298 Nathan Adams
</pre>
=={{header|zkl}}==
This reads a data file, the contents of which are as given by the task description (minus the header). To protect privacy, identifying information has been stripped from the output (aka laziness enhanced coding).
<
fcn topNsalaries(n){
Line 5,037 ⟶ 7,339:
"%s: %s".fmt(d,ss.concat(",")).println();
}
}(3);</
{{out}}
<pre>
|