Skip to main content

Python 3 port of the Java code in "Algorithms, 4th Edition" by Sedgewick and Wayne

Project description

Algs4 library for Python 3

itu.algs4 is a Python 3 port of the Java code in Algorithms, 4th Edition.

Build Status

Target audience

itu.algs4 is intended for instructors and students who wish to follow the textbook Algorithms, 4th Edition by Sedgewick and Wayne. It was first created in 2018 by teaching assistants and instructors at ITU Copenhagen, where the introductory course on Algorithms and Data Structures is taught bilingually in Java and Python 3.

Installation

This library requires a functioning Python 3 environment, for example the one provided by Anaconda.

Some optional visual and auditory features depend on the numpy and pygame packages. 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 pip

If you have previously installed this package under its old name, we recommend you remove it with

pip uninstall algs4 algs4_python

Then you can install itu.algs4 simply with

pip install itu.algs4

If you have already installed itu.algs4 and want to upgrade to a new version, run:

pip install itu.algs4 --upgrade

To 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 git

If git is available, the following command will install the library in your Python environment:

pip install git+https://github.com/itu-algorithms/itu.algs4

Alternative: With pip and zip

To install this library without git:

  1. Download and unzip the repository.
  2. Open a command prompt or terminal and navigate to the downloaded folder. There should be the file setup.py.
  3. Use the command pip3 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, use sudo pip3 install .

Alternative: Step-by-step guide for Windows

To install the Python package itu.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-master
  • When in the correct folder, type pip 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 structure

The Python package itu.algs4 has a hierarchical structure with seven sub-packages:

While deep nesting of packages is normally discouraged in Python, an important design goal of itu.algs4 was to mirror the structure of the original Java code. The first five packages correspond to the first five chapters of Algorithms, 4th Edition. The stdlib package is based on the one from the related book Introduction to Programming in Python. The package errors contains 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 example EdgeWeightedDigraph.java has been renamed to edge_weighted_digraph.py. Class names still use CamelCase though, which is consistent with naming conventions in Python.

Examples

The directory examples/ contains examples, some of which are described here.

Hello World

A simple program, stored as a file hello_world.py, looks like this:

from itu.algs4.stdlib import stdio

stdio.write("Hello World!\n")

It can be run with the command python hello_world.py.

Sort numbers

A slightly more interesting example is sort-numbers.py:

from itu.algs4.sorting import merge
from itu.algs4.stdlib import stdio

"""
Reads a list of integers from standard input.
Then prints it in sorted order.
"""
L = stdio.readAllInts()

merge.sort(L)

if len(L) > 0:
    stdio.write(L[0])
for i in range(1, len(L)):
    stdio.write(" ")
    stdio.write(L[i])
stdio.writeln()

This code uses the convenient function stdio.readAllInts() to read the integers (separated by whitespaces) from the standard input and put them in the array L. 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 classes

You can import classes, such as the class EdgeWeightedDigraph, with

from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph

Documentation

You can use Python's built-in help function 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 example help(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)

Development

itu.algs4 has 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 the CONTRIBUTING.md file.

Contributors

  • Andreas Holck Høeg-Petersen
  • Anton Mølbjerg Eskildsen
  • Frederik Haagensen
  • Holger Dell
  • Martino Secchi
  • Morten Keller Grøftehauge
  • Morten Tychsen Clausen
  • Nina Mesing Stausholm Nielsen
  • Otto Stadel Clausen
  • Riko Jacob
  • Thore Husfeldt
  • Viktor Shamal Andersen

License

This project is licensed under the GPLv3 License - see the LICENSE file for details

Links to other projects

  • algs4 is the original Java implementation by Sedgewick and Wayne.
  • The textbook Introduction to Programming in Python by Sedgewick, Wayne, and Dondero has a somewhat different approach from Algorithms, 4th Edition, and is therefore not suitable for a bilingual course. Nevertheless, our code in itu/algs4/stdlib/ is largely based on the source code associated with that book.
  • pyalgs is a Python port of algs4 that 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-Algorithms is a Scala port of algs4.
  • Algs4Net is a .NET port of algs4.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

itu.algs4-0.2.5.tar.gz (89.5 kB view details)

Uploaded Source

Built Distribution

itu.algs4-0.2.5-py3-none-any.whl (160.9 kB view details)

Uploaded Python 3

File details

Details for the file itu.algs4-0.2.5.tar.gz.

File metadata

  • Download URL: itu.algs4-0.2.5.tar.gz
  • Upload date:
  • Size: 89.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for itu.algs4-0.2.5.tar.gz
Algorithm Hash digest
SHA256 83836d60437f02f2ea7d59fc366448a02a3191459ac7080d75367de4bd6fcef9
MD5 7ee195ac387acfb84ddce662f4b219c7
BLAKE2b-256 757b74e0fe11e6c69d0b5137c23427df3315bb0de30a87b03799ecf9485cfd9f

See more details on using hashes here.

File details

Details for the file itu.algs4-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: itu.algs4-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 160.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for itu.algs4-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5060b9057d5955e087e07f218c3bb0900b4bafd182029d27b6a8b0f2dde52ded
MD5 6649d267f1eeebd9eef3835f07050cea
BLAKE2b-256 61606ec3b5c7f853db4e942c3d7213028958fa5b139c84597342758879ec8645

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page