× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



In another thread, Craig Pelkie presented a contrived programming task
being used as an interview question. I think we all agreed that there
were a number of problems with it as an interview question for an
actual, real-world RPG programming job.

But a few of us found it at least somewhat intriguing just as a
puzzle, and I wound up coding it myself, just for fun. It took me
significantly longer than I expected. I wasn't timing myself
carefully, but I was purposely trying to do it fairly quickly, and it
still took me probably two hours, give or take.

Then I tried to solve the same puzzle in Python, and it was kind of
ridiculous how easy it was, just taking a few minutes.

Here is the puzzle, restated to try to correct some of the terminology
problems in the original:

Write some RPG code which takes a string and transforms it according
to the following rules:

1. An "alphabetic character" is defined as a case-sensitive letter in
the English alphabet, so there are 52 possible different alphabetic
characters: A-Z and a-z. Everything else is a nonalphabetic character.

2. A "word" is defined as a contiguous sequence of one or more
alphabetic characters. Words are separated by one or more
nonalphabetic characters. Any two adjacent alphabetic characters must
belong to the same word (no two words can just butt up against each
other; they must be separated by at least one nonalphabetic
character).

3. Each word in the input must be replaced by the following: The first
character in the word, followed by the count of different characters
between (not inclusive of) the first character and the last character
in the word, followed by the last character in the word. This is
probably best understood with a few examples.

foo -> f1o
bar -> b1r
blue -> b2e
book -> b1k
Automotive -> A6e
AutomOtive -> A7e
my -> m0y
a -> a0a
aa -> a0a

4. All nonalphabetic characters should "pass through" unchanged to the
output. Examples of complete input -> complete output, using
single-quotes to denote the beginning and end of strings, as you would
see in normal RPG:

'foo bar' -> 'f1o b1r'
'Hello, world!' -> 'H2o, w3d!'
'Go4it!' -> 'G0o4i0t!'

5. You can use whatever mechanism of input and output you like. You
could write a program which accepts a string as a parameter and uses
the same parameter as output; you could write a subprocedure; you
could output to a spooled file; whatever.

I think that should be enough to clearly define the task. If you are
interested in trying this and still need more clarification, just ask.

I believe that the spirit of the problem involves using only RPG,
without embedded SQL, but this is just for fun so if you want to use
it, go ahead. Here's the Python solution I came up with, taking one
command-line parameter as input and printing the output to the screen
(well, technically to stdout):

import sys
import re

def transform_match(m):
w = m.group()
return w[0] + str(len(set(w[1:-1]))) + w[-1]

def transform_string(s):
return re.sub(r'[A-Za-z]+', transform_match, s)

print(transform_string(sys.argv[1]))

The above should work for both Python 2 and 3.

John Y.

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.