Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,32 @@
using Printf
struct KPCSupply{T<:Real}
item::String
weight::T
value::T
uvalue::T
end
function KPCSupply(item::AbstractString, weight::Real, value::Real)
w, v = promote(weight, value)
KPCSupply(item, w, v, v / w)
end
Base.show(io::IO, s::KPCSupply) = print(io, s.item, @sprintf " (%.2f kg, %.2f €, %.2f €/kg)" s.weight s.value s.uvalue)
Base.isless(a::KPCSupply, b::KPCSupply) = a.uvalue < b.uvalue
function solve(store::Vector{KPCSupply{T}}, capacity::Real) where T<:Real
sack = similar(store, 0) # vector like store, but of length 0
kweight = zero(T)
for s in sort(store, rev = true)
if kweight + s.weight ≤ capacity
kweight += s.weight
push!(sack, s)
else
w = capacity - kweight
v = w * s.uvalue
push!(sack, KPCSupply(s.item, w, v, s.value))
break
end
end
return sack
end

View file

@ -0,0 +1,14 @@
store = [KPCSupply("beef", 38//10, 36),
KPCSupply("pork", 54//10, 43),
KPCSupply("ham", 36//10, 90),
KPCSupply("greaves", 24//10, 45),
KPCSupply("flitch", 4//1, 30),
KPCSupply("brawn", 25//10, 56),
KPCSupply("welt", 37//10, 67),
KPCSupply("salami", 3//1, 95),
KPCSupply("sausage", 59//10, 98)]
sack = solve(store, 15)
println("The store contains:\n - ", join(store, "\n - "))
println("\nThe thief should take::\n - ", join(sack, "\n - "))
@printf("\nTotal value in the sack: %.2f €\n", sum(getfield.(sack, :value)))