Skip to main content

istr description

Project description

Introduction

The istrlib module has exactly one class: istr.

With this it is possible to interpret string as if they were integers. This can be very handy for solving puzzles, but also for other purposes.

Installation

Installing istrlib with pip is easy.

$ pip install istrlib

or when you want to upgrade,

$ pip install istrlib --upgrade

Alternatively, istrlib.py can be just copied into you current work directory from GitHub (https://github.com/salabim/istrlib).

No dependencies!

Usage

Just start with

from istrlib import istr

Now we can define some istrs:

four = istr("4")
five = istr("5")

Them we can do

x= four * five

, after which x is istr("40")

And now we can do

print(x == 40)
print(x == "40")

resulting in two times True. That's because istr instances are treated as int, although they are strings.

That means that we can also say

print(x < 50)
print(x >= "30")

again resulting in two times True.

In contrast to an ordinary string

print(four + five)

prints 9, as istr are treated as ints.

So, how can we concatenate istrs? Just use the or operator (|):

print(four | five)

will output 45.

And the result is again an istr.

That means that

(four | five) / 3

is istr("9").

In order to multiply a string in the usual sense, you cannot use 3 * four, as that will be 12.

We use the matrix multiplication operator (@) for this. So 3 @ four is 444.

Also allowed are

abs(four)
-four 

For the in operator a istr is treated as an ordinary string, although it is possible to use ints as well:

"34" in istr(1234)
34 in istr(1234)

On the left hand side an istr is always treated as a string:

istr(1234) in "01234566890ABCDEF"

Note that all calculations are strictly integer calculations. That means that if a float variale is ever produced it will be converted to an int. Also divisions are always floor divisions!

There's a special case for istr(""). This is a proper empty string, but also represents the value of 0. That is to allow for istr("").join(i for i in "01234"

Sorting a list of istrs is based on the integer value, not the string. So

' '.join(sorted('1 3 2 4 5 6 11 7 9 8 10 12 0'.split()))

is

'0 1 10 11 2 3 4 5 6 7 8 9'

,whereas

' '.join(sorted(istr('1 3 2 4 5 6 11 7 9 8 10 12 0'.split())))

is

'0 1 2 3 4 5 6 7 8 9 10 11'

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

istr-python-0.0.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

istr_python-0.0.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file istr-python-0.0.1.tar.gz.

File metadata

  • Download URL: istr-python-0.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for istr-python-0.0.1.tar.gz
Algorithm Hash digest
SHA256 730125104a410ebe02049724646dae356f505d67a5f37ae7e0e0e67beb0dd7d8
MD5 f0037f9d8e4553d8026d729a797248e6
BLAKE2b-256 1c5deb8492942cacc6763827d0620854f5d0486255c69e039d62ceda38c7e837

See more details on using hashes here.

File details

Details for the file istr_python-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: istr_python-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for istr_python-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5019eaecba24fc91d33ccec564bc1947b826aa44145f3759b28f205ecfa1e84
MD5 e4cef33a34eeaaefd752fbecbb87d159
BLAKE2b-256 2778ab6bd9c76b739cacc39668c030922ffe9f94172539ad7b62d8a105f5c19f

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