Proper way to make array from integer division in Ruby












1














tl;dr I want to make an array from the division by 5 results:



20 => [5,5,5,5]
16 => [5,5,5,1]
7 => [5,2]


My current implementation is straightforward yet too large. How can I make it simpler and shorter?



  max_count = 5
total_count = input_value

count_array =
div = total_count / max_count
mod = total_count % max_count
div.times { count_array << max_count }
count_array << mod unless mod == 0









share|improve this question




















  • 2




    16 => [5,5,5,1]
    – Sagar Pandya
    Nov 22 '18 at 21:27






  • 1




    20.divmod(5) #=> [4, 0]. Contains the all the information you require i.e. the quotient and the remainder.
    – Sagar Pandya
    Nov 22 '18 at 21:35
















1














tl;dr I want to make an array from the division by 5 results:



20 => [5,5,5,5]
16 => [5,5,5,1]
7 => [5,2]


My current implementation is straightforward yet too large. How can I make it simpler and shorter?



  max_count = 5
total_count = input_value

count_array =
div = total_count / max_count
mod = total_count % max_count
div.times { count_array << max_count }
count_array << mod unless mod == 0









share|improve this question




















  • 2




    16 => [5,5,5,1]
    – Sagar Pandya
    Nov 22 '18 at 21:27






  • 1




    20.divmod(5) #=> [4, 0]. Contains the all the information you require i.e. the quotient and the remainder.
    – Sagar Pandya
    Nov 22 '18 at 21:35














1












1








1


1





tl;dr I want to make an array from the division by 5 results:



20 => [5,5,5,5]
16 => [5,5,5,1]
7 => [5,2]


My current implementation is straightforward yet too large. How can I make it simpler and shorter?



  max_count = 5
total_count = input_value

count_array =
div = total_count / max_count
mod = total_count % max_count
div.times { count_array << max_count }
count_array << mod unless mod == 0









share|improve this question















tl;dr I want to make an array from the division by 5 results:



20 => [5,5,5,5]
16 => [5,5,5,1]
7 => [5,2]


My current implementation is straightforward yet too large. How can I make it simpler and shorter?



  max_count = 5
total_count = input_value

count_array =
div = total_count / max_count
mod = total_count % max_count
div.times { count_array << max_count }
count_array << mod unless mod == 0






arrays ruby integer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 10:22









Stefan

75.2k894141




75.2k894141










asked Nov 22 '18 at 20:45









laechoppe

14110




14110








  • 2




    16 => [5,5,5,1]
    – Sagar Pandya
    Nov 22 '18 at 21:27






  • 1




    20.divmod(5) #=> [4, 0]. Contains the all the information you require i.e. the quotient and the remainder.
    – Sagar Pandya
    Nov 22 '18 at 21:35














  • 2




    16 => [5,5,5,1]
    – Sagar Pandya
    Nov 22 '18 at 21:27






  • 1




    20.divmod(5) #=> [4, 0]. Contains the all the information you require i.e. the quotient and the remainder.
    – Sagar Pandya
    Nov 22 '18 at 21:35








2




2




16 => [5,5,5,1]
– Sagar Pandya
Nov 22 '18 at 21:27




16 => [5,5,5,1]
– Sagar Pandya
Nov 22 '18 at 21:27




1




1




20.divmod(5) #=> [4, 0]. Contains the all the information you require i.e. the quotient and the remainder.
– Sagar Pandya
Nov 22 '18 at 21:35




20.divmod(5) #=> [4, 0]. Contains the all the information you require i.e. the quotient and the remainder.
– Sagar Pandya
Nov 22 '18 at 21:35












4 Answers
4






active

oldest

votes


















4















  1. You don't need total_count.


  2. div.times { count_array << max_count } is [max_count] * count_array

  3. Using splat, we can simplify it further




max_count = 5

[*[max_count] * (input_value / max_count), input_value % max_count] - [0]


Alternatively, using divmod



max_count = 5

n, mod = input_value.divmod(max_count)
[*[max_count] * n, mod] - [0]


Last line can also be written as:



(Array.new(n) { max_count } << mod) - [0]


or as Stefan suggested in the comment, using Numeric#nonzero?:



Array.new(n, max_count).push(*mod.nonzero?)





share|improve this answer























  • ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
    – Cary Swoveland
    Nov 22 '18 at 22:11






  • 1




    I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
    – Stefan
    Nov 23 '18 at 9:09












  • @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
    – Marcin Kołodziej
    Nov 23 '18 at 9:12



















0














One option more:



d = 5
n = 24
Array.new(n/d){d}.tap{ |a| a << n%d if (n%d).nonzero? }
#=> [5, 5, 5, 5, 4]





share|improve this answer



















  • 1




    @MarcinKołodziej, fixed thanks.
    – iGian
    Nov 23 '18 at 9:14



















0














You can try this as well.



max=5
num=48
q, r=num.divmod(max) # => [9, 3]
Array.new.fill(max, 0, q).push(r.nonzero?).compact
# => [5, 5, 5, 5, 5, 5, 5, 5, 5, 3]





share|improve this answer



















  • 1




    This might result in an array ending with 0 (which the OP doesn't want)
    – Stefan
    Nov 23 '18 at 10:21










  • Just realized that :P.Thanks @Stefan.
    – Wysiati
    Nov 23 '18 at 10:23



















0














What about this?



[20].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 5]
[16].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 1]
[7] .tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 2]





share|improve this answer





















    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%2f53437796%2fproper-way-to-make-array-from-integer-division-in-ruby%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4















    1. You don't need total_count.


    2. div.times { count_array << max_count } is [max_count] * count_array

    3. Using splat, we can simplify it further




    max_count = 5

    [*[max_count] * (input_value / max_count), input_value % max_count] - [0]


    Alternatively, using divmod



    max_count = 5

    n, mod = input_value.divmod(max_count)
    [*[max_count] * n, mod] - [0]


    Last line can also be written as:



    (Array.new(n) { max_count } << mod) - [0]


    or as Stefan suggested in the comment, using Numeric#nonzero?:



    Array.new(n, max_count).push(*mod.nonzero?)





    share|improve this answer























    • ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
      – Cary Swoveland
      Nov 22 '18 at 22:11






    • 1




      I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
      – Stefan
      Nov 23 '18 at 9:09












    • @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
      – Marcin Kołodziej
      Nov 23 '18 at 9:12
















    4















    1. You don't need total_count.


    2. div.times { count_array << max_count } is [max_count] * count_array

    3. Using splat, we can simplify it further




    max_count = 5

    [*[max_count] * (input_value / max_count), input_value % max_count] - [0]


    Alternatively, using divmod



    max_count = 5

    n, mod = input_value.divmod(max_count)
    [*[max_count] * n, mod] - [0]


    Last line can also be written as:



    (Array.new(n) { max_count } << mod) - [0]


    or as Stefan suggested in the comment, using Numeric#nonzero?:



    Array.new(n, max_count).push(*mod.nonzero?)





    share|improve this answer























    • ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
      – Cary Swoveland
      Nov 22 '18 at 22:11






    • 1




      I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
      – Stefan
      Nov 23 '18 at 9:09












    • @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
      – Marcin Kołodziej
      Nov 23 '18 at 9:12














    4












    4








    4







    1. You don't need total_count.


    2. div.times { count_array << max_count } is [max_count] * count_array

    3. Using splat, we can simplify it further




    max_count = 5

    [*[max_count] * (input_value / max_count), input_value % max_count] - [0]


    Alternatively, using divmod



    max_count = 5

    n, mod = input_value.divmod(max_count)
    [*[max_count] * n, mod] - [0]


    Last line can also be written as:



    (Array.new(n) { max_count } << mod) - [0]


    or as Stefan suggested in the comment, using Numeric#nonzero?:



    Array.new(n, max_count).push(*mod.nonzero?)





    share|improve this answer















    1. You don't need total_count.


    2. div.times { count_array << max_count } is [max_count] * count_array

    3. Using splat, we can simplify it further




    max_count = 5

    [*[max_count] * (input_value / max_count), input_value % max_count] - [0]


    Alternatively, using divmod



    max_count = 5

    n, mod = input_value.divmod(max_count)
    [*[max_count] * n, mod] - [0]


    Last line can also be written as:



    (Array.new(n) { max_count } << mod) - [0]


    or as Stefan suggested in the comment, using Numeric#nonzero?:



    Array.new(n, max_count).push(*mod.nonzero?)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 9:14

























    answered Nov 22 '18 at 21:05









    Marcin Kołodziej

    4,256315




    4,256315












    • ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
      – Cary Swoveland
      Nov 22 '18 at 22:11






    • 1




      I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
      – Stefan
      Nov 23 '18 at 9:09












    • @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
      – Marcin Kołodziej
      Nov 23 '18 at 9:12


















    • ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
      – Cary Swoveland
      Nov 22 '18 at 22:11






    • 1




      I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
      – Stefan
      Nov 23 '18 at 9:09












    • @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
      – Marcin Kołodziej
      Nov 23 '18 at 9:12
















    ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
    – Cary Swoveland
    Nov 22 '18 at 22:11




    ...or ([max_count] * n).tap { |arr| arr << mod if mod > 0 }. rem may be a better name than mod.
    – Cary Swoveland
    Nov 22 '18 at 22:11




    1




    1




    I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
    – Stefan
    Nov 23 '18 at 9:09






    I'd use Array.new(n, max_count).push(*mod.nonzero?)nonzero? returns the receiver if it's not zero or nil otherwise.
    – Stefan
    Nov 23 '18 at 9:09














    @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
    – Marcin Kołodziej
    Nov 23 '18 at 9:12




    @Stefan that's really nice, I did not know about nonzero?, today I learned :) I'll edit and add it.
    – Marcin Kołodziej
    Nov 23 '18 at 9:12













    0














    One option more:



    d = 5
    n = 24
    Array.new(n/d){d}.tap{ |a| a << n%d if (n%d).nonzero? }
    #=> [5, 5, 5, 5, 4]





    share|improve this answer



















    • 1




      @MarcinKołodziej, fixed thanks.
      – iGian
      Nov 23 '18 at 9:14
















    0














    One option more:



    d = 5
    n = 24
    Array.new(n/d){d}.tap{ |a| a << n%d if (n%d).nonzero? }
    #=> [5, 5, 5, 5, 4]





    share|improve this answer



















    • 1




      @MarcinKołodziej, fixed thanks.
      – iGian
      Nov 23 '18 at 9:14














    0












    0








    0






    One option more:



    d = 5
    n = 24
    Array.new(n/d){d}.tap{ |a| a << n%d if (n%d).nonzero? }
    #=> [5, 5, 5, 5, 4]





    share|improve this answer














    One option more:



    d = 5
    n = 24
    Array.new(n/d){d}.tap{ |a| a << n%d if (n%d).nonzero? }
    #=> [5, 5, 5, 5, 4]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 9:11

























    answered Nov 23 '18 at 4:53









    iGian

    3,2702622




    3,2702622








    • 1




      @MarcinKołodziej, fixed thanks.
      – iGian
      Nov 23 '18 at 9:14














    • 1




      @MarcinKołodziej, fixed thanks.
      – iGian
      Nov 23 '18 at 9:14








    1




    1




    @MarcinKołodziej, fixed thanks.
    – iGian
    Nov 23 '18 at 9:14




    @MarcinKołodziej, fixed thanks.
    – iGian
    Nov 23 '18 at 9:14











    0














    You can try this as well.



    max=5
    num=48
    q, r=num.divmod(max) # => [9, 3]
    Array.new.fill(max, 0, q).push(r.nonzero?).compact
    # => [5, 5, 5, 5, 5, 5, 5, 5, 5, 3]





    share|improve this answer



















    • 1




      This might result in an array ending with 0 (which the OP doesn't want)
      – Stefan
      Nov 23 '18 at 10:21










    • Just realized that :P.Thanks @Stefan.
      – Wysiati
      Nov 23 '18 at 10:23
















    0














    You can try this as well.



    max=5
    num=48
    q, r=num.divmod(max) # => [9, 3]
    Array.new.fill(max, 0, q).push(r.nonzero?).compact
    # => [5, 5, 5, 5, 5, 5, 5, 5, 5, 3]





    share|improve this answer



















    • 1




      This might result in an array ending with 0 (which the OP doesn't want)
      – Stefan
      Nov 23 '18 at 10:21










    • Just realized that :P.Thanks @Stefan.
      – Wysiati
      Nov 23 '18 at 10:23














    0












    0








    0






    You can try this as well.



    max=5
    num=48
    q, r=num.divmod(max) # => [9, 3]
    Array.new.fill(max, 0, q).push(r.nonzero?).compact
    # => [5, 5, 5, 5, 5, 5, 5, 5, 5, 3]





    share|improve this answer














    You can try this as well.



    max=5
    num=48
    q, r=num.divmod(max) # => [9, 3]
    Array.new.fill(max, 0, q).push(r.nonzero?).compact
    # => [5, 5, 5, 5, 5, 5, 5, 5, 5, 3]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 10:28

























    answered Nov 23 '18 at 10:16









    Wysiati

    1044




    1044








    • 1




      This might result in an array ending with 0 (which the OP doesn't want)
      – Stefan
      Nov 23 '18 at 10:21










    • Just realized that :P.Thanks @Stefan.
      – Wysiati
      Nov 23 '18 at 10:23














    • 1




      This might result in an array ending with 0 (which the OP doesn't want)
      – Stefan
      Nov 23 '18 at 10:21










    • Just realized that :P.Thanks @Stefan.
      – Wysiati
      Nov 23 '18 at 10:23








    1




    1




    This might result in an array ending with 0 (which the OP doesn't want)
    – Stefan
    Nov 23 '18 at 10:21




    This might result in an array ending with 0 (which the OP doesn't want)
    – Stefan
    Nov 23 '18 at 10:21












    Just realized that :P.Thanks @Stefan.
    – Wysiati
    Nov 23 '18 at 10:23




    Just realized that :P.Thanks @Stefan.
    – Wysiati
    Nov 23 '18 at 10:23











    0














    What about this?



    [20].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 5]
    [16].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 1]
    [7] .tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 2]





    share|improve this answer


























      0














      What about this?



      [20].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 5]
      [16].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 1]
      [7] .tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 2]





      share|improve this answer
























        0












        0








        0






        What about this?



        [20].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 5]
        [16].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 1]
        [7] .tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 2]





        share|improve this answer












        What about this?



        [20].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 5]
        [16].tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 5, 5, 1]
        [7] .tap{|a| a.push(5, a.pop - 5) while a.last > 5} # => [5, 2]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 6:54









        sawa

        129k27197299




        129k27197299






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53437796%2fproper-way-to-make-array-from-integer-division-in-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







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...