shadow.T
shadow.T

Reputation: 135

How to do infinite string based on index and length in python?

I'm trying to do a method that takes three parameters. A string, index, and length. I'm trying to create an infinite string.

Example: method("book",-3,9)

Output: ookbookbo

Example 2: method("book",-5,15)

Output 2 kbookbookbookbo

My code so far:

def create_sequence(string, index, length):
    positive_index = abs(index)
    l = list(string)
    end = ""
    print(length- positive_index - len(l))
    for i in range(length- positive_index - len(l)):

        end += l[i]
    output = ''.join([string,end])

My logic : The string will always be used, so I just have to get the right-end part of the sequence and the beginning(left sided) part, and then just attach them together. The end part in my code works as long as length - positive_index - len(l) is less than or equal to the string length.. I also don't know how to get the sequence part that come before the original string.

I'm a beginner in python, any help is greatly appreciated.

EDIT: I want my input string to be the base. As in book, has 0,1,2 and 3 indices for each letter in it. All the added characters to the left of "0" which is "b" in book have negative indices.

Upvotes: 1

Views: 1727

Answers (3)

han solo
han solo

Reputation: 6600

You can use itertools to create something similar

>>> import itertools
>>> x = itertools.islice(itertools.cycle("book"), 1,5)
>>> ''.join(x)
'ookb'
>>> x = itertools.islice(itertools.cycle("book"), 3,9)
>>> ''.join(x)
'kbookb'
>>> x = itertools.islice(itertools.cycle("book"), 3,10)
>>> ''.join(x)
'kbookbo'

Updated as per @tobias_k suggestion for handling negative indexes,

>>> def mkseq(string, index, length):
...   return ''.join(itertools.islice(itertools.cycle(string), index % len(string), index % len(string) + length))
... 
>>> mkseq("book", -3, 9)
'ookbookbo'

Upvotes: 3

Karls
Karls

Reputation: 751

How about wihout any loop?

We have 3 part: Begin of string, Middle (repetition of word "book") and End

def create_sequence(string, index, length):
    word_length=len(string)
    string_repetition=((length+index)//word_length+(-index)//word_length) #How many full strings we have
    string_end=(length+index)%word_length #How manny letters are left after last full word
    inf_string_start= string[length-string_repetition*word_length:]
    inf_string_mid=string_repetition*string#MID
    inf_string_end=string[:string_end] #END
    inf_string=inf_string_start+inf_string_mid+inf_string_end #concatenation

    return inf_string

However itertools may be more friendly to use. But you can consider this solution if you don't want to use external libs.

Upvotes: 1

tobias_k
tobias_k

Reputation: 82929

Just use modulo % to "normalize" the index to the length of the string:

def create_sequence(string, start, length):
    return ''.join(string[i % len(string)] for i in range(start, start + length))

>>> create_sequence("book", -3, 9)
'ookbookbo'
>>> create_sequence("book", -5, 15)
'kbookbookbookbo'

Upvotes: 4

Related Questions