How to generate a collection?

N

Nanyang Zhan

Given any four letter word, for example: "card"
Replace one to three letters with "~",
How can I get the result collection ( ["~ard", "c~rd", ..., "~~r~",
"~~~d"])?
I know ruby, but I am bad at any algorithms, help please.
 
B

Bill Kelly

Nanyang said:
Given any four letter word, for example: "card"
Replace one to three letters with "~",
How can I get the result collection ( ["~ard", "c~rd", ..., "~~r~",
"~~~d"])?

One idea:

irb> 1.upto(14) {|i| printf("%04b\n", i)}
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110


Regards,

Bill
 
N

Nanyang Zhan

Bill said:
Nanyang said:
Given any four letter word, for example: "card"
Replace one to three letters with "~",
How can I get the result collection ( ["~ard", "c~rd", ..., "~~r~",
"~~~d"])?

One idea:

irb> 1.upto(14) {|i| printf("%04b\n", i)}
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110


Regards,

Bill

Thanks.
Given I have the 0 and 1 array,
Replacing all the 0s with "~", and replacing all the 1s with the right
letters, I think it should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top