Created
March 13, 2018 14:26
-
-
Save ChristopherBilg/e7a8cf89e3ddda03642d832213877af9 to your computer and use it in GitHub Desktop.
r/DailyProgrammer Bookshelf Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
500 500 500 | |
1309 Artamene | |
303 A la recherche du temps perdu | |
399 Mission Earth | |
500 500 500 | |
1309 Artamene | |
303 A la recherche du temps perdu | |
399 Mission Earth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!usr/bin/env python3 | |
""" | |
This script is the r/DailyProgrammer Easy Challenge #350 titled | |
Bookshelf problem. | |
Author: Christopher Bilger | |
""" | |
class Book(): | |
""" | |
This class handles the individual book that will be placed onto a bookshelf | |
""" | |
def __init__(self, book_info): | |
self.book_width = book_info.split(" ")[0] | |
self.book_title = book_info.split(" ", 1)[1] | |
def get_width(self): | |
""" | |
Getter method for the Book.book_width | |
@return int The book's width; needed to check if it will fit on a shelf | |
""" | |
return int(self.book_width) | |
def get_title(self): | |
""" | |
Getter method for the Book.book_title | |
@return String The title of the book | |
""" | |
return self.book_title | |
class Bookshelf(): | |
""" | |
This class handles the shelves that will hold the individual books | |
""" | |
def __init__(self, bookshelf_width): | |
self.bookshelf_width = int(bookshelf_width) | |
def get_available_width(self): | |
""" | |
Getter method for the Bookshelf.width | |
@return int The width of space currently available on the shelf | |
""" | |
return int(self.bookshelf_width) | |
def add_book(self, book): | |
""" | |
Method used for adding a book to the shelf; reducing the available | |
width on the shelf by the width of the book | |
@param Book The book to be added to the shelf | |
""" | |
self.bookshelf_width -= book.get_width() | |
def main(): | |
""" | |
Method that will run the actual code to put books onto the shelves | |
""" | |
bookshelves = [] | |
books = [] | |
def peek_first_line(data_file): | |
""" | |
Method that peeks at the first line of a file | |
""" | |
position = data_file.tell() | |
line = data_file.readline() | |
data_file.seek(position) | |
return line | |
def find_largest_bookshelf(all_shelves): | |
""" | |
Method that will find the largest bookshelf given a list | |
of bookshelves. | |
""" | |
a = all_shelves | |
changed = True | |
while changed: | |
changed = False | |
for p in range(len(a) - 1): | |
if a[p].get_available_width() > a[p + 1].get_available_width(): | |
a[p], a[p + 1] = a[p + 1], a[p] | |
changed = True | |
return a[-1] | |
with open("book_info.txt", "r", encoding="utf-8") as book_info: | |
for starting_width in peek_first_line(book_info)[:-1].split(" "): | |
bookshelves.append(Bookshelf(starting_width)) | |
for book in book_info.readlines()[1:]: | |
books.append(Book(book)) | |
for each_book in books: | |
largest_shelf = find_largest_bookshelf(bookshelves) | |
if largest_shelf.get_available_width() >= each_book.get_width(): | |
largest_shelf.add_book(each_book) | |
else: | |
print("Not all books will fit.") | |
return | |
print("All book were able to fit.") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment