0

For example, I need listBuilder('24+3-65*2') to return ['24', '+', '3', '-', '65', '*', '2']

We are not allowed to use custom imported functions. I have to make this work without them. This is what I have so far...

def listBuilder(expr):
    operators = ['+', '-', '*', '/', '^']
    result = []
    temp = []
    for i in range(len(expr)):
        if expr[i] in operators:
            result.append(expr[i])
        elif isNumber(expr[i]): #isNumber() returns true if the string can convert to float
            temp += expr[i]
            if expr[i+1] in operators:
                tempTwo = ''.join(temp)
                result.append(tempTwo)
                temp = []
                tempTwo = []
            elif expr[i+1] == None:
                break
            else:
                continue

    return result

At this point I am getting an error, string index out of range for the line including expr[i+1]. Help would be much appreciated. I have been stuck on this for hours.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
ColeFemo
  • 19
  • 2

3 Answers3

1

You are iterating over all the components of the list including the last item, and then testing if the next item is an operator. This means that when your loop gets to the last item, there are no further items to test and therefore the index error.

Note that an operator would never occur at the end of an expression. That is, you wouldn't get something like 2+3- as that doesn't make sense. As a result you could test all items except the last one:

 for idx, item in enumerate(expr):
    if item in operators or (idx == len(expr)-1):
        result.append(item)
    elif idx != len(expr)-1:
        temp += item
        if expr[idx+1] in operators:
            tempTwo = ''.join(temp)
            result.append(tempTwo)
            temp = []
            tempTwo = []
        elif expr[idx+1] == None:
            break
        else:
            continue
VMatić
  • 996
  • 2
  • 10
  • 18
0

I am not sure if this is the best solution, but works in the given case.

operators =  ['+', '-', '*', '/', '^']
s = '24+3-65*2/25'

result = []
temp = ''

for c in s:
    if c.isdigit():
        temp += c
    else:
        result.append(temp)
        result.append(c)
        temp = ''
# append the last operand to the result list
result.append(temp)

print result


# Output: ['24', '+', '3', '-', '65', '*', '2', '/', '25']
Manjunath
  • 150
  • 1
  • 6
0

I came up with a more concise version of your function which avoids using string indicies, which is faster in Python.

Your code was throwing the index error because on the last iteration you are checking for something at position i+1, which is one off the end of the list. The line

if expression[i+1] in operators:

throws the error because during the final iteration i is the final list index and you check for a list item which does not exist.

def list_builder(expression):
    operators = ['+','-','*','/','^']
    temp_string = ''
    results = []

    for item in expression:
        if item in operators:
            if temp_string:
                results.append(temp_string)
                temp_string = '' 
            results.append(item)

        else:
            if isNumber(item):
                temp_string = ''.join([temp_string, item])

    results.append(temp_string) #Put the last token in results 

    return results 
Ethan Henderson
  • 428
  • 4
  • 11