Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.4K Views
|
3 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Can we select from a list by position and value?

Posted 11 years ago
POSTED BY: Paul Cleary
3 Replies
Posted 11 years ago

Thank you both for your replies, both are excellent methods, in the end I adopted Sander's method even though slower it was more generic and adaptable, however I can see where I can utilise the faster method of Sandu's in other applications. Thanks again.

Paul.

POSTED BY: Paul Cleary
Posted 11 years ago

You may also work the problem the other way around: build the numbers with this pattern.

FromDigits[{#[[1]], 5, #[[2]], 2, 1, #[[3]]}] & /@ Select[Tuples[Range[0, 9], 3], #[[1]] != 0 &]

This performs a bit faster.


Or, if you want it even faster:

Flatten@Table[FromDigits[{a, 5, b, 2, 1, c}], {a, 1, 9}, {b, 0, 9}, {c, 0, 9}]
POSTED BY: Sandu Ursu

One can use IntegerDigits to get the digits separately:

a = Range[100000, 999999];
b = {2, 4, 5};
c = {5, 2, 1};
Select[a, IntegerDigits[#][[b]] === c &]
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard