473,414 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,414 developers and data experts.

Knapsack 0-1 Python binary & rosettacode & WE

24 16bit
Knapsack 0-1 Python binary & rosettacode & WE

Classic Knapsack problem is solved in many ways

My newest program synthesizes all ciphers from 0 & 1
adding an extra register and 0 remain on left in cipher

Number of comparisons decreases from N! to 2^N
for example N=10 & N!=3628800 >> 2^N=1024

Random values origin are automatically assigned
quantity and quality and integral of value is obtained
and in general: integral of quantity and quality

Expand|Select|Wrap|Line Numbers
  1. n=5; N=n+1; G=5; a=2**N # KNAPSACK 0-1 DANILIN  
  2. L=[];C=[];e=[];j=[];q=[];s=[] # rextester.com/BCKP19591
  3. d=[];L=[1]*n;C=[1]*n;e=[1]*a    
  4. j=[1]*n;q=[0]*a;s=[0]*a;d=[0]*a
  5.  
  6. from random import randint
  7. for i in range(0,n):
  8.     L[i]=randint(1,3)
  9.     C[i]=10+randint(1,9)
  10.     print(i+1,L[i],C[i])
  11. print()
  12.  
  13. for h in range(a-1,(a-1)//2,-1):
  14.     b=str(bin(h))
  15.     e[h]=b[3:len(b)]
  16.  
  17.     for k in range (n):
  18.         j[k]=int(e[h][k])
  19.         q[h]=q[h]+L[k]*j[k]*C[k]
  20.         d[h]=d[h]+L[k]*j[k]
  21.  
  22.     if d[h]<= G:
  23.         print(e[h], G, d[h], q[h])
  24. print()   
  25.  
  26. max=0; m=1 
  27. for i in range(a):
  28.     if d[i]<=G and q[i]>max:
  29.         max=q[i]; m=i   
  30. print (d[m], q[m], e[m])
Main thing is very brief and clear to even all

Results is reduced manually:

Expand|Select|Wrap|Line Numbers
  1.    # Mass Cost
  2.     1 2 12
  3.     2 3 17
  4.     3 1 14
  5.     4 3 17
  6.     5 1 13
  7.     Chifer Mass Cost 
  8.     11000 5 5 75
  9.     01001 5 4 64
  10.     00111 5 5 78 !!!
  11.     00110 5 4 65
  12.     00101 5 2 27
  13.     Mass MAX Chifer
  14.     5 78 00111
Nov 25 '22 #1
0 7145

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Phillip | last post by:
Some people tipped me off on some possibilities to tackle my https problem. Those have definitely gotten me further in cornering the problem. Thank you. But: No matter what I do to open a...
3
by: peetm | last post by:
Hi. I'd like to compile (?) the DiffLib Python code into a binary form that can be called by other Windows apps - like, I'd like to compile it into a DLL. Is this possible? Many thanks!
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
20
by: William | last post by:
Original question: "Give a one-line C expression to test whether a number is a power of 2. " Answer: if (x && !(x & (x-1)) == 0) My question: Why does this expression work?
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
4
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
2
by: CRAIG DALTON | last post by:
Hi, I'm looking to append several text files in one director and out put the combined files into another director. I'm new to Python and just can't get itto work. So far I've been able to create...
1
by: DANILIN | last post by:
Knapsack 0-1 C# binary & rosettacode & WE Classic Knapsack problem is solved in many ways My newest program synthesizes all ciphers from 0 & 1 adding an extra register and 0 remain on left in...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.