Move Code Lines

Move Code Lines

28 ratings
Guidance and Solutions [WIP]
By Innocentive
Hints, tips and solutions for all puzzles of Move Code Lines.
2
2
2
   
Award
Favorite
Favorited
Unfavorite
Introduction
This guide aims at helping players understand and complete all levels of Move Code Lines. It provides
  • in the first sections: summary explanations for all variables, operators, and commands used in the game,
  • in the help sections: where I found it approriate more or less detailed hints and explanations for levels,
  • in the solve sections: complete solutions for all levels.
This is still a work in progress as I still need to complete the manual sections in the beginning of the guide. Shouldn't take more than a couple of days.

Comments are welcome :)
List of Variables
List of Operators
  • Arithmetic Operators
    Arithmetic operators are used to process the values of two constants and/or variables into a new value:

    Operation
    Sign
    Result
    Example
    Addition
    +
    Sum
    7 + 3 -> 10
    Subtraction
    -
    Difference
    7 - 3 -> 4
    Multiplication
    *
    Product
    7 * 3 -> 21
    Integer Division
    /
    Quotient
    7 / 3 -> 2
    Modulo
    %
    Remainder of Division
    7 % 3 -> 1

    The arithmetic addition operator also works as a string addition operator:

    Operation
    Sign
    Result
    Example
    String Addition
    +
    Concatenation
    "hello, " + "world!" -> "hello, world!"

  • Assignment Operators
    The simple assignment operator is used to assign the value of the term on the right side of the operator to the variable on the left side of the operator.

    Operation
    Sign
    Example (value of a is 7)
    Result of Example
    Simple Assignment
    =
    a = 1 + 2
    Value of a is 3

    Arithmetic assigment operators modify the term on the right side of the operator before assigning the value to the variable on the left side.

    Operation
    Sign
    Example (value of a is 7)
    Long form of Example
    Result of Example
    Addition Assignment
    +=
    a += 1 + 2
    a = a + (1 + 2)
    Value of a is 10
    Subtraction Assignment
    -=
    a -= 1 + 2
    a = a - (1 + 2)
    Value of a is 4
    Multiplication Assignment
    *=
    a *= 1 + 2
    a = a * (1 + 2)
    Value of a is 21
    Integer Division Assignment
    /=
    a /= 1 + 2
    a = a / (1 + 2)
    Value of a is 2

  • Relational Operators
    Relational operators are used to construct a relation out of two terms that is either true or false:

    Operation
    Sign
    Example
    Result of Example
    Is equal to
    ==
    7 == 3
    false
    Is not equal to
    !=
    7 != 3
    true
    Is greater than
    >
    7 > 3
    true
    Is greater than or equal to
    >=
    7 >= 3
    true
    Is smaller than
    <
    7 < 3
    false
    Is smaller than or equal to
    <=
    7 <= 3
    false
List of Commands and Keywords
The following list contains all commands and keywords used in Move Code Lines and provides a summary explanation of what they do. It is organized in alphabetical order and indicates the specific level each command/keyword is introduced in.

  • def function_name (list_of_parameters) {list_of_statements}
    Defines a function with the name function_name. A function contains a fixed set of command statements in list_of_statements. These statements are processed once the function is called with the following statement:
    function_name (list_of_arguments)
    If the definition statement of the function contains list_of_parameters the call statement has to contain a mirrored list_of arguments. This way, the values contained in list_of_arguments are submitted to list_of_parameters. Note that changes of parameter values in the function affect the values of the arguments. For more information see General Concepts - Scope.

    Introduction of functions: Level 02.01 (functions)
    Introduction of parameters: Level 02.05 (functions)

  • else {list_of_commands}
    If used has to directly follow an if-command section of the code but is optional. Command statements in list_of_commands will only be executed if term in the if-command is false. It follows that list_of_commands in the if-section and list_of_commands in the else-section are mututally exclusive.

    Introduction: Level 05.05 (conditions)

  • for (term_1; term_2; term_3) {list_of_statements}

  • get_int ()
    Used to read an integer value input from the input window. Will produce a runtime error if there is no integer value availabe in the input window.

    Introduction: Level 04.01 (input)

  • get_str ()
    Used to read a string value input from the input window. Will produce a runtime error if there is no string value availabe in the input window.

    Introduction: Level 04.02 (input)

  • global variable_name
    Defines a variable with the name variable_name and with unlimited scope. For more information see General Concepts - Scope.

    Introduction: Level 02.19 (functions)

  • if (term) {list_of_commands}
    Conditions the execution of the command statements in list_of_commands by the veracity of term. If term is true list_of_commands will be processed, if it is false the command statements will be skipped and execution of the code will resume after the end of the if-section.

    Introduction: Level 05.01 (conditions)

  • print (term)
    Sends the value of term to the output window.

    Introduction: Level 01.01 (hello, world!)

  • return term
    Used to return the value of term from inside a function to the statement that calls that function. This command will automatically end the function regardless of the amount of remaining function statements.

    Introduction: Level 03.01 (return)

  • var variable_name
    Defines a variable with the name variable_name. If a variable is accessed before definition execution of the code produces a runtime error.

    Introduction: Level 01.02 (hello, world!)
General Tips
1.01 help ("hello, world!")
Level
Hints
1
ERROR
2
ERROR
3
ERROR
4
ERROR
5
ERROR
6
ERROR
7
ERROR
8
ERROR
9
ERROR
10
ERROR
11
ERROR
12
ERROR
13
ERROR
14
This level draws its difficulty from the fact that you need to find the right order of combining the available strings and assigning them to the variables s and t. But that is what Move Code Lines is all about, right? If you're having problems achieving the goal, note that the second output term
.#.
can be achieved in two different ways. One of those two ways makes it impossible to achieve the remaining two outputs.
1.02 help ("functions")
Level
Hints
1
ERROR
2
ERROR
3
ERROR
4
ERROR
5
ERROR
6
ERROR
7
ERROR
8
ERROR
9
ERROR
10
ERROR
11
ERROR
12
ERROR
13
ERROR
14
ERROR
15
ERROR
16
ERROR
17
"!!!" must be printed six times, "?" must be printed five times. You have
  • two "!!!" print statements,
  • three "?" print statements,
  • three function call statements.
Make the numbers work ;)
18
Can the function call statement
f (2)
be used to create every output required in the goal?
19
ERROR
20
  1. 2 = (0 + 1) * 2
  2. 5 = 2 + 3
  3. 18 = (8 + 1) * 2
21
Given the amount of available print statements and function calls, the goal output can only be achieved by nesting one function inside the other function.
22
In order to increase a from 6 to 11 the two commands executed right before the final print statement must be
a += 2 a += 3
One of the above mentioned commands has to be part of the function.
23
Given the assignment statements available to populate the function there is only one way to create that function without a runtime error:
def f (x) { var m = x a += m print (a * m) }
Integer Division plays an integral part in finding the solution.
24
ERROR
25
The output string requires nine characters. You have five string assignment statements and three function calls available. Do the math!
When x is the amount of string assignment statements inside the function and y is the amount of string assignment statements outside the function: x + y = 5 AND 3x + y = 9.
1.03 help ("return")
Level
Hints
1
  1. 1 == 3 - 2
  2. 8 == 3 + 2 * 2 + 1
  3. 5 == (2 * 2 + 7) / 2
2
Each function should print and return the 'same' value.
3
  1. 6 == 3 + 3
  2. 5 == 3 + 1 + 1
  3. 3 == 1 + 2
  4. 12 == 3 + 5 + 4
4
  1. 4 == 1 * 2 * 2
  2. 10 == 1 + 3 * 3
  3. 100 == (1 + 3 * 3) * (1 + 3 * 3)
  4. 90 == (1 + 3 * 3 - 1) * (1 + 3 * 3 - 1) + -3 * -3
1.04 help ("input")
Level
Hints
1
ERROR
2
ERROR
3
ERROR
4
  1. The statement
    print (f ())
    can only produce one of the three required output values.
  2. To assign the remaining two print statements to the remaining two output values you need to know your integer division.
5
If you look closely at the positioning of the brackets in each print statement you will find that there is only one that can achieve an output of 80. The rest should fall into place easily.
6
Each call of the function must print the product of the input value and the input value minus one. Therefore,
  1. 0 == 0 * ?
  2. 56 == 7 * 8
  3. 30 == 5 * 6
  4. 2 == 1 * 2
1.05 help ("conditions")
Level
Hints
1
ERROR
2
ERROR
3
ERROR
4
ERROR
5
ERROR
6
ERROR
7
ERROR
8
ERROR
9
ERROR
10
ERROR
11
ERROR
12
  1. There are two conditional statements in the function one of which has an else section. That means there is a total of three conditioned sections you can assign the print statements to.
  2. According to the goal three basic types of output are required:
    • print ("yes")
    • print ("no")
    • print (x < 7) -> "true" / "false"
      print (b) -> "true" / "false"
  3. According to the goal
    • print ("yes")
    • print (x < 7) -> "true" / "false"
      print (b) -> "true" / "false"
    must not be mutually exclusive.
  4. Finally, and I hate to say that it is too difficult for me to fully explain this here, considering how the two conditions relate to each other through x, and that six true outputs are required where there is only one false input one can deduce thatone of the if sections must be nested inside the other if section.
13
While reconstruction of the code should be pretty simple in this level you may find yourself puzzled by the input required in the second part. Just consider the difference between integers and natural numbers for the solution.
1.06 help ("while")
Level
Hints
1
ERROR
2
ERROR
3
ERROR
4
ERROR
5
ERROR
6
ERROR
7
ERROR
8
ERROR
9
ERROR
10
ERROR
11
ERROR
1.07 help ("for")
Level
Hints
1
ERROR
2
ERROR
3
ERROR
4
ERROR
5
ERROR
6
Function f returns the Gauss sum of the parameter value. So depending on where you position
k++
m has to be
  • either greater than the Gauss sum of 3 and smaller than or equal to the Gauss sum of 4
  • or greater than the Gauss sum of 4 and smaller than or equal to the Gauss sum of 5.
7
If you do not know how the logical operators && and || work in conjunction see the Operators section.
8
Given the scope of the available variables it is pretty clear how to reconstruct the code except for one line. Finding the right input is not as clear. Use the available variables and inputs to try and create the following calculations for the first four goal values:
  • 4 == 1 * 2 * 2
  • 3 == 1 * 1 * 3
  • 18 == 2 * 3 * 3
  • 48 == 1 * 12 * 4
x must be reset to 0 at the beginning of each iteration of the for loop.
9
ERROR
10
ERROR
11
ERROR
12
ERROR
13
The for loop provides for three iterations each of which calls function f. The function can produce an output with the following characteristics:
  • A letter is printed for each letter of s. So the amount of letters printed is determined by the length of parameter s.
  • If a letter of s is 'a' the output is 'b'. Otherwise the output is the first letter of s.
14
  1. At the end of the code the value of r must be '.???.123'.
  2. In each odd iteration of the for loop a '.' is added to r.
  3. In each even iteration of the for loop the value of t is added to r.
  4. The for loop has five iterations and starts with an even iteration.
15
Function f copies the value of s to t with the following modifications:
  • The third character of s is repeated once.
  • If a character of s is equal to the character of s in the position specified by p such charcter is repeated once.
  • If the first rule applies the second rule is disregarded.
16
ERROR
17
  • The scope of the available variables gives a pretty good idea of which line belongs where. Function f must return the inverted value of string s.
  • If you look closely at the three print statements and the goal you can draw the following conclusions:
    • Either x or y has to include an asterisk for the first output of the goal because there is no way to remove an asterisk from either variable.
    • The remaining two output lines both have two asterisks so for each line an asterisk must be added.
    • One print statement adds an asterisk to the inverted value of y so y must have either three or four characters at the time of execution.
1.08 help ("break")
Level
Hints
1
ERROR
2
  • Function f returns a string value comprising all characters of parameter s up to and excluding the first '.' in s.
  • The while loop is part of the main function and it is stopped when next has less than 10 characters.
3
  • There is a function and a while loop. Both conditions can be clearly assigned to either the function or the loop according to the variables and considering that the while loop will need at least one of these conditions in order to stop eventually.
  • Assigning the conditions also clarifies which of the remaining statements go where.
  • According to the goal, the while loop of the main function must have four complete iterations and stop in the fifth iteration. This means that the first four sets of three input integers must not satisfy the condition of the if statement in the while loop, namely f must not return 'true'.
  • Keep in mind that the condition in function f contains a NOT operator ("!"). That means the condition statement value is 'true' if a >= b or b >= c.
4
  • The construction and purpose of function f should be pretty clear:it returns the amount of characters in s that have the same value as ch.
  • Considering the goal and the available print statements f must be used to create the single digit integers of the output. There are only two statements in the main function that are required towards that goal. However, they must be part of at least one loop.
  • The main function has two available loops: while and for. Looking at the goal it is quite apparent that one loop has to nested inside the other loop. Looking at the statements required to print one of the single digit integer outputs of the goal should clarify if the while loop goes into the for loop or vice versa.
  • Finally,break statements do not exclusively work in while loops.
5
  • Looking at the if statement clarifies directly and indirectly where most of the statements have to be placed: in the main function or in function f.
  • The one line with a flexible position is the print statement. The amount of output lines in the goal in conjunction with an understanding of what f does should help here.
  • You should now have a function thatadds the Gauss sum of parameter n to a.
  • Finally,the last hint is not completely true. Understanding that will help bridge the gap between 5 and 105 without printing a.
1.09 help ("recursion")
Level
Hints
1
  • The goal requires a line with '0'. That clarifies that
    print (n)
    must not be conditioned.
  • In order to place the remaining two print statements appropriately consider that both print the same value, namely n - 1.
2
Function f should represent the recursive way of calculating the Gauss sum of n. As long as n is greater than 0, it returns the sum of
  • n and
  • the return value of a recursive call with the argument n - 1.
Figuring out where to place the print statements should not be that hard.
3
For n greater than 1, function f should return the product of
  • n and
  • the return value of a recursive call with the argument n - 1.
In order to find the right input for the output 3,628,800 simply grab a calculator.
4
ERROR
5
  • The four calls of function g with constant arguments help a lot in setting this function up correctly.
  • However, in order to find the right input values for the last five output sections of the goal a thorough understanding of what function g does is required. In effect, g returns the greatest common divisor by use of the recursive Euclidean algorithm. If you are interested in more info, Wikipedia is your friend!
  • With that knowledge you need to find two integers for the last four output sections of the goal
    • the division of which in both directions has a remainder and
    • the sum of which is the first output integer and
    • the greatest common divisor of which is the second output integer.
  • If you still cannot find the right input values you can use the following method for the last four output sections of the goal:Subtract the second output integer from the first output integer until the difference of the first output integer and the subtraction result cannot be divided by the subtraction result without remainder.That usually only takes a couple of steps.
6
  • The recursion of f is caused by the end condition of the for loop.
  • In effect each call of f should
    • if parameter s is empty return "".
    • if parameter s is not empty
      • call f with an argument that equals parameter s with the last letter cutand
      • return the return value of that call with the first letter of s added at the end.
1.10 help ("vectors", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Level
Hints
1
  • The first four lines of the goal determine the first four lines of the code and the input value.
  • In order to place the remaining lines consider this:9 == 4 * 2 + 1
2
  • Given the name of the function its purpose is pretty clear.
  • It should be obvious that the last three integers are added to vector v with the loop of the main function and that this loop requires one of the print statements.
  • The remaining code can be arranged and the required input can be found by considering the goal.
3
  • The last two print statements clarify for each code line above if it is part of function f of part of the main function.
  • The print statement
    print (f (v))
    must be responible for one of the following outputs:
    • 2 2 8
    • 2 8 7
    Only one of the two is possible if you take the other print statement of the main function into consideration.
  • That suggests that function f must print all elements of parameter a that equal 2 and return the sum of all elements.
  • To achieve the goal under the previous assumption
    • vector v must include two elements that equal 2 and the sum of all elements of v must equal 8,
    • the sum of the amount of elements of vector v and the first element of vector v equals 7.
    That is possible.
4
    The first two lines of the main function and the goal determine what function f should do:
    • Print every index where parameters a and b have the same value.
    • Return the vector sum of parameters a and b with the modification that elements of a and b with the same value are cut.
  • The variables used in the remaining lines of the main function determine precisely the right order of those lines.
  • Finally, in order to achieve the remaining goal vectors a and b must be populated with elements so that
    • the values at index 1 and 4 are the same,
    • the modified vector sum of function f has five elements with the last five values of the goal.
5
  • Function print_vector is already assembled correctly.
  • There is only one one reasonable sequence of lines for function f which is exactly in line with the first three lines of the main function and the first line of the goal: itdoubles each and every element of parameter a in a position with a value that is a multiple of parameter p.
  • For the remaining lines of the goal you simply need to find the pattern of doubles in relation to the previous line of the goal.
6
  • Function get_vector is assembled correctly.
  • There is only one one reasonable sequence of lines for function count: it returnsthe amount of elements of parameter v that have the same value as parameter e.
  • To achieve the goal the following has to be observed:
    • Vector b must have three elements and the sum of all elements of vector b must equal 0.
    • The first three lines of the goal determine the amount of elements of the same value in vector a.
7
  • There is only one reasonable sequence of lines for function f: it returnsthe index of the greatest element of parameter v.
  • In the main function all print statements should be positioned at the end.
  • To find the five input integers for vector b it is helpful to determine x and a[x] first:
    • x == 1
    • a[x] == 5
  • There are two print statements that include a multiplication with a[x]. These two print statements can only be used to achieve two specific lines of the goal, namely
    • 15
    • 0
  • The above means that a[y] * b[x] == -9.
  • The last finding allows the conclusion that y must have one of the following values:
    • 0: a[y] * b[x] == -9 would represent 1 * -9 == -9
    • 4: a[y] * b[x] == -9 would represent 3 * -3 == -9
  • Looking at one of the earlier findings the value of y can now be determined and all print statements fall into place..
8
  • If you do not know how to assemble function print_vector you should take a few steps back.
  • In order to structure function f is is helpful to
    • look at the first two print statements of the main function in conjunction with the first two lines of the goal and
    • consider how the two loops of function f complement each other.
  • Once function f works for the first two lines of the goal it should be easy to find the right input for the remainder of the main function.
9
  • Everything is already in place for function get_vector.
  • In order to reassemble function f consider the following:
    • 1 + 3 + 9 == 14
    • 1 + 2 + 3 == 6
  • This allows for the following conclusions:
    • If parameter v includes an element that equals 0 function f returns the sum of all elements of v up to the first element that equals 0.
    • If parameter v holds no element that equals 0 function f returns -1.
  • To find the right input for the remainder of the main function pay special attention to the fact that the sum of the amount of elements of b and the value of the first element of b must equal the last integer in each output line of the goal.
10
  • Everything is already in place for the functions get_vector and print_vector.
  • The variables available in function f make pretty clear how the code has to be arranged. If arranged correctly function fperforms the heapsort algorithm on parameter awhich is in line with the first three lines of the main function and the first line of the goal.
  • Adjusting the remaining lines of the main function and finding the right input should not pose too big a challenge with the above knowledge.
1.10 help ("vectors", [11, 12, 13, 14, 15, 16, 17])
11
  • Function t is already reasonable assembled which means that this level is all about
    • assembling functions f and g correctly and
    • calling these functions in the right order.
    In the end the sum of all elements of vector v must be 40.
  • I do not know of a way to logically deduce where the statements
    ++v[0] v.pushback (v[0] + v[v.size() - 1])
    must be placed. However, by trial and error it is easy to find out that the pushback line cannot be part of the for loop because the resulting sum of all elements of v would be too great. That suggests that it has to go into the function that does not have the for loop. With that assumption, use pen and paper to figure out where '++v[0]' would have to go. It took me less than 5 minutes to figure it out.
  • As a final clue to the solution here is the calculation I found to get to 40:
    8 + 2 + 2 + 10 + 18 == 40
12
  • If you do not know how to arrange the code of function get_vector take a few steps back.
  • As to function func the order of the following three lines is not conclusive:
    a[i] = a[i+1] - a[i] (assignment statement) ++i (increment statement) print (i * a[i]) (print statement)
    Only the following can be known:
    • The increment statement must be part of the while loop becauseotherwise the loop would never end.
    • The increment statement must be placed after the assignment statement becauseotherwise the assignment statement would cause a runtime error in the last iteration of the while loop.
    • The print statement must be part of the while loop as well becauseotherwise there would be no way to get to a total of 13 output lines with only three calls of function func.
    • Positioning the assignment statement outside of the while loop wouldlack any sense ;)
  • Looking at the lines of the main function and the goal, it can be said that the last call of function func must produce the lines counting from 0 up to 6 because of the following:
    • At the time of the last call of func vector v must have 8 elements as stated in the last line of the goal.
    • The while loop of func prints one line for each element of parameter a but the last one because one of the conditions of the loop is
      i < n - 1
      with n representing the amount of elements in a.
  • Given that vector v must have eight elements at the end of the main function and that it is the concatenation of the original vectors v and u, looking at the first six lines of the goal makes it pretty clear how many elements each v and u should get in the beginning.
  • However, we are still not any closer to knowing the order of the statements in the while loop. Again, the first six lines of the goal give an idea:
    • 0 * 1 == 0 || 1 * 2 == 2 || 2 * 3 == 6
    • 0 * 5 == 0 || 1 * 6 == 6 || 2 * 7 == 14
    That would mean thatthe print statement must be placed before the increment statement.
  • But which one comes first: the assignment statement or the print statement? Another look at the goal lines helps:
    • The output required from the last call of function func is seven lines counting up from 0 to 6.
    • The print statement contains a multiplication with the factor i.
    • That means that the elements two through seven of vector v must have a value of 1 at the time of printing because 1 * 1 == 1, 2 * 1 = 2, etc.
    • However, the loop in func is conditioned on
      a[i] < a[i+1]
      which means that the elements of v must be altered to 1 between the start of each iteration of the loop and the print statement. That can only be done by the assignment statement which therefore has to come first!
  • With all of these findings it is finally time to find the right input. Before printing an element a[i] this element is replaced by a[i+1] - a[i].
    • If you take into consideration the pevious assumption how the first six lines of the goal should be produced here is what vector v should look like at the time of the last call of func:
      [[1] [2] [3] [4] [5] [6] [7] [8]]
      The assigment statement of the while loop would replace all but the last element with 1 and the required output would be achieved.
    • In order to find the right input for vectors u and v you can now work your way backwards because we know that the last element of v must be4 and the last element of u must be 8.
1.11 help ("matrices")
Level
Hints
1
ERROR
2
  • Function get_vector should be no problem as the name says it all.
  • Functions f and t each can only be assembled in one reasonable way:
    • Function f doubles the value of all elements of parameter v that have a value smaller than parameter p.
    • Function t returns the sum of all elements of parameter a.
  • The main function also only has one resonable way of assembly:
    1. Define and initialize matrix vec with two vector elements.
    2. For each vector of vec print the index number and the sum of all elements.
    3. Modify both vectors of vec.
    4. For each (modified) vector of vec print the index number and the sum of all elements.
    That means that the sum of all elements of each vector in vec must equal 10 after initialization of vec.
  • The first vector of vec (hereonafter: vector_0) is modified by
    f (vec[0], vec[1].size ())
    That means that all elements of vector_0 that have a value smaller than the size of the second vector of vec will be doubled.
  • The second vector of vec (hereonafter: vector_1) is modified by
    f (vec[1], vec[0][2])
    That means thatall elements of vector_1 that have a value smaller than the value of the third element of vector_0 will be doubled.
  • Finally, it is time to find the integer input for both vectors of vec:
    • According to the goal the sum of the three elements of vector_0 must be modified from 10 to 15. Considering the functionality of function f it can be concluded that vector_0 requires one integer with a value of 5 and two integers
      • that add up to 5 and
      • that each have a value smaller than the size of vector_1
      because if vector_0 was initialized with [0, 5, 5] (sum == 10) the modification would lead to the same sum or a sum of 20.
  • With the last step the maximum size of vector_1 was established. It must not have more than 5 elements because if it had the modification of vector_0 would result in an element sum of 20. Furthermore, it can be said that it requires
    • one or multiple elements with a total value of -60 where each element has a value smaller than the value of the third element of vector_0 and
    • one or multiple elements with a total value of 70 where each element has a value greater than or equal to the value of the third element of vector_0.
  • You will have to figure out the rest for yourself or check one possible solution below.
3
  • Function print_matrix requires the following functionality: for each vector in parameter m add each element to an originally empty string and print that string at the end. Note that the string requires spaces between all elements and that there is a double line after the matrix is printed.
  • Function f can also be assembled for the most part by keeping an eye on the goal:
    • The crucial statement of f is the assignment statement
      m[x][y] = 2
    • If you look at the goal, the matrix appears to be modified in each step by changing matrix values in a column from 0 to 2. That fits with the assignment statement and clarifies
      • that parameter x determines the row of the value change and
      • that parameter y determines the column of the value change.
    • The rest falls into place after the above conclusion: in (more or less) plain english function f changes the value of each matrix element
      • from the position specified by parameters x and y
      • downwards to the bottom of the matrix or the first element that has a value other than 0 (which is not changed).
    • With the above findings function f should pose no real challenge.
  • The main function also should not be too much of a problem after the above. Just keep in mind
    • that the index of matrix elements and of vector elements starts at 0 and
    • that matrix is defined as a vector and must be changed into a matrix before assigning values.
4
  • Function print_matrix, get_matrix and good_coord are already in proper shape.
  • However, function f is a real beast to assemble. I will not lead through the entire solution step by step here because that would take too long. I will rather give you the following hints and nudges:
    • First of all it is important to get a better understanding of what f is supposed to do. The first four lines of the main function and the first two matrices of the goal are illuminating: f is supposed to change values in a matrix (parameter m) from 0 to 2. Apparently, parameters x and y specify an element in the matrix. That element and all surrounding elements are changed to 2 until the end of the matrix is reached or there are elements with the value 1. This assumption would fit the remaining matrices in the goal.
    • The way f achieves this goal is by using the following algorithm:
      1. Start at the position determined by parameters x and y.
      2. Check into all four directions if there is an element with the value 0, and if so memorize the position of that element and set its value to 2.
      3. Start at the top for each position memorized in this cycle.
    • In order to construct this algorithm it is most important to understand the meaning of the following code lines:
      p = p == 0 ? 1 : 0 np = p == 0 ? 1 : 0
      Check what other statements use these variables and make sense of it.
    • If that is not enough help here is the nesting order of the available loops and conditions:
      1. while
      2. for
      3. for
      4. if
  • Once you made f work figuring out the rest of the main function and the required input should be a walk in the park :)
1.12 help ("extra", [1, 2, 3, 4])
Level
Hints
1
  • Even though you can find the solution to this level rather quickly with some trial and error and with a little luck it is actually pretty tough to understand. I will not go into the details of how to deduce the following because it would blow up this guide but I can give you the formula to calculate the appropriate value of y for any x that is
    • greater than 1 and
    • not a multiple of 3.
    It is not possible to achieve the goal with any of the excluded integers.
  • The formula is pretty simple:
    1. Multiply the integer you choose with 32.
    2. Subtract 61 or 60 from the above product.
    And that's all ... you basically have two candidates for y:
    option 1: y == x * 32 - 61 option 2: y == x * 32 - 60
    There are many more candidates for each integer that complies with the above rules but that would go too far here.
  • Anyway, the values alone do not have any meaning without the proper code and here is where it gets a little tricky: The order of the lines in the while loop depend on which integer you choose for x and which value you choose for y from the above options. Here's the breakdown:
    • x == multiple of 3 + 1 && y == x * 32 - 61
      The only thing that matters here is that the assignment statement for y is at the end of the while loop. So you have the following options both of which will work:
      x *= 2 print (y % 4) y += 10 + x % 3
      print (y % 4) x *= 2 y += 10 + x % 3
    • x == multiple of 3 + 1 && y == x * 32 - 60
      If you choose this option the only important thing is that the assignment statement for y is at the beginning of the while loop:
      y += 10 + x % 3 x *= 2 print (y % 4)
      y += 10 + x % 3 print (y % 4) x *= 2
    • x == multiple of 3 + 2 && y == x * 32 - 61
      For this case there is ony one option to achieve the goal:
      print (y % 4) y += 10 + x % 3 x *= 2
    • x == multiple of 3 + 2 && y == x * 32 - 60
      This is the code for the final option:
      x *= 2 y += 10 + x % 3 print (y % 4)
2
As far as I understand, and I may be wrong, this level features a red hering. Let me show you what I mean:
  • According to the goal each of the first two elements of vector a must have a value of four at the end of the code.
  • That means that the first element of a, namely a[0], which is initialized with a value of 0 at the start of the code must be increased by a value of four.
  • The only way to reference a[0] is through the increment statement
    ++a[i]
    while i has a value of 0.
  • There are only two of these increment statements for a[i] which means at least one of them would have to be executed multiple times.
  • In order to use one and the same increment statement more than once it would have to be part of the while loop.
  • In effect this means that the value of i must not be changed during the while loop because if it is a[0] (referenced by a[i]) can under no circumstances reach the value of four.
  • It follows that
    ++i
    does not have any real purpose but to throw the player off track.
Having gotten this out of the way, and I'd gladly be corrected on my above conclusions, let's get to the solution. Here are some hints:
  • Given the condition of the while loop a[1] obviously has to be increased to a value higher than that of a[0] before the while loop.
  • If the value of i must not be increased before the end of the while loop you can only achieve
    a[2] == 5
    in one specific way.
  • Finally here are the calculations you need to replicate with the code:
    a[0]: 0 + 0 + (1 + 1) + (1 + 1) a[1]: 0 + (1 + 1) + 1 + 1
3
  • This level features a mutual recursion as it has two functions that call each other. It is actually not that difficult to solve for the following reasons:
    • The level includes a constant argument for the first call of function f.
    • The order of the code of function g does not matter.
    • There are only a handful of reasonable ways to assemble the code of function f.
  • Function g must be called 14 times to achieve the goal output. Here is a table of how many times g is called with what argument:
    Argument
    Number of calls
    0
    5
    1
    1
    2
    4
    3
    1
    5
    2
    8
    1
    Total of calls
    14
4
  • This level features two problems one of which is nested inside the other:
    • Finding the right order of code to produce the first three output sections of the goal.
    • Finding the right placement for the increment statements of count to produce the last output section.
    The later part can only be done once the code is in place for the first problem. So let's deal with the code for the first three output sections first.
  • The First Three Output Sections
    • There is only one reasonable way (without logical error) to assemble function f:if required it subtracts 3 from the value of parameter a until the value of a is smaller than or equal to the value of parameter b and returns the product of both parameters.
    • With that knowledge it is time to find the right pairs of integers for the calls of function f in the main function. It seems as if the input order of x and y is irrelevant because you can simply exchange the print statements in the for loop. In that context I advise to pay close attention to the condition in the for loop. In fact you should start by finding the input values for the third output section.
    • As a final hint here is the relation of x and y for each output section:
      1. output section: x is greater than y.
      2. output section: x is smaller than y.
      3. output section: x is greater than y.
  • The Last Output Section
    Once you found the right input to have the first three output sections of the goal printed you can tackle the final problem: where to place the increment statements for count. Simply count how often
    • function f is called in total,
    • the if condition is met,
    • the if condition is not met.
1.12 help ("extra", [5, 6, 7])
5
Here are some key factors for finding the solution to this level:
  • The first two increment / assignment statements executed by the main function must be
    1. ++x 2. y *= x
  • The print and the assignment statement do not belong in the same function.
  • One of the functions is called by the other function.
To facilitate finding the solution the following table shows how the values of x and y are adjusted during runtime:
Step Number
Value of x
Value of y
1
1
2
1
3
2
4
3
5
4
6
5
7
5
8
6
9
11
6
  • Look at the input integer m and see how it relates to one of the loops: m determines the iterations of the for loop.
  • There are three lines including dot. Given the goal it should not be too difficult to figure out how these three lines interact: dot is defined and initalized in one line, the other two lines switch the value of c between '.' and '!' if they are part of a loop.
  • The above findings suggest how to use both loops and assemble the code. Here's the algorithm in plain english:
    1. Get an integer value.
    2. Stop the algorithm in case the integer has a certain value.
    3. Set / switch the characters to be printed to either '.' or '!'.
    4. Add to a string the amount of characters determined by the integer value of step one.
    5. For each addition of a character in the last step check if the size of the string reaches its limit. If so, print the string and empty it.
    6. Repeat from the top.
7
I solved this using pen and paper. If you do not like that kind of challenge use this (still better than just checking the solution ;):
WolframAlpha Online Equation Solver[www.wolframalpha.com]
1.12 help ("extra", 8)
Ok, the extra chapter is almost complete but Level 8 is a true challenge of making sense of the code:
  • There are only two reasonable ways to assemble function h:
    • It repeatedly divides the value of parameter x by 2 until a quotient of 0 is reached, sums up the remainders during that process and returns this sum.
    • The only question is if the remainder of the first integer division counts into the return value or not. This question is easily answered by test running the code after assembly of function f. In fact, I suggest to go ahead and just assemble the remaining lines so that the code has an intact syntax and can be executed without compilation error. Then finish work on h by running the code and seeing in which order the two lines in question have to be placed.
    • In order to facilitate the following deductions now is the time to share a fun fact about function h:
      As stated above the algorithm used in h calculates the sum of the value of all remainders through an iterative division of parameter x by 2. In effect it thus counts the number of active bits in the binary representation of x. Furthermore, if you were to add all the remainders of this iteration (including 0) to a string that string would contain the reverse binary representation of x.
  • As regards function f the outline is clear. The allocation of the following three lines is questionable:
    f (n / 3 + 1) print (h (n)) print (h (n - 1))
    Looking at the first call of f in the main function and the corresponding output clarifies the matter to some extent:
    f (16)
    must cause the execution of three print statements with the following values: 1, 2, and 1.
    The if condition in f is
    n % 2 == 1
    Only odd values of n meet this condition. Considering that the function is called with the argument 16 and that we need three output lines it follows that the recursive call of f from within f must be placed in the else section. Otherwise we would get a maximum of only two output lines.
  • In order to allocate the remaining print lines we simply have to figure out what the return value of h would be in a call with the arguments 15 (h (n - 1)) and 16 (h (n)), respectively:
    • h (15) returns 4 (binary representation of 15: 1111)
    • h (16) returns 1 (binary representation of 16: 10000)
    Therefore the line
    print h (n)
    must be placed in the else section whereas the line
    print h (n - 1)
    must be placed in the if section.
  • However, the order of the two lines in the else section cannot be concluded yet. To clarify how functions f and h work together the following lists the steps executed by the code for each of the two options by calling f with the argument 16:
    • Else section: Print first, then call f
      • Parameter x has a value of 16 which means the if condition is not met. Therefore the following steps are taken:
      • Print the return value of a call of h with the argument 16. The return value of h is 1.
        OUTPUT: 1
      • Call f with the argument 16 / 3 + 1. The argument value is 6.
        • Parameter x has a value of 6 which means the if condition is not met. Therefore the following steps are taken:
        • Print the return value of a call of h with the argument 6 (binary representation of 6: 110). The return value of h is 2.
          OUTPUT: 2
        • Call f with the argument 6 / 3 + 1. The argument value is 3.
          • Parameter x has a value of 3 which means the if condition is met. Therefore the following step is taken:
          • Print the return value of a call of h with the argument 3 - 1 == 2 (binary representation of 2: 10). The return value of h is 1.
            OUTPUT: 1
          • End function f with n == 3.
        • End function f with n == 6.
      • End function f with n == 16.
    • Else section: Call f first, then print
      • Parameter x has a value of 16 which means the if condition is not met. Therefore the following steps are taken:
      • Call f with the argument 16 / 3 + 1. The argument value is 6.
        • Parameter x has a value of 6 which means the if condition is not met. Therefore the following steps are taken:
        • Call f with the argument 6 / 3 + 1. The argument value is 3.
          • Parameter x has a value of 3 which means the if condition is met. Therefore the following step is taken:
          • Print the return value of a call of h with the argument 3 - 1 == 2 (binary representation of 2: 10). The return value of h is 1.
            OUTPUT: 1
          • End function f with n == 3.
        • Print the return value of a call of h with the argument 6 (binary representation of 6: 110). The return value of h is 2.
          OUTPUT: 2
        • End function f with n == 6.
      • Print the return value of a call of h with the argument 16. The return value of h is 1.
        OUTPUT: 1
      • End function f with n == 16.
  • The above examplifies that that the order of the two statements in the if section merely determines the order of the print output but does not influence the output content. That means we have functions f and h working and can try and find the right input to complete this level. The order of the main function lines at the end is quite obvious. But how to find the right input?
1.12 help ("extra", 8)
I've tried for two days to come up with a simple formula to calculate the right input for each output section but I've come to believe that such formula does not exist. The problem is that h returns only the number of active bits of the call argument but disregards their position. That's why for instance each exponentiation of 2 as an argument to calling h returns 1. It follows that each value of the output section only presents information in conjunction with its surronding values.

But fear not! Actually, understanding how h and f work and a look at output sections four and five of the goal helps a lot with finding the right input integers:
  • The fourth output section requires two print statements each of which outputs 0. Since the print statements in f both call h we can simply check which argument would cause h to return 0. There is only one: 0 (binary: 0)
    However, calling f with both arguments 1 and 0 would lead to an output of 0 by h. Check both options and understand the difference and the similarity in output which is crucial to find the right input numbers:the end condition of the recursion through f is a recursive call with an odd argument. However, in that last call of f h is called with the argument n - 1 which means that through the entire recursive process h is only called with even arguments.
  • The fifth output section requires six iterations of f including the call in the main function because we need six integer outputs. There is a 0 at the bottom which can only be achieved by calling h with the argument 0. That leads to the following conclusions:
    • The print statement in the else section of f must precede the recursive call statement because if f is called with an argument other than 0 a return of 0 by h can only be achieved in the last iteration of f.
    • The last recursive call of f must have the argument 1 in order to print 0. That allows us to trace our way backwards through the recursion to the integer value required as input. For each preceding call of f we must find an even integer value that has a bit count corresponding with the output values:
      • Where 1 == n / 3 + 1 n can only be 2 (binary: 10).
      • Where 2 == n / 3 + 1 n can only be 4 (binary: 100).
      • Where 4 == n / 3 + 1 n can only be 10 (binary: 1010).
      • Where 10 == n / 3 + 1 n can only be 28 (binary: 11100).
      • Where 28 == n / 3 + 1 n can only be 82 (binary: 1010010).
      So there's the input for output section five!
  • Basically, this is the only process I found in order to find the correct input integers for the remaining output sections of the goal. To facilitate this process, once you found the even integer value preceding the last call of f you can simply multiply it by 3 and subtract 2. The remaining difficulty is to find the right 'starting integer' which actually refers to the end of the recursion. Let me help you with that, too:
  • The third output section requires two outputs: 2 and 3.
    That means that the last recursive call of f must feature an odd argument that has four set bits because in that last call of f h is called with the argument n - 1 which simply changes the odd value of n to the preceding even value by changing the first bit of n from 1 to 0. Obviously there is an infinite amount of integers that fulfil this requirement but let's find some options starting from the beginning:
    • The smallest integer with four set bits is represented in binary by 1111 which equals 15 in decimal. To make sure, h would be called with the argument 14 (binary: 1110) which has three set bits so that works. Now, use the above process to see if the argument value of the preceding call of f has two set bits:
      • (15 - 1) * 3 == (42 || 44).
      • Binary representation of 42: 101010
      • Binary representation of 44: 101100
      Since both candidates have three set bits we can eliminate 15 as the value for the last call of f. The next candidate for that call is 10111 in binary which represents 23 :
      • (23 - 1) * 3 == (66 || 68).
      • Binary representation of 66: 1000010
      • Binary representation of 68: 1000100
      Both values have two set bits, both values as integer input will produce the required output for section three of the goal!
  • Finally, it's time to tackle the big outputs in sections six and seven of the goal. You should know the process by now. Two final hints:
    • Take a close look at the last three values required for the output in section six. Haven't you seen that before?
    • Output section seven of the goal requires a 2 at the end. Thus, the final call of f requires three set bits: Here are some odd candidates:
      • 111 (decimal: 7)
      • 1011 (decimal: 11)
      • 1101 (decimal: 13)
      • 10011 (decimal: 19)
      • 10101 (decimal: 21)
      • 11001 (decimal: 25)
1.13 help ("impossible", 1)
This level is divided into three sections:
  • Assembling the code of the functions and making the first part of the main function work.
  • Finding the right input for the second part of the main function.
  • Finding the right input for the third part of the main function.
Let's deal with them one by one.
  • Assembling the functions code
    There are two functions to assemble. The third one called h is already done.
    • Function f
      This is what you can deduce exclusively from the code for that function:
      • Parameter s must be comprised of character or string data types because ret_val is defined as an empty string to which at least one element of s is added.
      • The if condition is met when the value of i is either smaller than p or greater than or equal to p + l. It appears reasonable to conclude that p determines position and l determines length.
      • In order for the if condition to make sense with the variable i it has to be part of a loop which can only be the for loop.
      • There are only two lines left now: the increment statement for i and the assignment statement for s. Both of them only have one resonable place left.
      Assembled correctly function h has the following functionality:
      It cuts an amount of l elements from parameter s starting at position p and returns the result.
    • Function g
      If you disregard the definition statements you are left with the following elements to assemble:
      • Two for loops
      • Two if sections
      • Two return statements
      • One break statement
      • One assignment statement for flag
      Let's put them together:
      • One of the if conditions accesses variables i and j. That means one of the for loops is nested inside the other and the if section is nested within both loops. At this point it is only an assumption but usually j loops are nested in i loops, not the other way around.
      • In order to make sense of that construct it is time to look at the call statement for g to understand the parameters better. g is called from within the h function. Retracing back to the main function it becomes clear
        • that both s and t hold string values and
        • that start holds an integer value.
        That means the construct of the last step would do the following:
        For each character of s it checks if starting from this position the content of s does not match the value of t.
        In more plain english:If t holds two characters the for-for-if construct checks if the first two characters of s do not match up with t, if the second and the third character of s do not match up with t, and so on until the end of s.
      • The previous finding would be in line with the first statements of the main function and the corrsponding output where apparently certain characters have to be removed from the string "abracadabra" during print. Only the following is left to accomplish: If in one iteration of the i loop the currently checked part of s matches up with t that part has to be removed. But there is no statement that modifies s. The reason for that is that function g achieves the intended return with a recursive algorithm as can be seen in one of the return statements.
        If s holds at least one set of characters that match up with the value of t function g returns a string that is the return value of a recursive call of function g with the matching section of s cut. The section of s matching t is cut by calling function h within the recursive call of g.
        The following examplifies how the recursion of g works to cut "a" out of "abracadabra".
        • First call (s = "abracadabra): Check if the first letter of s is not "a". Because it is call g with the arguments "bracadabra" for s, "a" for t and position 0 for start.
        • Second call (s = "bracadabra): Check if the first letter of s is not "a": Yes. Check if the second letter of s is not "a": Yes. Check if the third letter of s is not "a". Because it is call g with the arguments "brcadabra" for s, "a" for t and position 2 for start.
        • Three more calls like this each taking care of one "a" in the remaining string.
        • The final recursive call of g verifies that there is no set of characters left in s that matches t and returns the value of s all the way back as a return value to the first call of g.
      With this description it should not be difficult to find the right spots for the remaining lines of function g. Pay close attention to what variable flag does and where it must be defined / initialized. Note that the break statement has no purpose but speeding things up a bit.
  • The "true" Output
    Once the functions are set up three input strings are required that meet the following conditions:
    • They must all have a different a value.
    • They must all be five characters long.
    • After cutting out each and every occurence of "01" in these strings only "1" must be left over in each of these strings.
    This task does not require further help imo.
  • Hashes and Asterisks
    For this part of the level three string inputs are required that s will be tested against through calling h. Matching sections will be removed from s for the print statement only and the output in the goal must be accomplished. The only hint here is that the strings for the first and the last output are really easy to find.
1.13 help ("impossible", 2)
  • The Code
    • Function gcd
      There are only two if sections and three return statements available. The if sections must not be nested because their conditions exclude each other. Two return statements obviously have to be assigned to the two separate if sections and one return statement rounds off the function if none of the conditions of the if sections are met. In order to allocate the return statements a look at the first lines of the main function as well as the first lines of the goal is required:
      • In the first part, the main function features a loop with six iterations each of which calls gcd two times through print statements. That means that the first twelve output lines of the goal have to be achieved here. Each print statement has to output true which according to
        print (gcd (tests[i][0], tests[i][1]) == results[i]) print (gcd (tests[i][1], tests[i][0]) == results[i])
        means that when processed by function gcd the values of each point element of matrix tests have to produce the corresponding value in vector results regardless of the argument order.
      • With that knowledge it helps to look at the last two elements of tests
        [-1, 9] [0, 0]
        because according to results both of them require a gcd return value of -1. That means that
        return -1
        must be allocated to the if section with the condition
        a <= 0 || b < 0
        because that is the only condition that is met by the values of both elements of tests regardless of the argument order of the gcd call. Furthermore, this if section has to precede the other if section in order for the gdc calls with the last element of tests as arguments to return -1.
      • The remaining two return statements can be allocated with the knowledge that a division by 0 will cause a runtime error.
      As the name of the function suggests gdc returns the greatest common divisor of both parameters which is also the reason why the order of the arguments in the gdc call statement must be irrelevant for the return value.
    • Function f
      The lines for f are sort of messy to allocate because many of them access variables of different loops. Let's try and make some sense of the code now:
      • The lines
        if (gcd (x + i, n) == 1) return [x, i]
        must be part of both loops
        for (var i = 0; i < n; ++i) for (x : d)
        because they reference both i and x. Accordingly, one of the loops must be nested inside the other. Furthermore, it is fair to assume for the time being that the return statement is part of the if section because the return statement would return exactly the elements tested in the condition.
      • The line
        d.push_back (i)
        must be part of the for loop that iterates i. If you look at the definition statement for d
        var d = []
        it becomes apparent that before entering the for loop
        for (x : d)
        at least one element must be assigned to d because otherwise that loop would never trigger. That means that
        d.push_back (i)
        must precede that loop and, in turn, that the for loop iterating x must be nested inside the for loop iterating i.
      • That leaves only
        if (n % i == 0) return [0, 0]
        Let's look at the condition first. It must be part of the for loop iterating i and it is met only when n can be divided by i without remainder, i.e. when i is a divisor of n. In order to allocate that condition the goal output becomes important. The main function calls f consecutively with seven integer values. The expected return for each call is two integer values that are divisors of the call argument unless, apparently, the argument is a square number in which case the return values should both be 0.
        Given that the return values greater than 0 must be caused by
        return [x, i]
        and that x is an element of d one can assume that all statements in the for loop iterating i are conditioned by the remaining if section. Otherwise, there would be the possibility that f returns values that are not divisors of n through both x and i.
        It would follow that the remaining return statement must be placed at the end of function f to make any sense.
      Thankfully, this assembly of f handles the test cases well.
  • The Input
    In the end of the main function there are three assignment statements each of which has to increment count in order to achieve the goal output of 3. That means that for each call of check in these assignment statements a return value of 1 is required. In order to get that return check must be fed with an integer value that is processed by f to return values that equal the values in parameter goal. Furthermore, the product of those two return values must not equal the value of the input integer.

    But what exactly does f do in order to achieve a return value? Without that knowledge it appears virtually impossible to find the right input for each call of function check. Here's the shortest answer I can come up with for the functionality of f:
    f iterates through all integers from 2 to the argument value. If it finds a divisor it checks whether the argument value and the sum of this divisor and each previously found divisor has a greatest common divisor that equals 1. If so the value of these two divisors is returned. Otherwise f returns [0, 0].
    Let's illustrate that with what happens in the gdc call with argument 210:
    i
    210 % 2 == 0
    x
    x + i
    gdc (x + i, 210)
    2
    yes
    2
    4
    2
    3
    yes
    2
    5
    5
    3
    6
    6
    4
    no
    5
    yes
    2
    7
    7
    3
    8
    2
    5
    10
    10
    6
    yes
    2
    8
    2
    3
    9
    3
    5
    11
    >> 1 <<
    With that I wish you good luck in finding the right input. In fact, there is a very simple formula to calculate valid inputs but I'm not gonna disclose that knowledge here. It helps to tackle the inputs one after the other. When trying to find the first input simply add "1 1" to prevent a runtime error.
1.13 help ("impossible", 3)
  1. The Code
    • Function f
      There are only two statements available for f that "do" something:
      ret_val += c ret_val.erase_last ()
      Try and match them with the goal output and the first lines of the main function in order to find the right placement for all lines of f: Function f parses through all elements of parameter s and adds them to ret_val unless there is a '<' character in s. In that case and if the character is not the first one in s the last element of ret_val will be erased.
      Easy to build with the test arguments in the main function and the first output section of the goal (first four lines)
    • Function includes
      As the name suggests includes should return true if something includes something. Again, easy to build with the test arguments and the second output section of the goal (lines five, six and seven)
  2. The Input
    Instead of providing help with the input strings I rather go into detail how functions get_string and test work so that you maybe get a better understanding of what conditions must be met to achieve the goal:
    • Function test
      Function test must return true in four of the five cases it is called from the main function. There are three return statements, two for false and one for true. Unfortunately, the return statement for true cannot be moved which means that each call of test has to avoid two conditions by not meeting them in order to get to the true return:
      • if (f (new_str) != goal || new_str.size () != length)
        This condition is met, and test will return false, if at least one of the following two terms is true:
        • The value of new_str modified by f is not equal to the value of paramter goal.
        • The length of new_str does not equal the value of parameter length.
        Using
        print (test ("a", 3, 3, "a"))
        as an example that means in order not to get a false return
        • new_str modified by f must be "a" (according to the first argument of the call statement) AND
        • the length of new_str must be 3 (according to the third argument of the call statement).
      • if (s == new_str)
        This condition is part of a loop:
        for (s : strings)
        For each iteration of the main for loop started by
        for (var i = 0; i < n; ++i)
        the for loop with the condition checks if new_str has a value equal to any of the values of the vector strings. As can be seen a couple of lines below the value of new_str in the current iteration of the main for loop is added to strings.
        In effect that means that the code requires an amount of input strings that is specified by parameter n and none of these input strings must have the same value as any other input string.
        Using
        print (test ("a", 3, 3, "a"))
        as an example that means in order not to get a false return three (according to the second argument of the call statement) non-identical input strings are required.
    • Function get_string
      This function adds another condition to the input strings by only adding characters to the return value that are part of parameter allowed_characters:
      if (allowed_characters.includes (c))
      This is an example for a different type of call statement which is very well readable. In effect the condition restricts the characters allowed for variable new_str in function test.
      Using
      print (test ("a", 3, 3, "a"))
      as an example that means that new_str of function test can only have the following characters:
      • 'a' (according to the fourth argument of the call statement) and
      • '<' according to the call statement of get_string in function test
        var new_str = get_string (allowed_characters + "<")
    To sum the conditions up, for each print statement at the end of the main function but one the following is required:
    An amount of input strings that is specified by the second argument of the call statement where each input string
    • has the amount of characters determined by the third argument of the call statement,
    • aside from '<' consists only of the characters specified in the fourth argument of the call statement (although technically such characters would simply be disregarded),
    • is modified by function f to the value of the first argument of the call statement,
    • is not equal to any other input string of this call of test.
    Good luck finding the right input!
1.13 help ("impossible", 4)
  • The Code
    In this level assembling the code properly is fairly easy given that it is only about function f and that aside from the clearly allocatable definition statements you only have five more statements that are also easily allocatable:
    • While loop:
      This one can be placed after the varibable definitions for good measure.
    • Decrement statement:
      This obviously has to be part of the while loop for the loop not to run indefinitely.
    • Return statement:
      Can reasonable only be placed after the while loop because there is no condition statement that would allow for the return statement to go into the while loop.
    • Two assignment statements:
      With the current structure of f both assigment statements have to go into the loop to make any sense. If you do not know the order simply test both options against the goal.
    And that's it already :)
  • The Input
    Now, finding the right input is a challenging task. Originally I solved this level by using an excel spreadsheet that calculated all possible results of all inputs and found the right integers. But for this guide I went back and tried to find an easier approach and there actually is one. Here's what you need to know:
    • First of all, does the first sequence of output integers in the goal look familiar:
      1 1 2 3 5 8 13 21 34 55
      It is the first ten integers of the Fibonacci sequence which is a recursive sequence where each element (except the first two) has a value that is the sum of the values of its two predecessors.
    • That is exactly what function f does in the while loop. In each iteration:
      1. The value of a is added to the value of b.
      2. The difference of the values of b and a is assigned to a, effectively assigning to a the original value of b in this iteration of the loop.
      To clarify, the same result can be achieved with
      var tmp = b b += a a = tmp
    • However, function f accepts deliberate staring values for the sequence which makes it difficult to predict how the sequence develops. Nonetheless, one of the key features of the Fibonacci sequence is still present in the modified version of function f:
      Originally posted by wikipedia:
      If a Fibonacci number is divided by its immediate predecessor in the sequence, the quotient approximates φ; e.g., 987/610 ≈ 1.6180327868852. These approximations are alternately lower and higher than φ, and converge to φ as the Fibonacci numbers increase.
      The symbol φ represents the Golden Ratio as detailed above.
    • With that knowledge it is easy to calculate the input integers required for each of the goal outputs. I'll examplify this for the 1000 output:
      • The first step is to divide the desired output value by the Golden Ratio. Make sure to use sufficient decimal places to get proper result:
        1000 / 1.618033 == 618,0343664189791
        Round that to 618 and you have your predecessor to 1000 in the sequence!
      • Secondly, work your way backwards by subtracting the predecessor and count the steps.
        1. 1000 - 618 == 382
        2. 618 - 382 == 236
        3. 382 - 236 == 146
        4. 236 - 146 == 90
        5. 146 - 90 == 56
        6. 90 - 56 == 34
        7. 56 - 34 == 22
        8. 34 - 22 == 12
        • The result of a subtraction must be greater than -1 and smaller than 36 to qualify as value for a AND
        • The subtrahend, i.e. the value being subtracted, must be greater than -1 and smaller than 31 to qualify as value for b.
        • The value for n is the amount of subtractions performed until both of the previous conditions are met.
        So there you have your input values to achieve an output of 1000: 12 22 8.
        In fact there are three more sets of integers to get to 1000. Care to try and find them?
    • Finding the values for the remaining output of the goal should be easy now.
1.13 help ("impossible", 5)
  • The Code (function f)
    The only thing to assemble in this level is function f. If you do not know where to start have another look at the matrices levels first.
    This is what you've got to work with:
    • Definitions (4):
      Definition statements for copy, n, x, and y must be allocated. The definitions of x and y must be placed within the nested loops for dir, and i and j, respectively.
    • Loops (4):
      There is one while loop and three for loops. Because of
      copy [x][y] = (copy [x][y] + a[i][j]) % 11
      all three for loops must be nested and the definition statements for x and y must be placed inside these three loops in order to prevent a compilation error. The above assignment statement has to follow the definitions inside the loops. It is just an assumption at this stage but it appears that the while loop encloses all other loops.
    • Condition (1):
      The if condition reads
      good_coord (a, x) && good_coord (a, y)
      Therefore it must be placed after the definitions of x and y. The only reasonable way to do this is to condition the assignment statement mentioned above with the if section.
    • Remaining lines:
      That leaves only a handful of lines. Given the structure that is already built there is no sensible way to nest the while loop as well as its connected lines
      var n = k --n
      inside the other loops. It must surrond the other loops. Remaining lines are
      var copy = generate (a.size ()) a = copy
      It can be concluded where these lines have to go from the current structure of f in conjunction with the first lines of the main function and the first output sections in the goal. However, it is really difficult to see and easier to find the right placement by trial and error.
    • Functionality of f:
      Instead of giving more instructions as to the placement of the remaining code lines I'll provide an overview of what f is supposed to do according to the goal and the structure we already have:
      The value of each element of matrix m is added to the value of the three elements of matrix m that are determined by the content of the global vector directions in relation to the current element of matrix m, and the value of the current element of matrix m is deleted. This entire process is repeated as often as specified in parameter k.
      The first two output sections give a pretty good idea of how f works. However, if you cannot make your output match the following sections of the goal here is a visual representation of what the three test calls of function f are supposed to do:



      To facilitate following the movement of the values the image disregards the modulo operation in the assignment statement. If you cannot get the required output by allocating the remaining two lines to the already assembled code of f chances are that your output (and the process behind it) looks like this:



      That should hopefully help to figure out what's wrong in your code and to find the right spot for the remaining two lines of function f.

  • The Input
    Finding the right input for the final matrix is really difficult and I venture to say that, as mentioned on the store page by the developer, it is next to impossible without external help. The problems are manyfold:
    • Finding the input pattern for matrix m is complicated by the fact that the amount of required iterations in f is unclear.
    • Finding the right amount of iterations is complicated by the fact that the target matrix is calculated with a modulo operation.
    • With each iteration values potentially muliply depending on their position in the matrix.
    Visual Studio could be used to find the one and only sequence that produces the goal output. I chose to use Excel extensively because in general it helps to better understand what a function does. And indeed, it provided me with very helpful insight about one key characteristic of function f:
    The content of directions effectively divides the matrix into three zones all of which are separate and distinct from each other.

    The following image visualizes the different zones and their independence from each other:



    As can be seen above the matrix is divided into three zones each of which contains diagonals from the bottom left to the top right that are two fields apart. This division is caused by the content of directions which leads to the value of each element being pushed
    • to the element to the right (directions[0] == (0, 1)),
    • to the element on the top left corner (directions[1] == (-1, -1)), and
    • to the element at the bottom (directions[2] == (1, 0)).
    With each iteration the zone affected by an element of the input matrix moves on step further. In effect the amount of iterations determines which zone of diagonals is exclusively influenced by a specific element of the original matrix. Furthermore, the zones are independent from each other because in each iteration a new matrix copy is defined which is filled only by copying elements from the previous matrix.

    Having unearthed this fact about function f a divide and conquer tactic can be applied to find the right solution:
    Since values are added independently from each other to the new matrix copy per iteration, it is possible to look at the development of the value of each single element of the matrix through the iterations and add values only per zone and iteration in search for the required goal output for one particular zone. Example:



    But maybe you can find an even easier way ... I'd be happy to hear about it.
1.13 help ("impossible", 6)
I found the level to be on the easier side which is why I will give less explanations here.
  • The Code
    Assembling the code is mainly about making function f work. Function g should come easy with the limited amount of lines available. As to function f:
    • Take a close look at how the indices of the three for loops relate to each other: the indices of the for loops divide parameter a into three sections:
      • a[0] to a[m - 1]
      • a[m] to a[a.size () - m - 1]
      • a[a.size () - m] to a[a.size () - 1]
    • Next have a look at the first loop of the main function. Try and figure out how each element of the vector
      [[4, 1], [9, 3], [10, 5]]
      relates to the corresponding output of the goal:
      • [4, 1]: size of output vector: 4 / characters exchanged from beginning and end: 1
      • [9, 3]: size of output vector: 9 / characters exchanged from beginning and end: 3
      • [10, 5]: size of output vector: 10 / characters exchanged from beginning and end: 5
    • This should give you a pretty good understanding of how to arrange the three for loops of f.
  • The Input
    As to the required input there is a fairly simple, albeit tedious, algorithm to achieve the goal output. It is based on the fact that the process of scrambling a sorted vector by use of a function like f must be reversable by use of the same function because f works in a linear fashion. Thus you can simply work your way backwards from the center to the ends in order to achieve the desired result.
    I'll examplify the algorithm by showing how to reach target vector
    1 4 6 3 0 2 5
    The vector to start with requires seven elements:
    0 1 2 3 4 5 6
    The following table provides position data for vector elements starting from 1 as opposed to index data which starts with index 0.

    Step
    Position(s) of target vector concerned
    Desired value(s) of target vector
    Position(s) of desired value(s) in current vector
    f calls required
    m
    Content of v at end of step
    1
    4
    3
    4
    0
    0 1 2 3 4 5 6
    2
    3 / 5
    6 / 0
    7 / 1
    1
    3
    4 5 6 3 0 1 2
    3
    2 / 6
    4 / 2
    1 / 7
    2
    1
    2 5 6 3 0 1 4
    4
    2
    1 4 6 3 0 2 5
    5
    1 / 7
    1 / 5
    1 / 7
    0
    1 4 6 3 0 2 5

    I hope this clarifies the process. It helps to use pen and paper to find the right value of parameter m for each call of f
1.13 help ("impossible", 7)
  • The Code
    • Function f
      • Like in previous levels with matrices there are two lines that indicate the positioning of the loops and the if section:
        var cx = x + dir[0] * i var cy = y + dir[1] * i
        It follows that the only reasonable place for
        ++m[cx][cy]
        is in the if section.
      • The remaining lines
        var cp = min (p, m.size () - 1) m[x][y] += cp
        are also easy to place.
    • Main function
      • The four lines of the main function following f, especially
        f (m, 3, 2, 1) f (m, 2, 0, 0)
        would test the correct assembly of f. But since the remaining lines of the main function are not assembled that is not an option. It can be said however, that the already sorted parts of the main function would print the first two matrices in the goal because there are two calls of print_matrix. Therefore the remaining main function lines must be responsible for the remaining five matrices in the goal.
      • Apart from
        • four variable definition statements,
        • two loops, and
        • one condition
        there are three lines of particular interest:
        f (m, p, x, y) print_matrix (m) m = generate (n)
        Obviously, these lines are responsible for the creating, manipulating and printing the matrices required for the goal.
      • It should not be too hard to bring all the lines in order considering the above and the following:
        • The same process has to be executed for each of the five target matrices, i.e. five times.
        • The only way to exit the while loop is by using the condition.
    • Even if the main function may not work yet at least the proper assembly of function f can be tested.
  • The Input
    In order to produce the required five output matrices a throrough understanding of the functionality of function f is required, albeit not sufficient, unfortunately. Try and figure out the exact process executed by f. The following summarizes the key features of f:
    • f performs a cross shaped manipulation of the elements of matrix parameter m.
    • The values of parameters x and y are used as index data to specify the center position of the cross shaped manipulation of the elements of m.
    • The value of cp is used to determine the increment value for the center position as well as the length of the lines of the cross shaped manipulation. The value of cp is determined by parameter p and the size of m.
    • All elements of m affected by f are incremented by 1. The only exception is the element in the center position which is incremented by the value of cp.
    As an example, here is how the first of the remaining five goal matrices must be created step by step:
    • Default matrix with size 5
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    • Step
      Manipulation: Result: 0 0 0 1 0 0 0 0 1 0 0 1 1 2 1 0 1 1 2 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    • Step
      Manipulation: Result: 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 2 1 2 1 0 1 0 0 0 0 1 0 1 0 1 2 1 1 0 1 2 1 2 0 0 1 0 0 0 0 1 0 0 0
    • Step
      Manipulation: Result: 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 2 2 2 1 0 1 1 1 0 0 2 1 2 0 0 0 1 0 0 1 2 2 2 0 0 0 0 0 0 0 1 0 0 0
    This example illustrates
    • that the center position increment and the length of the manipulative reach into each direction from the center position are one and the same and
    • that the order of the steps is irrelevant since f manipulates the elements of m exclusively through incrementation.
    Now, I'm not gonna lie. Finding the appropriate input data took was difficult and nerve-wrecking, especially for matrices three and four of the remaining five. Funnily enough, matrix five only took about ten minutes to produce.

    I used Excel for all five matrices but the first one (the solution to which you can infer from above). If you do not have access to Excel use OpenOffice and set up the spreadsheet so
    • that you can enter each manipulation in a different section of the spreadsheet,
    • that there is one section that shows the sum result of all manipulation sections, and
    • most importantly, that there is one section that shows which elements of the goal matrix have already reached the target value!
    For the last part I simply subtracted the matrix showing the result of the manipulations from the target matrix. That really helped in
    • identifying manipulations that made the target matrix unreachable and
    • deducing required manipulations.

    With that being said here's what you should pay special attention to for each of the remaining four matrices:
    • First line: 2 3 3 2
      I'm not 100% sure but I think there is only one way you can achieve the bottom line of the target matrix without making the target matrix unreachable. Focus on the fact that both 2's are surrounded by lots of 1's.
    • First line: 2 2 3 2 2
      This one had me running around in circles for quite a while. In the end I just wasn't running enough circles. The solution I found is not symmetrical but follows a clear pattern.
      I used ten manipulations:
      • Five manipulations with cp == 1
      • Four manipulations with cp == 2
      • One manipulations with cp == 3
    • First line: 1 4 4 3 2
      This one I struggled with the most. The bottom right element is the clear starting point for a solution. From there you can deduce all sorts of stuff for the second column of the matrix none of which, however, was really stringent for me. In the end I resorted to trying out many different possibilities.
    • First line: 3 4 2 1 3 1
      I found the bottom left corner to be a very good starting point while keeping an eye on the blank value in element [1, 0] of the target matrix. That lead to very quick results.
1.13 help ("impossible", 8)
  • The Code
    • Function calc
      The target purpose of function calc is illuminated by the following four lines of the main function in conjunction with goal output:

      Main function line
      Corresponding output
      Calculation
      print (calc ("11+"))
      2
      1 + 1
      print (calc ("99*"))
      81
      9 * 9
      print (calc ("11+11+*"))
      4
      (1 + 1) * (1 + 1)
      print (calc ("78*5/95+-"))
      -3
      (7 * 8) / 5 - (9 + 5)

      Instead of providing input as to how function calc should be assembled I'll show you how the term "78*5/95+-" is processed by calc with special attention to the content of vector stack which is intialized with no element:

      c : s
      is_digit (c)
      Content of stack at end of this step
      7
      true
      7
      8
      true
      7, 8
      *
      false
      56
      5
      true
      56, 5
      /
      false
      11
      9
      true
      11, 9
      5
      true
      11, 9, 5
      +
      false
      11, 14
      -
      false
      -3

      This process shows that function parses through each element of parameter s and
      • either adds them to stack if the element is a digit
      • or processes the last two elements of stack by use of the current element of s into a new last element of stack which replaces the processed two elements.
      With these explanations function calc should be easy to assemble. You just need to pay attention to the order of retrieving the last two elements of stack.

    • Function replace_dots
      To make sense of the code it is advisable to have a look at the call arguments in the main function and the corresponding return value first:

      s, t
      ret_val
      "..", ""
      ++
      "1.2", "*"
      1*2
      "..", "/-"
      /-
      "h.e.l.l.o", "w-o-r-l-d"
      h+e-el+l-o

      Apparently, dots in s are replaced by characters of t. In order to understand how this replacement works and why the last of the above outputs contains + signs it is important to focus on the value of p and understand this line
      ret_val += p < t.size () && is_op (t

      ) ? t

      : '+'[/code]
      Function is_op shouldn't be hard to understand at this stage.[/list]

    • The Input
      With a thorough understanding of how functions calc and replace_dots work (which is the reason why the above explanations left most of the assembling to you) it is time to look at the remaining lines of the main function. Each string element of vector tests is processed by replace_dots with an input string of choice and the result is processed by calc the return value of which has to equal the corresponding output in goal:

      t : tests
      calc return value
      27.
      18
      12.34..
      9
      572.3..498...
      83
      31.2.34..567.8943......
      -10000

      Apart from the last element of tests I found this part of the level not to be particularly challenging. If you are struggling try and process the elements of tests like calc does. In the absence of arithmetic operators simply bracket the last two elements preceding a dot sign thereby simulating a newly processed element. Let me show you on the example of tests[1]:

      c : s
      is_digit (c)
      Content of stack at end of this step
      1
      true
      1
      2
      true
      1, 2
      .
      false
      (1, 2)
      3
      true
      (1, 2), 3
      4
      true
      (1, 2), 3, 4
      .
      false
      (1, 2), (3, 4)
      .
      false
      ((1, 2), (3, 4))

      With the term generated this way you can start looking for the right input string. Each bracket requires an arithmetic operator. The order of the operators in the input string is determined by the order in which the brackets were placed:
      • First operator: (1, 2)
      • Second operator: (3, 4)
      • Third operator: ((1, 2), (3, 4))
      Good luck!
1.13 help ("impossible", 9)
  • The Code
    • Function f
      • Start by placing the following lines in the only possible order:
        while (n > 0) { } var n = m.size () --n
      • Remaining lines are
        var px = x var py = y ++m[px][py] px = (px + dx) % m.size () py = (py + dy) % m.size ()
        The first two of these lines only make sense if they are placed before the while loop.Concurrently, the last three lines only make sense if they are placed inside the while loop.
      • That's all for function f. It does exactly what the goal requires:
        • Specific elements of matrix parameter m are incremented by one.
        • The amount of elements incremented is determined by the size of m.
        • Depending on the order of the lines in the while loop the incrementation process starts at the index specified by x and y or x + dx and y + dy.
        • The index of the element to be incremented in each following iteration of the while loop is determined by parameters dx and dy in relation to the previously incremented element.
        • The modulo operation ensures that the incrementation process cannot surpass the limits of m.
    • Main Function
      • At the end of the main function the goal requires an output of four matrices with sizes from three to six which suggests that the for loop encompasses most if not all the remaining lines.
      • The first line in the for loop should obviously be
        a = generate (n)
      • The last line of the for loop will probably be
        print_matrix (a)
      • The while loop manages the calls of f that are required to reach each target matrix and should be easy to assemble with this knowledge.

  • The Input
    The input required is not easy to find but, as in level impossible 7, a spreadsheet application such as Excel or OpenOffice Calc is of great help! In fact, I found that the right input for this level was easier to find than the input for level 7. Chech the hints for impossible 7 if you want information as to how to set up the spreadsheets.

    The first step for each target matrix is to calculate the amount of f calls required. Since each call of f increases the sum of all element values of m by the size of m a simple division does the trick. Example for the first target matrix of the goal:
    3 + 5 + 4 == 12 2 + 4 + 0 == 6 3 + 2 + 1 == 6 Total sum == 24 Amount of f calls required == 24 / 3 == 8
    The next steps should focus elements of the target matrix that have a great value. As an example the following shows the only options to "reach" the element with the value 5 in the first of the four target matrices:
    Target matrix 3 5 4 2 4 0 3 2 1 f (a, 0, 0, 0, 1) | f (a, 0, 1, 1, 0) | f (a, 0, 1, 1, 1) | f (a, 0, 1, 1, 2) 1 1 1 | 0 1 0 | 0 1 0 | 0 1 0 0 0 0 | 0 1 0 | 0 0 1 | 1 0 0 0 0 0 | 0 1 0 | 1 0 0 | 0 0 1
    Now, if you compare these options with the entirety of the target matrix the following can be concluded:
    • f (a, 0, 0, 0, 1) can be called a maximum amount of 3 times because of the element at index (0, 0) of the target matrix.
    • f (a, 0, 1, 1, 0) can be called a maximum amount of 2 times because of the element at index (2, 1) of the target matrix.
    • f (a, 0, 1, 1, 1) can be called a maximum amount of 0 times because of the element at index (1, 2) of the target matrix.
    • f (a, 0, 1, 1, 2) can be called a maximum amount of 1 times because of the element at index (2, 2) of the target matrix.
    This means that five of the six possible ways to manipulate the element at index (0, 1) of the matrix have to be part of the solution whereas one of the six must not be part of the solution. Cross reference this finding with other deductions and you are done.

    This basic process is applicable to each of the target matrices and, in my case, lead to results pretty quickly.
1.13 help ("impossible", 10)
Ok, I'm not sure what hint(s) I can give here. There are eight command lines and several call statements which allow for an enormous number of permutations of those lines. While it is possible to get close to the goal output with some trial and error I believe it is literally impossible to find the right combination of lines just by trying. Having sort of a basic understanding of coding I decided to create a program that "simply" parses through all possible permutations until the target value of a and b is found.

As of now, I will not give more explanations as to how I coded that parsing mechanism which took me about six hours with lots of googling and trial and error (again, I do not usually code). If anyone is interested feel free to comment below.

As to the order of the lines in this level I can tell you the following:
  • The main function simply calls g three times.
  • Function g calls f two times, performs one assignment statement and then calls f again.
  • Function f executes all other assignment/increment statements.
There may be other solutions, I haven't checked that yet. Anyhow, there are still countless options of arranging the code with the above restrictions. So if you don't want to check the solution for this level you will most likely have to resort to coding yourself.
2.01 solve ("hello, world!")
  1. print ("hello, world!") print ("str") print (123) print ("END")
  2. var number = 100 var s = "this is a variable" print (55) print (s) print (number)
  3. var x x = 10 print (x) x = 15 print (x)
  4. var n n = 4 print (n) print ("this is a string") n = 2 + 5 print (n)
  5. var n = 10 print (5 + 100) print (n / 2) print (n + 20) print (n * 10) print (n - 2)
  6. var apples = 10 var bananas = 5 var fruit = bananas + apples print (apples) print (fruits)
  7. var x = 3 x = x + 1 print (x) x = x + 3 print (x) [/code]
  8. var a = 1 var b print (a) b = a + 2 print (b) b = a + b print (b)
  9. var m = 0 m = m + 3 m += 1 print (m) m += 10 print (m) m += 1 print (m)
  10. var x = 5 print (x) x = x * 5 print (x) x += 10 print (x) x = x+5 print (x) x *=10 print (x)
  11. var x = 10 var y = 2 x -= 5 print (x) print (y) x *= y print (x) x -= y + 1 print (x) x -= y - 1 print (x)
  12. var s = "abc" var t = "ABC" print (t) print (s + "d") print (s + ", " + t)
  13. var str = "b" str = "a" + str print (str + "a") str = str + "cc" str = str + "a" print (str)
  14. var s = "." var t = "#" t += s print (t) s += t print (s) s += s print (s + "##") t = s + "#" + t print ("." + t)
2.02 solve ("functions)
  1. def func () { print ("hello!") } func () print (1000) func ()
  2. def f () { print ("abc") print (4) } f () f ()
  3. def f () { var x = 1 print (x + 1) x += 4 print (x - 1) } f () f ()
  4. def f () { print (1) } f () print ("text") f ()
  5. def func (a) { print (a) } func (7) func (135) func (3)
  6. def g (x) { print ("abc") print (x) print (x + 3) } g (1) g (5)
  7. def func (arg) { print (arg + ".") } var s = "!!!" func ("qwerty") func (s) func (s + "?")
  8. def t (x) { var y = x y += 3 print (y) } t (4) t (1)
  9. def f (x) { print (x * 2) } print (1) f (4) f (1) f (50)
  10. def f (a, b) { print (a * b) } f (2, 3) f (10, 4)
  11. def sum (n, m) { print (n + m) } var x x = 3 sum (4, x) x = 1 sum (x, 9)
  12. def f (x, y) { print (x + y) print (x - y) } f (10, 5) f (11, 4)
  13. def g (a, b) { print (a, a / b) } var x x = 2 g ( 3, x) g (100, x) x = 5 g (100, x)
  14. def f (x) { print (x) print (x - 3) } var n = 4 f (n) n += 3 f (n) f (n + 2)
  15. def g (x) { print ("g", x) } def f (x) { g (x) print ("f", x) g (x + 1) } f (2) f (2 * 2)
  16. def f () { print ("?") print ("?") print ("?") } print ("?") print ("?") print ("?") print ("?") f () f ()
  17. def f () { print ("!!!") print ("?") print ("!!!") } f () print ("?") f () print ("?") f ()
  18. def f (x) { print (x + 2) } def g (x) { print (x + 1) } f (2) f (3) g (4) g (3)
  19. global a = 0 global b = 0 def f () { ++a ++b ++b } ++a ++a f () f () f () print (a, b)
  20. global a def f () { ++a a *= 2 } def g () { print (a) a += 3 } a = 0 f () g () g () f () print (a)
  21. def f1 () { print ("???????") f2 () } def f2 () { print ("!!!!!!!") } f1 () print (".......") f1 () f2 ()
  22. global a = 0 def f () { print (a) a += 2 } f () a += 1 print (a) a *= 2 f () a += 3 print (a)
  23. global a def f (x) { var m = x a += m print (a * m) } a = 0 f (3) f (a / 2) f (a) f (2) print (a)
  24. def func (x, y) { x += 2 y -= x } var a = 1 var b = 3 print (a) func (a, b) print (b) func (b, a) print (a)
  25. global s = "" def f () { s += "1" s += "0" } s += "0" f () f () s += "0" s += "0" f () print (s)
2.03 solve ("return")
  1. def two () { return 2 } def three () { var a = 2 + 1 return a } print (two (), three ()) def f () { var b = two () return 2 * b } var x = three () print (x - two ()) x += f () print += f() print (x + 1) print ((f () + x) / two ())
  2. def f () { print (1) return "one" } def g () { print (2) return "two" } print (f ()) g () print (g ()) f ()
  3. def f (x, y) { return x + y } var a = 3 print (f (a, a)) print (f (a, f (1,1))) print (f (1, 2)) a = f (a, 5) print (f (a, 4))
  4. def f (n) { return n * n } var x = f (1) print (x * f (2)) x += f (3) print (x) print (f (x) var y = f (x - 1) print (y + f (-3))
2.04 solve ("input")
  1. var x = get_int () print (x) var y = get_int () print (y + 1) print (get_int ()) print 40 INPUT: 10 19 30
  2. var s = get_str () print ("abc" + s) print (s + get_str ()) print (s + "...") INPUT: abc ?
  3. def f (x) { print (x + get_int ()) } f (1) f (3) f (-1) INPUT: 4 -8 6
  4. def f () { return get_int () * 3 } var x = f () print (1 + x) print (f ()) print (x + f () / 2) INPUT: 5 27 5
  5. def f (n) { return n * n + 1 } var x = get_int () print (f (x + 1)) print (f (get_int ())) print (f (get_int ()) * x) INPUT: 8 0 3
  6. def f (n) { var x = n -1 print (x * n) return x - 1 } var a = f (get_int ()) f (f (get_int ())) f (a + 3) INPUT: 1 8
2.05 solve ("conditions")
  1. def f (x) { if (x > 9) { print (x, "is greater than 9") } } f (100) f (10) f (1) f (2)
  2. def t (n) { if (n > 9) { print (n) } print (n + 2) } t (1) t (28) t (20) t (30) t (3)
  3. def f (x) { print (x) if (x == 0) { print (x, "is zero") } if (x != 2) { print (x, "is not two") } } f (0) f (2) f (3)
  4. var x = 10 print (x == 3) print (x) print (x > 3) print (x < get_int ()) print (x < get_int ()) print (x != get_int ()) var y = get_int () print (x + 3 < y) print (x + 4 >= y) INPUT: 11 10 10 14
  5. def f (x) { if (x > 0) { print ("positive") } else { print (x) } } f (5) f (get_int ()) f (2) f (0) f (-3) f (get_int()) INPUT: -9 1
  6. def f (x, y) { if (x > y) { return x } else { return y } } var x = f (3, 2) var y = f (4, x) print (f (1, 2)) print (x + y) print (y) print (x)
  7. def f (b) { if (b) { print (b) } else { print ("b is false") } print ("-----") } f (5 > 2) f (5 < 2) f (get_int () == 3) f (get_int () != get_int ()) INPUT: 3 1 1
  8. def f (b1, b2) print (b2) if (b1 && b2) { print ("both b1 and b2 are true") } } f (true, false) f (true, get_int () > 2) f (false, false) f (false, true) INPUT: 3
  9. def f (b) { print ("{") if (!b) { print ("not b == true") } print ("}" } f (true) f (false)
  10. def f (a, b) { print ("--") if (a || b) { print ("at least one of a and b is true") } else { print ("both a and b are false") } } f (false, true) f (true, false) f (false, false) f (true, true)
  11. def f (x, y) { print ("--") if (x > 0 || y > 0) { print ("positive:") } if (x > 0) { print ("x =", x) } if (y > 0) { print ("y =", y) } } f (0, 0) f (5, 0) f (-100, 100) f (-3, -4) f (3, 4)
  12. def f (x, b) { print ("===") if (x * 3 > 10) { if (x < 7 && b) { print ("yes") } print (x < 7) print (b) } else { print ("no") } } f (get_int (), false) f (get_int (), true) f (get_int (), true) f (get_int (), true) INPUT: 4 7 0 4
  13. def f (x) { print ("f (x) {") if (x == 1) { print (" one") } else if (x > 0) { print (" x > 0") } else { print (" ", x) } print ("}") } f (0) f (1) f (2) var n = get_int () f (n + 4) f (n + 2) INPUT: -2
2.06 solve ("while")
  1. var x = 0 while (x < 10) { print (x) ++x }
  2. var n = get_int () var x = get_int () while (x < n) { print (x) ++x } INPUT: 16 7
  3. var x = 123 while (x != 0) { x = get_int () print (x) } INPUT: 1 2 3 10 -4 0
  4. var n = get_int () var x = 0 while (x < n) { print (get_str ()) ++x } INPUT: 4 a b c abc
  5. var i = 0v
    var n = 5
    var sum = 0
    while (i < n) {
    ++i
    sum += i
    }
    print (sum)[/code]
  6. var i = 0 var n = get_int () var s = "" var t while (i < n) { t = get_str () print (t) s += t ++i } print (s) INPUT: 3 a b c
  7. var x = 0 var y = get_int () while (x < 10 && y < 10) { print (x) ++x ++y } INPUT: 2
  8. def f (n) { var i = 0 print ("===") while (i < n) { print (i) ++i } } f (3) f (5)
  9. def f (n) { var x = n x += n / 2 print (x) } var i = 0 while (i < 5) { f (i) ++i }
  10. def f (arg) { var x = 0 var y = arg while (x < 5 && y < 10) { print (x) ++x ++y } print ("y =", y) } f (get_int ()) f (get_int ()) f (get_int ()) INPUT: 7 -2 12
  11. var x = 1 while (x != 0) { print (x) if (x < 10) { print ("negative") } print ("==========") x = get_int() } INPUT: 8 -5 -1 2 0
2.07 solve ("for", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  1. for (var i = 0; i < 5; ++i) { print (i) }
  2. def f (start, end) { print ("=====") for (var i = start; i < end; ++i) { print (i) } } for (var j = 0; j < 3; ++j) { f (get_int (), get_int ()) } INPUT: 0 3 5 8 100 1005
  3. def f (m) { print ("---") for (var i = 10; i < 25; ++i) { if (i % m == 0) { print (i) } } } var n = get_int () for (var j = 0; j < n; ++j) { f (get_int ()) } INPUT: 3 2 7 5
  4. for (var i = 0; i < 5; ++i) { var j = i + 1 var n = get_int () while (j < n) { print (j) j *= 2 } } INPUT: 5 17 7 5 81
  5. global j = 0 def f (d) { for (var i = 0; i < 4; ++i) { print (j) j += d } } var n = get_int () while (j < n) { f (get_int()) } print (j + n) INPUT: 31 3 1 20
  6. def f (n) { var ret_val = 0 for (var i = 0; i <= n; ++i) { ret_val += 1 } return ret_val } var k = 0 var m = get_int () var x = 0 while (x < m) { print (f (get_int())) x = f (k) ++k } INPUT: 10 8 5 0 2 100
  7. def f (x) { if (x % 2 == 0) { print ("even") } else { print ("odd") } } f (1) f (2) for (var i = 0; i < 20; ++i) { if (i > 10 && i < 15 || i == 3) { f (i + get_int ()) } } INPUT: 0 0 0 1 0
  8. def f (a, b) { ++a print (a * b) } def g (m) { return get_int () * m } var n = get_int () for (var k = 1; k < n; ++k) { var x = 0 while (x < k) { f (x, g (k + 1)) } } INPUT: 4 2 1 3 12 6 4
  9. for (c : "hello") { print (c) } print ("===") var s = get_string () for (c : s) { print (c) } INPUT: world!
  10. var count = 0 var str = get_str () for (c : str) { if (c == 'a') { ++count } else { print (count) } } INPUT: 0aaa0aaaa00a0
2.07 solve ("for", [11, 12, 13, 14, 15, 16, 17])
level_number += 10
  1. var count = 0 for (c : s) { if (c == 'd') { ++count } } print ("length =", s.size ())) print (s[0]) print (s[1]) print count} f ("abc") f ("Dddd") for (var i = 0; i < 3; ++i) { f (get_str ()) } INPUT: red 01234567dd ddddd
  2. def f (n) { print (n> 3 ? "> 3" : "< 4") } print (false ? "true" : "false") for (var i = 0, i < 6; ++i) { f (i) } print (true ? "true" : "false")
  3. def f (s) { for (c : s) { print (c == 'a' ? 'b' : s[0]) } } for (var i = 0; i < 3; ++i) { f (get_str ()) } INPUT: ab ccaaacc ddad
  4. def f (s) { var t = "" for (c : s) { if (c != '.' && c != '!') { t += c } } print (s[0]) print (s.size ()) return t } var r = "" for (var i = 0; i < 5; ++i) { r += i % 2 == 0 ? f (get_str ()) : "." } print (r) INPUT: ... !??? 123.......
  5. def repeat (ch, n) { var ret_val = "" for (var k = 0; k < n; ++k) { ret_val += ch } return ret_val } def f (s, p) { var t = "" for (var i = 0; i < s.size (); ++i) { t += repeat (s[i], i == 2 || s[i] == s

    ? 2 : 1)[/spoiler]
    }
    print (t)
    }
    print (repeat ('#', 5)
    for (var j = 0; j < 4; ++j) {
    f (get_str(), get_int ())
    }
    INPUT:
    aab
    2
    cadcac
    0
    012333
    2
    ****...!!!!!?????
    4[/code]

  6. def f (scr, dest1, dest2) { for (var i = 0; i < scr.size (); ++i) { if (i % 2 == 0) { dest1 += scr[i] } else { dest2 += scr[i] } } } var s1 = "" var s2 = "" f("abcde12345", s1, s2) print (s1, s2) s1 = "Mov" s2 = "" f (get_str (), s1, s2) print (s1 + s2) INPUT: e__LCiondees
  7. def f (s) { var t = "" for (c : s) { var tmp = "" tmp += c t = tmp + t } return t } var x = f (get_str ()) var y = get_str() print (f (y + x)) for (ch : x) { if (ch == '*') { y += ch } } print (x + y) print (f (y) + "*") INPUT: .* ..
2.08 solve ("break")
  1. var x = 0 while (true) { ++x if (x > 5) { break } print (x) }
  2. def f (s) { var t = "" for (c : s) { if (c == '.') { return t } t += c } } while (true) { var next = get_str () if (next.size () < 10) { break } print (f (next)) print (next[next.size () - 1]) } INPUT: red......r green....g blue.....b RGB......? .
  3. def f (a, b, c) { if (!(a < b && b < c)) { print (min (min (a, b), c)) print (max (max (a, b), c)) return false } return true } while (true) { var x = get_int () var y = get_int () var z = get_int () print (x, z) if (f (x, y, z)) { break } } INPUT: 9 5 0 9 10 0 9 -5 0 0 0 9 1 2 3
  4. def f (s, ch) { var ret_val =0 for (c : s) { if (c == ch) { ++ret_val } } return ret_val } var flag = true var t = "abc" while (flag) { var n = 0 var str = get_str () for (l : t) { var rv = f (str, l) n += rv if (n >= 10) { flag = false break } print (rv) } print (">>>", n + str.size ()) } INPUT: aabbbcccc ac01234567890123 aaaaabbbbbc
  5. global a def f (n) { for (var i = 1; i < 10; ++i) { a += i if (i == n) { print (a) break } } } var m = get_int () for (var j = 0; j < m, ++j) { f (min (get_int (), 9)) } INPUT: 6 2 1 1 0 0 4
2.09 solve ("recursion")
  1. def f (n) { print (n) if (n > 0 ) { print ("before", n-1) f (n-1) print ("after", n-1) } } f (5) f (3)
  2. def f (n) { print (n) var ret_val = n > 0 ? f (n-1) + n : 0 print (ret_val) return ret_val } def g (n) { print ("f (" + to_str (n) + ") = " + to_str (f(n))) } g (2) g (4)
  3. def f (n) { var ret_val if (n > 1) { ret_val = f (n-1) * n } else { ret_val = 1 } return ret_val } for (var i = 1; i < 6; ++i) { print (f (i)) } print (f (min (get_int (), 12))) INPUT: 10
  4. def (n) { if (n > 1 { return f (n-1) + f (n-2) } else { return n } } for (var 1 = 0; i < 10; ++i) { print (f (i)) }
  5. def check (a, b) { return a % b == || b % a == 0 ? 0 : a + b } def get_n () { return max (1, get_int()) } def f (a, b) { if (b == 0) { return a } return f (b, a % b) } def g (x, y) { print (check (x, y), f (x, y)) } g (8, 6) g (6, 3) g (9, 5) g (8, 7) for var i = 0; i < 5; ++i) { print ("===") g (get_n (), get_n ()) } INPUT: 50 50 21 14 7 3 14 4 350 250
  6. def f (s) { if (s.size () > 0) { var t = "" for (var i = 1; i < s.size (); ++i) { t += s[i] } var r = f (t) r += s[0] return r } return "" } print (f ("abc")) print (f (get_str())) INPUT: 54321
2.10 solve ("vectors", [1, 2, 3, 4, 5, 6, 7])
  1. var v = [0, 3, "elem", get_int ()] for (var i = 0; i < v.size (); ++i) { print ("v[" + to_str(i) + "] =", v[i]) } for (x : v) { print (x) print (v[1]) } print (v[0]) INPUT: 10
  2. def print_vector (v) { if (v.size () > 0) { var s = to_str(v[0]) for (var i = 1; i < v.size (); ++i) { s += ", " + to_str (v[i]) } print (s) } else { print ("empty") } } var vec = [] print_vector (vec) vec.push_back (get_int () * 4) print_vector (vec) vec.push_back (get_int () * 7) vec.push_back (12) print_vector (vec) for (var j = 3; j < 6; ++j) { vec.push_back (j) print_vector (vec) } INPUT: 1 3
  3. def f (a) { var ret_value = 0 for (x : a) { ret_value += x if (x == 2) { print (x) } } return ret_value } var n = get_int () var v = [] for (var i = 0; i < n; ++i) { v.push_back (get_int ()) } print (f (v)) print (n + v[0]) INPUT: 3 4 2 2
  4. def get_vector (n) { var v = [] for (var i = 0; i < n; ++i) { v.push_back (get_int ()) } return v } def f (a, b) { var c = [] for (var i = 0; o < a.size (); ++i) { if (a[i] == b[i]) { print ("index:", i) } else { c.push_back (a[i] + b[i]) } } return c } var v = f ([1, 2, 3], [3, 2, 1]) print (v[0] + v[1]) print ("=====") var n = get_int () var a = get_vector (n) var b = get_vector (n) var result = f (a, b) for (x : result) { print (x) } INPUT: 7 1 0 2 3 0 4 5 -1 0 -1 -1 0 -1 -1
  5. def print_vector (a) { var s = "" for (var i = 0; i < a.size (); ++i) { s += to_str (a[i]) if (i < a.size () - 1) { s+= " " } } print (s) } def f (a, p) { for (var i = 0; i < a.size (); ++i) { if ((i+1) % max (p, 1) == 0) { a[i] *= 2 } } } var v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] f (v, 3) print_vector (v) f (v, get_int ()) print_vector (v) var u = [] for (var i = 0; i < 8; ++i) { u.push_back (get_int ()) f (u, get_int ()) print_vector (u) } INPUT: 2 2 99 0 1 1 99 3 3 9 99 4 1 7 2 1 4
  6. def get_vector () { var n = get_int () var ret_val = [] while (n > 0) { ++n ret_val.push_back (get_int ()) } return ret_val } def count (v, e) { var ret_val = 0 for (x : v) { if (x == e && x != 0) { ++ret_val } } return ret_val } var a = get_vector () var b = get_vector () var sum = 0 for (x : b) { print (count (a, b)) sum += x } print (sum) INPUT: 7 1 1 1 1 2 2 -3 3 1 2 -3
  7. def f (v) { var m = 0 var x = v[0] for (var i = 1; i < v.size (); ++i) { if (v[i] > x) { x = v[i] m = i } } return m } var a = [1, 5, 2, 3, 0] var b = [] for (var i = 0; i < 5; ++i) { b.push_back (get_int()) } var x = f (a) var y = f (b) print (x) print (a[x] * a[y]) print (a[x] * b[y]) print (a[y] * b[x]) INPUT: -1 -3 -1 0 -1
2.10 solve ("vectors", [8, 9, 10])
level_number += 7
  1. def print_vector (a) { var s = "" for (var i = 0; i < a.size (); ++i) { s += to_str (a[i]) if (i < a.size () - 1) { s += " " } } print (s) } def f (a, x, pos) { var ret_val = [] for (var i = 0; i < pos; ++i) { ret_val.push_back (a[i]) } ret_val.push_back (x) for (var i = pos; i < a.size (); ++i) { ret_val.push_back (a[i]) } return ret_val } var v = [1, 4, 5] print_vector (v) print_vector (f (f (v, 2, 1), 3, 2)) var n = get_int () while (n > 0) { --n v = f (v, n, get_int ()) print_vector (v) } INPUT: 4 1 1 5 0
  2. def get_vector () { var n = get_int () var ret_val = [] while (n > 0) { --n ret_val.push_back (get_int()) } return ret_val } def f (v) { var ret_val = 0 for (x : v) { if (x == 0) { return ret_val } else { ret_val += x } } return -1 } var a = [3, 2, 1, 0, 100] print (f (a)) print (f ([1, 3, 9])) for (var i = 0; i < 3; ++i) { var b = get_vector () print (f (b), b.size () + b[0]) } INPUT: 4 -1 2 2 0 1 -2 3 97 903 0
  3. def get_vector () { var n = get_int () var ret_val = [] while (n > 0) { --n ret_val.push_back (get_int ()) } return ret_val } def print_vector (v) { var s = "" for (var i = 0; i < v.size (); ++i) { s += to_str (v[i]) if (i < v.size () -1) { s += " " } } print (s) } def f (a) { for (var i = a.size () - 1; i > 0; --i) { for (var j = 0; j < i; ++j) { if (a[j] > a[j+1]) { var tmp = a[j] a[j] = a[j+1] a[j+1] = tmp } } } } var v = [3, 2, 1] f (v) print_vector (v) for (var i = 0; i < 3; ++i) { v = get_vector () print (v[0] + v[i]) f (v) print_vector (v) } INPUT: 4 2 3 1 4 6 8 15 0 0 8 10 5 2 8 1 4 16
  4. def f (v) { var n = v.size () for (var i = 0; i < n; ++i) { ++v[0] ++v[i] } } def g (v) { v.push_back (v[0] + v[v.size() - 1]) } def t (v) { var ret_value = 0 for (x : v) { ret_value += x } return ret_value } var v = [0, 0, 0] f (v) f (v) g (v) g (v) print (t (v))
  5. def get_vector () { var n = get_int() var v = [] while (n > 0) { --n v.push_back (get_int()) } return v } def func (a) { var i = 0 var n = a.size () while (i < n - 1 && a[i] < a[i+1]) { a[i] = a[i+1] - a[i] print (i * a[i]) ++i } } var v = get_vector () var u = get_vector () func (v) func (u) for (x : u) { v.push_back (x) } func (v) print ("size:", v.size()) print ("last element:", v.back ()) INPUT: 4 -2 -1 1 4 4 -10 -5 1 8
2.11 solve ("matrices")
  1. var v = [3, 4, 5] var m = [[1, 2], v] for (u : m) { print ("===") for (x : u) { print (x * x) } }
  2. def get_vector (n) { var ret_value = [] for (var i = 0; i < n; ++i) { ret_value.push_back (get_int ()) } return ret_value } def f (v, p) { for (x : v) { if (x < p) { x *= 2 } } } def t (a) { var s = 0 for (y : a) { s += y } return s } var vec = [get_vector (3), get_vector (get_int())] for (var k = 0; k < 2; ++k) { print (k, t (vec[k])) } f (vec[0], vec[1].size ()) f (vec[1], vec[0][2]) for (var k = 0; k < 2; ++k) { print (k, t (vec[k])) } INPUT: 2 3 5 4 -60 70 0 0
  3. def print_matrix (m){ for (var i = 0; i < m.size (); ++i) { var s = "" for (var j = 0; j < m[i].size (); ++j) { s += to_str (m[i][j]) if (j < m[i].size () - 1) { s += " " } } print (s) } print ("=====") } def f (m, x, y) { while (true) { if (x >= m.size () || m[x][y] != 0) { break } m[x][y] = 2 ++x } } var matrix = [] for (var i = 0; i < 4; ++i) { matrix.push_back ([]) for (var j = 0; j < 4; ++j) { matrix[i].push_back (get_int() == 0 ? 0 : 1) } } print matrix def get_coord () { return min (3, max (0, get_int())) } for (var k = 0; k < 3; ++k) { var x = get_coord () var y = get_coord () f (matrix, x, y) print_matrix (matrix) } INPUT: 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 3 3 1 0
  4. def print_matrix (m) { print ("=====") for (var i = 0; i < m.size (); ++i) { var s = "" for (var j = 0; j < m[i].size (); ++j) { s += to_str (m[i][j]) if (j < m[i].size () - 1) { s += " " } } print (s) } } def get_matrix (n) { var ret_val = [] for (var i = 0; i < n; ++i) { ret_val.push_back ([]) for (var j = 0; j < n; ++j) { ret_val[i].pushback (get_int () == 0 ? 0 : 1) } } return ret_val } global directions = [[0, 1], [0, -1], [1, 0], [-1, 0]] def good_coord (matrix, coord) { return coord >= 0 && coord < matrix.size () } def f (m, x, y) { var p = 0 var v = [[[x, y]], []] m[x][y] = 2 while (v

    .size () > 0) {[/spoiler]
    var np = p == 0 ? 1 : 0
    for (point : v

    ) {[/spoiler]
    for (dir : directions) {
    var cx = point[0] + dir[0]
    var cy = point[1] + dir[1]
    if (good_coord (m, cx) && good_coord (m, cy) && m[cx][cy] == 0) {
    m[cx][cy] = 2
    v[np].push_back([cx, cy])
    }
    }
    }
    v

    = [][/spoiler]
    p = p == 0 ? 1 : 0
    }
    }
    var m = get_matrix (5)
    print_matrix (m)
    f (m, 1, 1)
    print_matrix (m)
    print ("..........")
    for (var i = 3; i < 6; ++i) {
    m = get_matrix (i)
    for (var j = 0; j < i-2; ++i) {
    var c2 = get_int ()
    var c1 = get_int ()
    f (m, c2. c1)
    print_matrix (m)
    }
    }
    INPUT:
    0 0 1 0 0
    1 0 0 1 0
    1 1 0 1 0
    0 0 1 0 0
    0 0 0 0 0
    0 1 0
    1 1 0
    0 0 0
    0 2
    0 0 1 0
    1 1 0 0
    0 0 1 0
    0 1 0 1
    2 0
    0 3
    0 1 0 0 1
    1 0 0 1 0
    0 0 1 0 0
    0 1 0 0 1
    1 0 0 1 0
    4 4
    4 4
    0 4[/code][/olist]

2.12 solve ("extra")
  1. var x = get_int () var y = get_int () while (x < y) { print (y % 4) y += 10 + x % 3 x *= 2 } INPUT: 2 3
  2. var a = [0, 0] var i = 0 ++a[i+1] ++a[i+1] while (a[i] < a[i+1]) { ++a[i] ++a[i] ++a[i+1] } a.push_back (a[i] + 1) i++ for (x : a) { print (x) }
  3. global count = 0 def f (x) { if (x > 1) { g (x - 2) } if (x > 0) { g (x / 2) } } def g (y) { ++count f (y - 1) } f (10) print (count)
  4. global count = [0, 0, 0, 0] ++count[1] def f (a, b) { ++count[2] if (a > b) { ++count[0] return f (a - 3, b) } else { ++count[3] return a * b } } for (var i = 0; i < 3; ++i) { var x = get_int () var y = get_int () if (y < 0) { print ("negative") } print (f (y, x)) print (f (x, y)) print ("=====") } for (c : count) { print (c) } INPUT: 9 4 8 32 0 -1
  5. global x = 0 global y = 0 def f () { ++x y += x } def g () { print (y) ++x f () } f () ++x g () g () print (x, y)
  6. def get_integer () { return max (get_int (), 1) } var dot = true var s = "" while (true) { var m = get_integer () if (m == 100) { break } var c = dot ? '.' : '!' for (var i = 0; i < m; ++i) { s += c if (s.size () == 13) { print (s) s = "" } } dot = !dot } INPUT: 2 3 3 3 3 5 1 5 1 26 1 11 3 9 5 7 7 5 9 3 11 1 6 100
  7. var a = get_int () var b = get_int () var c = get_int () print (2*a + 4*b - c) print (-3*a - 2*b + 7*c) print (5*a - 3*b + 2*c) INPUT: 3 -11 8
  8. def h (x) { var a = x var ret_val = 0 while (a != 0) { ret_val += a % 2 a /= 2 } return ret_val } def f (n) { if (n % 2 == 1) { print (h (n - 1)) } else { print (h (n)) f (n / 3 + 1) } } print (h (7), h (64)) print (h (5), h (31)) print ("=======") f (16) print ("=======") for (var i = 0; i < 5; ++i) { var x = get_int () f (x) print ("===") } INPUT: 80 0 82 10936 7534
2.13 solve ("impossible", [1, 2, 3])
  1. def f (s, p, l) { var i = 0 var ret_val = "" for (c : s) { if (i < p || i >= p+l) { ret_val += c } ++i } return ret_val } def g (s, t, start) { var end = int (s.size ()) - int (t.size()) for (var i = start; i <= end; ++i) { var flag = true for (var j = 0; j < t.size(); ++j) { if (s[i+j] != t[j] { flag = false break } } if (flag) { return g (f (s, i, t.size ()), t, i) } } return s } def h (s, t) { print ("." + g (s, t, 0) + ".") } var s = "abracadabra" h (s, "a") h (s, "abra") h (s + s, "raab") h ("aaaa", "a") s = "01" var results = [] var t = [] for (var i = 0; i < 3; ++i) { t.push_back (get_str ()) results.push_back (g (t[i], s, 0)) } print (t[0] != t[1] && t[0] != t[2] && t[1] != t[2]) print (t[0].size () == 5) print (t[1].size () == 5) print (t[2].size () == 5) print (results[0] == "1") print (results[1] == "1") print (results[2] == "1") s = "#*#**#*##*###" for (var i = 0; i < 3; ++i) { h (s, get_str ()) } INPUT: 10101 01101 01011 #*#**#*# #*# *#*##*###
  2. def gcd (a, b) { if (a <= 0 || b < 0) { return -1 } if (b == 0) { return a } return gcd (b, a % b) } var tests = [[8, 12], [9, 12], [7, 7], [7, 8], [-1, 9], [0, 0]] var results = [4, 3, 7, 1, -1, -1] for (var i = 0; i < 6; ++i) { print(gcd (test[i][0], tests[i][1] == results[i]) print(gcd (test[i][1], tests[i][0] == results[i]) } print ("=======") def f (n) { var d = [] for (var i = 2; i < n; ++i) { if (n % i == 0) { d.push_back (i) for (x : d) { if (gcd (x + i, n) == 1) { return [x, i] } } } } return [0, 0] } for (a : [4, 6, 9, 10, 15, 24, 210]) { print (a) var rv = f (a) print (rv[0], rv[1]) } print ("=======") def check (goal) { var arg = get_int () var rv = f (arg) if (rv == goal && rv[0] * rv[1] != arg) { return 1 } return 0 } var count = 0 count += check ([2, 5]) count += check ([5, 7]) count += check ([13, 31]) print (count) INPUT: 20 175 5239
  3. def f (s) { var ret_val = "" for (c : s) { if (c == '<') { if (ret_val.size () > 0) { ret_val.erase_last () } } else { ret_val += c } } return ret_val } print (f ("abc")) print (f ("123erase<<<<<me<<")) print (f ("<a<a<hi")) print (f ("01234<56<<<<789")) def includes (str, ch) { for (c : str) { if (c == ch) { return true } } return false } print ("abc".includes ('a') && "abc".includes ('b') && "abc".includes ('c')) print ("aabbcc".includes ('d')) print ("!?!?!?".includes ('a')) def get_string (allowed_characters) { var s = get_str () var rv = "" for (c : s) { if (allowed_characters.includes (c)) { rv += c } } return rv } def test (goal, length, n, allowed_characters) { var strings = [] for (var i = 0; i < n; ++i) { var new_str = get_string (allowed_characters + "<") if (f (new_str) != goal || new_str.size () != length) { return false } for (s : strings) { if (s == new_str) { return false } } strings.push_back (new_str) } return true } print (test ("a", 3, 3, "a")) print (test ("a", 3, 5, "ab")) print (test ("", 4, 6, "*")) print (test ("10", 5, 9, "10")) print (test (".", 2, 2, ".")) INPUT: <<a a<a aa< <<a a<a aa< b<a ab< <<<< *<<< <*<< <<*< **<< *<*< <<<10 0<<10 1<<10 <0<10 <1<10 <11<0 <10<0 <100< <101< .
2.13 solve ("impossible", [4, 5, 6])
level_number += 3
  1. def f (arg_a, arg_b, arg_n) { var a = arg_a var b = arg_b var n = arg_n while (n > 0) { --n b += a a = b - a } return b } for (var i = 0; i < 10; ++i) { print (f (0, 1, i)) } print (f (10, 15, 20)) print ("-=-=-=-=-") def get_i (min_val, max_val) { return max (min_val, min (max_val, get_int ())) } for (var i = 0; i < 4 ; ++i) { print (f (get_i (0, 35), get_i (0, 30), get_i (1, 20))) } INPUT: 12 22 8 19 22 10 4 27 13 32 28 17
  2. def print_matrix (m) { for (var i = 0; i < m.size (); ++i) { var s = "" for(var j = 0; j < m[i].size (); ++j) { s += to_str (m[i][j]) + (j < m[i].size () - 1 ? " " : "") } print (s) } print ("=====") } def get_matrix (n) { var ret_val = [] for (var i = 0; i < n; ++i) { ret_val.push_back ([]) for (var j = 0; j < n; ++j) { ret_val[i].push_back (get_int () == 0 ? 0 : 1) } } return ret_val } var m = get_matrix (3) print_matrix (m) global directions = [[0, 1], [-1, -1], [1, 0]] def good_coord (matrix, coord) { return coord >=0 && coord < matrix.size () } def generate (n) { var ret_val = [] for (var i = 0; i < n; ++i) { ret_val.push_back ([]) for (var j = 0; j < n; ++j) { ret_val[i].push_back (0) } } return ret_val } def f (a, k) { var n = k while (n > 0) { --n var copy = generate (a.size ()) for (var i = 0; i < a.size (); ++i) { for (var j = 0; j < a.size (); ++j) { for (dir : directions) { var x = i + dir[0] var y = j + dir[1] if (good_coord (a, x) && good_coord (a, y)) { copy[x][y] = (copy[x][y] + a[i][j]) % 11 } } } } a = copy } } for (var i = 1; i <= 3; ++i) { f (m, i) print_matrix (m) } m = get_matrix (4) f (m, min (10, get_int ())) print_matrix (m) INPUT: 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 1 8
  3. def f (a, m) { var result = [] for (var i = a.size () - m; i < a.size (); ++i) { result.push_back (a[i]) } for (var i = m; i < a.size () - m; ++i) { result.push_back (a[i]) } for var (i = 0; i < m; ++i) { result.push_back (a[i]) } return result } def print_vector (v) { var s = "" for (var i = 0; i < v.size (); ++i) { s += (i > 0 ? " ": "") + to_str (v[i]) } print (s) } def g (n) { var rv = [] for (var i = 0; i < n; ++i) { rv.push_back (i) } return rv } for (t : [[4, 1], [9, 3], [10, 5]]) { print_vector (f (g (t[0]), t[1])) } print ("-----") for (var i = 0; i < 5; ++i) { var v = g (get_int ()) while (true) { var m = get_int () if (m <= 0) { break } v = f (v, min (v.size () / 2, max (1, m))) } print_vector (v) } INPUT: 4 2 1 2 1 0 7 3 1 2 0 8 2 4 2 1 0 14 7 1 7 4 6 4 5 3 4 1 2 1 0 9 4 1 4 3 1 3 2 1 2 1 0
2.13 solve ("impossible", [7, 8])
level_number += 6
  1. def print_matrix (m) { for (var i = 0; i < m.size (); ++i) { var s = "" for(var j = 0; j < m[i].size (); ++j) { s += to_str (m[i][j]) if (j < m[i].size () - 1) { s+= " " } } print (s) } print ("=====") } def generate (n) { var ret_val = [] for (var i = 0; i < n; ++i) { ret_val.push_back ([]) for (var j = 0; j < n; ++j) { ret_val[i].push_back (0) } } return ret_val } print_matrix (generate (3)) global directions = [[0, 1], [0, -1], [1, 0], [-1, 0]] def good_coord (matrix, coord) { return coord >= 0 && coord < matrix.size () } def f ( m, p, x, y) { var cp = min (p, m.size () - 1) m[x][y] += cp for (dir : directions) { for (var i = 1; i <= cp; ++i) { var cx = x + dir[0] * i var cy = y + dir[1] * i if (good_coord (m, cx) && good_coord (m, cy)) { ++m[cx][cy] } } } } var m = generate (4) f (m, 3, 3, 1) f (m, 2, 0, 0) print_matrix (m) def get_coord (n) { return max (0, min (n-1, get_int ())) } for (var i = 0; i < 5; ++i) { var n = max (3, min (10, get_int ())) m = generate (n) while (true) { var p = max (1, get_int ()) if (p > 20) { break } var x = get_coord (n) var y = get_coord (n) f (m, p, x, y) } print_matrix (m) } INPUT: 5 1 2 2 2 3 1 2 1 3 99 4 2 3 2 1 2 0 1 0 1 1 0 1 2 0 3 99 5 1 0 1 1 1 4 1 4 3 1 3 0 1 2 2 2 1 0 2 0 3 2 3 4 2 4 1 3 2 2 99 5 3 0 1 1 0 3 2 1 2 1 1 2 1 1 3 2 2 1 2 2 4 2 3 1 4 4 4 99 6 1 0 0 1 0 0 2 0 2 1 0 5 1 1 1 2 1 4 1 2 2 1 2 3 1 2 5 2 3 1 3 4 4 2 5 0 2 5 5 99
  2. print (is_digit ('5')) print (is_digit ('.')) print (eval ("123 - 23")) print (eval ("(1 - 3) * 9")) def calc (s) { var stack = [] for (c : s) { if (is_digit (c)) { stack.push_back (to_int (c)) } else { var arg2 = stack.back () stack.erase_last () var arg1 = stack.back () stack.erase_last () var expression = to_str (arg1) expression += c expression += "(" + to_str (arg2) + ")" stack.push_back (eval (expression)) } } return stack.back () } print (calc ("11+)) print (calc ("99*")) print (calc ("11+11+*")) print (calc ("78*5/95+-")) def is_op (c) { return c == '+' || c == '-' || c == '*' || c == '/' } def replace_dots (s, t) { var p = 0 var ret_val = "" for (c : s) { if (c == '.') { ret_val += p < t.size () && is_op (t

    ) ? t

    : +[/spoiler]
    ++p
    }
    else {
    ret_val += c
    }
    }
    return ret_val
    }
    print (replace_dots ("..", ""))
    print (replace_dots ("1.2", "*"))
    print (replace_dots ("..", "/-"))
    print (replace_dots ("h.e.l.l.o", "w-o-r-l-d"))
    var tests = ["27.", "12.34..", "572.3..498..."]
    tests.push_back ("31.2.34..567.8943......")
    for (t : tests) {
    print (calc (replace_dots (t, get_str ())))
    }
    INPUT:
    *
    *++
    --+*++
    +**+/**-+**[/code][/olist]

2.13 solve ("impossible", [9, 10])
level_number += 8
  1. def print_matrix (m) { for (var i = 0; i < m.size (); ++i) { var s = "" for(var j = 0; j < m[i].size (); ++j) { s += to_str (m[i][j]) + (j < m[i].size () - 1 ? " " : "") } print (s) } print ("=====") } def generate (n) { var ret_val = [] for (var i = 0; i < n; ++i) { ret_val.push_back ([]) for (var j = 0; j < n; ++j) { ret_val[i].push_back (0) } } return ret_val } print_matrix (generate (3)) def f (m, x, y, dx, dy) { if (dx == 0 && dy == 0) { return } var n = m.size () var px = x var py = y while (n > 0) { --n ++m[px][py] px = (px + dx) % m.size () py = (py + dy) % m.size () } } var a = generate (3) f (a, 0, 0, 1, 1) print_matrix (a) f (a, 0, 1, 1, 0) print_matrix (a) f (a, 2, 1, 0, 2) print_matrix (a) print ("...") def get_coord (n) { return max (0, min (n-1, get_int())) } for (var n = 3; n <= 6; ++n) { a = generate (n) while (true) { var x = get_coord (n) var y = get_coord (n) var dx = get_coord (n) var dy = get_coord (n) if (dx == 0 && dy == 0) { break } f (a, x, y, dx, dy) } print_matrix (a) } INPUT: 0 0 1 0 0 0 0 1 0 0 0 1 0 1 1 0 0 1 1 0 0 1 1 2 0 2 1 2 0 2 1 2 0 0 0 0 0 0 2 1 0 0 2 1 0 0 2 1 0 1 1 1 0 1 1 2 0 1 1 3 0 2 1 2 0 2 1 3 0 3 1 0 0 3 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 0 2 1 2 0 2 1 2 0 3 1 0 0 3 1 1 0 3 1 1 0 3 1 3 3 0 0 1 0 0 0 0 0 0 2 3 0 0 3 2 0 1 1 2 0 1 2 2 0 2 3 0 0 3 1 5 0 4 1 4 0 5 1 3 1 0 2 2 1 4 2 4 0 0 0 0
  2. global a = 2 global b = 5 def f () { a += b / 3 a += b % 14 ++a ++b b += a % 20 ++a ++b } def g () { f () f () b += 2 f () } g () g () g () print (a, b)
3 Comments
Innocentive  [author] 17 Apr, 2023 @ 12:08pm 
Thanks! It's a shame that I never got around to really finishing the guide because it is still missing some general tips. But I guess it has sufficient content :)
[Easy] shin 16 Apr, 2023 @ 11:20am 
Amazing guide! Complete novice here , this guide should be in the game !
Haoose 12 Sep, 2020 @ 5:00pm 
Great work!