Integer comparison: Difference between revisions

Content added Content deleted
(Factor)
Line 880: Line 880:
(Note: in Python3 ''raw_input()'' will become ''input().'').
(Note: in Python3 ''raw_input()'' will become ''input().'').


An alternative implementation could use a Python dictionary to house a small dispatch table to be indexed by the results of the build-in ''cmp()'' function. ''cmp()'' returns a value suitable for use as a comparison function in a sorting algorithm: -1, 0 or 1 for <, = or > respectively. Thus we could use:
An alternative implementation could use a Python dictionary to house a small dispatch table to be indexed by the results of the built-in ''cmp()'' function. ''cmp()'' returns a value suitable for use as a comparison function in a sorting algorithm: -1, 0 or 1 for <, = or > respectively. Thus we could use:


{{works with|Python|2.x}}
<lang Python>#!/usr/bin/env python
<lang Python>#!/usr/bin/env python
import sys
import sys
Line 899: Line 900:


In this case the use of a dispatch table is silly. However, more generally in Python the use of dispatch dictionaries or tables is often preferable to long chains of '''''elif'''' clauses in a condition statement. Python's support of classes and functions (including currying, partial function support, and lambda expressions) as first class objects obviates the need for a "case" or "switch" statement.
In this case the use of a dispatch table is silly. However, more generally in Python the use of dispatch dictionaries or tables is often preferable to long chains of '''''elif'''' clauses in a condition statement. Python's support of classes and functions (including currying, partial function support, and lambda expressions) as first class objects obviates the need for a "case" or "switch" statement.

=={{header|R}}==
=={{header|R}}==
<lang R>print("insert number a")
<lang R>print("insert number a")