In the Fundamental Information Technology Engineer Examination (hereinafter referred to as the FE Examination), the programming questions changed from COBOL to Python. Python will be given from the fall exam of 2020 (the original plan was from the spring exam of 2020, but the spring exam was canceled due to the influence of Corona). One of the features of Python is that it is easy for beginners to learn. However, even if it is easy to learn as a language, it will be difficult for beginners to prepare an "environment to run and try Python" by themselves. Therefore, in this article, I will explain the preparation of the Python environment for those who "want to try Python for the FE exam but do not know how to make it work". In addition, let's actually move the Python sample problem published by IPA (FE test executing agency).
――First time in programming. ――I want to take the FE exam. ――I want to learn Python. But I don't know how to run a Python program. ――I can understand the keywords of the morning questions of the FE exam somehow, or I can understand them if I look them up.
--Google account (OK if you have a gmail account) --Environment where browsers such as Chrome can be used (Mac or Windows is OK)
In order to operate a programming language, an editor or compiler interpreter is often installed on a personal computer. Moreover, you have to overcome quite high hurdles such as installing the necessary libraries and getting used to the operation of the development environment (IDE). Thankfully, Google Sama provides an environment where you can run Python with just a web browser, and you can use it for free. Originally it seems to be an environment for practicing machine learning (although it is quite full-scale), but you can also move the sample questions of the FE exam. This environment is called Colaboratory. In this article, we will use Colaboratory to run the sample program.
Let's access it now. Go to https://colab.research.google.com/ in your browser (Chrome recommended). If you are not logged in to your Google account, please log in from the login icon in the upper right.
When you log in, the notebook list screen appears. A notebook is like a folder for organizing related files. If you are studying for the FE exam, it is a good idea to make one question correspond to one notebook. Click Create New Notebook to create a notebook for the sample questions.
The code input screen will appear immediately. Rename your notebook to make it easier to read later when you open it. Click the default name, Untitled0.ipynb, and change it to sample.ipynb.
After renaming the file, enter the sample program in the code entry area (the area surrounded by the line, called the cell).
Of course, manual input is the most studying. However, for those who want to have fun, I will post a sample program. Please copy and paste.
sample.py
#The copyright belongs to IPA.
import math
import matplotlib.pyplot as plt
def parse(s):
return [(x[0],int(x[1:])) for x in s.split(';')]
class Marker:
def __init__(self):
self.x, self.y, self.angle = 0, 0, 0
plt.xlim(-320, 320)
plt.ylim(-240, 240)
def forward(self, val):
rad = math.radians(self.angle)
dx = val * math.cos(rad)
dy = val * math.sin(rad)
x1, y1, x2, y2 = self.x, self.y, self.x + dx, self.y + dy
plt.plot([x1, x2], [y1, y2], color='black', linewidth=2)
self.x, self.y = x2, y2
def turn(self, val):
self.angle = (self.angle + val) % 360
def show(self):
plt.show()
def draw(s):
insts = parse(s)
marker = Marker()
stack = []
opno = 0
while opno < len(insts):
print(stack)
code, val = insts[opno]
if code == 'F':
marker.forward( val )
elif code == 'T':
marker.turn( val )
elif code == 'R':
stack.append({'opno': opno, 'rest': val })
elif code == 'E':
if stack[-1]['rest'] > 1 :
opno = stack[-1]['opno']
stack[-1]['rest'] -= 1
else:
stack.pop()
opno += 1
marker.show()
After entering the code, let's execute it. Click the run icon on the left. The first run will take some time.
... Nothing comes out. That should be the case, in this sample program, only the definitions of functions and classes are written, and there is no equivalent to the main routine. Now let's call this function or class. Click "+ Code" at the top of the page.
Then, another cell will appear below the cell where you entered the sample program.
In this cell, execute the draw function explained in the sample problem as "Interpret and execute each instruction in the instruction sequence given as an argument and display the drawing result". For the argument, use the option (R5; F100; T72; E0) that is the answer to the question sentence blank b, and enter "draw ('R5; F100; T72; E0')". After entering, press the execute icon on the left.
If you enter the sample code and the entered character string matches, the pentagon should be displayed as shown below.
When you're done, save your notebook. You can open the saved notebook next time. Click "Save" from "File" at the top of the screen. Once you have saved it, you can close your browser.
Thank you for your hard work.
Recommended Posts