M
M
Mikhail_1232020-08-28 11:08:32
Kotlin
Mikhail_123, 2020-08-28 11:08:32

How to solve an interview problem?

At the interview, they asked me to solve a problem, write a function for: "ab2(3(c)d)" -> "abcсcdccсd"
I made a solution for a specific example, they asked me to make a function for any similar data, I couldn’t, tell me how to solve it in Kotlin

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
acwartz, 2020-08-28
@acwartz

An extremely simple task with a while loop + recursion.
parentheses form a recursive function call.
I don't know Kotlin.
More or less like this:

function task(str) result string {
  i, j integer = 0;
  max integer = str.length;
  ch char = #0;
  while (i < max) {
    ch = str[i];
    if ch in [0..9] {
      //делаем рекурсию столько раз сколько предписано
      //начало строки которую надо обработать, не включая тек. символ и символ начала рекурсии (
      substrStart Integer = i + 2;
      //конец строки которую надо обработать, ссылается на закрывающую рекурсию скобку )
      substrEnd Integer = str.pos(")", substrStart );
      //цикл повторов выражения в скобках
      for j = 1; j < ch; j++:
        //копируем из строки 2(3(c)d) значение 3(с)d не включая скобки и текущий символ указывающий кол-во повторов
        result = result + task(str.substring(substrStart, substrEnd -1)); 
      end for;
      //Прыгаем за закрываюющую скобку т.к. этот участок обработан рекурсивно, и нужно обработать хвост если он есть, например: aaa2(3(c)dd)QQ
      //в первой итерации task("aaa2(3(c)dd)QQ")
      //тут будет переход на позици подстроки "QQ"
      i = substrEnd + 1;
    } else {
      result = result + ch;
      i = i + 1;
    }
  }
}
}

A
alekseyHunter, 2020-08-28
@alekseyHunter

write a function for: "ab2(3(c)d)" -> "abcсcdccсd"

Paraphrased problem from the university. We sounded - "Calculate the result of an expression consisting of numbers, arithmetic operations and brackets." That is, a calculator through infix / postfix notation, whichever is more convenient for you. Here is a similar approach.
I made a solution for a specific example, they asked me to make a function for any similar data

If the interview was for the position of Senior, then how the interviewer's joke is ridiculous. In other cases, not so much.

S
Stalker_RED, 2020-08-28
@Stalker_RED

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question