Split a number into equal parts given the number of partsPythonic split list into n random chunks of roughly...
How to mitigate "bandwagon attacking" from players?
How to kill a localhost:8080
Where is the fallacy here?
If nine coins are tossed, what is the probability that the number of heads is even?
Where is the line between being obedient and getting bullied by a boss?
Make me a metasequence
Is there a limit on the maximum number of future jobs queued in an org?
How to fix my table, centering of columns
Levi-Civita symbol: 3D matrix
Create chunks from an array
Must 40/100G uplink ports on a 10G switch be connected to another switch?
Is there a way to find out the age of Camp ropes?
Wardrobe above a wall with fuse boxes
How can I be pwned if I'm not registered on the compromised site?
Misplaced tyre lever - alternatives?
How to get the first first element while continue streaming?
Can a gentile pronounce a blessing for a Jew? Are there songs I can sing that will bring peace?
Why is it "take a leak?"
Would the melodic leap of the opening phrase of Mozart's K545 be considered dissonant?
Can an earth elemental drown/bury its opponent underground using earth glide?
Is the NES controller port identical to the port on a Wii remote?
What are SHA-rounds?
When do _WA_Sys_ statistics Get Updated?
How can neutral atoms have exactly zero electric field when there is a difference in the positions of the charges?
Split a number into equal parts given the number of parts
Pythonic split list into n random chunks of roughly equal sizeSplit Django into appsUtility function to split a number into n parts in the given ratioSplit a list into a dictionaryCategorizing gene sequences read from a CSV fileSplit DAG into disjoint setsSplitting the coordinates of the world into equal blocksGenerating a bytearray by choosing an exclusive set of parametersSplit a given number so that their sum adds to another given numberGiven 4 vertices representing a quadrilateral, divide it into N parts
$begingroup$
I want to split a song into multiple parts, given the song duration and number of parts.
My code achieves that, but it feels a little "stupid" and I would like to learn a more sophisticated - and shorter - way. Particularly, I feel that the marker variable is a little overkill. I would welcome any suggestions.
song_duration = 20 # these two
num_of_parts = 4 # are given
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
print(parts)
# parts is : [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
python
New contributor
$endgroup$
add a comment |
$begingroup$
I want to split a song into multiple parts, given the song duration and number of parts.
My code achieves that, but it feels a little "stupid" and I would like to learn a more sophisticated - and shorter - way. Particularly, I feel that the marker variable is a little overkill. I would welcome any suggestions.
song_duration = 20 # these two
num_of_parts = 4 # are given
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
print(parts)
# parts is : [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
python
New contributor
$endgroup$
1
$begingroup$
Welcome to CodeReview ! From the output sample you've provided, it looks like you are using Python 3 (which is great). Can you confirm ? (The behavior for division is different which leads to different behaviors in your case)
$endgroup$
– Josay
1 hour ago
1
$begingroup$
Thanks for the warm welcome:). Yes, I am using Python 3.
$endgroup$
– barciewicz
1 hour ago
add a comment |
$begingroup$
I want to split a song into multiple parts, given the song duration and number of parts.
My code achieves that, but it feels a little "stupid" and I would like to learn a more sophisticated - and shorter - way. Particularly, I feel that the marker variable is a little overkill. I would welcome any suggestions.
song_duration = 20 # these two
num_of_parts = 4 # are given
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
print(parts)
# parts is : [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
python
New contributor
$endgroup$
I want to split a song into multiple parts, given the song duration and number of parts.
My code achieves that, but it feels a little "stupid" and I would like to learn a more sophisticated - and shorter - way. Particularly, I feel that the marker variable is a little overkill. I would welcome any suggestions.
song_duration = 20 # these two
num_of_parts = 4 # are given
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
print(parts)
# parts is : [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
python
python
New contributor
New contributor
New contributor
asked 1 hour ago
barciewiczbarciewicz
1211
1211
New contributor
New contributor
1
$begingroup$
Welcome to CodeReview ! From the output sample you've provided, it looks like you are using Python 3 (which is great). Can you confirm ? (The behavior for division is different which leads to different behaviors in your case)
$endgroup$
– Josay
1 hour ago
1
$begingroup$
Thanks for the warm welcome:). Yes, I am using Python 3.
$endgroup$
– barciewicz
1 hour ago
add a comment |
1
$begingroup$
Welcome to CodeReview ! From the output sample you've provided, it looks like you are using Python 3 (which is great). Can you confirm ? (The behavior for division is different which leads to different behaviors in your case)
$endgroup$
– Josay
1 hour ago
1
$begingroup$
Thanks for the warm welcome:). Yes, I am using Python 3.
$endgroup$
– barciewicz
1 hour ago
1
1
$begingroup$
Welcome to CodeReview ! From the output sample you've provided, it looks like you are using Python 3 (which is great). Can you confirm ? (The behavior for division is different which leads to different behaviors in your case)
$endgroup$
– Josay
1 hour ago
$begingroup$
Welcome to CodeReview ! From the output sample you've provided, it looks like you are using Python 3 (which is great). Can you confirm ? (The behavior for division is different which leads to different behaviors in your case)
$endgroup$
– Josay
1 hour ago
1
1
$begingroup$
Thanks for the warm welcome:). Yes, I am using Python 3.
$endgroup$
– barciewicz
1 hour ago
$begingroup$
Thanks for the warm welcome:). Yes, I am using Python 3.
$endgroup$
– barciewicz
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Firstly you should be able to see that the left value in each part is the same as the right value in the previous part. This can be implemented by using the pairwise
recipe:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
From this you should be able to generate all the wanted numbers using a list, or generator, comprehension:
part_duration = song_duration / num_of_parts
parts = [i * part_duration for i in range(num_of_parts + 1)]
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def song_segments(duration, segments):
delta = duration / segments
return pairwise([i * delta for i in range(segments + 1)])
print(list(song_segments(20, 4)))
$endgroup$
add a comment |
$begingroup$
Here are a few suggestions.
Write a function
Your code could be moved into a function on its own. It has the benefit of giving the code a clear name, a clear input, a clear output and we could go further and add documentation and tests.
def split_song(song_duration, num_of_parts):
"""Returns parts when a song of duration song_duration is split into num_of_parts parts."""
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
return parts
assert split_song(20, 4) == [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
assert split_song(21, 4) == [[0, 5.25], [5.25, 10.5], [10.5, 15.75], [15.75, 21.0]]
Proper data structure
You are returning a list of list. In Python, there is a cultural difference in how tuple
and list
are used.
In our case, we know that each piece will contain 2 pieces of information: the begining and the end. It would be more relevant to use tuples here.
part = (marker, marker + part_duration)
$endgroup$
add a comment |
$begingroup$
A few things:
First, you can make use of the third parameter of range
, which is the step, IF you can guarantee that part_duration
is an integer (which is the case for the example you posted here):
# Integer division
part_duration = song_duration // num_of_parts
parts = []
# I rearranged this a bit too
for i in range(0, song_duration, part_duration):
part = [i, i + part_duration]
parts.append(part)
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]] # Note they're integers
Note how this is just a transformation from a range
to a list
though. If you're transforming one collection to another, list comprehensions should come to mind:
# List comprehension split over two lines
parts = [[i, i + part_duration]
for i in range(0, song_duration, part_duration)]
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]]
If you can't guarantee integer steps though, I'm not sure of a good way. Unfortunately, Python doesn't allow fractional steps for its range
.
$endgroup$
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
add a comment |
$begingroup$
In general, building a list using a loop of the form
some_list = []
for …:
some_list.append(…)
… would be better written using a list comprehension.
Each interval always has two elements: a start time and an end time. These two-element lists would be better represented as tuples instead of lists. (Tuples have a connotation that they have a fixed length, whereas lists can grow to arbitrary lengths.)
Finally, I'd package the code into a function.
def intervals(parts, duration):
part_duration = duration / parts
return [(i * part_duration, (i + 1) * part_duration) for i in range(parts)]
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
barciewicz is a new contributor. Be nice, and check out our Code of Conduct.
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%2fcodereview.stackexchange.com%2fquestions%2f214857%2fsplit-a-number-into-equal-parts-given-the-number-of-parts%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
$begingroup$
Firstly you should be able to see that the left value in each part is the same as the right value in the previous part. This can be implemented by using the pairwise
recipe:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
From this you should be able to generate all the wanted numbers using a list, or generator, comprehension:
part_duration = song_duration / num_of_parts
parts = [i * part_duration for i in range(num_of_parts + 1)]
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def song_segments(duration, segments):
delta = duration / segments
return pairwise([i * delta for i in range(segments + 1)])
print(list(song_segments(20, 4)))
$endgroup$
add a comment |
$begingroup$
Firstly you should be able to see that the left value in each part is the same as the right value in the previous part. This can be implemented by using the pairwise
recipe:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
From this you should be able to generate all the wanted numbers using a list, or generator, comprehension:
part_duration = song_duration / num_of_parts
parts = [i * part_duration for i in range(num_of_parts + 1)]
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def song_segments(duration, segments):
delta = duration / segments
return pairwise([i * delta for i in range(segments + 1)])
print(list(song_segments(20, 4)))
$endgroup$
add a comment |
$begingroup$
Firstly you should be able to see that the left value in each part is the same as the right value in the previous part. This can be implemented by using the pairwise
recipe:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
From this you should be able to generate all the wanted numbers using a list, or generator, comprehension:
part_duration = song_duration / num_of_parts
parts = [i * part_duration for i in range(num_of_parts + 1)]
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def song_segments(duration, segments):
delta = duration / segments
return pairwise([i * delta for i in range(segments + 1)])
print(list(song_segments(20, 4)))
$endgroup$
Firstly you should be able to see that the left value in each part is the same as the right value in the previous part. This can be implemented by using the pairwise
recipe:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
From this you should be able to generate all the wanted numbers using a list, or generator, comprehension:
part_duration = song_duration / num_of_parts
parts = [i * part_duration for i in range(num_of_parts + 1)]
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def song_segments(duration, segments):
delta = duration / segments
return pairwise([i * delta for i in range(segments + 1)])
print(list(song_segments(20, 4)))
answered 1 hour ago
PeilonrayzPeilonrayz
25.5k337108
25.5k337108
add a comment |
add a comment |
$begingroup$
Here are a few suggestions.
Write a function
Your code could be moved into a function on its own. It has the benefit of giving the code a clear name, a clear input, a clear output and we could go further and add documentation and tests.
def split_song(song_duration, num_of_parts):
"""Returns parts when a song of duration song_duration is split into num_of_parts parts."""
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
return parts
assert split_song(20, 4) == [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
assert split_song(21, 4) == [[0, 5.25], [5.25, 10.5], [10.5, 15.75], [15.75, 21.0]]
Proper data structure
You are returning a list of list. In Python, there is a cultural difference in how tuple
and list
are used.
In our case, we know that each piece will contain 2 pieces of information: the begining and the end. It would be more relevant to use tuples here.
part = (marker, marker + part_duration)
$endgroup$
add a comment |
$begingroup$
Here are a few suggestions.
Write a function
Your code could be moved into a function on its own. It has the benefit of giving the code a clear name, a clear input, a clear output and we could go further and add documentation and tests.
def split_song(song_duration, num_of_parts):
"""Returns parts when a song of duration song_duration is split into num_of_parts parts."""
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
return parts
assert split_song(20, 4) == [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
assert split_song(21, 4) == [[0, 5.25], [5.25, 10.5], [10.5, 15.75], [15.75, 21.0]]
Proper data structure
You are returning a list of list. In Python, there is a cultural difference in how tuple
and list
are used.
In our case, we know that each piece will contain 2 pieces of information: the begining and the end. It would be more relevant to use tuples here.
part = (marker, marker + part_duration)
$endgroup$
add a comment |
$begingroup$
Here are a few suggestions.
Write a function
Your code could be moved into a function on its own. It has the benefit of giving the code a clear name, a clear input, a clear output and we could go further and add documentation and tests.
def split_song(song_duration, num_of_parts):
"""Returns parts when a song of duration song_duration is split into num_of_parts parts."""
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
return parts
assert split_song(20, 4) == [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
assert split_song(21, 4) == [[0, 5.25], [5.25, 10.5], [10.5, 15.75], [15.75, 21.0]]
Proper data structure
You are returning a list of list. In Python, there is a cultural difference in how tuple
and list
are used.
In our case, we know that each piece will contain 2 pieces of information: the begining and the end. It would be more relevant to use tuples here.
part = (marker, marker + part_duration)
$endgroup$
Here are a few suggestions.
Write a function
Your code could be moved into a function on its own. It has the benefit of giving the code a clear name, a clear input, a clear output and we could go further and add documentation and tests.
def split_song(song_duration, num_of_parts):
"""Returns parts when a song of duration song_duration is split into num_of_parts parts."""
part_duration = song_duration / num_of_parts
parts = []
marker = 0
for _ in range(num_of_parts):
part = [marker, marker + part_duration]
marker += part_duration
parts.append(part)
return parts
assert split_song(20, 4) == [[0, 5.0], [5.0, 10.0], [10.0, 15.0], [15.0, 20.0]]
assert split_song(21, 4) == [[0, 5.25], [5.25, 10.5], [10.5, 15.75], [15.75, 21.0]]
Proper data structure
You are returning a list of list. In Python, there is a cultural difference in how tuple
and list
are used.
In our case, we know that each piece will contain 2 pieces of information: the begining and the end. It would be more relevant to use tuples here.
part = (marker, marker + part_duration)
answered 1 hour ago
JosayJosay
25.9k14087
25.9k14087
add a comment |
add a comment |
$begingroup$
A few things:
First, you can make use of the third parameter of range
, which is the step, IF you can guarantee that part_duration
is an integer (which is the case for the example you posted here):
# Integer division
part_duration = song_duration // num_of_parts
parts = []
# I rearranged this a bit too
for i in range(0, song_duration, part_duration):
part = [i, i + part_duration]
parts.append(part)
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]] # Note they're integers
Note how this is just a transformation from a range
to a list
though. If you're transforming one collection to another, list comprehensions should come to mind:
# List comprehension split over two lines
parts = [[i, i + part_duration]
for i in range(0, song_duration, part_duration)]
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]]
If you can't guarantee integer steps though, I'm not sure of a good way. Unfortunately, Python doesn't allow fractional steps for its range
.
$endgroup$
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
add a comment |
$begingroup$
A few things:
First, you can make use of the third parameter of range
, which is the step, IF you can guarantee that part_duration
is an integer (which is the case for the example you posted here):
# Integer division
part_duration = song_duration // num_of_parts
parts = []
# I rearranged this a bit too
for i in range(0, song_duration, part_duration):
part = [i, i + part_duration]
parts.append(part)
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]] # Note they're integers
Note how this is just a transformation from a range
to a list
though. If you're transforming one collection to another, list comprehensions should come to mind:
# List comprehension split over two lines
parts = [[i, i + part_duration]
for i in range(0, song_duration, part_duration)]
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]]
If you can't guarantee integer steps though, I'm not sure of a good way. Unfortunately, Python doesn't allow fractional steps for its range
.
$endgroup$
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
add a comment |
$begingroup$
A few things:
First, you can make use of the third parameter of range
, which is the step, IF you can guarantee that part_duration
is an integer (which is the case for the example you posted here):
# Integer division
part_duration = song_duration // num_of_parts
parts = []
# I rearranged this a bit too
for i in range(0, song_duration, part_duration):
part = [i, i + part_duration]
parts.append(part)
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]] # Note they're integers
Note how this is just a transformation from a range
to a list
though. If you're transforming one collection to another, list comprehensions should come to mind:
# List comprehension split over two lines
parts = [[i, i + part_duration]
for i in range(0, song_duration, part_duration)]
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]]
If you can't guarantee integer steps though, I'm not sure of a good way. Unfortunately, Python doesn't allow fractional steps for its range
.
$endgroup$
A few things:
First, you can make use of the third parameter of range
, which is the step, IF you can guarantee that part_duration
is an integer (which is the case for the example you posted here):
# Integer division
part_duration = song_duration // num_of_parts
parts = []
# I rearranged this a bit too
for i in range(0, song_duration, part_duration):
part = [i, i + part_duration]
parts.append(part)
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]] # Note they're integers
Note how this is just a transformation from a range
to a list
though. If you're transforming one collection to another, list comprehensions should come to mind:
# List comprehension split over two lines
parts = [[i, i + part_duration]
for i in range(0, song_duration, part_duration)]
print(parts)
# [[0, 5], [5, 10], [10, 15], [15, 20]]
If you can't guarantee integer steps though, I'm not sure of a good way. Unfortunately, Python doesn't allow fractional steps for its range
.
edited 1 hour ago
answered 1 hour ago
CarcigenicateCarcigenicate
3,59211631
3,59211631
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
add a comment |
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
$begingroup$
Wow, tough crowd.
$endgroup$
– Carcigenicate
1 hour ago
add a comment |
$begingroup$
In general, building a list using a loop of the form
some_list = []
for …:
some_list.append(…)
… would be better written using a list comprehension.
Each interval always has two elements: a start time and an end time. These two-element lists would be better represented as tuples instead of lists. (Tuples have a connotation that they have a fixed length, whereas lists can grow to arbitrary lengths.)
Finally, I'd package the code into a function.
def intervals(parts, duration):
part_duration = duration / parts
return [(i * part_duration, (i + 1) * part_duration) for i in range(parts)]
$endgroup$
add a comment |
$begingroup$
In general, building a list using a loop of the form
some_list = []
for …:
some_list.append(…)
… would be better written using a list comprehension.
Each interval always has two elements: a start time and an end time. These two-element lists would be better represented as tuples instead of lists. (Tuples have a connotation that they have a fixed length, whereas lists can grow to arbitrary lengths.)
Finally, I'd package the code into a function.
def intervals(parts, duration):
part_duration = duration / parts
return [(i * part_duration, (i + 1) * part_duration) for i in range(parts)]
$endgroup$
add a comment |
$begingroup$
In general, building a list using a loop of the form
some_list = []
for …:
some_list.append(…)
… would be better written using a list comprehension.
Each interval always has two elements: a start time and an end time. These two-element lists would be better represented as tuples instead of lists. (Tuples have a connotation that they have a fixed length, whereas lists can grow to arbitrary lengths.)
Finally, I'd package the code into a function.
def intervals(parts, duration):
part_duration = duration / parts
return [(i * part_duration, (i + 1) * part_duration) for i in range(parts)]
$endgroup$
In general, building a list using a loop of the form
some_list = []
for …:
some_list.append(…)
… would be better written using a list comprehension.
Each interval always has two elements: a start time and an end time. These two-element lists would be better represented as tuples instead of lists. (Tuples have a connotation that they have a fixed length, whereas lists can grow to arbitrary lengths.)
Finally, I'd package the code into a function.
def intervals(parts, duration):
part_duration = duration / parts
return [(i * part_duration, (i + 1) * part_duration) for i in range(parts)]
answered 36 mins ago
200_success200_success
130k16153417
130k16153417
add a comment |
add a comment |
barciewicz is a new contributor. Be nice, and check out our Code of Conduct.
barciewicz is a new contributor. Be nice, and check out our Code of Conduct.
barciewicz is a new contributor. Be nice, and check out our Code of Conduct.
barciewicz is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f214857%2fsplit-a-number-into-equal-parts-given-the-number-of-parts%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
1
$begingroup$
Welcome to CodeReview ! From the output sample you've provided, it looks like you are using Python 3 (which is great). Can you confirm ? (The behavior for division is different which leads to different behaviors in your case)
$endgroup$
– Josay
1 hour ago
1
$begingroup$
Thanks for the warm welcome:). Yes, I am using Python 3.
$endgroup$
– barciewicz
1 hour ago