Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Forgotten Code
#1
I wrote this code about a year ago and barely understood it then, could someone please break down for me what my code is doing as I am highly confused. Very new to the Python programming language and am trying to get better.

I wrote this in Spyder if that helps.
Edit: This is a program to prove the fact that a descending sort is faster than an unordered sort in the well known bin packing problem.


from random import triangular
import matplotlib.pyplot as plt
import numpy as np
           
def packing(books,boxSize = 1.0):
    totalBooks=len(books)
    boxes=np.zeros(totalBooks)
    for book in books:
        for i,box in enumerate(boxes):
            if book + box <= boxSize:
                boxes[i] += book
                break
            
    return boxes[boxes>0.0]

def bookCreation(totalBooks, sorting):
    bookSet=[triangular(0, 1, 0.4) for i in range(totalBooks)]
    bookSet.sort(reverse=sorting)
    return bookSet
    
def increaseBookTotal(totalBooks, increment = 5):
    sorting = [False, True]
    descending = []
    unordered = []
    nbooks=[]
    localBooks = totalBooks
    
    for sort in sorting:
        totalBooks = localBooks
        for i in range(100):
            boxesAverage = 0.0
            if sort: nbooks.append(totalBooks)
            for j in range(100):

                boxes = packing(bookCreation(totalBooks, sort)).shape[0]
                boxesAverage+=(boxes-boxesAverage)/(1.0+j)
            if sort:
                descending.append(boxesAverage)
            else:

                unordered.append(boxesAverage)
                
            totalBooks += increment
            
    return [nbooks,descending, unordered]

def percentage(totalBooks):
    
    
    nbooks,descending,unordered = increaseBookTotal(totalBooks)
    ratios=np.array(descending)/np.array(unordered)

    return nbooks,ratios
    

totalBooks = 20

nbooks,ratios=percentage(totalBooks)
plt.plot(nbooks,ratios)
plt.ylabel('Improvement.')
plt.xlabel('Number of Books.')
plt.savefig('graph.png')
plt.show()
Reply
#2
"You wrote it" , yea yea.
In 1 year you forget totally that.
Atleast try not to lie.
The code it's not as hard.
Go ahead and "re-learn" what you "forgot"
Reply
#3
(Apr-30-2017, 08:47 AM)Turry Wrote: "You wrote it" , yea yea. In 1 year you forget totally that. Atleast try not to lie. The code it's not as hard. Go ahead and "re-learn" what you "forgot"

I'm sorry to burst your bubble but I'm a 16 year old doing GCSEs who wrote this on work experience last year with someone who knew a lot about python. I wrote 90% of this code and had some help but as I code in Visual Basic at school not python I can't remember how it works. I only found this code on my old laptop about an hour ago so I thought I would ask about it. Sorry for asking for help.
Reply
#4
I don't really understand the code, it's using some numpy stuff I'm not familiar with. I would say this is a perfect reason to comment your code to explain what each piece of it does. The person who will need to understand the code may well be you, a year later.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Apr-30-2017, 10:51 AM)ichabod801 Wrote: I don't really understand the code, it's using some numpy stuff I'm not familiar with. I would say this is a perfect reason to comment your code to explain what each piece of it does. The person who will need to understand the code may well be you, a year later.

Thanks for the advice.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020