Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation

Introduction

I explained the Python sample problem published by IPA (FE test executing agency). Any mistakes or confusing parts will be corrected and supplemented one by one, so please ask questions or comment. Of course, knowledge of Python is required because it is a problem of Python, but in addition, knowledge of junior high school level mathematics (figures) and high school level mathematics (trigonometric functions) is required.

Question 1

Blanks a1, a2

The position of the marker (blank a1) and the direction of travel (blank a2) after executing the instruction sequence α (R3; R4; F100; T90; E0; F100; E0) are asked. The command sequence α consists of a double repeating interval as shown in Fig. 2. Let the outer repeat interval indicated by instruction 0 (R3) be repeat 0 (green), and the repeat interval indicated by instruction 1 (R4) be repeat 1 (red). image.png When the instruction sequence α is executed, instruction 2 (F100) draws the line segment first. After that, command 3 (T90) rotates 90 ° counterclockwise. Since this is repeated 4 times with command 1, draw a square (red dotted line in the figure below). Then, command 5 (F100) overwrites the already drawn line segment and arrives at the lower right corner of the square. Until you reach the right side of this square, it is one repetition of command 0 (R3) (green dotted line in the figure below). When the second and third repetitions of instruction 0 are executed, the squares are incremented by one. image.png The position of the marker after the execution of the instruction sequence α is the position (2) in Fig. 3 because it is at the lower right of the rightmost square (option a1). The direction of the marker is to the right, that is, the positive direction of the x-axis (option a2). Therefore, the correct answer is the combination of option c (①, positive direction of x-axis).   Answer: Choice C (a1: ②, a2: positive direction of x-axis)

Blank b

The command sequence that draws the regular pentagon in Fig. 4 is being asked. Before considering the command sequence, let's review junior high school level mathematics. Recall the property that the sum of the external angles of a polygon (this time a regular pentagon) is 360 °. For a regular pentagon, the outer angle is 360 ° ÷ 5 = 72 °. image.png To draw the regular pentagon in Fig. 4, advance 100 and rotate 72 ° counterclockwise 5 times. image.png The command to advance 100 is F100, and the command to rotate 72 ° counterclockwise is T72. To repeat this 5 times, you can enclose it in R5 and E0, so the command sequence is R5; F100; T72; E0. Therefore, option F is the correct answer.

Question 2

Blank c


def parse(s):
    return [(x[0],Blank c) for x in s.split(';')]

Blank C is a fill-in-the-blank question that answers the return value of the function parse. This question requires two points: to understand the specifications of the function parse shown in the problem statement, and to implement the understood specifications with Python grammatical knowledge. It's a big challenge.

Understanding the function Parse from the problem statement

The function Parse is a function that parses a given character string (for example,'R4: F100: T90: E0') and converts it into a format that is easy for the program to interpret. Indicates the relevant part of the problem statement. image.png The "character string given as an argument" is'R4: F100: T90: E0'. There are four tuples, ('R', 4), ('F', 100), ('T', 90) and ('E', 0). One tuple (for example, ('R', 4)) is one instruction. R is the instruction code and 4 is the numeric parameter. Puts these tuples (4) in a list and returns them as a return value.

grammar

The Python grammatical knowledge required for this question is as follows. --Inclusive --Tuple --List

def parse(s):
  return [(x[0],Blank c) for x in s.split(';')]

Only one line of return processes "convert the instruction sequence given as an argument into a list whose elements are tuples". It's a complicated process, but let's understand it by breaking it down into individual elements. Suppose you are given'R4: F100: T90: E0'as the instruction string. This string is assigned to the variable s. First is split. split is a function that splits a string and returns it as a list. Using the split function on the string s returns a list like this: Since';' is specified as the delimiter, it is divided into R4, F100, T90, and E0 and displayed as a list. image.png Next is the for statement. This for statement is used as "inclusive". For the processing target x of the for statement, x [0] and the blank c are output as tuples. I don't know what you are saying, so I will explain in order using the result of split ['R5','F100','T72','E0'] as an example. First, there are simple instructions. image.png This is the same as returning the contents of the array as a list. The leftmost x simply outputs x as an element of the list, so the same list is displayed as a result. This is not very interesting. Then, how about setting x [0] according to the problem statement? image.png x [0] represents the first character of the variable x (character string). Therefore, the first character of each element is returned as a list. If you want to display the second character, x [1], so let's try this as well. image.png The second character is displayed. In the problem statement, the values after the second character are treated as "numerical parameters". Therefore, consider a method to extract the second and subsequent characters as numbers. Use x [1:] to retrieve the second and subsequent characters. The first 1 is the second character, and if nothing is specified after:, it means "everything after that". Let's try using comprehension to see what the result of x [1:] will be. image.png I was able to retrieve the numbers safely. However, at this point it is a character string. Use the int function to convert a string of numbers to a number. image.png I was able to retrieve the numbers safely. Now, let's change it slightly so that the first character (x [0]) and the numbers after the second character are returned as tuples. image.png Therefore, the blank c is filled with the option A "int (x [1:])".

Blank d1, d2, e

This is a fill-in-the-blank problem for the forward function. image.png

The problem statement describes the forward method of class Makert as follows.

forward(val) Advance the position coordinates of the marker by the length specified by val in the current traveling direction, and draw a line segment. Argument: val length

So, this question is about "how to draw a line segment". High school level math knowledge (basic trigonometric functions) is required. As a premise, self.x and self.y contain the current position coordinates of the marker, and self.angle contains the frequency display (90 °, 180 °, etc.) in the current direction of travel. The figure below shows the operation of the forward method. image.png The red arrow is the line segment you want to draw. Draw a line segment from (x1, y1) in the lower left to (x2, y2) in the upper right. Here is the explanation of the program.


rad = math.radians(self.angle)

In this line, the value stored in the variable angle (the direction of travel stored in the radian method) is converted to the radian method and stored in the variable rad. The degree method or radian method is a notation method for angles. In Python's math library (math. *), The arc degree method is used for angles, so here it is converted to the arc degree method (radian notation) by the math.radians method.

dx = val *Blank d1
dy = val *Blank d2

Find the length dx in the x-axis direction and the length dy in the y-axis direction of the line segment in the above figure. From the definition of trigonometric function, dx = val * cos (rad), dy = val * sin (rad). Therefore, blank d1 will contain `math.cos (rad)`, and blank d2 will contain `` math.sin (rad) `.

In the blank e, the value to be assigned to x1 and y1 is asked. As for what x1 and y1 are, the hint is that the line below them explains "draw a line segment" in a comment. The coordinates of the starting point of the line segment are x1 and y1. The current location of the marker is stored in the variables self.x and self.y. Therefore, self.x and self.y are the starting points of the line segment. Therefore, x1 contains self.x and y1 contains self.y. The self.x + dx and `` `self.y + dy``` that follow the blank e are also great hints. dx and dy were the length of the line segment to be drawn in the x-axis direction and the length in the y-axis direction. Therefore, the value obtained by subtracting dx from self.x + dx (that is, self.x) is the x coordinate of the start point of the line segment, and the value obtained by subtracting dy from self.y + dy (that is, self.y) is the line segment. It can also be read as the y coordinate of the starting point. Therefore, the blank e is filled with the option c "self.x, self.y".

Blank f

It is a fill-in-the-blank problem of the function draw. As for the function draw, there is an explanation in the problem statement, so read the program and the problem statement in association with each other.

(3) The function draw interprets and executes each instruction in the instruction sequence given as an argument, and displays the drawing result. (Omitted) The outline of the function draw is shown below. (1) Convert the instruction sequence into a list with tuples as elements using the function parse. (2) Use the class Maker to operate the marker. ③ Use the stack to handle the nesting of repeating intervals. (4) The stack is represented by a list, and each element is a dictionary with the repeat start position opno and the remaining number rest. ⑤ Use the print function at program position β to output the change of state of the stack.

The figure below shows the above explanation associated with the program. image.png

The point to pay attention to is (1) intsts. The result of using the instruction sequence "R2; R3; E0; E0" of execution result 2 as the argument of the parse function [('R', 2), ('R', 3), ('E', 0), (' The list of E', 0)] is assigned to insts. Since insts is a list, for example, the value of inst [0] is a tuple ('R', 2), and the value of inst [1] is a tuple ('R', 3). So, for example, if opno is 0 (initial value) and you execute ``` code, val = insts [opno]` ``, ‘R’ is assigned to code and 2 is assigned to val. That is, the instruction code is assigned to code and the numeric parameter is assigned to val. Note that opno is a variable that indicates "what instruction sequence is currently being executed". Well, it is blank f.


    if code == 'F':
        marker.forward(Blank f)
    elif code == 'T':
        marker.turn(Blank f)
    elif code == 'R':
        stack.append({'opno':opno, 'rest':Blank f})

When the instruction code (code variable) is'F',' T', or'R', the argument to be passed to each function of the Marker class is asked. In the previous ``` code, val = insts [opno]` ``, it is known that the pair with code is val, so you can understand it relatively intuitively. The blank f is filled with val. For example, when the instruction code is'F', a line segment with the length specified by the numerical parameter (val) is drawn as shown in Table 1. The explanation of the function forward also says "Advance by the length specified by val and draw a line segment", and you can see that val should be specified as the argument of forward. (The turn function corresponds to the instruction code'T') Therefore, in the blank f, option d (val) is the correct answer.

Blank g, h, i


    elif code == 'E':
        if stack[-1]['rest']Blank g:
Blank h
            stack[-1][['rest'] -= 1
        else:
Blank i

The processing is being asked when the instruction code is'E', that is, the end point of repetition. To explain this if statement roughly, --If the condition of the if statement is true (if it is not the end of the iteration), it returns to the beginning of the iteration. --In all other cases (else), end the iteration is what it means. Let's take a closer look. stack [-1] means the last element of the list stack. For example, if the contents of stack are [{'opno': 0,'rest': 2}, {'opno': 1,'rest': 1}]` `, stack [-1] will be The last element is {'opno': 1,'rest': 1}``. If the rest of the last element of the stack (innermost iteration) is greater than 1 (that is, greater than or equal to 2), then opno is given the value at the beginning of the iteration (in this case,'opno': 1) to make the iteration. , Programmatically substitute stack [-1] ['opno']). This operation returns the execution position of the instruction sequence to the beginning of the iteration. However, that alone will result in an infinite loop, so ``stack [-1] [['rest']-= 1``` will reduce the number of repetitions by 1. If rest is 1, no further repetition is required, so the repetition is terminated. To end the iteration, delete the last element of the list stack. Use pop () to remove the last element in the list. Applying these actions to the blanks, the blank g is the option o "> 1", the blank h is the option A "opno = stack [-1] ['opno'], and the blank i is the option c. "Stack.pop ()" of is entered respectively.

Supplementary explanation

Supplementary explanation of inclusion

Internal capsule is a grammar that takes out an element that meets condition C for an array B and returns the result of processing A for that element as a list.

[Processing for x A for x in array B if x condition C]

(Note that the whole is enclosed in []). This means that the result of process A is returned as a list for the elements of array B that satisfy condition C. For example, for an even number from 0 to 9, the squared value of that even number is returned as a list. image.png Of course, you can do the same by initializing the array, writing an if statement in the for loop, and appending to the array. However, that method requires about 4 lines. It's pretty powerful to be able to write this in just one line. By the way, the blank c was a simplified usage without the if part of the inclusion.

Frequency method and radian method

In mathematics up to junior high school, angles are expressed in radians (90 °, etc.), but in mathematics from high school, radians (π / 2, etc.) are used. This is because it is particularly compatible with calculus. The unit is ° for the frequency method and [rad](read as radian) for the arc degree method. The conversion between the radian method and the radian method is performed by the following equation.  1 [rad] = 180°/π  1° = π/180 [rad] It's complicated, so you only need to remember the typical values. For example, 45 ° = π / 4 [rad], 90 ° = π / 2 [rad], 180 ° = π [rad], 360 ° = 2π [rad], and so on.

Recommended Posts

Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation
Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
python Basic sorting algorithm summary (Basic Information Technology Engineer Examination)
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
[Examination Report] Python 3 Engineer Certified Data Analysis Exam
Experience of taking the Applied Information Technology Engineer Examination
python engineer certification exam