June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,29 +1,30 @@
immutable KPCSupply{S<:String, T<:Real}
item::S
struct KPCSupply{T<:Real}
item::String
weight::T
value::T
uvalue::T
end
Base.isless(a::KPCSupply, b::KPCSupply) = a.uvalue<b.uvalue
function KPCSupply{S<:String, T<:Real}(item::S, weight::T, value::T)
KPCSupply(item, weight, value, value/weight)
function KPCSupply(item::AbstractString, weight::Real, value::Real)
w, v = promote(weight, value)
KPCSupply(item, w, v, v / w)
end
function solve{S<:String, T<:Real}(store::Array{KPCSupply{S,T},1},
capacity::T)
ksack = KPCSupply{S,T}[]
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
for s in sort(store, rev = true)
if kweight + s.weight capacity
kweight += s.weight
push!(ksack, s)
push!(sack, s)
else
w = capacity-kweight
v = w*s.uvalue
push!(ksack, KPCSupply(s.item, w, v, s.uvalue))
w = capacity - kweight
v = w * s.uvalue
push!(sack, KPCSupply(s.item, w, v, s.value))
break
end
end
return ksack
return sack
end