Skip to main content

Iterate over sums of a certain number of elements

Project description

Info:

This is the README file for Sum walker.

Author:

Shlomi Fish <shlomif@cpan.org>

Date:
2020-02-25
Version:
0.6.4
https://travis-ci.org/shlomif/sum_walker.svg?branch=master

PURPOSE

The sum_walker PyPI distribution allows one to iterate over increasing sums of a certain number (e.g: 2 or 3) of elements out of a stream of increasing integers.

INSTALLATION

pip3 install sum_walker

USAGE

A simple example of printing sums of two integers:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 Shlomi Fish <shlomif@cpan.org>
#
# Distributed under the terms of the MIT license.
#
# This program displays increasing sums of two positive integers

from six import print_

from sum_walker import DWIM_SumWalker


def main():
    seq = [1, 2, 3, 4]

    def request_more():
        nonlocal seq
        seq.append(seq[-1] + 1)

    it = DWIM_SumWalker(2, seq, request_more)

    def print_next():
        nonlocal it
        sum_, coords = next(it)
        print_("{} = {}".format(
            sum_, " ; ".join(
                [" + ".join([str(seq[x]) for x in permutation])
                 for permutation in coords])))

    # Prints «2 = 1 + 1»
    print_next()

    # Prints «3 = 1 + 2»
    print_next()

    # Prints «4 = 1 + 3 ; 2 + 2»
    print_next()

    # Prints «5 = 1 + 4 ; 2 + 3»
    print_next()

    # Prints «6 = 1 + 5 ; 2 + 4 ; 3 + 3»
    print_next()


main()

A more interesting example:

# Finding sums of two powers of 3 (= i**3 ) in two or more
# different ways:
#
# https://en.wikipedia.org/wiki/Taxicab_number
from sum_walker import DWIM_SumWalker

seq = [0, 1]
reached = 2

def request_more():
    nonlocal reached
    nonlocal seq
    seq.append(reached ** 3)
    reached += 1

it = DWIM_SumWalker(2, seq, request_more)
for sum_, coords in it:
    len_ = len(coords)
    if len_ > 1:
        print("{}\t{}\t{}".format(
            len_, sum_, " ; ".join(
                [" + ".join(["{} ** 3".format(x) for x in c])
                 for c in coords])))

NOTES

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

sum_walker-0.6.4.tar.gz (13.6 kB view details)

Uploaded Source

File details

Details for the file sum_walker-0.6.4.tar.gz.

File metadata

  • Download URL: sum_walker-0.6.4.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.2

File hashes

Hashes for sum_walker-0.6.4.tar.gz
Algorithm Hash digest
SHA256 9dc8cb94cb7cdd061b5135adcb7f85495417aa473dfdd9d4dc9666767f8cdc2a
MD5 db3bef69069d698ee56b9a5b88559047
BLAKE2b-256 b9bb3792773dc6d4309f29c8ed7c015dc8d06716e314dd758e9a820e05724123

See more details on using hashes here.

Supported by

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