Instead of computer science lessons, boy Vasya was engaged in preparing for programming contests and achieved great success in this. Unfortunately, as a result of constant training, Vasya began to think "inside the box." For example, after seeing the task below, Vasya thought: “Hmm, strange, where are the restrictions? Okay, I will write a long arithmetic, it will pass. So, what to choose? Reverse Polish notation or recursive analysis of the expression? ...”. Of course, the task is solved much easier than Vasya thinks, but for this you need to think outside the box. And the limitations can be found by looking at the input file that is attached to the condition.

The task is formulated as follows: You are given a set of correct arithmetic expressions that use integers, mathematical operations +, - and ∗, and parentheses. Each expression begins with the = sign. For each of the expressions, calculate the result of the calculation.

Here is the input file. He himself was looking for some kind of trick in the task, which greatly simplifies the solution, but he did not find it. I understand that it is possible to use the arrester itself, about which it is stated in the condition, but the task should be solved somehow easier - this is clearly indicated.

  • Well, the first "oddity" of the data is that there are no two opening brackets in a row. only closing. so you can try to go back to front, but priorities must be respected ... - Mike
  • All numbers within 100, no more than 4 multiplications - no long arithmetic is needed here. - Harry

4 answers 4

Implementing the idea from the response , only in python3:

# Скачивание файла import requests rs = requests.get('https://cloclo18.datacloudmail.ru/weblink/view/emCb/gXFkchRJ2?etag=7706DA739680EAC4A5B9044E9767047365988F54&key=a91762c6f1d8a559f6d780934b2509c728b33df4') # Получение текста, разделение его построчно, пронумерование for i, line in enumerate(rs.text.split('\n'), 1): # Если строка пустая if not line: continue # Обрезание первого символа, удаление ' ', '\n', '\r' и т.п. line = line[1:].strip() print('{}. {} = {}'.format(i, line, eval(line))) 

Console:

 1. 1+97*98*5 = 47531 2. 39-56 = -17 3. 92*66 = 6072 4. 40+66 = 106 5. 95-80 = 15 ... 69997. 11+8*28+(37) = 272 69998. 33+21-(16+(20)-(58)) = 76 69999. (64) = 64 70000. (26)*(59-(48)-(13-67)) = 1690 

    Read the line, replace the equal sign with the ordinal number of the line and the word print. Save the resulting file with a bas extension. Run the program in the Basic interpreter.

      If you need a solution "outside the box":

      The lines in the file are written in the form of Microsoft Excel formulas. Therefore, we copy the file to the clipboard, open a new Excel sheet, paste it into the first column, select the first column, copy the result: http://pastebin.com/KdCy9nRg .

      • one
        you can and without copying, just rename to .csv and open to excel =) specially recheck, it works =) - rdorn
      • @minusator: Excuse me to explain your minus. - VladD

      Operations no more than 4. That is, given 4 or less operations and the order of their execution. Well, no long arithmetic.