Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

Electronic problem book on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2025

 

Examples | Python and R | Simple task

PrevNext


Solution of the simple task: Begin3

This section contains description of solving the following simple task in Python and R:

Begin3°. The sides a and b of a rectangle are given. Find the area S = ab and the perimeter P = 2(a + b) of the rectangle.

We shall use the IDLE Python as a programming environment, however the same results may be received in the VS Code, PyCharm, and Wing IDEs for Python and RStudio IDE for R that are also supported by Programming Taskbook.

Creating a template and acquaintance with the task

To create a template of the required task you should use the PT4Load tool (starting with version 4.22, you can use the PT4Panel tool for quick launch of all Programming Taskbook tools; the PT4Panel shortcut is located on the desktop and in any working directory).

The template created for IDLE Python consists of two files: the file pt4.py which contains the pt4 module and the file Begin3.pyw which should contain the task solution. The file Begin3.pyw will be loaded into the IDLE Python (and to the editor of VS Code, PyCharm, or Wing IDEs when using them to solve a task on Python):

The project template created for R language also consists of two files: the PT4.R file contains auxiliary taskbook functions for R language, and the Begin3.R file contains the Solve function, where you need to input the solution of the task. The Begin3.R file will be automatically loaded into the RStudio IDE. Here are the contents of the Begin3.R file:

[R]

source("PT4.R")
Solve <- function() {
  Task("Begin1")

}

Start(Solve)

Comparing the Python and R language program templates, it is easy to see many similarities.

The text of a Python program consists of only four non-empty lines. The first line imports all the functions defined in the pt4 module to the program. The second line is the header of the solve function in which the solution to the problem should be input, and the third line (which is the only statement of the solve function) calls the task function, which is defined in the pt4 module and initializes the task with the specified name. In the final, fourth non-empty line, the start function from the pt4 module is called, which provides a call to the solve function to check the correctness of the proposed solution.

The operators in a program in the R language have a completely similar meaning. The only difference (which we will not mention further on) is that the names of the taskbook functions related to the R language start with a capital letter.

This program can already be launched for execution. To do this in the IDLE environment, just press the [F5] key (or execute the "Run | Run Module" command). Note that the same [F5] key can be used to run Python programs in Wing IDE and VS Code environments (in PyCharm environment the [Shift]+[F10] combination is used to run programs).

In RStudio IDE, it is enough to press the [Ctrl]+[Shift]+[S] key combination to launch a program for execution.

When the program is launched in IDLE, you will see two new windows: the Python Shell window and the Programming Taskbook window with a task text and initial data. When using Wing IDE, VS Code and PyCharm for Python, and RStudio for R, only the taskbook window appears, since the Python Shell section (and the corresponding Console section for RStudio) is displayed at the bottom of the window of these environments.


This running is considered as acquaintance running because the program does not perform input-output operations. To close the Programming Taskbook window click the Exit button or press [Esc] or the [F5], [F6], or [F10] keys. Note that after closing the taskbook window, the Python Shell window will display a description of the results of this program run (similar information will be displayed in the Python Shell section of the windows of other Python environments and in the Console section of the RStudio environment).

For return to the IDLE code editor, you should click on its window or press the [Alt]+[Tab] key combination.

Initial data input

Before solving tasks you should input initial data in the program. In the Begin3 task, the initial data are real numbers a and b that refer to sides of the rectangle.

There are two functions that provide input of the real numbers: the get function and the get_float function. The get_float function also checks type of the current data item and output error message if the type of data item differs from the float. Using the "universal" get function does not allow the taskbook to recognize errors related to the mismatch of the input data type, but the universal function is easier to use. There are no specialized input functions for the R language, and data of any type must be input using the universal Get function. Therefore, for a uniform description of programs in Python and R, we will use the universal input function (hereinafter, we will show only the text of the solve function, since the lines preceding and following this function do not need to be changed):

[Python]

def solve():
    task("Begin3")
    a = get()
    b = get()

[R]

Solve <- function() {
    Task("Begin3")
    a <- Get()
    b <- Get()
}

Note that in Python, it is important to use the same indentation for all operators in a given function. R traditionally uses <- ("left arrow") for assignment, although modern versions of the language allow the more traditional "=" symbol.

Run the program once again. You will see the new set of initial data. For each running of the program a new initial data are generated, so it is necessary to develop an algorithm that processes correctly all admissible sets of initial data.

Data input is performed correctly, but the program does not output results. In such situation we have the following message: "Correct data input: all required data are input, no data are output" (Programming Taskbook version 4.15 or higher) or "Some data are not output" (previous versions).

Calculation and output results

Let's perform the required calculations and output results using the put function:

[Python]

def solve():
    task("Begin3")
    a = get()
    b = get()
    p = 2 * (a + b)
    s = a * b
    put(P, S)

[R]

Solve <- function() {
    Task("Begin3")
    a <- Get()
    b <- Get()
    p <- 2 * (a + b)
    s <- a * b
    Put(P, S)
}

In this case, the running leads to the following information in the status bar: "Error NameError: name 'P' is not defined" (similar message will be displayed in case of R language). After closing the Programming Taskbook windows the more detailed error description will be output in the Python Shell window (for R language, a similar description will be displayed in the Console section):

To correct the program it is enough to change its last statement as follows:

[Python]

  put(p, s)

[R]

  Put(p, s)

When the program is running, you can see output values at the panel of results. Because we output data in inverse order, the status bar contains the error message "Wrong solution".


Right solution and its testing

To correct the last error it is enough to change order of two parameters of the put function:

[Python]

  put(s, p)

[R]

  Put(s, p)

When this program is running, you will see the testing panel on screen:

This panel appears when the program processes successfully at least one set of input data. The progress bar shows the amount of tests which are already performed, the text above the bar allows to determine how much tests should be performed successfully. The program testing finishes in two cases: when all required tests are performed successfully or when some test is failed.

In our case, the algorithm is correct, therefore the message "The task is solved!" will be shown.


To browse information about the task solving you can use PT4Results tool (you may run this tool by pressing the [F2] key or clicking the Results label in the upper-right corner of the Programming Taskbook window):

Begin3      y23/02 20:13 Acquaintance with the task.
Begin3      y04/02 15:07 Correct data input.
Begin3      y04/02 15:09 Error NameError.
Begin3      y04/02 15:12 Wrong solution.
Begin3      y04/02 15:15 The task is solved!

The letter "y" denotes the programming language being used (Python).

In the case of R language, the information will be similar, except that "R" is used as the language letter and instead of the text "Error NameError" the text "Error in Solve() : object 'P' not found" will be shown.

Remark 1. It is not necessary to use additional variables s and p, because you can output expressions.

[Python]

def solve():
    task("Begin3")
    a = get()
    b = get()
    put(a * b, 2 * (a + b))

[R]

Solve <- function() {
    Task("Begin3")
    a <- Get()
    b <- Get()
    Put(a * b, 2 * (a + b))
}

Remark 2. The text of the Python program can be shortened a bit more if you use the get2 function for input, which inputs two elements of the source data at once and returns them as a tuple (this function, along with other additional input functions, appeared in taskbook version 4.19):

[Python]

def solve():
    task("Begin3")
    a, b = get2()
    put(a * b, 2 * (a + b))

The R language does not have a multiple assignment operator, but you can use the GetV function, which allows you to input a vector of the desired size. In this case, you must use the indexing operation to access the elements of the vector (note that indices in R, unlike in Python, start at 1):

[R]

Solve <- function() {
    Task("Begin3")
    a <- GetV(2)
    Put(a[1] * a[2], 2 * (a[1] + a[2]))
}

PrevNext

 

  Рейтинг@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
01.01.2025