Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
Homework 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jguzman12
Homework 3
Commits
19bc18b0
Commit
19bc18b0
authored
6 months ago
by
jguzman12
Browse files
Options
Downloads
Patches
Plain Diff
fixed errors
parent
76ec8fc5
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
minimal-app/src/features/counter/counterSlice.js
+41
-13
41 additions, 13 deletions
minimal-app/src/features/counter/counterSlice.js
minimal-app/src/index.js
+1
-1
1 addition, 1 deletion
minimal-app/src/index.js
with
42 additions
and
14 deletions
minimal-app/src/features/counter/counterSlice.js
+
41
−
13
View file @
19bc18b0
...
@@ -3,23 +3,51 @@ import { createSlice } from '@reduxjs/toolkit';
...
@@ -3,23 +3,51 @@ import { createSlice } from '@reduxjs/toolkit';
const
counterSlice
=
createSlice
({
const
counterSlice
=
createSlice
({
name
:
'
counter
'
,
name
:
'
counter
'
,
initialState
:
{
initialState
:
{
value
:
0
,
candies
:
[],
maxCandyWeight
:
0
,
result
:
{
totalTastiness
:
0
,
quantities
:
[],
},
},
},
reducers
:
{
reducers
:
{
setValue
:
(
counter
,
action
)
=>
{
setCandies
:
(
state
,
action
)
=>
{
const
{
state
.
candies
=
action
.
payload
;
value
,
}
=
action
.
payload
;
counter
.
value
=
value
;
},
},
setMaxWeight
:
(
state
,
action
)
=>
{
state
.
maxWeight
=
action
.
payload
;
},
},
});
knapsack
:
(
state
)
=>
{
export
default
counterSlic
e
;
const
{
candies
,
maxWeight
}
=
stat
e
;
export
const
{
const
dp
=
Array
(
maxWeight
+
1
).
fill
(
0
);
setValue
,
const
quantities
=
Array
(
maxWeight
+
1
)
}
=
counterSlice
.
actions
;
.
fill
(
0
)
.
map
(()
=>
Array
(
candies
.
length
).
fill
(
0
));
export
function
selectValue
(
state
)
{
for
(
let
w
=
0
;
w
<=
maxWeight
;
w
++
)
{
return
state
.
counter
.
value
;
for
(
let
i
=
0
;
i
<
candies
.
length
;
i
++
)
{
const
{
tastiness
,
weight
}
=
candies
[
i
];
if
(
weight
<=
w
)
{
const
newTastiness
=
dp
[
w
-
weight
]
+
tastiness
;
if
(
newTastiness
>
dp
[
w
])
{
dp
[
w
]
=
newTastiness
;
quantities
[
w
]
=
[...
quantities
[
w
-
weight
]];
quantities
[
w
][
i
]
+=
1
;
}
}
}
}
}
state
.
result
.
totalTastiness
=
dp
[
maxWeight
];
state
.
result
.
quantities
=
quantities
[
maxWeight
];
},
},
});
export
const
{
setCandies
,
setMaxWeight
,
knapsack
}
=
counterSlice
.
actions
;
export
const
selectCandies
=
(
state
)
=>
state
.
counter
.
candies
;
export
const
selectMaxWeight
=
(
state
)
=>
state
.
counter
.
maxWeight
;
export
const
selectResult
=
(
state
)
=>
state
.
counter
.
result
;
export
default
counterSlice
.
reducer
;
This diff is collapsed.
Click to expand it.
minimal-app/src/index.js
+
1
−
1
View file @
19bc18b0
...
@@ -3,7 +3,7 @@ import { createRoot } from 'react-dom/client';
...
@@ -3,7 +3,7 @@ import { createRoot } from 'react-dom/client';
import
{
Provider
}
from
'
react-redux
'
;
import
{
Provider
}
from
'
react-redux
'
;
import
{
HashRouter
as
Router
}
from
'
react-router-dom
'
;
import
{
HashRouter
as
Router
}
from
'
react-router-dom
'
;
import
{
store
}
from
'
./app/store.js
'
;
import
store
from
'
./app/store.js
'
;
import
{
App
}
from
'
./app.js
'
;
import
{
App
}
from
'
./app.js
'
;
import
'
./index.css
'
;
import
'
./index.css
'
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment