How to resolve the algorithm Top rank per group step by step in the Transd programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Top rank per group step by step in the Transd 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 Transd programming language
Source code in the transd programming language
#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")))))))
}
#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()),
N: 2,
curN: 0,
dep: String(),
deps: Index(),
_start: (λ
(with recs Vector( untypedTable tbl "," "\n" )
res Vector()
(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"))
))
}
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the ATS programming language
You may also check:How to resolve the algorithm Amb step by step in the F# programming language
You may also check:How to resolve the algorithm Algebraic data types step by step in the TXR programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the PHP programming language
You may also check:How to resolve the algorithm Parse an IP Address step by step in the Wren programming language