Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
230 views

Programming Assignment 6: Dynamic Programming 2

This document describes 3 programming challenges involving dynamic programming: 1) Finding the maximum weight of gold that fits in a knapsack of capacity W by selecting bars of gold weights wi ≤ W. 2) Partitioning a set of souvenir values vi into 3 subsets with equal sums, outputting 1 if possible and 0 otherwise. 3) Finding the maximum value of an arithmetic expression by adding parentheses to specify the order of operations.

Uploaded by

Piyush Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views

Programming Assignment 6: Dynamic Programming 2

This document describes 3 programming challenges involving dynamic programming: 1) Finding the maximum weight of gold that fits in a knapsack of capacity W by selecting bars of gold weights wi ≤ W. 2) Partitioning a set of souvenir values vi into 3 subsets with equal sums, outputting 1 if possible and 0 otherwise. 3) Finding the maximum value of an arithmetic expression by adding parentheses to specify the order of operations.

Uploaded by

Piyush Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Assignment 6:

Dynamic Programming 2
Revision: June 11, 2019

Introduction
In this programming assignment, you will continue practicing implementing dynamic programming solutions.

Passing Criteria: 2 out of 3


Passing this programming assignment requires passing at least 2 out of 3 programming challenges from this
assignment. In turn, passing a programming challenge requires implementing a solution that passes all the
tests for this problem in the grader and does so under the time and memory limits specified in the problem
statement.

Contents
1 Maximum Amount of Gold 2

2 Partitioning Souvenirs 3

3 Maximum Value of an Arithmetic Expression 4

1
1 Maximum Amount of Gold
Problem Introduction
You are given a set of bars of gold and your goal is to take as much gold as possible into
your bag. There is just one copy of each bar and for each bar you can either take it or not
(hence you cannot take a fraction of a bar).

Problem Description
Task. Given 𝑛 gold bars, find the maximum weight of gold that fits into a bag of capacity 𝑊 .
Input Format. The first line of the input contains the capacity 𝑊 of a knapsack and the number 𝑛 of bars
of gold. The next line contains 𝑛 integers 𝑤0 , 𝑤1 , . . . , 𝑤𝑛−1 defining the weights of the bars of gold.
Constraints. 1 ≤ 𝑊 ≤ 104 ; 1 ≤ 𝑛 ≤ 300; 0 ≤ 𝑤0 , . . . , 𝑤𝑛−1 ≤ 105 .
Output Format. Output the maximum weight of gold that fits into a knapsack of capacity 𝑊 .

Sample 1.
Input:
10 3
148
Output:
9
Here, the sum of the weights of the first and the last bar is equal to 9.

Starter Files
Starter files contain an implementation of the following greedy strategy: scan the list of given bars of gold
and add the current bar if it fits into the current capacity (note that, in this problem, all the items have the
same value per unit of weight, for a simple reason: they are all made of gold). As you already know from the
lectures, such a greedy move is not safe. You may want to additionally submit a starter file as a solution to
the grading system to ensure that this greedy algorithm indeed might produce a non-optimal result.

Need Help?
Ask a question or see the questions asked by other learners at this forum thread.

Solution
A detailed solution (with Python code) for this challenge is covered in the companion MOOCBook. We
strongly encourage you to do your best to solve the challenge yourself before looking into the book! There
are at least three good reasons for this.
∙ By solving this challenge, you practice solving algorithmic problems similar to those given at technical
interviews.
∙ The satisfaction and self confidence that you get when passing the grader is priceless =)
∙ Even if you fail to pass the grader yourself, the time will not be lost as you will better understand the
solution from the book and better appreciate the beauty of the underlying ideas.

2
2 Partitioning Souvenirs
You and two of your friends have just returned back home after visiting various countries. Now you would
like to evenly split all the souvenirs that all three of you bought.

Problem Description
Input Format. The first line contains an integer 𝑛. The second line contains integers 𝑣1 , 𝑣2 , . . . , 𝑣𝑛 separated
by spaces.

Constraints. 1 ≤ 𝑛 ≤ 20, 1 ≤ 𝑣𝑖 ≤ 30 for all 𝑖.


Output Format. Output 1, if it possible to partition 𝑣1 , 𝑣2 , . . . , 𝑣𝑛 into three subsets with equal sums, and
0 otherwise.

Sample 1.
Input:
4
3333
Output:
0

Sample 2.
Input:
1
40
Output:
0

Sample 3.
Input:
11
17 59 34 57 17 23 67 1 18 2 59
Output:
1
34 + 67 + 17 = 23 + 59 + 1 + 17 + 18 = 59 + 2 + 57.

Sample 4.
Input:
13
1 2 3 4 5 5 7 7 8 10 12 19 25
Output:
1
1 + 3 + 7 + 25 = 2 + 4 + 5 + 7 + 8 + 10 = 5 + 12 + 19.

3
3 Maximum Value of an Arithmetic Expression
Problem Introduction
In this problem, your goal is to add parentheses to a given arithmetic
expression to maximize its value. max(5 − 8 + 7 × 4 − 8 + 9) =?

Problem Description
Task. Find the maximum value of an arithmetic expression by specifying the order of applying its arithmetic
operations using additional parentheses.
Input Format. The only line of the input contains a string 𝑠 of length 2𝑛 + 1 for some 𝑛, with symbols
𝑠0 , 𝑠1 , . . . , 𝑠2𝑛 . Each symbol at an even position of 𝑠 is a digit (that is, an integer from 0 to 9) while
each symbol at an odd position is one of three operations from {+,-,*}.

Constraints. 1 ≤ 𝑛 ≤ 14 (hence the string contains at most 29 symbols).


Output Format. Output the maximum possible value of the given arithmetic expression among different
orders of applying arithmetic operations.

Sample 1.
Input:
1+5
Output:
6

Sample 2.
Input:
5-8+7*4-8+9
Output:
200
200 = (5 − ((8 + 7) × (4 − (8 + 9))))

Need Help?
Ask a question or see the questions asked by other learners at this forum thread.

You might also like