{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A challenge to transform a list of books into a single string of comma-separated values\n", "# The result should be the string: 'Piers Plowman, The Canterbury Tales, Revelations of Divine Love, The Decameron, Le Morte d'Arthur'\n", "book_list = ['Piers Plowman', 'The Canterbury Tales', 'Revelations of Divine Love', 'The Decameron', \"Le Morte d'Arthur\"]\n", "\n", "## For even more challenge, transform a list of lists containing books and authors into a single string of comma-separated values with an 'and' before the final value. Oxford comma please.\n", "# The result should be the string: 'Piers Plowman by William Langland, The Canterbury Tales by Geoffrey Chaucer, Revelations of Divine Love by Julian of Norwich, The Decameron by Giovanni Boccaccio, and Le Morte d'Arthur by Sir Thomas Malory'\n", "\n", "books_with_authors = [['Piers Plowman', 'William Langland'], ['The Canterbury Tales', 'Geoffrey Chaucer'], ['Revelations of Divine Love', 'Julian of Norwich'], ['The Decameron', 'Giovanni Boccaccio'], [\"Le Morte d'Arthur\", 'Sir Thomas Malory']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# First Challenge" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 1\n", "# For loop\n", "\n", "for book in book_list[:-1]: \n", " print(book + ', ', end='')\n", "print(book_list[-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 2 \n", "# For i in range()\n", "\n", "for i in range(len(book_list) - 1):\n", " print(book_list[i] + ', ', end='')\n", "print(book_list[-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 3\n", "# While loop\n", "\n", "# While loop\n", "i=0\n", "while i < (len(book_list) - 1):\n", " print(book_list[i] + ', ', end='')\n", " i += 1\n", "print(book_list[-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 4\n", "# String join method (we did not learn this join method, but it does exactly what we want)\n", "print(', '.join(book_list))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Second Challenge" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 1\n", "# For loop\n", "for b in books_with_authors[:-1]:\n", " print(b[0] + ' by ' + b[1] + ', ', end='')\n", "print('and ' + books_with_authors[-1][0] + ' by ' + books_with_authors[-1][1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 2\n", "# For i in range()\n", "\n", "for i in range(len(books_with_authors) - 1):\n", " print(books_with_authors[i][0] + ' by ' + books_with_authors[i][1] + ', ', end='')\n", "print('and ' + books_with_authors[-1][0] + ' by ' + books_with_authors[-1][1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 3\n", "# While loop\n", "i=0\n", "while i < (len(books_with_authors) - 1):\n", " print(books_with_authors[i][0] + ' by ' + books_with_authors[i][1] + ', ', end='')\n", " i += 1\n", "print('and ' + books_with_authors[-1][0] + ' by ' + books_with_authors[-1][1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution 4\n", "# String join method (we did not learn this method)\n", "\n", "new_list = []\n", "for b in books_with_authors[:-1]:\n", " new_list.append(str(b[0] + ' by ' + b[1]))\n", "\n", "print(', '.join(new_list) + ', and ' + books_with_authors[-1][0] + ' by ' + books_with_authors[-1][1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }