Skip to content

Instantly share code, notes, and snippets.

@duncan60
Last active July 11, 2019 03:39
Show Gist options
  • Save duncan60/cce794d7849d94b8c7f426512120ba49 to your computer and use it in GitHub Desktop.
Save duncan60/cce794d7849d94b8c7f426512120ba49 to your computer and use it in GitHub Desktop.
online edit: https://repl.it/languages/python3
1)Exercise.
Given a two list of equal size create a set such that it shows the element from both lists in the pair
For example:
firstList = [1, 2, 3, 4, 5]
secondList = [10, 20, 30, 40, 50]
Expected Output:
result = {(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)}
2)Exercise.
Transpose of a matrix is the interchanging of rows and columns. It is denoted as X'.
The element at ith row and jth column in X will be placed at jth row and ith column in X'.
Given X = [[12, 7], [4, 5], [3, 8]], if X is a 3x2 matrix, X' will be a 2x3 matrix.
Use Nested Loop and Nested List Comprehension to design your program.
3)Exercise.
Jack is a thief. After months of careful planning, he finally manages to crack the security systems
of a high-class apartment.
In front of him are many items, each with a value (v) and weight (w). Jack, of course, wants to
maximize the total value he can get; he would gladly take all of the items if he could.
However, to his horror, he realizes that the knapsack he carries with him can only hold so much weight (W).
Given a knapsack with a specific carrying capacity (W), help Jack determine the maximum
value he can get from the items in the house. Note that Jack can take only one of each item.
All values given will be strictly positive. Items will be represented as a list of pairs,
wi and vi, where the first element wi is the weight of the ith item and vi is the value for that item.
For example:
Items: [ { "weight": 5, "value": 10 }, { "weight": 4, "value": 40 }, { "weight": 6, "value": 30 }, { "weight": 4, "value": 50 } ]
Knapsack Limit: 10
For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on.
In this example, Jack should take the second and fourth item to maximize his value, which, in this case, is 90.
He cannot get more than 90 as his knapsack has a weight limit of 10.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment