Trabb Pardo–Knuth algorithm

From Rosetta Code
Revision as of 20:17, 22 May 2012 by rosettacode>Paddy3118 (New task and Python solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Trabb Pardo–Knuth algorithm
You are encouraged to solve this task according to the task description, using any language you may know.

From the wikipedia entry: <lang pseudocode>

ask for 11 numbers to be read into a sequence S
reverse sequence S
for each item in sequence S
    call a function to do an operation
    if result overflows
        alert user
    else
        print result

</lang>

The task is to implement the algorithm:

  1. Use the function
  2. The overflow condition is an answer of greater than 400.
  3. The 'user alert' should not not stop processing of other items of the sequence.
  4. Print a prompt before accepting textual numeric input.
  5. You may optionally print the item as well as its associated result, but the results must be in reverse order of input.
  6. Show output from a typical run here.
  7. The sequence S may be 'implied' and so not shown explicitly.

Python

Functional

<lang python>Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> def f(x): return abs(x) ** 0.5 + 5 * x**3

>>> print(', '.join('%s:%s' % (x, v if v<=400 else "TOO LARGE!") for x,v in ((y, f(float(y))) for y in input('\nnumbers: ').strip().split()[::-1])))

numbers: 1 2 3 4 5 6 7 8 9 10 11 11:TOO LARGE!, 10:TOO LARGE!, 9:TOO LARGE!, 8:TOO LARGE!, 7:TOO LARGE!, 6:TOO LARGE!, 5:TOO LARGE!, 4:322.0, 3:136.73205080756887, 2:41.41421356237309, 1:6.0 >>> </lang>