Skip to main content
Info:

This is the README file for Sum walker.

Author:

Shlomi Fish <shlomif@cpan.org>

Date:
2020-02-25
Version:
0.8.2
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_
import sum_walker.iterator_wrapper


def main():
    def natural_nums_iter():
        ret = 1
        while True:
            yield ret
            ret += 1

    walker = sum_walker.iterator_wrapper.Walker(
        counts=[2], iterator=natural_nums_iter())

    def print_next():
        nonlocal walker
        sum_, coords = next(walker)
        print_("{} = {}".format(
            sum_, " ; ".join(
                [" + ".join([str(x.value) 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, this time using the more beaurocratic sum_walker module:

# 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

Metadata

Release files for sum-walker 0.8.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sum-walker 0.8.2
File Size Uploaded
sum_walker-0.8.2.tar.gz 14.4 kB Details

Release files / sum_walker-0.8.2.tar.gz

Download URL sum_walker-0.8.2.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
648905302ff29ccf9928955b0de73e395e68eab45e2533404cdf60b4c8842247
BLAKE2b-256 checksum
How to use checksums
9e2dadeab72c263d4ec5b29ddd594cfcc1de251549e604a0f5e26d559ea1ef7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.8.2 This release

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.4.0

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page