Combinatorics Ruby












0















I need to make a list of numbers.
Rules:




  1. 12 numbers in a string;

  2. First number its always "1";

  3. Numbers 2-3 its an items from array = [05..15];

  4. 4-5 its always "78";

  5. 6-7 its an items from array = [02,03,04,07,08,09,10,11,12,15..27];

  6. 8-12 its all options of five-digit numbers;


I wrote this code:



file = File.new('./file.txt', 'w')
one = 1

(5..15).to_a.each do |two_three|
if two_three.to_s.length == 1
two_three = "0#{two_three}"
end

four_five = 78

arr1 = (2..27).to_a
arr2 = [0,1,5,6,13,14]
arr3 = arr1-arr2
arr3.each do |six_seven|
if six_seven.to_s.length == 1
six_seven = "0#{six_seven}"
else
six_seven = "#{six_seven}"
end

Array.new(100000) do |index|
index = index + 1

if index.to_s.length == 1
eight_twelve = "0000#{index}"
elsif index.to_s.length == 2
eight_twelve = "000#{index}"
elsif index.to_s.length == 3
eight_twelve = "00#{index}"
elsif index.to_s.length == 4
eight_twelve = "0#{index}"
end

file.puts "#{one}#{two_three}#{four_five}#{six_seven}#{eight_twelve}"
end
end
end


Its working, but incorrectly.
Results
And continues to jump throughout all results (return thirteen-digit only on at string number 200000).
Thanks for your help.










share|improve this question

























  • Weird requirements. Why do you need to create such an array? Perhaps a proper class might be more better?

    – spickermann
    Nov 19 '18 at 13:14











  • @spickermann, year, i know. This is like an registration datas. About classes, i dont know. Im not an pro developer. Its just an amateur project. Could you help me?

    – Da Ka
    Nov 19 '18 at 13:26











  • Seems like you have some typos or incorrect code near Array.new(100000) do |index| index + 1. If your index is nil eight_twelve would be nil as well, because you are not checking for string of length 0. Moreover you can reduce many if/else conditionals in your code and improve readability by using format function format("%01d%02d%02d%02d%05d", one, two_three, four_five, six_seven, eight_twelve)

    – rubish
    Nov 19 '18 at 13:29


















0















I need to make a list of numbers.
Rules:




  1. 12 numbers in a string;

  2. First number its always "1";

  3. Numbers 2-3 its an items from array = [05..15];

  4. 4-5 its always "78";

  5. 6-7 its an items from array = [02,03,04,07,08,09,10,11,12,15..27];

  6. 8-12 its all options of five-digit numbers;


I wrote this code:



file = File.new('./file.txt', 'w')
one = 1

(5..15).to_a.each do |two_three|
if two_three.to_s.length == 1
two_three = "0#{two_three}"
end

four_five = 78

arr1 = (2..27).to_a
arr2 = [0,1,5,6,13,14]
arr3 = arr1-arr2
arr3.each do |six_seven|
if six_seven.to_s.length == 1
six_seven = "0#{six_seven}"
else
six_seven = "#{six_seven}"
end

Array.new(100000) do |index|
index = index + 1

if index.to_s.length == 1
eight_twelve = "0000#{index}"
elsif index.to_s.length == 2
eight_twelve = "000#{index}"
elsif index.to_s.length == 3
eight_twelve = "00#{index}"
elsif index.to_s.length == 4
eight_twelve = "0#{index}"
end

file.puts "#{one}#{two_three}#{four_five}#{six_seven}#{eight_twelve}"
end
end
end


Its working, but incorrectly.
Results
And continues to jump throughout all results (return thirteen-digit only on at string number 200000).
Thanks for your help.










share|improve this question

























  • Weird requirements. Why do you need to create such an array? Perhaps a proper class might be more better?

    – spickermann
    Nov 19 '18 at 13:14











  • @spickermann, year, i know. This is like an registration datas. About classes, i dont know. Im not an pro developer. Its just an amateur project. Could you help me?

    – Da Ka
    Nov 19 '18 at 13:26











  • Seems like you have some typos or incorrect code near Array.new(100000) do |index| index + 1. If your index is nil eight_twelve would be nil as well, because you are not checking for string of length 0. Moreover you can reduce many if/else conditionals in your code and improve readability by using format function format("%01d%02d%02d%02d%05d", one, two_three, four_five, six_seven, eight_twelve)

    – rubish
    Nov 19 '18 at 13:29
















0












0








0








I need to make a list of numbers.
Rules:




  1. 12 numbers in a string;

  2. First number its always "1";

  3. Numbers 2-3 its an items from array = [05..15];

  4. 4-5 its always "78";

  5. 6-7 its an items from array = [02,03,04,07,08,09,10,11,12,15..27];

  6. 8-12 its all options of five-digit numbers;


I wrote this code:



file = File.new('./file.txt', 'w')
one = 1

(5..15).to_a.each do |two_three|
if two_three.to_s.length == 1
two_three = "0#{two_three}"
end

four_five = 78

arr1 = (2..27).to_a
arr2 = [0,1,5,6,13,14]
arr3 = arr1-arr2
arr3.each do |six_seven|
if six_seven.to_s.length == 1
six_seven = "0#{six_seven}"
else
six_seven = "#{six_seven}"
end

Array.new(100000) do |index|
index = index + 1

if index.to_s.length == 1
eight_twelve = "0000#{index}"
elsif index.to_s.length == 2
eight_twelve = "000#{index}"
elsif index.to_s.length == 3
eight_twelve = "00#{index}"
elsif index.to_s.length == 4
eight_twelve = "0#{index}"
end

file.puts "#{one}#{two_three}#{four_five}#{six_seven}#{eight_twelve}"
end
end
end


Its working, but incorrectly.
Results
And continues to jump throughout all results (return thirteen-digit only on at string number 200000).
Thanks for your help.










share|improve this question
















I need to make a list of numbers.
Rules:




  1. 12 numbers in a string;

  2. First number its always "1";

  3. Numbers 2-3 its an items from array = [05..15];

  4. 4-5 its always "78";

  5. 6-7 its an items from array = [02,03,04,07,08,09,10,11,12,15..27];

  6. 8-12 its all options of five-digit numbers;


I wrote this code:



file = File.new('./file.txt', 'w')
one = 1

(5..15).to_a.each do |two_three|
if two_three.to_s.length == 1
two_three = "0#{two_three}"
end

four_five = 78

arr1 = (2..27).to_a
arr2 = [0,1,5,6,13,14]
arr3 = arr1-arr2
arr3.each do |six_seven|
if six_seven.to_s.length == 1
six_seven = "0#{six_seven}"
else
six_seven = "#{six_seven}"
end

Array.new(100000) do |index|
index = index + 1

if index.to_s.length == 1
eight_twelve = "0000#{index}"
elsif index.to_s.length == 2
eight_twelve = "000#{index}"
elsif index.to_s.length == 3
eight_twelve = "00#{index}"
elsif index.to_s.length == 4
eight_twelve = "0#{index}"
end

file.puts "#{one}#{two_three}#{four_five}#{six_seven}#{eight_twelve}"
end
end
end


Its working, but incorrectly.
Results
And continues to jump throughout all results (return thirteen-digit only on at string number 200000).
Thanks for your help.







ruby-on-rails ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:19









rubish

10.2k33754




10.2k33754










asked Nov 19 '18 at 13:05









Da KaDa Ka

31




31













  • Weird requirements. Why do you need to create such an array? Perhaps a proper class might be more better?

    – spickermann
    Nov 19 '18 at 13:14











  • @spickermann, year, i know. This is like an registration datas. About classes, i dont know. Im not an pro developer. Its just an amateur project. Could you help me?

    – Da Ka
    Nov 19 '18 at 13:26











  • Seems like you have some typos or incorrect code near Array.new(100000) do |index| index + 1. If your index is nil eight_twelve would be nil as well, because you are not checking for string of length 0. Moreover you can reduce many if/else conditionals in your code and improve readability by using format function format("%01d%02d%02d%02d%05d", one, two_three, four_five, six_seven, eight_twelve)

    – rubish
    Nov 19 '18 at 13:29





















  • Weird requirements. Why do you need to create such an array? Perhaps a proper class might be more better?

    – spickermann
    Nov 19 '18 at 13:14











  • @spickermann, year, i know. This is like an registration datas. About classes, i dont know. Im not an pro developer. Its just an amateur project. Could you help me?

    – Da Ka
    Nov 19 '18 at 13:26











  • Seems like you have some typos or incorrect code near Array.new(100000) do |index| index + 1. If your index is nil eight_twelve would be nil as well, because you are not checking for string of length 0. Moreover you can reduce many if/else conditionals in your code and improve readability by using format function format("%01d%02d%02d%02d%05d", one, two_three, four_five, six_seven, eight_twelve)

    – rubish
    Nov 19 '18 at 13:29



















Weird requirements. Why do you need to create such an array? Perhaps a proper class might be more better?

– spickermann
Nov 19 '18 at 13:14





Weird requirements. Why do you need to create such an array? Perhaps a proper class might be more better?

– spickermann
Nov 19 '18 at 13:14













@spickermann, year, i know. This is like an registration datas. About classes, i dont know. Im not an pro developer. Its just an amateur project. Could you help me?

– Da Ka
Nov 19 '18 at 13:26





@spickermann, year, i know. This is like an registration datas. About classes, i dont know. Im not an pro developer. Its just an amateur project. Could you help me?

– Da Ka
Nov 19 '18 at 13:26













Seems like you have some typos or incorrect code near Array.new(100000) do |index| index + 1. If your index is nil eight_twelve would be nil as well, because you are not checking for string of length 0. Moreover you can reduce many if/else conditionals in your code and improve readability by using format function format("%01d%02d%02d%02d%05d", one, two_three, four_five, six_seven, eight_twelve)

– rubish
Nov 19 '18 at 13:29







Seems like you have some typos or incorrect code near Array.new(100000) do |index| index + 1. If your index is nil eight_twelve would be nil as well, because you are not checking for string of length 0. Moreover you can reduce many if/else conditionals in your code and improve readability by using format function format("%01d%02d%02d%02d%05d", one, two_three, four_five, six_seven, eight_twelve)

– rubish
Nov 19 '18 at 13:29














1 Answer
1






active

oldest

votes


















0














Array#product is the method of choice here.



First construct the following array.



arr = [['1'], [*'05'..'15'], ['78'], [*'02'..'04', *'07'..'12', *'15'..'27'],
[*'00000'..'99999']]
#=> [["1"], ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15"],
# ["78"], ["02", "03", "04", "07", "08", "09", "10", "11", "12", "15",
# "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"],
# "00000", "00001",..., "99999"]


The sizes of each element (array) of arr is as follows.



arr.map(&:size)
#=> [1, 11, 1, 22, 100000]


To generate all combinations of arrays obtained by selecting one element from each element (array) of arr and then converting the resultant array to a string we write the following (which takes awhile to execute):



combos = arr.first.product(*arr.drop(1)).map(&:join)
n = combos.size
#=> 24200000

combos.first(2)
#=> ["105780200000", "105780200001"]
combos.last(2)
#=> ["115782799998", "115782799999"]
5.times do
i = rand(n)
puts "%5d -> %s" % [i, combos[i]]
end
9379583 -> 109780979583
12310295 -> 110781910295
11619403 -> 110781019403
3571703 -> 106781971703
18237011 -> 113781037011


Note:



size.reduce(:*)
#=> 24200000





share|improve this answer


























  • "takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

    – Jörg W Mittag
    Nov 20 '18 at 5:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375300%2fcombinatorics-ruby%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Array#product is the method of choice here.



First construct the following array.



arr = [['1'], [*'05'..'15'], ['78'], [*'02'..'04', *'07'..'12', *'15'..'27'],
[*'00000'..'99999']]
#=> [["1"], ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15"],
# ["78"], ["02", "03", "04", "07", "08", "09", "10", "11", "12", "15",
# "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"],
# "00000", "00001",..., "99999"]


The sizes of each element (array) of arr is as follows.



arr.map(&:size)
#=> [1, 11, 1, 22, 100000]


To generate all combinations of arrays obtained by selecting one element from each element (array) of arr and then converting the resultant array to a string we write the following (which takes awhile to execute):



combos = arr.first.product(*arr.drop(1)).map(&:join)
n = combos.size
#=> 24200000

combos.first(2)
#=> ["105780200000", "105780200001"]
combos.last(2)
#=> ["115782799998", "115782799999"]
5.times do
i = rand(n)
puts "%5d -> %s" % [i, combos[i]]
end
9379583 -> 109780979583
12310295 -> 110781910295
11619403 -> 110781019403
3571703 -> 106781971703
18237011 -> 113781037011


Note:



size.reduce(:*)
#=> 24200000





share|improve this answer


























  • "takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

    – Jörg W Mittag
    Nov 20 '18 at 5:54
















0














Array#product is the method of choice here.



First construct the following array.



arr = [['1'], [*'05'..'15'], ['78'], [*'02'..'04', *'07'..'12', *'15'..'27'],
[*'00000'..'99999']]
#=> [["1"], ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15"],
# ["78"], ["02", "03", "04", "07", "08", "09", "10", "11", "12", "15",
# "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"],
# "00000", "00001",..., "99999"]


The sizes of each element (array) of arr is as follows.



arr.map(&:size)
#=> [1, 11, 1, 22, 100000]


To generate all combinations of arrays obtained by selecting one element from each element (array) of arr and then converting the resultant array to a string we write the following (which takes awhile to execute):



combos = arr.first.product(*arr.drop(1)).map(&:join)
n = combos.size
#=> 24200000

combos.first(2)
#=> ["105780200000", "105780200001"]
combos.last(2)
#=> ["115782799998", "115782799999"]
5.times do
i = rand(n)
puts "%5d -> %s" % [i, combos[i]]
end
9379583 -> 109780979583
12310295 -> 110781910295
11619403 -> 110781019403
3571703 -> 106781971703
18237011 -> 113781037011


Note:



size.reduce(:*)
#=> 24200000





share|improve this answer


























  • "takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

    – Jörg W Mittag
    Nov 20 '18 at 5:54














0












0








0







Array#product is the method of choice here.



First construct the following array.



arr = [['1'], [*'05'..'15'], ['78'], [*'02'..'04', *'07'..'12', *'15'..'27'],
[*'00000'..'99999']]
#=> [["1"], ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15"],
# ["78"], ["02", "03", "04", "07", "08", "09", "10", "11", "12", "15",
# "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"],
# "00000", "00001",..., "99999"]


The sizes of each element (array) of arr is as follows.



arr.map(&:size)
#=> [1, 11, 1, 22, 100000]


To generate all combinations of arrays obtained by selecting one element from each element (array) of arr and then converting the resultant array to a string we write the following (which takes awhile to execute):



combos = arr.first.product(*arr.drop(1)).map(&:join)
n = combos.size
#=> 24200000

combos.first(2)
#=> ["105780200000", "105780200001"]
combos.last(2)
#=> ["115782799998", "115782799999"]
5.times do
i = rand(n)
puts "%5d -> %s" % [i, combos[i]]
end
9379583 -> 109780979583
12310295 -> 110781910295
11619403 -> 110781019403
3571703 -> 106781971703
18237011 -> 113781037011


Note:



size.reduce(:*)
#=> 24200000





share|improve this answer















Array#product is the method of choice here.



First construct the following array.



arr = [['1'], [*'05'..'15'], ['78'], [*'02'..'04', *'07'..'12', *'15'..'27'],
[*'00000'..'99999']]
#=> [["1"], ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15"],
# ["78"], ["02", "03", "04", "07", "08", "09", "10", "11", "12", "15",
# "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"],
# "00000", "00001",..., "99999"]


The sizes of each element (array) of arr is as follows.



arr.map(&:size)
#=> [1, 11, 1, 22, 100000]


To generate all combinations of arrays obtained by selecting one element from each element (array) of arr and then converting the resultant array to a string we write the following (which takes awhile to execute):



combos = arr.first.product(*arr.drop(1)).map(&:join)
n = combos.size
#=> 24200000

combos.first(2)
#=> ["105780200000", "105780200001"]
combos.last(2)
#=> ["115782799998", "115782799999"]
5.times do
i = rand(n)
puts "%5d -> %s" % [i, combos[i]]
end
9379583 -> 109780979583
12310295 -> 110781910295
11619403 -> 110781019403
3571703 -> 106781971703
18237011 -> 113781037011


Note:



size.reduce(:*)
#=> 24200000






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 5:53









Jörg W Mittag

291k63356549




291k63356549










answered Nov 19 '18 at 19:18









Cary SwovelandCary Swoveland

69.5k54166




69.5k54166













  • "takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

    – Jörg W Mittag
    Nov 20 '18 at 5:54



















  • "takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

    – Jörg W Mittag
    Nov 20 '18 at 5:54

















"takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

– Jörg W Mittag
Nov 20 '18 at 5:54





"takes awhile to execute" – And that is why "combinatorial explosion" is called combinatorial explosion.

– Jörg W Mittag
Nov 20 '18 at 5:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375300%2fcombinatorics-ruby%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini