Top rank per group: Difference between revisions

Content added Content deleted
Line 113: Line 113:
=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>void
<lang aime>void
Add_Employee(record employees, text name, text id, integer salary,
Add_Employee(record employees, text name, id, integer salary, text department)
text department)
{
{
employees[name] = list(name, id, salary, department);
employees[name] = list(name, id, salary, department);
Line 120: Line 119:


void
void
collect(record top, record employees)
collect(record top, employees)
{
{
list l;
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);
for (, l in employees) {
list department;
if (N < ~t.reverse) {
integer i, salary;
t.erase(N, -1);

department = r_v_list(top, l[3]);

salary = l[2];
i = 0;
while (i < ~department) {
if (__list(department[i])[2] < salary) {
break;
}
i += 1;
}

l_l_list(department, i, l);

if (~department == N + 1) {
department.delete(N);
}
}
top[department] = t;
}
}
}
}


void
void
print_department(text department, list employees)
print(record top)
{
{
text department;
o_("Department ", department, "\n");
list employees;


for (department, employees in top) {
for (, list l in employees) {
list l;
o_form(" ~ | ~ | ~\n", l[0], l[1], l[2]);

o_("Department ", department, "\n");

for (, l in employees) {
o_form(" ~ | ~ | ~\n", l[0], l[1], l[2]);
}
}
}
}
}
Line 185: Line 166:
collect(top, employees);
collect(top, employees);


print(top);
top.wcall(print_department, 0, 1);


return 0;
0;
}</lang>Run as:
}</lang>Run as:
<pre>aime rcs/top_rank_per_group c N 5</pre>{{out}}
<pre>aime rcs/top_rank_per_group c N 5</pre>{{out}}