Home > Mobile >  Is there a solution to separating number and operators of a string?
Is there a solution to separating number and operators of a string?

Time:10-30

I want separate numbers and operator signs from a string.

Example:

input - "12 34=46" output - 12, , 34, 46

Code Used:

def seperator(runes):
    runes = list(runes)
    operators = ["-", " ", "/", "*"]
    
    index1 = 0
    index2 = 0
    for i in range(len(runes)):
        cond1 = runes[i-1] >"-1" and runes[i-1] < "10"
        cond2 = runes[i-1] == "?"
        if runes[i] in operators and (cond1 or cond2):
            index1 = i
        if runes[i] == "=":
            index2 = i

    num1 = runes[:index1]
    operator = runes[index1]
    num2 = runes[index1 1: index2]
    output = runes[index2 1:len(runes)]

    return num1,  operator, num2, output

Inputs

print(seperator("1 1=2"))
print(seperator("123*456=56088"))
print(seperator("-58*-1=50"))

Results

  1. ('1', ' ', '1', '2')
  2. ('', '1', '23*456', '56088')
  3. ('-50', '*', '-1', '50')

Problem

I was wondering why I am getting the wrong output for the 2nd result.

I tried to remove the conditions cond1 and cond2 which seems to work for 2nd result but fail for others.

CodePudding user response:

The simplest solution I can think of is a regex. Here's what I came up with:

import re


def seperator(runes):
    match = re.search('(-?[0-9] )([ \-*\/])(-?[0-9] )(=)(-?[0-9] )', runes.replace(' ', ''))
    return match.groups()


print(seperator("1 1=2"))  # => ('1', ' ', '1', '=', '2')
print(seperator("123*456=56088"))  # => ('123', '*', '456', '=', '56088')
print(seperator("-58*-1=50"))  # => ('-58', '*', '-1', '=', '50')

The regex here is (-?[0-9] )([ \-*\/])(-?[0-9] )(=)(-?[0-9] ). Note that each (-?[0-9] ) represents a number and ([ \-*\/]) represents the operator.

CodePudding user response:

The following solution uses a loop instead of any inbuilt function and passes all the test cases mentioned.

def seperator(my_string):
    my_string = "".join(my_string.split(" "))
    res = []
    number_stack = []
    for i in range(0, len(my_string)):
        char = my_string[i]
        if char in ["1","2","3","4","5","6","7","8","9","0"]:
            number_stack.append(char)
        else:
            if char == "-":
                if i == 0:
                    number_stack.append(char)
                else:
                    if my_string[i-1] in ["1","2","3","4","5","6","7","8","9","0"]:
                        res.append(''.join(number_stack))
                        number_stack = []
                        res.append(char)
                    else:
                        number_stack.append(char)
            else:
                res.append(''.join(number_stack))
                number_stack = []
                res.append(char)
    res.append(''.join(number_stack))
    return res

print(seperator("1 1=2"))  # ['1', ' ', '1', '=', '2']
print(seperator("123*456=56088"))  # ['123', '*', '456', '=', '56088']
print(seperator("-58*-1=50"))  # ['-58', '*', '-1', '=', '50']
print(seperator("10-10=0"))  # ['10', '-', '10', '=', '0']
print(seperator("10--10=0"))  # ['10', '-', '-10', '=', '0']
print(seperator("1   1 = 2"))  # ['1', ' ', '1', '=', '2']
  • Related