Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2022

 

Examples | Python | PT4 types and functions

PrevNext


Additional functions

Functions and the Node class described below will be available in a program if the PT4.py module is imported in the program. Any template program created for task solving on Python imports this module.

Task initialization, data input-output


task(name)

This function initializes a task named name (a string parameter). It must be called at the beginning of the program. If the source code does not contain the task function call then the program output the error message "The task function with a task name is not called".

A task name must contain a name of a task group and an order number (for example, "Begin3"). If the group name is invalid then the program output the error message "Invalid task group". If the task number is invalid then the program output the error message with information about the range of the available task numbers for this group. The symbol "?" placed after the task name (for example, "Begin3?") means that the program will be run in the demo mode.

The task function may be also used for creating and displaying an html-page containing a description of a task or a task group. In this case the name of the required task or the task group ended by the "#" symbol must be used for the name parameter, for example, "Begin3#" or "Begin#".

Starting from the version 4.13, the Programming Taskbook provides a program testing on several sets of input data during a single running of the program. You should place the sumbol "!" after the task name (for example, "Begin3!") in order to switch off this new feature.

All subsequent calls of the task function are ignored with the following exception: if several calls are used for creation of an html-page containing a description of various tasks or task groups then all these calls will be taken into account.

Starting from the version 4.12, the name parameter may include the "_en" or "_ru" suffix that determines the interface language (English or Russian respectively) for Programming Taskbook window and the task itself. The "?", "#", and "!" symbols should be placed before the suffix, for example, "Begin3#_en". If several calls of the task function are used (for creation of an html-page) then only the suffix of the first call will be taken into account. If the suffix is absent then the default language is applied (the default language may be changed by means of a pop-up menu of the PT4Load tool).


get() get_bool() get_int() get_float() get_str() get_Node()

These functions must be used to input initial data values in the program. They must be called after the task function, otherwise the program output the error message "The task function with a task name is not called at the beginning of the program".

Each function call returns the next item of input data. The get function allows to input data item of any type (bool, int, float, str, Node), whereas the other functions allow to input data item of definite type and must correspond to the type of data item, otherwise the program output the following message: "Invalid type is used for an input data item". For example, this message will be output if get_int function will be called for input a string.

In the case of input of more data than the task requires, the program output the error message "An attempt to input superfluous data". In the case of input of less data than the task requires, the program output the error message "Some required data are not input".


get2() get3() get4() get5() get_list(n = -1) get_matr(m = -1, n = -1)

These functions appeared in version 4.19. They make it easy to organize the input of multiple scalar data, as well as data sets to be stored in a list or in a matrix (list of lists).

The get2, get3, get4, get5 functions provide sequential calls to 2, 3, 4, and 5 get functions and return a tuple containing all the input data. For example, to enter three source data and assign them to variables a, b, c, it is sufficient to use the operator

a, b, c = get3()

The function get_list without parameters firstly reads the size n of the input list (using the get_int function), then reads the n subsequent elements of the original data (using the get function) and adds them to the list, which is returned as the result of this function.

If a non-negative integer parameter n is explicitly specified when calling the get_list function, it is considered the size of the list; in this case, the function only reads list elements. Negative values of the parameter n mean that it is necessary to read the size of the list before reading the elements (thus, in the case of a negative parameter, the function behaves the same as when it is called without parameters). If the parameter is not an integer, an exception is thrown.

The function get_matr without parameters firstly reads the sizes m and n of the input matrix (using two get_int functions), then reads m*n subsequent elements of the original data (using the get function) and adds them to the list, which is returned as the result of this function. It is assumed that value m specifies the number of rows and corresponds to the first index of the resulting matrix, and value n specifies the number of columns and corresponds to the second index of the matrix. It is also assumed that matrix elements are read by rows. The resulting matrix is a list of lists. Thus, the operator a = get_matr() is equivalent to the following sequence of operators:

m, n = get_int(), get_int()
a = [[get() for j in range(n)] for i in range(m)]

If two non-negative integer parameters m and n are explicitly specified when you call get_matr, they are treated as matrix dimensions; in this case, the function only reads matrix elements. If the second parameter (n) is not specified or is negative, and the first parameter (m) is specified and is non-negative, it is assumed that the matrix being input is a square matrix of order m; in this case, the function also inputs only matrix elements. Thus, to enter a square matrix (assuming that there is only one value before its elements, which is the matrix order), just execute the following operator:

a = get_matr(get())

put(a, ...)

The put function must be used to output results on the screen and compare obtained results with control data (i.e., correct results). It must be called after the task function, otherwise the program output the error message "The task function with a task name is not called at the beginning of the program".

The put function may be used for output one or more data items of any admissible type: bool, int, float, str, Node. Also it may output tuples and lists with items of above-mentioned types. If one of parameters is of invalid type then the program raises the ValueError exception with the error message "The put function has an argument of invalid type". A parameter type must correspond to the type of output data item, otherwise the program output the error message "Invalid type is used for an output data item".

In the case of output of more data than the task requires, the program output the error message "An attempt to output superfluous data". In the case of output of less data than the task requires, the program output the error message "Some data are not output".

The Node class


# Constructors:
   Node(data = 0, next = None, prev = None)
   Node.for_tree(data = 0, left = None, right = None, parent = None)
# Properties (available to read and write):
   Data
   Next
   Prev
   Left
   Right
   Parent
# Method that releases unmanaged resources used by the Node object:
   dispose()

The Node class is used in the Dynamic and Tree group tasks. In the introductory tasks of the Dynamic group (Dynamic1–Dynamic2) and in the tasks devoted to stacks and queues (Dynamic3–Dynamic28) the Data and Next properties of the Node class are used. In the tasks devoted to lists (Dynamic29–Dynamic80) all properties (Data, Next, Prev) of Node class are used. In the most tasks devoted to binary trees the Data, Left, and Right properties of the Node class are used. The Parent property is used in the tasks devoted to doubly linked binary trees (Tree48–Tree56 and Tree70–Tree71).

Note that the dispose method should be called for any Node object, except objects that are output data for this task. If the dispose method is not called for required objects then the program output the error message.

Output debug info

Functions described below are intended to output some debug data into the debug panel located below the task panels of the Programming Taskbook window.

Since version 4.22, the debug panel can contain text data with any Unicode characters.


show(a, ...)

This function shows the debug data in the debug window located below the task window. The arguments of the show function may be of any type including tuples and lists; all data items are converted into their string representations (see below the description of the set_width and set_precision functions).

The string parameter may contain explicit line-breaks as follows: chr(13) or "\r" (carriage return), chr(10) or "\n" (new line) or their combination chr(13) + chr(10) or "\r\n".

Using the + operator you may output data item jointly with the preceding comment as follows:

show("a = " + str(a))

In version 4.19, the implementation of the show function has been changed so as to provide formatted output of data structures, including nested ones.


show_line(a, ...)

The show_line function additionally breaks the current screen line after output debug information.


set_width(w)

This function defines the minimal width of the output field for each data item (w>=0, the default value ofw is0) The function affects all subsequent calls of the show/show_line function.


set_precision(d)

This function defines the count of decimals in the fractional part of a real number (if d=0 then the floating point format is used, the default value ofd is2). The function affects all subsequent calls of the show/show_line function.


hide_task()

The hide_task function hides all sections of the Programming Taskbook window with the exception of the debug section (and therefore increases the debug panel height). If the debug panel contains no data then the call of the hide_task function is ignored. All subsequent calls of this function are ignored too.

After displaying the Programming Taskbook window you can hide/restore task sections of this window by means of the space key or the command of the pop-up menu.


PrevNext

 

  @Mail.ru

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

Last revised:
19.04.2022