Skip to content

Instantly share code, notes, and snippets.

Created February 23, 2021 04:02
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Short F# script to generate problem sets of a specified size containing problems from multiple sections of a textbook
#r "nuget: FSharp.Json"
open FSharp.Json
open System.IO
type SubSection =
{ Name: string
; Weight: float
; ProblemRange: int * int }
type Section =
{ Name: string
; SubSections: SubSection list }
type ProblemSet =
{ Section: string
; Problems: (string * int list) list }
let fromRange (a, b) = [a..b]
let totalWeight section =
|> (fun it -> it.Weight)
|> List.reduce (+)
let selectProblems problemCount section (subsection: SubSection) =
// Number of questions that should be chosen from this subsection
let quantity = int (subsection.Weight / (totalWeight section) * (float problemCount))
let problems = fromRange subsection.ProblemRange
let problemsLength = (snd subsection.ProblemRange) - (fst subsection.ProblemRange)
if quantity >= problemsLength then
(subsection.Name, problems)
// If there are more problems in the subsection than we want to include
// in the problemset, determine a number n such that we can choose
// every nth problem in the subsection to get as close as possible to
// the desired quantity
let nth = problemsLength / quantity
|> List.indexed
|> List.filter (fun (i, _) -> i % nth = 0)
|> snd
|> (fun plist -> (subsection.Name, plist))
let genPSet problemCount section =
let problems =
|> (selectProblems problemCount section)
{Section = section.Name ; Problems = problems}
let args = System.Environment.GetCommandLineArgs()
let numberOfProblems = int args.[2]
let sectionDataPath = args.[3..] |> String.concat " "
|> String.concat "\n"
|> Json.deserialize<Section list>
|> (genPSet numberOfProblems)
|> printfn "%A"
Copy link

rewrite it in Ada

Copy link

I'm pretty tempted to do so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment