In a given sequence of numbers, count the number of pairs (consecutive numbers), represented by positive values, and the number of pairs, the values ​​of whose elements have the opposite sign. Display a message about the number of which pairs prevail.

local function main() print("Введите строку") p = 0 m = 0 str = io.read() a = string.len(str) for i = 1, a do if str:sub(i,i) == "+" and str:sub(i+3,i+3) == "+" then p=p+1 else if str:sub(i,i)== "-" and str:sub(i+3,i+3) == "-" then m = m+1 end end end if p > m then print("Положительная последовательность больше") else if m > p then print("Отрицательная последовательность больше") else print("Последовательности равны") end end end main() 

in such a sequence +3 + 4-5 -5-5, I compare the first character with the third and so on. but the problem is that there may be a two-digit number and then the program will not work correctly, how to get around this problem. match can help me? Well, to look for a space and compare with the sign after it? or something else is possible?

    2 answers 2

    Rewrote the code from the answer above, but on the templates. Here's what happened:

     local function get_counts(str) local plus, minus = 0, 0 local last_sign = nil for sign, digit in str:gmatch("([+-])(%d+)") do print(sign .. digit) -- печатает найденные знак и число if sign == last_sign then -- нашли пару if sign == "+" then plus = plus + 1 else minus = minus + 1 end else last_sign = sign end end return plus, minus end local function main() local positive_count, negative_count = get_counts("+3 +4 -5 -55 -5 -200 +10") print("Положительных пар: " .. positive_count) print("Отрицательных пар: " .. negative_count) if positive_count > negative_count then print("Положительная последовательность больше") elseif negative_count > positive_count then print("Отрицательная последовательность больше") else print("Последовательности равны") end end main() 

    Here is what the function prints:

     +3 +4 -5 -55 -5 -200 +10 Положительных пар: 1 Отрицательных пар: 3 Отрицательная последовательность больше 

    The key point of this function is the string:

     for sign, digit in str:gmatch("([+-])(%d+)") do 

    here an iterator is created for the given string str , with a pattern ([+-])(%d+) . For each match, this iterator returns the separate sign of the number in sign and the number itself in digit .

    • so it was conceived -5 -55 -5 is a sequence of two pairs -5 -55 and -55 -5 - PL
    • @PL ok, fixed the answer. - zed

    Rewrote your code. The basic logic for counting pairs is performed in the getPositiveNegativePairsCount function. Input of the source data you can also expect from the standard input stream, as it was before.

    The code makes a very simple analysis of the line: we run through all the characters and when we meet the + or - signs - set last_symbol . At the next meeting of one of the two characters, either count the pair's counter, or switch the last_symbol to another character

     SIGN = { PLUS="+", MINUS="-" } local function getPositiveNegativePairsCount(inputStr) local result = {} for key, val in pairs(SIGN) do result[val] = 0 end local last_symbol = nil local length = string.len(inputStr) print("length = ", length) for idx = 1, length do local symbol = string.sub(inputStr, idx, idx) if result[symbol] then if symbol == last_symbol then result[symbol] = result[symbol] + 1 else last_symbol = symbol end end end return result[SIGN.PLUS], result[SIGN.MINUS] end local function main() local positive_count, negative_count = getPositiveNegativePairsCount("+3 +4 -5 -55 -5") print("positive pairs count = ", positive_count) print("negative pairs count = ", negative_count) if positive_count > negative_count then print("Положительная последовательность больше") elseif negative_count > positive_count then print("Отрицательная последовательность больше") else print("Последовательности равны") end end main() 

    PS It seems that in lua you cannot get a symbol by an index using the operator [], local symbol = inputStr[idx] returns nil . For this reason, I use the call local symbol = string.sub(inputStr, idx, idx)