cancel
Showing results for 
Search instead for 
Did you mean: 
Security Alert Log4j Security Vulnerability. Click here to know more.
cancel
Showing results for 
Search instead for 
Did you mean: 

Rosetta Code - Compare a list of strings

StuartBruff
23-Emerald I

Rosetta Code - Compare a list of strings

I've added the following solution to the Rosetta Code task: Compare a list of strings

 

The new Mathcad entry is at https://rosettacode.org/wiki/Compare_a_list_of_strings#Mathcad

 

Given a list of arbitrarily many strings, show how to:
•	test if they are all lexically equal
•	test if every string is lexically less than the one after it (i.e. whether the list is in strict ascending order)
Each of those two tests should result in a single true or false value, which could be used as the condition of an if statement or similar.

If the input list has less than two elements, the tests should always return true.

 

2021 10 21 A.png

 

As always, please feel free to improve upon and submit your own solution to this and the other Rosetta Stone tasks.

 

Stuart

8 REPLIES 8
VladimirN
23-Emerald IV
(To:StuartBruff)

Nice done, Stuart!


@VladimirN wrote:

Nice done, Stuart!


Спасиво, Владимир.

 

(Как вы говорите «no worries» в разговорном русском языке?? «не беспокойтесь»? «Без пробле́м»? «не надо»? «не вопро́с»?)

VladimirN
23-Emerald IV
(To:StuartBruff)

"Нет проблем".


@VladimirN wrote:

"Нет проблем".


Ещё раз спасибо, Владимир!

I suppose you realise that, when ORIGIN is set to another value than 0, by accident or purposely, many of these functions will fail.

 

Luc


@LucMeekes wrote:

I suppose you realise that, when ORIGIN is set to another value than 0, by accident or purposely, many of these functions will fail.

 

Luc


Yes, but I'm writing for those who will use/encounter vanilla Mathcad "out of the box" (like me! 😁).  

 

I have a worksheet full of ORIGIN-independent functions, but I often find that, for illustrative purposes at least, the additional "ORIGIN"s tend to obscure the working of a function(*).  They also take up more of the very limited horizontal space than I like, even if I use "O" as a shorthand notation for "ORIGIN".  If I do use a base other than 0, I  tend to write functions specifically for that origin.  Besides, it's easy enough to "escape" the functions in an ORIGIN wrapper ... I think I did that in one example in the recent TDMA thread  ... yep, I did:

 

StuartBruff_0-1634911251501.png

 

And, of course, there's no reason why some kind-hearted soul could not improve upon the current Rosetta Code Mathcad entries, or add a programmed version in M11/M15/Prime n ... 😈

 

Stuart

 

(*) To be more precise, I have a core set of functions that are ORIGIN-independent, which I then use to define other functions (eg, I use "head(v) in place of v{ORIGIN}, as in the string comparison excerpt above).

 

2021 10 22 A.png

 

I could do the same with strings (eg, replace the 0 in strhead). but I'd have to know that the Calculation option was set and I don't know how to do that in Express (I suppose a try-catch would work in Prime). 

 

2021 10 22 B.png

That should be easy, even in Express.

LucMeekes_0-1634917067789.png

With the Calculation Option "ORIGIN in strings" not set (the default), you get:

LucMeekes_1-1634917229254.png

With the Calculation option set, you get:

LucMeekes_2-1634917299930.png

So you can replace the    0     as index of the first element

with       search("_0","0",ORIGIN)-1

 

Success!
Luc

 

Incidentally: I suppose you know about HAL 9000 and whether or not it was in any way "IBM compatible"...

 


@LucMeekes wrote:

That should be easy, even in Express.

LucMeekes_0-1634917067789.png

 

 


Neat, Luc.  And, yes, it works nicely in Express 7, as you surmised ...

 

2021 10 23 A.png

 

... with a caveat.  Unless, I've made a typo (highly likely),  the method only works if ORIGIN = 0 or 1 when the string ORIGIN option isn't ticked.

 

2021 10 23 B.png

 

I vaguely recall seeing a few worksheets that use oddball origins for specific purposes, including negative ORIGINS.  In fact, I dragged my ORIGIN-independent functions from one by @ValeryOchkov that I'd tried to Expressify: https://community.ptc.com/t5/PTC-Mathcad/Study-16/m-p/642393

 

I think the following might work for any ORIGIN but, again, I might be making some mistake ...

 

2021 10 23 E.png

 

I can't think of a neat way to functionalize it in Express 7, as search has to be within the scope of the associated ORIGIN.

 

Incidentally: I suppose you know about HAL 9000 and whether or not it was in any way "IBM compatible"...


Who me?  No, I was getting ready for Buzz to fizz off into space at the time the film came out ...

 

Hmm, "HAL", "IBM", ... 🤔

 

2021 10 23 F.png

 

Nope, can't think of a thing ... 🙂

 

(I started out writing a simple, and I mean simple, FizzBuzz implementation in Express and kind of got carried away.   I'm too lazy to create new "buzzwords" for every "buzznumber" (prime) I added to (3,Fizz),(5,Buzz), so I ended up writing a little routine to extrapolate new words from a pair of seed words.  It's great for creating names for aliens)

 

Stuart

 

 

 

 

 

 

Announcements
Check out the latest
Mathcad Tip
"PTC Mathcad 15 / Prime 1-6 Update."