Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries

Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python sample questions have been published at the following URL. https://www.jitec.ipa.go.jp/1_00topic/topic_20191028.html

I am using an external library called matplotlib. You cannot run this sample just by installing Python3. Therefore, I implemented it only with the turtle module without using an external library. I will introduce you.

"""
Read the command sequence from a file
File name is specified by command argument
"""
import sys
import turtle
import re
 
def parse(str): 
    return [(x[0], int(x[1:])) for x in str.split(';')] 

def draw_init():
    turtle.shape('turtle')
    turtle.home()
    turtle.clear()
 
def draw(str): 
    opelist = parse(str) 
    stack = [] 
    openo = 0 
    while openo < len(opelist): 
        code, num = opelist[openo] 
        if code == 'U': 
            turtle.penup() 
        elif code == 'D': 
            turtle.pendown() 
        elif code == 'F': 
            turtle.forward(num) 
        elif code == 'B': 
            turtle.backward(num) 
        elif code == 'T': 
            turtle.left(num) 
        elif code == 'H':
            turtle.home() 
        elif code == 'X':
            turtle.setx(num) 
        elif code == 'Y':
            turtle.sety(num) 
        elif code == 'R': 
            stack.append({'openo': openo, 'rest': num}) 
        elif code == 'E': 
            if stack[-1]['rest'] > 1: 
                openo = stack[-1]['openo']
                stack[-1]['rest'] -= 1 
            elif stack[-1]['rest'] <= 1: 
                stack.pop() #Remove the last element of the stack
        openo += 1 

#Extract the file name from the command argument
filename = sys.argv[1]
#Open the file and read the operation string
try:
    file = open(filename)
    opestr = file.read()
    opestr = re.sub('#.*\n', '', opestr)            #Peel off comments to the end of the line
    opestr = re.sub(' \t\n', '', opestr)            #Strip blank lines
    opestr = re.sub(r"([A-Z]{1})", r";\1", opestr)  #Insert a delimiter semicolon
    opestr = opestr.lstrip(";")                     #Remove the extra semicolon at the beginning
#draw
    draw_init()
    draw(opestr)
#Handle exceptions
except Exception as e:
    print(e)
#Close file
finally:
    file.close()

The following is an example of a file to operate.

#Draw a dotted star
R5
    R20
        D0
        F5
        U0
        F5
    E0
    T216
E0


Execution result
点線の星.PNG

Recommended Posts

Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries
Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation
[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)
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
Experience of taking the Applied Information Technology Engineer Examination
Using Python #external packages
Run LINE Bot implemented in Python (Flask) "without using Heroku"