⚠️ Warning: This is a draft ⚠️
This means it might contain formatting issues,
incorrect code, conceptual problems, or other severe issues.
If you want to help to improve and eventually enable this page,
please fork
RosettaGit's repository
and open a merge request on GitHub.
{{task}}
;Task:
Find the top ''N'' salaries in each department, where ''N'' is provided as a parameter.
Use this data as a formatted internal data structure (adapt it to your language-native idioms, rather than parse at runtime), or identify your external data source:
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
Ada
top.adb:
with Ada.Containers.Vectors;
with Ada.Text_IO;
procedure Top is
type Departments is (D050, D101, D190, D202);
type Employee_Data is record
Name : String (1 .. 15);
ID : String (1 .. 6);
Salary : Positive;
Department : Departments;
end record;
package Employee_Vectors is new Ada.Containers.Vectors
(Element_Type => Employee_Data, Index_Type => Positive);
function Compare_Salary (Left, Right : Employee_Data) return Boolean is
begin
return Left.Salary > Right.Salary;
end Compare_Salary;
package Salary_Sort is new Employee_Vectors.Generic_Sorting
("<" => Compare_Salary);
function Compare_Department (Left, Right : Employee_Data) return Boolean is
begin
return Left.Department < Right.Department;
end Compare_Department;
package Department_Sort is new Employee_Vectors.Generic_Sorting
("<" => Compare_Department);
Example_Data : Employee_Vectors.Vector;
begin
-- fill data
Example_Data.Append (("Tyler Bennett ", "E10297", 32000, D101));
Example_Data.Append (("John Rappl ", "E21437", 47000, D050));
Example_Data.Append (("George Woltman ", "E00127", 53500, D101));
Example_Data.Append (("Adam Smith ", "E63535", 18000, D202));
Example_Data.Append (("Claire Buckman ", "E39876", 27800, D202));
Example_Data.Append (("David McClellan", "E04242", 41500, D101));
Example_Data.Append (("Rich Holcomb ", "E01234", 49500, D202));
Example_Data.Append (("Nathan Adams ", "E41298", 21900, D050));
Example_Data.Append (("Richard Potter ", "E43128", 15900, D101));
Example_Data.Append (("David Motsinger", "E27002", 19250, D202));
Example_Data.Append (("Tim Sampair ", "E03033", 27000, D101));
Example_Data.Append (("Kim Arlich ", "E10001", 57000, D190));
Example_Data.Append (("Timothy Grove ", "E16398", 29900, D190));
-- sort by salary
Salary_Sort.Sort (Example_Data);
-- output each department
for Department in Departments loop
declare
Position : Employee_Vectors.Cursor := Example_Data.First;
Employee : Employee_Data;
begin
Ada.Text_IO.Put_Line ("Department " & Departments'Image (Department));
for I in 1 .. 3 loop
Employee := Employee_Vectors.Element (Position);
while Employee.Department /= Department loop
Position := Employee_Vectors.Next (Position);
Employee := Employee_Vectors.Element (Position);
end loop;
Ada.Text_IO.Put_Line (" " & Employee.Name & " | " &
Employee.ID & " | " &
Positive'Image (Employee.Salary));
Position := Employee_Vectors.Next (Position);
end loop;
exception
when Constraint_Error =>
null;
end;
end loop;
end Top;
{{out}}
Department D050
John Rappl | E21437 | 47000
Nathan Adams | E41298 | 21900
Department D101
George Woltman | E00127 | 53500
David McClellan | E04242 | 41500
Tyler Bennett | E10297 | 32000
Department D190
Kim Arlich | E10001 | 57000
Timothy Grove | E16398 | 29900
Department D202
Rich Holcomb | E01234 | 49500
Claire Buckman | E39876 | 27800
David Motsinger | E27002 | 19250
```
## Aime
```aime
Add_Employee(record employees, text name, id, integer salary, text department)
{
employees[name] = list(name, id, salary, 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);
if (N < ~t.reverse) {
t.erase(N, -1);
}
top[department] = t;
}
}
print_department(text department, list employees)
{
o_("Department ", department, "\n");
for (, list l in employees) {
o_form(" ~ | ~ | ~\n", l[0], l[1], l[2]);
}
}
main(void)
{
record employees, top;
Add_Employee(employees, "Tyler Bennett ", "E10297", 32000, "D101");
Add_Employee(employees, "John Rappl ", "E21437", 47000, "D050");
Add_Employee(employees, "George Woltman ", "E00127", 53500, "D101");
Add_Employee(employees, "Adam Smith ", "E63535", 18000, "D202");
Add_Employee(employees, "Claire Buckman ", "E39876", 27800, "D202");
Add_Employee(employees, "David McClellan", "E04242", 41500, "D101");
Add_Employee(employees, "Rich Holcomb ", "E01234", 49500, "D202");
Add_Employee(employees, "Nathan Adams ", "E41298", 21900, "D050");
Add_Employee(employees, "Richard Potter ", "E43128", 15900, "D101");
Add_Employee(employees, "David Motsinger", "E27002", 19250, "D202");
Add_Employee(employees, "Tim Sampair ", "E03033", 27000, "D101");
Add_Employee(employees, "Kim Arlich ", "E10001", 57000, "D190");
Add_Employee(employees, "Timothy Grove ", "E16398", 29900, "D190");
collect(top, employees);
top.wcall(print_department, 0, 1);
0;
}
```
Run as:
```txt
aime rcs/top_rank_per_group c N 5
```
{{out}}
Department D050
John Rappl | E21437 | 47000
Nathan Adams | E41298 | 21900
Department D101
George Woltman | E00127 | 53500
David McClellan | E04242 | 41500
Tyler Bennett | E10297 | 32000
Tim Sampair | E03033 | 27000
Richard Potter | E43128 | 15900
Department D190
Kim Arlich | E10001 | 57000
Timothy Grove | E16398 | 29900
Department D202
Rich Holcomb | E01234 | 49500
Claire Buckman | E39876 | 27800
David Motsinger | E27002 | 19250
Adam Smith | E63535 | 18000
```
## AutoHotkey
```autohotkey
Departments = D050, D101, D190, D202
StringSplit, Department_, Departments, `,, %A_Space%
; Employee Name, Employee ID, Salary, Department
Add_Employee("Tyler Bennett ", "E10297", 32000, "D101")
Add_Employee("John Rappl ", "E21437", 47000, "D050")
Add_Employee("George Woltman ", "E00127", 53500, "D101")
Add_Employee("Adam Smith ", "E63535", 18000, "D202")
Add_Employee("Claire Buckman ", "E39876", 27800, "D202")
Add_Employee("David McClellan", "E04242", 41500, "D101")
Add_Employee("Rich Holcomb ", "E01234", 49500, "D202")
Add_Employee("Nathan Adams ", "E41298", 21900, "D050")
Add_Employee("Richard Potter ", "E43128", 15900, "D101")
Add_Employee("David Motsinger", "E27002", 19250, "D202")
Add_Employee("Tim Sampair ", "E03033", 27000, "D101")
Add_Employee("Kim Arlich ", "E10001", 57000, "D190")
Add_Employee("Timothy Grove ", "E16398", 29900, "D190")
; display top 3 ranks for each department
Loop, %Department_0% ; all departments
MsgBox,, % "Department: " Department_%A_Index%
, % TopRank(3, Department_%A_Index%)
;---------------------------------------------------------------------------
TopRank(N, Department) { ; find the top N salaries in each department
;---------------------------------------------------------------------------
local Collection := Msg := ""
Loop, %m% ; all employees
If (Employee_%A_Index%_Dept = Department)
; collect all the salaries being paid in this department
Collection .= (Collection ? "," : "") Employee_%A_Index%_Salary
Sort, Collection, ND,R
StringSplit, Collection, Collection, `,
Loop, % (N < Collection0) ? N : Collection0 {
Salary := Collection%A_Index%
Loop, %m% ; find the respective employee
If (Employee_%A_Index%_Salary = Salary)
; and put out his/her details
Msg .= Employee_%A_Index%_Name "`t"
. Employee_%A_Index%_ID "`t"
. Employee_%A_Index%_Salary "`t"
. Employee_%A_Index%_Dept "`t`n"
}
Return, Msg
}
;---------------------------------------------------------------------------
Add_Employee(Name, ID, Salary, Department) {
;---------------------------------------------------------------------------
global
m++
Employee_%m%_Name := Name
Employee_%m%_ID := ID
Employee_%m%_Salary := Salary
Employee_%m%_Dept := Department
}
```
{{out}}
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
```
## AWK
```AWK
# syntax: GAWK -f TOP_RANK_PER_GROUP.AWK [n]
#
# sorting:
# PROCINFO["sorted_in"] is used by GAWK
# SORTTYPE is used by Thompson Automation's TAWK
#
BEGIN {
arrA[++n] = "Employee Name,Employee ID,Salary,Department" # raw data
arrA[++n] = "Tyler Bennett,E10297,32000,D101"
arrA[++n] = "John Rappl,E21437,47000,D050"
arrA[++n] = "George Woltman,E00127,53500,D101"
arrA[++n] = "Adam Smith,E63535,18000,D202"
arrA[++n] = "Claire Buckman,E39876,27800,D202"
arrA[++n] = "David McClellan,E04242,41500,D101"
arrA[++n] = "Rich Holcomb,E01234,49500,D202"
arrA[++n] = "Nathan Adams,E41298,21900,D050"
arrA[++n] = "Richard Potter,E43128,15900,D101"
arrA[++n] = "David Motsinger,E27002,19250,D202"
arrA[++n] = "Tim Sampair,E03033,27000,D101"
arrA[++n] = "Kim Arlich,E10001,57000,D190"
arrA[++n] = "Timothy Grove,E16398,29900,D190"
for (i=2; i<=n; i++) { # build internal structure
split(arrA[i],arrB,",")
arrC[arrB[4]][arrB[3]][arrB[2] " " arrB[1]] # I.E. arrC[dept][salary][id " " name]
}
show = (ARGV[1] == "") ? 1 : ARGV[1] # employees to show per department
printf("DEPT SALARY EMPID NAME\n\n") # produce report
PROCINFO["sorted_in"] = "@ind_str_asc" ; SORTTYPE = 1
for (i in arrC) {
PROCINFO["sorted_in"] = "@ind_str_desc" ; SORTTYPE = 9
shown = 0
for (j in arrC[i]) {
PROCINFO["sorted_in"] = "@ind_str_asc" ; SORTTYPE = 1
for (k in arrC[i][j]) {
if (shown++ < show) {
printf("%-4s %6s %s\n",i,j,k)
printed++
}
}
}
if (printed > 0) { print("") }
printed = 0
}
exit(0)
}
```
Sample command and output:
```txt
GAWK -f TOP_RANK_PER_GROUP.AWK 3
DEPT SALARY EMPID NAME
D050 47000 E21437 John Rappl
D050 21900 E41298 Nathan Adams
D101 53500 E00127 George Woltman
D101 41500 E04242 David McClellan
D101 32000 E10297 Tyler Bennett
D190 57000 E10001 Kim Arlich
D190 29900 E16398 Timothy Grove
D202 49500 E01234 Rich Holcomb
D202 27800 E39876 Claire Buckman
D202 19250 E27002 David Motsinger
```
## 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: a^b*a^c
→ a^(b+c)
. This built-in behaviour is made use of in this solution.
```bracmat
(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)
: ?employees
& ( toprank
= employees N n P "Employee Name" "Employee ID" SalaryDepartment
. !arg:(?employees.?N)
& 1:?P
& whl
' ( !employees
: (?"Employee Name",?"Employee ID",?Salary,?Department)
?employees
& !Department^(!Salary.!"Employee Name".!"Employee ID")*!P:?P
)
& out$(Top !N "salaries per department.")
& whl
' ( !P:%?Department^?employees*?P
& out$(str$("Department " !Department ":"))
& !N:?n
& whl
' ( !n+-1:~<0:?n
& !employees
: ?employees+(?Salary.?"Employee Name".?"Employee ID")
& out
$ (str$(" " !"Employee Name" " (" !"Employee ID" "):" !Salary))
)
)
)
& toprank$(!employees.3)
& ;
```
{{out}}
Top 3 salaries per department.
Department D050:
JohnRappl (E21437):47000
NathanAdams (E41298):21900
Department D101:
GeorgeWoltman (E00127):53500
DavidMcClellan (E04242):41500
TylerBennett (E10297):32000
Department D190:
KimArlich (E10001):57000
TimothyGrove (E16398):29900
Department D202:
RichHolcomb (E01234):49500
ClaireBuckman (E39876):27800
DavidMotsinger (E27002):19250
```
## C
```c
#include
#include
#include
typedef struct {
const char *name, *id, *dept;
int sal;
} person;
person ppl[] = {
{"Tyler Bennett", "E10297", "D101", 32000},
{"John Rappl", "E21437", "D050", 47000},
{"George Woltman", "E00127", "D101", 53500},
{"Adam Smith", "E63535", "D202", 18000},
{"Claire Buckman", "E39876", "D202", 27800},
{"David McClellan", "E04242", "D101", 41500},
{"Rich Holcomb", "E01234", "D202", 49500},
{"Nathan Adams", "E41298", "D050", 21900},
{"Richard Potter", "E43128", "D101", 15900},
{"David Motsinger", "E27002", "D202", 19250},
{"Tim Sampair", "E03033", "D101", 27000},
{"Kim Arlich", "E10001", "D190", 57000},
{"Timothy Grove", "E16398", "D190", 29900},
};
int pcmp(const void *a, const void *b)
{
const person *aa = a, *bb = b;
int x = strcmp(aa->dept, bb->dept);
if (x) return x;
return aa->sal > bb->sal ? -1 : aa->sal < bb->sal;
}
#define N sizeof(ppl)/sizeof(person)
void top(int n)
{
int i, rank;
qsort(ppl, N, sizeof(person), pcmp);
for (i = rank = 0; i < N; i++) {
if (i && strcmp(ppl[i].dept, ppl[i - 1].dept)) {
rank = 0;
printf("\n");
}
if (rank++ < n)
printf("%s %d: %s\n", ppl[i].dept, ppl[i].sal, ppl[i].name);
}
}
int main()
{
top(2);
return 0;
}
```
{{out}}
```txt
D050 47000: John Rappl
D050 21900: Nathan Adams
D101 53500: George Woltman
D101 41500: David McClellan
D190 57000: Kim Arlich
D190 29900: Timothy Grove
D202 49500: Rich Holcomb
D202 27800: Claire Buckman
```
## C++
```cpp
#include
#include
#include
#include