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 | Ruby | PT4 types and functions

PrevNext


Additional functions

Functions and the Node class described below will be available in a program if the PT.rb module is imported in the program. Any template program created for task solving on Ruby 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_i() get_f() get_s() 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: boolean (TrueClass and FalseClass), Fixnum, Float, String, 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_i 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, ...) a.put()

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: boolean (TrueClass and FalseClass), Fixnum, Float, String, Node. Also it may output arrays with items of above-mentioned types. If one of parameters is of invalid type then the program raises the RuntimeError 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".

You can also use the put method without parameters; this method should be applied to the data item of the TrueClass, FalseClass, Fixnum, Float, String, Node, and Array type. For instance, you can output the integer value 1 by means one of the following expressions: put 1 and 1.put.

The Node class


# Constructors:
   Node.new()
   Node.new(data)
   Node.new(data, next)
   Node.new(data, next, prev)
   Node.new(left, right, data)
   Node.new(left, right, data, parent)
# Properties (available to read and to write):
   data
   next
   prev
   left
   right
   parent
# Method that releases resources used by the Node object:
   dispose()

The Node class is used in the Dynamic and Tree task groups. 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 data items of any type in the debug panel. The parameters of the method are automatically converted to their string representations.

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


show_line(a, ...)

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


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