Given an array of positive integers. All numbers occur an even number of
times except one number which occurs odd number of times. Find the
number in string Occurring Odd number of times.
Example: I/P = [1, 2, 3, 2, 3, 1, 3] 3 occurs and odd number of times. I've been trying to go thru the list using Countstr but you almost need a Sortstr function. Bob Hamilton Richardson Texas USA _______________________________________________ Ibm-netrexx mailing list [hidden email] |
On Wed, 27 Apr 2011 12:33:56 -0500
Robert Hamilton <[hidden email]> wrote: > Given an array of positive integers. All numbers occur an even number > of times except one number which occurs odd number of times. Find the > number in string Occurring Odd number of times. > > Example: > I/P = [1, 2, 3, 2, 3, 1, 3] > > 3 occurs and odd number of times. I've been trying to go thru the > list using Countstr but you almost need a Sortstr function. I'd set all elements of a stem to false. Then go through the array once, flipping the state of the appropriate stem variable for each element.. Then scan the stem looking for the one still set to true. _______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by Robert L Hamilton
One possible approach (haven't tried it) is to form and parse the string of
numbers using the first token as a following variable pattern. If the second token is found, concatenate the remaining string pieces (thus eliminating the integer pair) and repeat. If not found, it must be an odd occurrence (unpaired) integer. This has the advantage of stopping as soon as the token is found. On 4/27/2011 1:33 PM, Robert Hamilton wrote: > Given an array of positive integers. All numbers occur an even number of times except one number which occurs odd number of times. Find the number in string Occurring Odd number of times. > > Example: > I/P = [1, 2, 3, 2, 3, 1, 3] > > 3 occurs and odd number of times. I've been trying to go thru the list using Countstr but you almost need a Sortstr function. > > Bob Hamilton > Richardson Texas USA > > > _______________________________________________ > Ibm-netrexx mailing list > [hidden email] -------------------------------------------------------------------- myhosting.com - Premium Microsoft® Windows® and Linux web and application hosting - http://link.myhosting.com/myhosting _______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by Robert L Hamilton
Don't know if this is any help, but something like this would find the first value occurring an odd number of times. Could be adapted not to exit after finding the first, and find them all of course. I'm sure there are cleverer ways of doing it, and it does depend on making sure the number list is formatted appropriately, but it seems to work.
NumberList = ' 1 2 3 2 3 1 3 ' loop while NumberList.words() > 0 First = NumberList.word(1) NewNumberList = NumberList.changestr(First' ','') if (NumberList.words() - NewNumberList.words())//2 \=0 then do say First 'appears an odd number of times' exit end NumberList = NewNumberList end say 'There are no numbers appearing an odd number of times' Phil Warren On 27 Apr 2011, at 18:33, Robert Hamilton wrote: > Given an array of positive integers. All numbers occur an even number of times except one number which occurs odd number of times. Find the number in string Occurring Odd number of times. > > Example: > I/P = [1, 2, 3, 2, 3, 1, 3] > > 3 occurs and odd number of times. I've been trying to go thru the list using Countstr but you almost need a Sortstr function. > > Bob Hamilton > Richardson Texas USA > _______________________________________________ > Ibm-netrexx mailing list > [hidden email] > _______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by Robert L Hamilton
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Robert Hamilton schrieb am 27.04.2011 19:33: > Given an array of positive integers. All numbers occur an even number of > times except one number which occurs odd number of times. Find the > number in string Occurring Odd number of times. > > Example: > I/P = [1, 2, 3, 2, 3, 1, 3] > > 3 occurs and odd number of times. I've been trying to go thru the list > using Countstr but you almost need a Sortstr function. > import java.util.HashSet ip=[int 1, 2, 3, 2, 3, 1, 3] s=HashSet() loop i=0 to ip.length-1 ipo=Integer.valueOf(ip[i]) if s.remove(ipo)=null then s.add(ipo) end it=s.iterator() loop while it.hasNext() say (Integer it.next()) 'contained odd number of times' end hope that helps... - -- cu, Patric -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: GnuPT 2.5.2 iEYEARECAAYFAk25GM0ACgkQfGgGu8y7ypCNsgCgrLIaNVvesXoimjKwj2QjFchk 8TwAoMgVjDFuuFOrEytIRmYWXOYplxVH =xkx0 -----END PGP SIGNATURE----- _______________________________________________ Ibm-netrexx mailing list [hidden email] |
How about this way?:
ip=[int 1, 2, 3, 2, 3, 1, 3] rexxvar=0 loop i=0 to ip.length-1;rexxvar[ip[i]]=rexxvar[ip[i]]+1;end loop i over rexxvar if rexxvar[i]=rexxvar[i]%2*2 then say i "is even" else say i "is odd" end Rexx class is your friend! On 4/28/2011 12:35 AM, Patric Bechtel wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Robert Hamilton schrieb am 27.04.2011 19:33: >> Given an array of positive integers. All numbers occur an even number of >> times except one number which occurs odd number of times. Find the >> number in string Occurring Odd number of times. >> >> Example: >> I/P = [1, 2, 3, 2, 3, 1, 3] >> >> 3 occurs and odd number of times. I've been trying to go thru the list >> using Countstr but you almost need a Sortstr function. >> > import java.util.HashSet > > ip=[int 1, 2, 3, 2, 3, 1, 3] > s=HashSet() > loop i=0 to ip.length-1 > ipo=Integer.valueOf(ip[i]) > if s.remove(ipo)=null then > s.add(ipo) > end > it=s.iterator() > loop while it.hasNext() > say (Integer it.next()) 'contained odd number of times' > end > > hope that helps... > > - -- > cu, Patric > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.10 (GNU/Linux) > Comment: GnuPT 2.5.2 > > iEYEARECAAYFAk25GM0ACgkQfGgGu8y7ypCNsgCgrLIaNVvesXoimjKwj2QjFchk > 8TwAoMgVjDFuuFOrEytIRmYWXOYplxVH > =xkx0 > -----END PGP SIGNATURE----- > _______________________________________________ > Ibm-netrexx mailing list > [hidden email] > > > Ibm-netrexx mailing list [hidden email] |
In jEdit Test:
2 is even 3 is odd 1 is even => Finished <= Kermit wins. Thanks for your time and Enjoy the Day Bob Hamilton Richardson, Texas USA
On Thu, Apr 28, 2011 at 4:18 AM, Kermit Kiser <[hidden email]> wrote: How about this way?: _______________________________________________ Ibm-netrexx mailing list [hidden email] |
With apologies to Kermit, I think Bob Eager's solution was closest to the spirit of NetRexx (others seemed to see it as a Java or even FORTRAN program). I've attached an implementation of his algorithm.
On Thu, Apr 28, 2011 at 6:09 AM, Robert Hamilton <[hidden email]> wrote: In jEdit Test: _______________________________________________ Ibm-netrexx mailing list [hidden email] Odd.pdf (125K) Download Attachment |
Bob's could be improved (simplified) by using the prefix \
operator (rather than 1-) perhaps.
Mike
_______________________________________________ Ibm-netrexx mailing list [hidden email] |
Quite! That was my slip, not Bob's (I fell into an old FORTRAN idiom - alternate between two values by subtracting from their sum).
George On Thu, Apr 28, 2011 at 10:00 AM, Mike Cowlishaw <[hidden email]> wrote:
_______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by pwarren
Sorry - correcting (I hope!) an obvious error in the previous suggestion (why does one make one's best mistakes in public?). 'Double padding' of numbers still not elegant though.
Phil Warren -------------------------------------------------------------- NumberList = ' 1 2 1 10 112 2 3 5 5 3 112 6 10 12 12 ' loop while NumberList.words() > 0 First = NumberList.word(1) NewNumberList = NumberList.changestr(' 'First' ',' ') if (NumberList.words() - NewNumberList.words())//2 \=0 then do say First 'appears an odd number of times' exit end NumberList = NewNumberList end say 'There are no numbers appearing an odd number of times' > > > On 27 Apr 2011, at 18:33, Robert Hamilton wrote: > >> Given an array of positive integers. All numbers occur an even number of times except one number which occurs odd number of times. Find the number in string Occurring Odd number of times. >> >> Example: >> I/P = [1, 2, 3, 2, 3, 1, 3] >> >> 3 occurs and odd number of times. I've been trying to go thru the list using Countstr but you almost need a Sortstr function. >> >> Bob Hamilton >> Richardson Texas USA >> _______________________________________________ >> Ibm-netrexx mailing list >> [hidden email] >> > > > _______________________________________________ > Ibm-netrexx mailing list > [hidden email] > _______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by George Hovey-2
By the way, FORTRAN 77 (especially GEISCO's implemention on the GE
(General Electric's) Mark III Service, including STRING's,
and a very powerful set of run-time routines) has been a *very nice language*. Also, with all the additional Fourth Generation Languages like DMS and TABOL, which did GENERATE FORTRAN 77, we did have on those old times ;-) Thomas. ================== Am 28.04.2011 16:47, schrieb George Hovey: Quite! That was my slip, not Bob's (I fell into an old FORTRAN idiom - alternate between two values by subtracting from their sum). --
Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
In reply to this post by George Hovey-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Hi, not to be nitpicking, but the question given was: > Given an array of positive integers. All numbers occur an even number > of times except one number which occurs odd number of times. Find the > number in string Occurring Odd number of times. So the ones with even occurance are completely uninteresting, so why should they still be included in the result set? As the Rexx class doesn't allow that, I was forced to use the Java native HashSet; the nice loop over syntax (I suggested that 9 years ago already...) doesn't work with Iterator, so my example gets quite java-ish, sorry. Hopefully, some time in the future, we could write it like this: import java.util.HashSet ip=[int 1, 2, 3, 2, 3, 1, 3] s=HashSet() loop i over ip -- loop over array if s.remove(i)=null then s.add(i) -- autoboxing end loop it over s -- loop over iterator say it 'contained odd number of times' end or, as Groovy nowadays would write this: [1, 2, 3, 2, 3, 1, 3] .groupBy{ it } .findAll{ it.value.size % 2 != 0 } .each{ n,v -> println "$n contained odd number of times" } which is compact, readable and avoids any boilerplate. *Sigh*. Wish NetRexx was open sourced already, we could at least implement the loop over stuff already. - -- cu, Patric -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: GnuPT 2.5.2 iEYEARECAAYFAk25kKgACgkQfGgGu8y7ypA5fwCfYpvSHRVG8mN7AjuA7DWYHnuJ /VEAoK0a7SkSjL1y3RzSwtMcaeoALD1A =Z/Ot -----END PGP SIGNATURE----- _______________________________________________ Ibm-netrexx mailing list [hidden email] |
Patric,
>So the ones with even occurance are completely uninteresting, so why should they still be included in the result set? To give some assurance that the algorithm/implementation isn't working fortuitously. Suppose, say, the loop with 'order' returned the integers in the order 3, 1, 2 and the algorithm falsely decided both 3 and 2 had odd parity. If we stopped at 3, we wouldn't know that it had gotten 2 wrong. I didn't want to pound this little program into the ground, but if I were writing it for real use, I'd subject it to a number of test data sets and type out internal indicators of it's operation as well as the complete results. >my example gets quite java-ish I'm not rejecting the idea of using java classes in NetRexx; after all, it's designed to do just that. It's just that NetRexx is well prepared to handle this problem on its own, using type Rexx. Perhaps because the problem mentioned "integers" some people felt that Java type int is required, but type Rexx deals with arithmetic without requiring any action on our part. My policy is to use type Rexx in calculations unless it can be shown to significantly reduce performance. Avoiding Java native types results in simpler, clearer programs. And NetRexx jumps through hoops silently casting types as required by Java methods. >we could at least implement the loop over stuff Sounds interesting. But no doubt due to the ravages of age I tend to regard language innovations as guilty until proven innocent. George On Thu, Apr 28, 2011 at 12:07 PM, Patric Bechtel <[hidden email]> wrote:
_______________________________________________ Ibm-netrexx mailing list [hidden email] |
Hello George, *and* all,
Why are we always discussing *PEANUTS* at this Forum ? For me, it is *ridicolous* to see, what correspondence did came out of such an *easy* question. For sure, there are hundred's of ways to code this *PUZZLE* in plain NetRexx. *Either* class oriented, *or* function oriented, *or* Stem oriented (called INDEXED String's in NetRexx, for yet unknown reason, *or* by brute force loop's, *or* whatever. ;-) Shall we not concentrate, please, in the evolution of this marvelous language, please??? :-) Thomas. ======================================================================================================= Am 28.04.2011 20:04, schrieb George Hovey: Patric, --
Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
In reply to this post by George Hovey-2
On thinking about it .. but not having tried it .. would not
DROP be useful here? As each number is discovered: register it when first
seen or DROP it if already seen. Then a DO OVER at the end would
only list the odd occurences. [Left to the reader to work out how to
handle the dual.]
_______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by Robert L Hamilton
This version is a bit more general and treats the numbers as strings, not
integers. /* NetRexx 2 puzzle*/ ip = [1, 2, 3, 2, 3, 1, 3] -- Note that ip = ['a', 'b', 'c', 'b', 'c', 'a', 'c'] also works parse "" s t1 t2 loop i = 1 to ip.length; s = s ip[i -1]; end loop until t1 \= t2 | s = '' parse s t1 temp; parse temp s1 (t1) -0 t2 s2 s = s1 s2 end if t1 = t2 then say "No odd count was found." else say "The first odd count is for '"t1"'." -- parse s t1 s1 (t1) -0 t2 s2 -- Does not seem to work, but should? -- Possibly a bug in the NetRexx PARSE instruction? My misunderstanding? ---------------------------------------------- On 4/28/2011 2:04 PM, George Hovey wrote: > Patric, > > >So the ones with even occurance are completely uninteresting, so why > should they still be included in the result set? > > To give some assurance that the algorithm/implementation isn't working fortuitously. Suppose, say, the loop with 'order' returned the integers in the order 3, 1, 2 and the algorithm falsely decided both 3 and 2 had odd parity. If we stopped at 3, we wouldn't know that it had gotten 2 wrong. > > I didn't want to pound this little program into the ground, but if I were writing it for real use, I'd subject it to a number of test data sets and type out internal indicators of it's operation as well as the complete results. > > >my example gets quite java-ish > > I'm not rejecting the idea of using java classes in NetRexx; after all, it's designed to do just that. It's just that NetRexx is well prepared to handle this problem on its own, using type Rexx. Perhaps because the problem mentioned "integers" some people felt that Java type int is required, but type Rexx deals with arithmetic without requiring any action on our part. My policy is to use type Rexx in calculations unless it can be shown to significantly reduce performance. Avoiding Java native types results in simpler, clearer programs. And NetRexx jumps through hoops silently casting types as required by Java methods. > > >we could at least implement the loop over stuff > > Sounds interesting. But no doubt due to the ravages of age I tend to regard language innovations as guilty until proven innocent. > > George > > > On Thu, Apr 28, 2011 at 12:07 PM, Patric Bechtel <[hidden email]> wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > not to be nitpicking, but the question given was: > > > Given an array of positive integers. All numbers occur an even > > of times except one number which occurs odd number of times. Find the > > number in string Occurring Odd number of times. > > So the ones with even occurance are completely uninteresting, so why > should they still be included in the result set? > As the Rexx class doesn't allow that, I was forced to use the Java > native HashSet; the nice loop over syntax (I suggested that 9 years ago > already...) doesn't work with Iterator, so my example gets quite > java-ish, sorry. > > Hopefully, some time in the future, we could write it like this: > > import java.util.HashSet > > ip=[int 1, 2, 3, 2, 3, 1, 3] > s=HashSet() > loop i over ip -- loop over array > if s.remove(i)=null then > s.add(i) -- autoboxing > end > loop it over s -- loop over iterator > say it 'contained odd number of times' > end > > or, as Groovy nowadays would write this: > > [1, 2, 3, 2, 3, 1, 3] > .groupBy{ it } > .findAll{ it.value.size % 2 != 0 } > .each{ n,v -> println "$n contained odd number of times" } > > which is compact, readable and avoids any boilerplate. *Sigh*. Wish > NetRexx was open sourced already, we could at least implement the loop > over stuff already. > > - -- > cu, Patric > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.10 (GNU/Linux) > Comment: GnuPT 2.5.2 > > iEYEARECAAYFAk25kKgACgkQfGgGu8y7ypA5fwCfYpvSHRVG8mN7AjuA7DWYHnuJ > /VEAoK0a7SkSjL1y3RzSwtMcaeoALD1A > =Z/Ot > -----END PGP SIGNATURE----- > _______________________________________________ > Ibm-netrexx mailing list > [hidden email] > > > > > _______________________________________________ > Ibm-netrexx mailing list > [hidden email] -------------------------------------------------------------------- mail2web.com What can On Demand Business Solutions do for you? http://link.mail2web.com/Business/SharePoint _______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by Patric Bechtel
Well, as you have sighed upon not having something like Groovy features
at your hands in NetRexx, how about a relative of NetRexx then, namely
ooRexx, which seems to be more related to NetRexx than Groovy:
This works and is quite close to your suggestion of an extended NetRexx version that you gave./* ooRexx version */ ip=.bag~of(1,2,3,2,3,1,3) /* define elements in a bag collection */ unique=.set~new~union(ip) /* create a set from the bag */ do val over unique /* loop over unique elements */ /* query elements in bag, count them, determine oddness */ if ip~allAt(val)~items//2=1 then say val "-> odd" end ---rony On 28.04.2011 18:07, Patric Bechtel wrote: Hopefully, some time in the future, we could write it like this: _______________________________________________ Ibm-netrexx mailing list [hidden email] |
In reply to this post by Mike Cowlishaw
By the Way, Mike, when *I do have you on the chat*:
IS: abc=Null (in NetRexx) *exactly the same* as: DROP abc (in classic Rexx) ??? Thomas. ============================================================================ Am 28.04.2011 20:46, schrieb Mike Cowlishaw:
--
Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
In reply to this post by Rony G. Flatscher (wu-wien)
Can you please, for the next month, *NOT FORCE ME* to learn *GROOVY*, too ?? ;-) What I'm trying here is: To allow classic Rexx, ooRexx, and NetRexx *syntax* and *sematic's* in the same source program! ... and then tranlsate them to the STATE of the ART NetRexx 2.05 Language (when desired to do so!) :-) ;-) Just as a portation aid ... :-) Thomas. ======================================================================================= Am 28.04.2011 20:50, schrieb Rony G. Flatscher: Well, as you have sighed upon not having something like Groovy features at your hands in NetRexx, how about a relative of NetRexx then, namely ooRexx, which seems to be more related to NetRexx than Groovy: --
Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
Free forum by Nabble | Edit this page |