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



Did you try it? I did, and all of the test cases in John's post pass. It does have a bug for a string that goes all the way to the end of the input variable

Fix by putting this at the end of the loop:
if InWord;
if not added;
count += 1;
endif;
result += %char(count - 1) + lastchr;
endif;

instead of
if InWord;
result += %char(count-1) + lastchr;
endif;

As for procedures, this is a single procedure were the only duplicated code is in the block mentioned above. I guess I could have broken that out. Otherwise I would be splitting things up for the sake of splitting them up?

And here is the code that counts only distinct characters
elseif %check(content: chr) > 0;
count += 1;
content += chr;
added = *On;
endif;

Mark Murphy
STAR BASE Consulting, Inc.
mmurphy@xxxxxxxxxxxxxxx


-----Charles Wilt <charles.wilt@xxxxxxxxx> wrote: -----
To: "RPG programming on the IBM i (AS/400 and iSeries)" <rpg400-l@xxxxxxxxxxxx>
From: Charles Wilt <charles.wilt@xxxxxxxxx>
Date: 07/01/2016 11:27AM
Subject: Re: Clarified interview question (programming puzzle)


Think you'd fail...

The OP mentioned counting "distinct" characters...
book -> b1k

Also, points off for not using procedures! ;p

Charles


On Fri, Jul 1, 2016 at 10:56 AM, Mark Murphy/STAR BASE Consulting Inc. <
mmurphy@xxxxxxxxxxxxxxx> wrote:

While RPG was never designed to be a heavy duty string handling language,
it really wasn't too hard.

ctl-opt Option(*SrcStmt : *NoDebugIo: *NoUnref)
DftActGrp(*No) ActGrp(*New)
Main(challenge);

dcl-proc challenge;
dcl-pi *n ExtPgm('CHALLENGE');
string Char(25) const;
end-pi;

dcl-c alpha 'abcdefghijklmnopqrstuvwxyz+
ABCDEFGHIJKLMNOPQRSTUVWXYZ';

dcl-s content Varchar(64) Inz('');
dcl-s result Varchar(44) Inz('');
dcl-s chr Char(1) Inz('');
dcl-s lastchr Char(1) Inz('');
dcl-s i Int(5) Inz(0);
dcl-s j Int(5) Inz(0);
dcl-s InWord Ind Inz(*Off);
dcl-s count Int(5);
dcl-s added Ind Inz(*Off);

for i=1 to %size(string);
chr = %subst(string: i: 1);
if %check(alpha: chr) > 0;
if InWord;
if not added;
count += 1;
endif;
result += %char(count - 1) + lastchr;
endif;
InWord = *Off;
result += chr;
else;
lastchr = chr;
added = *Off;
if not InWord;
result += chr;
count = 0;
content = '';
elseif %check(content: chr) > 0;
count += 1;
content += chr;
added = *On;
endif;
InWord = *On;
endif;
endfor;
if InWord;
result += %char(count-1) + lastchr;
endif;

dsply 'Result: ' '*EXT' result;
end-proc;

Mark Murphy
STAR BASE Consulting, Inc.
mmurphy@xxxxxxxxxxxxxxx


-----John Yeung <gallium.arsenide@xxxxxxxxx> wrote: -----
To: "RPG programming on the IBM i / System i" <rpg400-l@xxxxxxxxxxxx>
From: John Yeung <gallium.arsenide@xxxxxxxxx>
Date: 06/30/2016 08:29PM
Cc: IBMi Open Source Roundtable <opensource@xxxxxxxxxxxx>
Subject: Clarified interview question (programming puzzle)


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.
--
This is the RPG programming on the IBM i (AS/400 and iSeries) (RPG400-L)
mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxx for any subscription related
questions.
--
This is the RPG programming on the IBM i (AS/400 and iSeries) (RPG400-L)
mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxx for any subscription related
questions.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

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.