示例#1
0
def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


def by_total_length(book_a, book_b):
    return len(book_a['author_lower']) + len(book_a['title_lower']) > len(
        book_b['author_lower']) + len(book_b['title_lower'])


# Comparison functions end

bookshelf = utils.load_books('books_small.csv')
bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()

long_bookshelf = utils.load_books('books_large.csv')

sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)
sort_2 = sorts.bubble_sort(bookshelf, by_author_ascending)
# sort_3 = sorts.bubble_sort(long_bookshelf, by_total_length)
'''
for book in sort_1:
  print(book['title'])

for book in sort_2:
  print(book['author'])
示例#2
0
import utils
import sorts

def by_title_ascending(book_a, book_b):
    return book_a["title_lower"] > book_b["title_lower"]

def by_author_ascending(book_a, book_b):
    return book_a["author_lower"] > book_b["author_lower"]

def by_total_length(book_a, book_b):
    book_a_len = len(book_a["title"]) + len(book_a["author"])
    book_b_len = len(book_b["title"]) + len(book_b["author"])

    return book_a_len > book_b_len

bookshelf = utils.load_books("books_small.csv")

sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)
for book in sort_1:
    print("{} - {}".format(book["title"], book["author"]))

bookshelf_v1 = bookshelf.copy()
sort_2 = sorts.bubble_sort(bookshelf_v1, by_author_ascending)
for book in sort_2:
    print("{} - {}".format(book["title"], book["author"]))

bookshelf_v2 = bookshelf.copy()
sorts.quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_author_ascending)
for book in bookshelf_v2:
    print("{} - {}".format(book["title"], book["author"]))
示例#3
0
import utils
import sorts

bookshelf = utils.load_books('./A Sorted Tale/books_small.csv')
# for item in bookshelf:
#     print(item['title'])


def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


def by_total_length(book_a, book_b):
    return len(book_a['author']) + len(book_a['title']) > len(
        book_b['author']) + len(book_b['title'])


sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)
# for item in sort_1:
#     print(item['title'])

bookshelf_v1 = bookshelf.copy()
sort_2 = sorts.bubble_sort(bookshelf_v1, by_author_ascending)
# for item in sort_2:
#     print(item['author'])

bookshelf_v2 = bookshelf.copy()
示例#4
0
import utils
import sorts

bookshelf = utils.load_books('books_small.csv')
bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()
long_bookshelf = bookshelf.copy()
def by_title_ascending(book_a,book_b):
  if book_a['title_lower'] > book_b['title_lower']:
    return True
  return False

def by_author_ascending(book_a,book_b):
  return book_a['author_lower'] > book_b['author_lower']

def by_total_length(book_a,book_b):
  return len(book_a['author_lower'])+len(book_a['title_lower']) > len(book_b['author_lower'])+len(book_b['title_lower'])

print('\nPerforming Quicksort\n')
sort1 = sorts.bubble_sort(bookshelf,by_title_ascending)
sort2 =sorts.bubble_sort(bookshelf_v1,by_author_ascending)
sort3 = sorts.bubble_sort(long_bookshelf,by_total_length)
sorts.quicksort(long_bookshelf,0,len(long_bookshelf)-1,by_total_length)
sorts.quicksort(bookshelf_v2,0,len(bookshelf_v2)-1,by_author_ascending)

print('\n Sorting the Bookshelf based on Title : Bubble Sort\n')
for book in sort1:
  print(book['title'])clear
print('\n Sorting the Bookshelf based on Author : Bubble Sort\n')
for book in sort2:
  print(book['author'])
示例#5
0
import utils
import sorts
from sorts import bubble_sort, quicksort

bookshelf = utils.load_books(
    "E:/git/codeacademy_python/12_sorting/project_sorted_tale/a.csv")
long_bookshelf = utils.load_books(
    "E:/git/codeacademy_python/12_sorting/project_sorted_tale/b.csv")
bookshelf_copy_v1 = bookshelf.copy()
bookshelf_copy_v2 = bookshelf.copy()
bookshelf_copy_v3 = bookshelf.copy()


def display_unicode(letter):
    if type(letter) == str:
        print("Unicode value for \'{}\': {}".format(letter, ord(letter)))
    else:
        print('Expect a string.')


def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a["author_lower"] > book_b["author_lower"]


def by_total_length(book_a, book_b):
    return len(book_a["title_lower"]) + len(book_a["author_lower"]) > len(
        book_b["title_lower"]) + len(book_b["author_lower"])
示例#6
0
import utils
import sorts

bookshelf = utils.load_books('books_large.csv')


def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


def by_author_ascending(book_a, book_b):
    return book_a['author_lower'] > book_b['author_lower']


def by_total_length(book_a, book_b):
    return (len(book_a['title']) + len(book_a['author'])) > (
        len(book_b['title']) + len(book_b['author']))


#bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()
#sort_2 = sorts.bubble_sort(bookshelf_v1, by_author_ascending)
sorts.quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_author_ascending)
#sorts.quicksort(bookshelf_v2, 0, len(bookshelf_v2) - 1, by_total_length)

for book in bookshelf_v2:
    print(book['author'])
import utils
import sorts
import os

path = os.path.dirname(__file__)
book_small_csv__path = os.path.join(path, 'books_small.csv')
book_large__csv_path = os.path.join(path, 'books_large.csv')

bookshelf = utils.load_books(book_small_csv__path)
bookshelf_copy_one = bookshelf.copy()
bookshelf_copy_two = bookshelf.copy()

print()
for book in bookshelf:
    print(book["title_lower"])


# Title comparison function:
def by_title_ascending(book_a, book_b):
    return book_a['title_lower'] > book_b['title_lower']


bubble_sorted_by_title = sorts.bubble_sort(bookshelf, by_title_ascending)

print()
for book in bubble_sorted_by_title:
    print(book['title'])


# Author comparison function:
def by_author_ascending(book_a, book_b):
import utils
import sorts

bookshelf = utils.load_books(
    'C:/Users/Jonathan/LambdaSchoolProjects/python-data-structures-alogos/sorting/A_Sorted_Tale/books_small.csv'
)
bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()

long_bookshelf = utils.load_books(
    'C:/Users/Jonathan/LambdaSchoolProjects/python-data-structures-alogos/sorting/A_Sorted_Tale/books_large.csv'
)


def by_title_ascending(book_a, book_b):
    """
    This function will be used to compare two books and returns 
    true or false based off if book a title_lower is greater than book b title_lower
    comparision done on the books  title_lower key. 
    """
    return book_a['title_lower'] > book_b['title_lower']


# end of by_title_ascending


def by_author_ascending(book_a, book_b):
    """
    This function will be used to compare two books and returns
    true or false based off if book a author_lower is greater than book b
    author_lower keys.