473,407 Members | 2,359 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,407 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 7143

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.