Skip to main content
23-Emerald V
October 21, 2021
Question

Rosetta Code - Compare a list of strings

  • October 21, 2021
  • 2 replies
  • 2931 views

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

2 replies

24-Ruby III
October 21, 2021

Nice done, Stuart!

23-Emerald V
October 22, 2021

@VladimirN wrote:

Nice done, Stuart!


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

 

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

24-Ruby III
October 22, 2021

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

23-Emerald IV
October 22, 2021

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

23-Emerald V
October 22, 2021

@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

23-Emerald IV
October 22, 2021

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