Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

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

 

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".


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 to 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.


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))

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:
06.05.2018