Question

In: Computer Science

PLEASE I HAVE TO RETURN IN 20 MINUTES, I HAVE ALREADY DO THE QUESTION 1, 2,...

PLEASE I HAVE TO RETURN IN 20 MINUTES, I HAVE ALREADY DO THE QUESTION 1, 2, 3, 4 BUT THE QUESTION 5 I CAN'T

In python :

1)Write a sum function expecting an array of integers and returning its sum.

2) Write a multiply function excepting two arrays of integers and returning the product array term by term.

For example mutliply([1,2,3], [5,4,3]) will give [5,8,9]

3) Write a nb1 function expecting an array containing only 0 and 1 and returning the number of 1 present in the array.

4)Write a conFilter function expecting an integer n and creating an array of size n randomly filled with 0 and 1.

5) With these functions offer a random solution to the backpack problem. Here are some indications:

- The consFilter function allows you to construct an array retaining only one part of the objects to place in your backpack. So if the list of objects is [1,4,6,1] and the filter is [1,0,1,0], then by doing the product of the two you will get [1,0,6,0].

- You can generate a large number of random filters, for each of these filters to test if the objects thus filtered fit in the backpack. The rest comes down to a maximum calculation.

- You must make the best list of the weights of the objects fitting in the backpack. For example, if your backpack can hold 20kg, the list of items is [10, 1, 5, 8, 3, 9], one of the ways to optimize your bag is to put [10, 1, 3, 5] or 19kg.

Test on tables of 10-15 objects between 1 and 100kg each for a bag weight of 200 to 300kg.

Solutions

Expert Solution

There are two functions for part 5 in the code below. Modify no. of iterations for more optimised answer for second function.

from random import randint
def sum(L):
    s = 0
    for x in L:
        s += x
    return s

def multiply(L, F):
    C = [L[i] * F[i] for i in range(len(L)) ]
    return C

def nb1(A):
    count = 0
    for x in A:
        if x == 1:
            count += 1
    return count

# uses all possible filters
def consFilter(L, maxCapacity):
    n = len(L)
    maxSum = 0
    optimumSequence = None
    # generate all filters
    for i in range(pow(2, n)):
        # convert i to binary and fill with zeroes
        binary_string = f'{i:b}'.zfill(n)
        F = [int(x) for x in binary_string]
        
        P = multiply(L, F)
        Sum = sum(P)
        
        if Sum <= maxCapacity and Sum > maxSum:
            maxSum = Sum
            optimumSequence = P
    return maxSum, optimumSequence

# generates N filter randomly
def consFilterRandom(L, maxCapacity, N):
    n = len(L)
    maxSum = 0
    optimumSequence = None
    for i in range(N):
        # list of n random integers bw 0 and 1
        F = [ randint(0, 1) for i in range(n) ]
        P = multiply(L, F)
        Sum = sum(P)
        
        if Sum <= maxCapacity and Sum > maxSum:
            maxSum = Sum
            optimumSequence = P
    return maxSum, optimumSequence

maxCapacity = int(input())
objects = [int(x) for x in input().split()]

print(consFilter(objects, maxCapacity))

iterations = 100
print(consFilterRandom(objects, maxCapacity, iterations))

Screenshots:


Related Solutions

*********Please do not answer the question partially. I have already done step #1, now just needing...
*********Please do not answer the question partially. I have already done step #1, now just needing to complete all of the other steps (2-10)  an explantion included would be nice as well, I would appriciate it! :) *If you can not help completely or entirely; to the furthest extent please do not leave an answer******* Question 1        Required: #1. Prepare journal entries to record the December transactions in the General Journal Tab in the excel template file "Accounting Cycle Excel Template.xlsx"....
INSTRUCTIONS: I HAVE ALREADY ANSWERED QUESTION 1 AND 2. I NEED ASSISTANCE WITH QUESTIONS 3 AND...
INSTRUCTIONS: I HAVE ALREADY ANSWERED QUESTION 1 AND 2. I NEED ASSISTANCE WITH QUESTIONS 3 AND 4. I HAVE FILLED OUT THE PERCENTAGE CHANGE FOR QUESTION 3, AND NEED HELP ON CALCULATING THE OPERATING, INVESTING, AND FINANCIAL SECTIONS. AS WELL AS, THE EQUATIONS FOR QUESTION 4. IF YOU CAN ANSWER QUESTIONS 3 & 4 I WILL AWARD CREDIT. Question 1: Common size for income statement Income Statement (Common Size) :                                                                  Consolidated Income Statement 2011 % 2010 % Revenue $19,176.1...
QUESTION 2: LOAN AMORTIZATION [35 MARKS] Please answer iii only. I already have the answer for...
QUESTION 2: LOAN AMORTIZATION [35 MARKS] Please answer iii only. I already have the answer for i and ii. I. A family buys a house worth $326,000. They pay $75,000 deposit and take a mortgage for the balance at J12=9% p.a. to be amortized over 30 years with monthly payments. A. Find the value of the mortgage on their house? (1 mark) B. Find the value of the monthly payment? C. Find the loan outstanding after making 20 payments? D....
Please note, questions 1 and 2 have already been answered and are listed down below. I...
Please note, questions 1 and 2 have already been answered and are listed down below. I need help with question 3. Case Study: Topic and Detailed Instructions A central theme of contemporary operations management is focus on the customer. This is commonly understood to mean that if a company does focus on its customers and it if is able to consistently deliver what the customer wants in a cost-effective manner, then the company should be successful. The hard part is...
BEFORE YOU ANSWER THIS QUESTION, PLEASE READ: I HAVE ALREADY POSTED THIS QUESTION MULTIPLE TIMES, AND...
BEFORE YOU ANSWER THIS QUESTION, PLEASE READ: I HAVE ALREADY POSTED THIS QUESTION MULTIPLE TIMES, AND I NEED HELP WITH THE JOURNAL ENTRIES. PLEASE INCLUDE THEM IN THE ANSWER. THANK YOU Overhead Variances, Four-Variance Analysis, Journal Entries Laughlin, Inc., uses a standard costing system. The predetermined overhead rates are calculated using practical capacity. Practical capacity for a year is defined as 1,000,000 units requiring 200,000 standard direct labor hours. Budgeted overhead for the year is $750,000, of which $300,000 is...
Can someone please explain how to do the steps for this? I already have part a...
Can someone please explain how to do the steps for this? I already have part a completed but I need help with b-e. Thank you. Problem 1: (a) What is spurious regression? Explain. (b) Assuming , randomly generate 1000 observations of variables X and Y using the following equations: Report the graphs of X and Y. (c) Run the regression: and report the estimated results. (d) What did you expect about the magnitudes and R-square? How are the estimated values...
Please answer this question and do steps 1-7. I've already posted this question and they did...
Please answer this question and do steps 1-7. I've already posted this question and they did not complete the answer. PB2-3    Recording Transactions (in a Journal and T-Accounts); Preparing and Interpreting the Balance Sheet (LO 2-2, 2-3, 2-4, 2-5 and a little Chapter 1) Starbuck$ is a coffee company – a big coffee company. During a 10-year period, the number of Starbucks locations grew from 165 to over 8,500 stores in 50 countries. The following is adapted from Starbucks’s annual...
#2 I post this question twice, please don't answer this post if you already answer the...
#2 I post this question twice, please don't answer this post if you already answer the other one, if you can answer different answer that's fine. REASON WHY I POST IT TWICE, I NEED TWO DIFFERENT VIEW. Discussion: Suicidal Crickets, Zombies Roaches and Other Parasites Tales Please watch the TED Talk ‘Suicidal crickets, zombies roaches and other parasite tales' and post something that you learn as well as whether you believe there could be parasites living inside of humans possibly...
Windows PowerShell Scripting Please answer using a switch statement. I have asked this question already and...
Windows PowerShell Scripting Please answer using a switch statement. I have asked this question already and it was wrong. Thank you and I will upvote. 1) Write a PowerShell script that displays the total count of each type (dll, txt, log) of files in c:\windows using switch statement.
PLEASE DO ONLY THE "JOURNAL" SECTION. I ALREADY HAVE ANSWERS TO ALL THE OTHER ONES!!! Beacon...
PLEASE DO ONLY THE "JOURNAL" SECTION. I ALREADY HAVE ANSWERS TO ALL THE OTHER ONES!!! Beacon Signals Company maintains and repairs warning lights, such as those found on radio towers and lighthouses. Beacon Signals Company prepared the following end-ofperiod spreadsheet at December 31, 2019, fiscal year: Beacon Signals Company End-of-Period Spreadsheet For the Year Ended December 31, 2019 Unadjusted Trial Balance Adjustments Adjusted Trial Balance Account Title Dr. Cr. Dr. Cr. Dr. Cr. Cash 13,000.00 13,000.00 Accounts Receivable 40,500.00 (a)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT