Package: itu.algs4
Algs4 library for Python 3itu.algs4is a Python 3 port of the Java code inAlgorithms, 4th Edition.Target audienceitu.algs4is intended for instructors and students who wish to follow the textbookAlgorithms, 4th Editionby Sedgewick and Wayne.
It was first created in 2018 by teaching assistants and instructors atITU Copenhagen, where the introductory course on Algorithms and Data Structures is taught bilingually in Java and Python 3.InstallationThis library requires a functioning Python 3 environment, for example the one provided byAnaconda.Some optional visual and auditory features depend on thenumpyandpygamepackages. These features are not used in the ITU course, and you shouldn’t spend extra time on installing those packages unless you already have them or want to play around with the those parts on your own.With pipIf you have previously installed this package under its old name, we recommend you remove it withpipuninstallalgs4algs4_pythonThen you can installitu.algs4simply withpipinstallitu.algs4If you have already installeditu.algs4and want to upgrade to a new version, run:pipinstallitu.algs4--upgradeTo test that you have installed the library correctly, run this command:python-c'from itu.algs4.stdlib import stdio; stdio.write("Hello World!")'It should greet you. If an error message appears instead, the library is not installed correctly.Alternative: With pip and gitIf git is available, the following command will install the library in your Python environment:pipinstallgit+https://github.com/itu-algorithms/itu.algs4Alternative: With pip and zipTo install this library without git:Download and unzip the repository.Open a command prompt or terminal and navigate to the downloaded folder. There should be the filesetup.py.Use the commandpip3 install .to install the package (this will also work for updating the package, when a newer version is available).  If your Python installation is system-wide, usesudo pip3 install .Alternative: Step-by-step guide for WindowsTo install the Python packageitu.algs4:Download the repository by pressing the green "Clone or download" button, and pressing "Download ZIP".Extract the content of the zip to your Desktop (you can delete the folder after installing the package).Open the "Command Prompt" by pressing "Windows + R", type "cmd" in the window that appears, and press "OK".If you saved the folder on the Desktop you should be able to navigate to the folder by typing "cd Desktop\itu.algs4-master".C:\Users\user>cd Desktop\itu.algs4-masterWhen in the correct folder, typepip install .to install the package.C:\Users\user\Desktop\itu.algs4-master>pip install .After this, the package should be installed correctly and you can delete the folder from your Desktop.Package structureThe Python packageitu.algs4has a hierarchical structure with seven sub-packages:itu.algs4.fundamentalsitu.algs4.sortingitu.algs4.searchingitu.algs4.graphsitu.algs4.stringsitu.algs4.stdlibitu.algs4.errorsWhile deep nesting of packages is normallydiscouragedin Python, an important design goal ofitu.algs4was to mirror the structure of the original Java code.
The first five packages correspond to the first five chapters ofAlgorithms, 4th Edition. Thestdlibpackage is based on the one from the related bookIntroduction to Programming in Python. The packageerrorscontains some exception classes.All filenames and package names have been written in lower_case style with underscores instead of the CamelCase style of the Java version. For exampleEdgeWeightedDigraph.javahas been renamed toedge_weighted_digraph.py. Class names still use CamelCase though, which is consistent with naming conventions in Python.ExamplesThe directoryexamples/contains examples, some of which are
described here.Hello WorldA simple program, stored as a filehello_world.py, looks like this:fromitu.algs4.stdlibimportstdiostdio.write("Hello World!\n")It can be run with the commandpython hello_world.py.Sort numbersA slightly more interesting example issort-numbers.py:fromitu.algs4.sortingimportmergefromitu.algs4.stdlibimportstdio"""Reads a list of integers from standard input.Then prints it in sorted order."""L=stdio.readAllInts()merge.sort(L)iflen(L)>0:stdio.write(L[0])foriinrange(1,len(L)):stdio.write(" ")stdio.write(L[i])stdio.writeln()This code uses the convenient functionstdio.readAllInts()to read the
integers (separated by whitespaces) from the standard input and put them in the
arrayL. It then sorts the elements of the array. Finally, it outputs the
sorted list -- the code to do so is somewhat less elegant to get the whitespace
exactly right. (Of course, advanced Python users know more concise ways to
produce the same output:print(" ".join(map(str, L))))Import classesYou can import classes, such as the class EdgeWeightedDigraph, withfromitu.algs4.graphs.edge_weighted_digraphimportEdgeWeightedDigraphDocumentationYou can use Python's built-inhelpfunction on any package, sub-package, public class, or function to get a description of what it contains or does. This documentation should also show up in your IDE of choice.
For examplehelp(itu.algs4)yields the following:Help on package itu.algs4 in itu:

NAME
    itu.algs4

PACKAGE CONTENTS
    errors (package)
    fundamentals (package)
    graphs (package)
    searching (package)
    sorting (package)
    stdlib (package)
    strings (package)

FILE
    (built-in)Developmentitu.algs4has known bugs and has not been tested systematically. We are open to pull requests, and in particular, we appreciate the contribution of high-quality test cases, bug-fixes, and coding style improvements. For more information, see theCONTRIBUTING.mdfile.ContributorsAndreas Holck Høeg-PetersenAnton Mølbjerg EskildsenFrederik HaagensenHolger DellMartino SecchiMorten Keller GrøftehaugeMorten Tychsen ClausenNina Mesing Stausholm NielsenOtto Stadel ClausenRiko JacobThore HusfeldtViktor Shamal AndersenLicenseThis project is licensed under the GPLv3 License - see theLICENSEfile for detailsLinks to other projectsalgs4is the original Java implementation by Sedgewick and Wayne.The textbookIntroduction to Programming in Pythonby Sedgewick, Wayne, and Dondero has a somewhat different approach fromAlgorithms, 4th Edition, and is therefore not suitable for a bilingual course. Nevertheless, our code initu/algs4/stdlib/is largely based on thesource codeassociated with that book.pyalgsis a Python port ofalgs4that uses a more idiomatic Python coding style. In contrast, our port tries to stay as close to the original Java library and the course book’s Java implementations as possible, so that it can be used with less friction in a bilingual course.Scala-Algorithmsis a Scala port ofalgs4.Algs4Netis a .NET port ofalgs4.