How to resolve the algorithm Top rank per group step by step in the TXR programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Top rank per group step by step in the TXR programming language
Table of Contents
Problem Statement
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:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Top rank per group step by step in the TXR programming language
Source code in the txr programming language
@(next :args)
@{n-param}
@(next "top-rank-per-group.dat")
Employee Name,Employee ID,Salary,Department
@(collect :vars (record))
@name,@id,@salary,@dept
@(bind record (@(int-str salary) dept name id))
@(end)
@(bind (dept salary dept2 name id)
@(let* ((n (int-str n-param))
(dept-hash [group-by second record :equal-based])
(dept (hash-keys dept-hash))
(ranked (collect-each ((rec (hash-values dept-hash)))
[apply mapcar list [[sort rec > first] 0..n]])))
(cons dept [apply mapcar list ranked])))
@(output)
@ (repeat)
Department: @dept
@ (repeat)
@{name 15} (@id) $@{salary -6}
@ (end)
@ (end)
@(end)
@(next :args)
@{n-param}
@(next "top-rank-per-group.dat")
Employee Name,Employee ID,Salary,Department
@(collect :vars (record))
@name,@id,@salary,@dept
@(bind record (@(int-str salary) dept name id))
@(end)
@(bind (dept salary dept2 name id)
@(let* ((n (int-str n-param))
(dept-hash [group-by second record :equal-based])
(dept (hash-keys dept-hash))
(ranked (collect-each ((rec (hash-values dept-hash)))
[apply mapcar list [[sort rec > first] 0..n]])))
(cons dept [apply mapcar list ranked])))
@(output)
@ (repeat)
Department: @dept
@ (repeat)
@{name 15} (@id) $@{salary -6}
@ (end)
@ (end)
@(end)
@(next :args)
@{n-param}
@(next "top-rank-per-group.dat")
Employee Name,Employee ID,Salary,Department
@(collect :vars (record))
@name,@id,@salary,@dept
@(bind record (@(int-str salary) dept name id))
@(end)
@(do
(let* ((n (int-str n-param))
(dept-hash [group-by second record :equal-based])
(ranked (collect-each ((rec (hash-values dept-hash)))
[[sort rec > first] 0..n])))
(each ((d (hash-keys dept-hash))
(dept-recs ranked))
(put-line `Department: @d`)
(each ((r dept-recs))
(put-line ` @{r[2] 15} (@{r[3]}) $@{r[0] -6}`)))))
You may also check:How to resolve the algorithm Arithmetic derivative step by step in the Haskell programming language
You may also check:How to resolve the algorithm Next highest int from digits step by step in the Julia programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the 11l programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the PL/I programming language
You may also check:How to resolve the algorithm 100 doors step by step in the 8086 Assembly programming language