FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python/dynamic_programming/FractionalKnapsack.py at master · wcfylcf/Python · GitHub
wcfylcf
/
Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Python
/
dynamic_programming
/
FractionalKnapsack.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
12 lines (9 loc) · 420 Bytes
Breadcrumbs
Python
/
dynamic_programming
/
FractionalKnapsack.py
Copy path
File metadata and controls
12 lines (9 loc) · 420 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
from
itertools
import
accumulate
from
bisect
import
bisect
def
fracKnapsack
(
vl
,
wt
,
W
,
n
):
r
=
list
(
sorted
(
zip
(
vl
,
wt
),
key
=
lambda
x
:
x
[
0
]
/
x
[
1
],
reverse
=
True
))
vl
,
wt
=
[
i
[
0
]
for
i
in
r
],[
i
[
1
]
for
i
in
r
]
acc
=
list
(
accumulate
(
wt
))
k
=
bisect
(
acc
,
W
)
return
0
if
k
==
0
else
sum
(
vl
[:
k
])
+
(
W
-
acc
[
k
-
1
])
*
(
vl
[
k
])
/
(
wt
[
k
])
if
k
!=
n
else
sum
(
vl
[:
k
])
print
(
"%.0f"
%
fracKnapsack
([
60
,
100
,
120
],[
10
,
20
,
30
],
50
,
3
))
Back
|
FazBrowse Home
|
New Git URL