Message Boards Message Boards


[✓] Make a test using pattern matching for pairs of numbers?

Posted 6 days ago
2 Replies
2 Total Likes

I need to make a test that resolves to true or false for this problem, given a list of pairs of numbers for example:-


and a smaller list of pairs of numbers


Lets say the designation is {a, b} for each pair of numbers. The first test is do all "a" position numbers from "num" appear at some place in the "a" positions of "testno" and if so are the magnitude of the corresponding "b" position numbers in "testno" greater than or equal to the corresponding "b" position numbers in "num"? in the example shown "num" passes the test, however, num1 would fail. In the final setup the number of elements of both "testno" and "num" can vary even to where the lengths of both are the same. I know this can be done by using some form of do loop or even table and test each sub element in turn, but I am sure there is a more elegant way with pattern matching, any pointers would be welcome.

2 Replies

My attempt:

test[{a_, b_}] := Or @@ Cases[testno, {a, c_} -> (c >= b)];
And @@ Map[test, num]
And @@ Map[test, num1]
Posted 6 days ago

Thank you Gianluca that works perfectly.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract