Proper way to make array from integer division in Ruby
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
add a comment |
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
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
add a comment |
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
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
arrays ruby integer
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
- You don't need
total_count
.
div.times { count_array << max_count }
is[max_count] * count_array
- 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?)
...or([max_count] * n).tap { |arr| arr << mod if mod > 0 }
.rem
may be a better name thanmod
.
– Cary Swoveland
Nov 22 '18 at 22:11
1
I'd useArray.new(n, max_count).push(*mod.nonzero?)
–nonzero?
returns the receiver if it's not zero ornil
otherwise.
– Stefan
Nov 23 '18 at 9:09
@Stefan that's really nice, I did not know aboutnonzero?
, today I learned :) I'll edit and add it.
– Marcin Kołodziej
Nov 23 '18 at 9:12
add a comment |
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]
1
@MarcinKołodziej, fixed thanks.
– iGian
Nov 23 '18 at 9:14
add a comment |
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]
1
This might result in an array ending with0
(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
add a comment |
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]
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
- You don't need
total_count
.
div.times { count_array << max_count }
is[max_count] * count_array
- 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?)
...or([max_count] * n).tap { |arr| arr << mod if mod > 0 }
.rem
may be a better name thanmod
.
– Cary Swoveland
Nov 22 '18 at 22:11
1
I'd useArray.new(n, max_count).push(*mod.nonzero?)
–nonzero?
returns the receiver if it's not zero ornil
otherwise.
– Stefan
Nov 23 '18 at 9:09
@Stefan that's really nice, I did not know aboutnonzero?
, today I learned :) I'll edit and add it.
– Marcin Kołodziej
Nov 23 '18 at 9:12
add a comment |
- You don't need
total_count
.
div.times { count_array << max_count }
is[max_count] * count_array
- 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?)
...or([max_count] * n).tap { |arr| arr << mod if mod > 0 }
.rem
may be a better name thanmod
.
– Cary Swoveland
Nov 22 '18 at 22:11
1
I'd useArray.new(n, max_count).push(*mod.nonzero?)
–nonzero?
returns the receiver if it's not zero ornil
otherwise.
– Stefan
Nov 23 '18 at 9:09
@Stefan that's really nice, I did not know aboutnonzero?
, today I learned :) I'll edit and add it.
– Marcin Kołodziej
Nov 23 '18 at 9:12
add a comment |
- You don't need
total_count
.
div.times { count_array << max_count }
is[max_count] * count_array
- 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?)
- You don't need
total_count
.
div.times { count_array << max_count }
is[max_count] * count_array
- 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?)
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 thanmod
.
– Cary Swoveland
Nov 22 '18 at 22:11
1
I'd useArray.new(n, max_count).push(*mod.nonzero?)
–nonzero?
returns the receiver if it's not zero ornil
otherwise.
– Stefan
Nov 23 '18 at 9:09
@Stefan that's really nice, I did not know aboutnonzero?
, today I learned :) I'll edit and add it.
– Marcin Kołodziej
Nov 23 '18 at 9:12
add a comment |
...or([max_count] * n).tap { |arr| arr << mod if mod > 0 }
.rem
may be a better name thanmod
.
– Cary Swoveland
Nov 22 '18 at 22:11
1
I'd useArray.new(n, max_count).push(*mod.nonzero?)
–nonzero?
returns the receiver if it's not zero ornil
otherwise.
– Stefan
Nov 23 '18 at 9:09
@Stefan that's really nice, I did not know aboutnonzero?
, 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
add a comment |
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]
1
@MarcinKołodziej, fixed thanks.
– iGian
Nov 23 '18 at 9:14
add a comment |
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]
1
@MarcinKołodziej, fixed thanks.
– iGian
Nov 23 '18 at 9:14
add a comment |
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]
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]
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
add a comment |
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
add a comment |
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]
1
This might result in an array ending with0
(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
add a comment |
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]
1
This might result in an array ending with0
(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
add a comment |
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]
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]
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 with0
(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
add a comment |
1
This might result in an array ending with0
(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
add a comment |
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]
add a comment |
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]
add a comment |
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]
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]
answered Nov 26 '18 at 6:54
sawa
129k27197299
129k27197299
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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