1

I am writing a code that takes in a tuple of integer as intervals and outputs a range of alphabets. I could use some help on return statement.

>>> arrangement((5, 3, 11, 7))
'A-E F-H I-S T-Z'

Below is what I have written so far. The return statement seems to return only the last iteration of the loop:

def arrangement(numerical_representation):
    interval = ''
    step = 65
    for integer in numerical_representation:
        interval = chr(step) + '-' + chr(step + integer - 1)
        step += integer
    return interval

>>> arrangement((5, 3, 11, 7))
'T-Z'

With print statements I am able to go through all the iteration, but I can't seem to make them print in a single line with quotation marks on both ends.

def arrangement(numerical_representation):
    interval = ''
    step = 65
    for integer in numerical_representation:
        interval = chr(step) + '-' + chr(step + integer - 1)
        step += integer
        print(interval)

>>> arrangement((5, 3, 11, 7))
A-E
F-H
I-S
T-Z

How should I proceed?

halfer
  • 19,824
  • 17
  • 99
  • 186
V Anon
  • 543
  • 3
  • 8

4 Answers4

1

You need to use += instead of =:

def arrangement(numerical_representation):
    interval = ''
    step = 65
    for integer in numerical_representation:
        interval += chr(step) + '-' + chr(step + integer - 1)
        step += integer
    return interval

>>> arrangement((5, 3, 7, 11))
'A-EF-HI-OP-Z'

An alternative, what about using yield?

def arrangement(numerical_representation):
    interval = ''
    step = 65
    for integer in numerical_representation:
        interval = chr(step) + '-' + chr(step + integer - 1)
        step += integer
        yield interval

>>> ''.join([x for x in arrangement((5, 3, 7, 11))])
'A-EF-HI-OP-Z'
Maor Refaeli
  • 2,417
  • 2
  • 19
  • 33
1

You could have a list and append each interval to it. And at the end return the list

def arrangement(numerical_representation):
    interval = ''
    step = 65
    l=[]
    for integer in numerical_representation:
        interval = chr(step) + '-' + chr(step + integer - 1)
        step += integer
        l.append(interval)
    return " ".join(l)


arrangement((5, 3, 11, 7))
A-E F-H I-S T-Z
Sruthi
  • 2,908
  • 1
  • 11
  • 25
0

Try it like this:

def arrangement(numerical_representation):
    start = 65
    result = []
    for n in numerical_representation:
        result.append('{}-{}'.format(chr(start), chr(start+n-1)))
        start += n
    return ' '.join(result)

print(arrangement((5, 3, 11, 7)))
#A-E F-H I-S T-Z
zipa
  • 27,316
  • 6
  • 40
  • 58
0

instead of using chr, why not use string.ascii_uppercase and string indexing? You can use itertools.accumulate to generate the cumulative sum of the indices

from itertools import accumulate
from string import ascii_uppercase


def generate_indices(groups):
    begin = 0
    for end in accumulate(groups):
        yield begin, end-1
        begin = end


groups = (5, 3, 11, 7)
result = " ".join(
    (f"{ascii_uppercase[begin]}-{ascii_uppercase[end]}")
    for begin, end in generate_indices(groups)
)
'A-E F-H I-S T-Z'
Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36