Platform
Resources
Pricing
Sign in
Contact us
Chris Ryan
Senior Art Editor at Nature News in London.
Workspace
Fork
Public
By
Chris Ryan
Edited
Nov 14, 2023
Insert cell
Insert cell
tasks
=
[
{
name
:
"Task 1"
,
duration
:
4
}
,
{
name
:
"Task 2"
,
duration
:
2
}
,
{
name
:
"Task 3"
,
duration
:
7
}
,
{
name
:
"Task 4"
,
duration
:
5
}
,
{
name
:
"Task 5"
,
duration
:
1
}
,
{
name
:
"Task 6"
,
duration
:
3
}
]
Insert cell
timeToWork
=
6
Insert cell
sumDuration
=
(
previousValue
,
currentValue
)
=>
{
let
task_names
=
[
...
previousValue
.
task_names
,
currentValue
.
name
]
;
let
duration
=
previousValue
.
duration
+
currentValue
.
duration
;
if
(
duration
<=
timeToWork
)
{
return
{
task_names
,
duration
}
;
}
else
{
return
previousValue
;
}
}
Insert cell
groupTasks
=
(
item
,
index
,
array
)
=>
{
const
sliced_array
=
array
.
slice
(
index
)
;
// 2. Group the tasks into smaller lists starting from the current task
const
sumed_array
=
sliced_array
.
flatMap
(
(
sub_item
,
sub_index
,
sub_array
)
=>
sub_array
.
slice
(
sub_index
)
.
reduce
(
sumDuration
,
{
task_names
:
[
]
,
duration
:
0
}
)
)
;
return
sumed_array
;
}
Insert cell
sortByTaskNameLength
=
(
a
,
b
)
=>
b
.
task_names
.
length
-
a
.
task_names
.
length
Insert cell
doWork
=
(
task_list
,
total_time
)
=>
{
let
total_duration
=
0
;
return
task_list
.
filter
(
(
d
)
=>
d
.
duration
<=
timeToWork
)
// 1. Remove any tasks which are too long to complete on their own
.
flatMap
(
groupTasks
)
.
filter
(
(
d
)
=>
d
.
task_names
.
length
>
1
)
.
sort
(
sortByTaskNameLength
)
;
}
Insert cell
doWork
(
tasks
,
timeToWork
)
Insert cell
function
findTaskCombinations
(
tasks
,
timeToWork
,
currentCombo
=
[
]
,
currentIndex
=
0
,
results
=
[
]
)
{
// Break out clause
if
(
currentIndex
>=
tasks
.
length
)
{
// We've reached the end of our recursion
// If the totalDuration is within bounds,
// work out the list of names and add the result to the results array
let
totalDuration
=
currentCombo
.
reduce
(
(
acc
,
task
)
=>
acc
+
task
.
duration
,
0
)
;
if
(
totalDuration
<=
timeToWork
&&
currentCombo
.
length
>
1
)
{
results
.
push
(
{
taskNames
:
currentCombo
.
map
(
(
task
)
=>
task
.
name
)
,
duration
:
totalDuration
}
)
;
}
return
;
}
// Include current task
findTaskCombinations
(
tasks
,
timeToWork
,
currentCombo
.
concat
(
tasks
[
currentIndex
]
)
,
currentIndex
+
1
,
results
)
;
// Exclude current task
findTaskCombinations
(
tasks
,
timeToWork
,
currentCombo
,
currentIndex
+
1
,
results
)
;
return
results
;
}
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
tasks
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
timeToWork
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
sumDuration
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
groupTasks
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
sortByTaskNameLength
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
doWork
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
findTaskCombinations
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML