Fivenum and a little bitCompute the MedianShow the Top Five Comment Scores on a SE PostDifference of the...
What to do with a rabbit in a survival situation?
Indesign - how to change the style of the page numbers?
How are mathematicians paid to do research?
Do I have a right to cancel a purchase of foreign currency in the UK?
Is there any reason why MCU changed the Snap to Blip
Is anyone advocating the promotion of homosexuality in UK schools?
Is it possible to create a craft with specific bones, like the bones of a forgotten beast?
Why return a static pointer instead of an out parameter?
How do you move up one folder in Finder?
Find The One Element In An Array That is Different From The Others
How can I get a player to accept that they should stop trying to pull stunts without thinking them through first?
Power/Loss diagram
Can you cast a blanket Invisibility and let the targets see each other?
Why was hardware diversification an asset for the IBM PC ecosystem?
Optimization terminology: "Exact" v. "Approximate"
Employers keep telling me my college isn't good enough - is there any way to fix this?
Shortest hex dumping program
How do native German speakers usually express skepticism (using even) about a premise?
Why did Harry Potter get a bedroom?
Misspelling my name on my mathematical publications
Salt, pepper, herbs and spices
Was I subtly told to resign?
Fivenum and a little bit
Why are they 'nude photos'?
Fivenum and a little bit
Compute the MedianShow the Top Five Comment Scores on a SE PostDifference of the square of the sumPost-determined Array SortingFun with strings and string lengthsFolding NumbersSuper Folding NumbersClark's TrianglePiles and Piles of PebblesThe Median of Practically Recreational LanguagesCoat of Many Colours
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
(a paradox, a paradox, a most ingenious paradox)
This is the first part of a multipart series inspired by different R functions.
The Task
Given a dataset $D$ of positive integers, I need you to compute the 5 number summary of $D$. However, I'm working on large datasets, so I need your code to be as small as possible, allowing me to store it on my computer.
The five number summary consists of:
- Minimum value
- First quartile (Q1)
- Median / Second quartile (Q2)
- Third quartile (Q3)
- Maximum value
There are several different ways of defining the quartiles, but we will use the one implemented by R:
Definitions:
- Minimum and maximum: the smallest and largest values, respectively.
- Median: the middle value if $D$ has an odd number of entries, and the artithmetic mean of the two middle-most values if $D$ has an even number of entries. Note that this means the median may be a non-integer value. We have had to Compute the Median before.
- First and Third Quartiles: Divide the data into two halves, including the central element in each half if $D$ has an odd number of entries, and find the median value of each half. The median of the lower half is the First Quartile, and the median of the upper half is the Third Quartile.
Examples:
$D=[1,2,3,4,5]$. The median is then $3$, and the lower half is $[1,2,3]$, yielding a first quartile of $2$, and the upper half is $[3,4,5]$, yielding a third quartile of $4$.
$D=[1,3,3,4,5,6,7,10]$. The median is $4.5$, and the lower half is $[1,3,3,4]$, yielding a first quartile of $3$, and the upper half is $[5,6,7,10]$, yielding a third quartile of $6.5$.
Additional rules:
- Input is as an array or your language's nearest equivalent.
- You may assume the array is sorted in either ascending or descending order (but please specify which).
- You may return/print the results in any consistent order, and in whichever flexible format you like, but please denote the order and format in your answer.
- Built-in functions equivalent to
fivenum
are allowed, but please also implement your own solution. - You may not assume each of the five numbers will be an integer.
- Explanations are encouraged.
- This is code-golf, so shortest answer in each language wins!
Randomly generated test cases
1 1 1 1 1 2 2 2 2 2 3 3 4 4 4 4 4 5 5 5 -> 1 1.5 2.5 4 5
1 2 2 2 4 4 5 5 6 7 7 8 9 9 9 9 9 10 10 10 -> 1 4 7 9 10
2 2 2 6 8 10 15 16 21 22 23 24 26 33 35 38 38 45 46 47 48 -> 2 10 23 38 48
1 2 9 -> 1 1.5 2 5.5 9
1 2 3 3 3 4 9 -> 1 2.5 3 3.5 9
1 1 2 5 7 7 8 8 15 16 18 24 24 26 26 27 27 28 28 28 29 29 39 39 40 45 46 48 48 48 48 49 50 52 60 63 72 73 79 85 86 87 88 90 91 93 94 95 95 97 100 -> 1 25 45 76 100
2 2 4 4 6 8 10 11 13 14 14 15 17 21 23 24 26 27 27 28 28 30 31 33 33 34 36 36 38 38 39 40 41 42 42 43 45 45 47 47 47 47 47 48 48 48 50 51 53 53 55 56 56 56 57 57 58 62 62 63 64 64 65 65 66 67 67 67 68 69 69 71 71 71 74 79 80 81 81 81 82 82 83 83 86 86 86 87 89 94 94 94 95 95 97 98 99 100 100 100 -> 2 33.5 54 76.5 100
code-golf array-manipulation statistics
$endgroup$
add a comment |
$begingroup$
(a paradox, a paradox, a most ingenious paradox)
This is the first part of a multipart series inspired by different R functions.
The Task
Given a dataset $D$ of positive integers, I need you to compute the 5 number summary of $D$. However, I'm working on large datasets, so I need your code to be as small as possible, allowing me to store it on my computer.
The five number summary consists of:
- Minimum value
- First quartile (Q1)
- Median / Second quartile (Q2)
- Third quartile (Q3)
- Maximum value
There are several different ways of defining the quartiles, but we will use the one implemented by R:
Definitions:
- Minimum and maximum: the smallest and largest values, respectively.
- Median: the middle value if $D$ has an odd number of entries, and the artithmetic mean of the two middle-most values if $D$ has an even number of entries. Note that this means the median may be a non-integer value. We have had to Compute the Median before.
- First and Third Quartiles: Divide the data into two halves, including the central element in each half if $D$ has an odd number of entries, and find the median value of each half. The median of the lower half is the First Quartile, and the median of the upper half is the Third Quartile.
Examples:
$D=[1,2,3,4,5]$. The median is then $3$, and the lower half is $[1,2,3]$, yielding a first quartile of $2$, and the upper half is $[3,4,5]$, yielding a third quartile of $4$.
$D=[1,3,3,4,5,6,7,10]$. The median is $4.5$, and the lower half is $[1,3,3,4]$, yielding a first quartile of $3$, and the upper half is $[5,6,7,10]$, yielding a third quartile of $6.5$.
Additional rules:
- Input is as an array or your language's nearest equivalent.
- You may assume the array is sorted in either ascending or descending order (but please specify which).
- You may return/print the results in any consistent order, and in whichever flexible format you like, but please denote the order and format in your answer.
- Built-in functions equivalent to
fivenum
are allowed, but please also implement your own solution. - You may not assume each of the five numbers will be an integer.
- Explanations are encouraged.
- This is code-golf, so shortest answer in each language wins!
Randomly generated test cases
1 1 1 1 1 2 2 2 2 2 3 3 4 4 4 4 4 5 5 5 -> 1 1.5 2.5 4 5
1 2 2 2 4 4 5 5 6 7 7 8 9 9 9 9 9 10 10 10 -> 1 4 7 9 10
2 2 2 6 8 10 15 16 21 22 23 24 26 33 35 38 38 45 46 47 48 -> 2 10 23 38 48
1 2 9 -> 1 1.5 2 5.5 9
1 2 3 3 3 4 9 -> 1 2.5 3 3.5 9
1 1 2 5 7 7 8 8 15 16 18 24 24 26 26 27 27 28 28 28 29 29 39 39 40 45 46 48 48 48 48 49 50 52 60 63 72 73 79 85 86 87 88 90 91 93 94 95 95 97 100 -> 1 25 45 76 100
2 2 4 4 6 8 10 11 13 14 14 15 17 21 23 24 26 27 27 28 28 30 31 33 33 34 36 36 38 38 39 40 41 42 42 43 45 45 47 47 47 47 47 48 48 48 50 51 53 53 55 56 56 56 57 57 58 62 62 63 64 64 65 65 66 67 67 67 68 69 69 71 71 71 74 79 80 81 81 81 82 82 83 83 86 86 86 87 89 94 94 94 95 95 97 98 99 100 100 100 -> 2 33.5 54 76.5 100
code-golf array-manipulation statistics
$endgroup$
$begingroup$
Suggested test case:[1 3 3 4]
; and maybe[1 3 3 3 4]
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
I changed the wording a little bit to clarify what the Q1 and Q3 halves are if the input has an odd length. Feel free to edit if this isn't accurate.
$endgroup$
– Erik the Outgolfer
6 hours ago
$begingroup$
@EriktheOutgolfer much better, thank you. I wasn't happy about it in the Sandbox but figured it was good enough to post.
$endgroup$
– Giuseppe
5 hours ago
add a comment |
$begingroup$
(a paradox, a paradox, a most ingenious paradox)
This is the first part of a multipart series inspired by different R functions.
The Task
Given a dataset $D$ of positive integers, I need you to compute the 5 number summary of $D$. However, I'm working on large datasets, so I need your code to be as small as possible, allowing me to store it on my computer.
The five number summary consists of:
- Minimum value
- First quartile (Q1)
- Median / Second quartile (Q2)
- Third quartile (Q3)
- Maximum value
There are several different ways of defining the quartiles, but we will use the one implemented by R:
Definitions:
- Minimum and maximum: the smallest and largest values, respectively.
- Median: the middle value if $D$ has an odd number of entries, and the artithmetic mean of the two middle-most values if $D$ has an even number of entries. Note that this means the median may be a non-integer value. We have had to Compute the Median before.
- First and Third Quartiles: Divide the data into two halves, including the central element in each half if $D$ has an odd number of entries, and find the median value of each half. The median of the lower half is the First Quartile, and the median of the upper half is the Third Quartile.
Examples:
$D=[1,2,3,4,5]$. The median is then $3$, and the lower half is $[1,2,3]$, yielding a first quartile of $2$, and the upper half is $[3,4,5]$, yielding a third quartile of $4$.
$D=[1,3,3,4,5,6,7,10]$. The median is $4.5$, and the lower half is $[1,3,3,4]$, yielding a first quartile of $3$, and the upper half is $[5,6,7,10]$, yielding a third quartile of $6.5$.
Additional rules:
- Input is as an array or your language's nearest equivalent.
- You may assume the array is sorted in either ascending or descending order (but please specify which).
- You may return/print the results in any consistent order, and in whichever flexible format you like, but please denote the order and format in your answer.
- Built-in functions equivalent to
fivenum
are allowed, but please also implement your own solution. - You may not assume each of the five numbers will be an integer.
- Explanations are encouraged.
- This is code-golf, so shortest answer in each language wins!
Randomly generated test cases
1 1 1 1 1 2 2 2 2 2 3 3 4 4 4 4 4 5 5 5 -> 1 1.5 2.5 4 5
1 2 2 2 4 4 5 5 6 7 7 8 9 9 9 9 9 10 10 10 -> 1 4 7 9 10
2 2 2 6 8 10 15 16 21 22 23 24 26 33 35 38 38 45 46 47 48 -> 2 10 23 38 48
1 2 9 -> 1 1.5 2 5.5 9
1 2 3 3 3 4 9 -> 1 2.5 3 3.5 9
1 1 2 5 7 7 8 8 15 16 18 24 24 26 26 27 27 28 28 28 29 29 39 39 40 45 46 48 48 48 48 49 50 52 60 63 72 73 79 85 86 87 88 90 91 93 94 95 95 97 100 -> 1 25 45 76 100
2 2 4 4 6 8 10 11 13 14 14 15 17 21 23 24 26 27 27 28 28 30 31 33 33 34 36 36 38 38 39 40 41 42 42 43 45 45 47 47 47 47 47 48 48 48 50 51 53 53 55 56 56 56 57 57 58 62 62 63 64 64 65 65 66 67 67 67 68 69 69 71 71 71 74 79 80 81 81 81 82 82 83 83 86 86 86 87 89 94 94 94 95 95 97 98 99 100 100 100 -> 2 33.5 54 76.5 100
code-golf array-manipulation statistics
$endgroup$
(a paradox, a paradox, a most ingenious paradox)
This is the first part of a multipart series inspired by different R functions.
The Task
Given a dataset $D$ of positive integers, I need you to compute the 5 number summary of $D$. However, I'm working on large datasets, so I need your code to be as small as possible, allowing me to store it on my computer.
The five number summary consists of:
- Minimum value
- First quartile (Q1)
- Median / Second quartile (Q2)
- Third quartile (Q3)
- Maximum value
There are several different ways of defining the quartiles, but we will use the one implemented by R:
Definitions:
- Minimum and maximum: the smallest and largest values, respectively.
- Median: the middle value if $D$ has an odd number of entries, and the artithmetic mean of the two middle-most values if $D$ has an even number of entries. Note that this means the median may be a non-integer value. We have had to Compute the Median before.
- First and Third Quartiles: Divide the data into two halves, including the central element in each half if $D$ has an odd number of entries, and find the median value of each half. The median of the lower half is the First Quartile, and the median of the upper half is the Third Quartile.
Examples:
$D=[1,2,3,4,5]$. The median is then $3$, and the lower half is $[1,2,3]$, yielding a first quartile of $2$, and the upper half is $[3,4,5]$, yielding a third quartile of $4$.
$D=[1,3,3,4,5,6,7,10]$. The median is $4.5$, and the lower half is $[1,3,3,4]$, yielding a first quartile of $3$, and the upper half is $[5,6,7,10]$, yielding a third quartile of $6.5$.
Additional rules:
- Input is as an array or your language's nearest equivalent.
- You may assume the array is sorted in either ascending or descending order (but please specify which).
- You may return/print the results in any consistent order, and in whichever flexible format you like, but please denote the order and format in your answer.
- Built-in functions equivalent to
fivenum
are allowed, but please also implement your own solution. - You may not assume each of the five numbers will be an integer.
- Explanations are encouraged.
- This is code-golf, so shortest answer in each language wins!
Randomly generated test cases
1 1 1 1 1 2 2 2 2 2 3 3 4 4 4 4 4 5 5 5 -> 1 1.5 2.5 4 5
1 2 2 2 4 4 5 5 6 7 7 8 9 9 9 9 9 10 10 10 -> 1 4 7 9 10
2 2 2 6 8 10 15 16 21 22 23 24 26 33 35 38 38 45 46 47 48 -> 2 10 23 38 48
1 2 9 -> 1 1.5 2 5.5 9
1 2 3 3 3 4 9 -> 1 2.5 3 3.5 9
1 1 2 5 7 7 8 8 15 16 18 24 24 26 26 27 27 28 28 28 29 29 39 39 40 45 46 48 48 48 48 49 50 52 60 63 72 73 79 85 86 87 88 90 91 93 94 95 95 97 100 -> 1 25 45 76 100
2 2 4 4 6 8 10 11 13 14 14 15 17 21 23 24 26 27 27 28 28 30 31 33 33 34 36 36 38 38 39 40 41 42 42 43 45 45 47 47 47 47 47 48 48 48 50 51 53 53 55 56 56 56 57 57 58 62 62 63 64 64 65 65 66 67 67 67 68 69 69 71 71 71 74 79 80 81 81 81 82 82 83 83 86 86 86 87 89 94 94 94 95 95 97 98 99 100 100 100 -> 2 33.5 54 76.5 100
code-golf array-manipulation statistics
code-golf array-manipulation statistics
edited 6 hours ago
Erik the Outgolfer
34.5k4 gold badges30 silver badges108 bronze badges
34.5k4 gold badges30 silver badges108 bronze badges
asked 8 hours ago
GiuseppeGiuseppe
18.9k3 gold badges14 silver badges61 bronze badges
18.9k3 gold badges14 silver badges61 bronze badges
$begingroup$
Suggested test case:[1 3 3 4]
; and maybe[1 3 3 3 4]
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
I changed the wording a little bit to clarify what the Q1 and Q3 halves are if the input has an odd length. Feel free to edit if this isn't accurate.
$endgroup$
– Erik the Outgolfer
6 hours ago
$begingroup$
@EriktheOutgolfer much better, thank you. I wasn't happy about it in the Sandbox but figured it was good enough to post.
$endgroup$
– Giuseppe
5 hours ago
add a comment |
$begingroup$
Suggested test case:[1 3 3 4]
; and maybe[1 3 3 3 4]
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
I changed the wording a little bit to clarify what the Q1 and Q3 halves are if the input has an odd length. Feel free to edit if this isn't accurate.
$endgroup$
– Erik the Outgolfer
6 hours ago
$begingroup$
@EriktheOutgolfer much better, thank you. I wasn't happy about it in the Sandbox but figured it was good enough to post.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
Suggested test case:
[1 3 3 4]
; and maybe [1 3 3 3 4]
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
Suggested test case:
[1 3 3 4]
; and maybe [1 3 3 3 4]
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
I changed the wording a little bit to clarify what the Q1 and Q3 halves are if the input has an odd length. Feel free to edit if this isn't accurate.
$endgroup$
– Erik the Outgolfer
6 hours ago
$begingroup$
I changed the wording a little bit to clarify what the Q1 and Q3 halves are if the input has an odd length. Feel free to edit if this isn't accurate.
$endgroup$
– Erik the Outgolfer
6 hours ago
$begingroup$
@EriktheOutgolfer much better, thank you. I wasn't happy about it in the Sandbox but figured it was good enough to post.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@EriktheOutgolfer much better, thank you. I wasn't happy about it in the Sandbox but figured it was good enough to post.
$endgroup$
– Giuseppe
5 hours ago
add a comment |
6 Answers
6
active
oldest
votes
$begingroup$
Jelly, 13 bytes
œs2a;WÆṁ;Ḣ;Ṫ
Try it online!
Order: [Q1, Q3, Q2/med, min, max]
.
$endgroup$
add a comment |
$begingroup$
MATL, 18 bytes
tno?t.5Xqh]5:q4/Xq
Output order is increasing, as in the test cases.
Try it online! Or verify all test cases.
Explanation
MATL, like MATLAB, computes quantiles using linear interpolation (just as specified in the challenge for the median). To achieve the required behaviour for the first and third quartiles, it suffices to repeat the median if the length of the input is odd. Then the results are just the 0, .25, .5, .75 and 1 quantiles.
t % Implicit input: numeric row array. Duplicate
no % Length, parity
? % If not zero (that is, if input length is odd)
.5 % Push .5
Xq % .5-quantile: median
h % Concatenate horizontall
] % End
5:q % Push [0 1 2 3 4]
4/ % Divide by 4, element-wise: gives [0 .25 .5 .75 1]
Xq % [0 .25 .5 .75 1]-quantiles. Implicit display
$endgroup$
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.[1, 3, 3, 4]
). :D
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
add a comment |
$begingroup$
Python 3.8, 97 bytes
lambda l:[l[0],l[-1]]+[(i[x(i)//2]+i[~x(i)//2])/2for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])]
This assumes that the input list is sorted in ascending order. f
is the function to return the 5-number summary.
The 5-number summary is in the order: ${min, max, Q1, Q2,Q3}$
I took off a few bytes by taking some hints from FlipTack's answer to Compute the Median.
Try it online!
How does it work?
lambda l:
[l[0],l[-1]] # The minimum and maximum, because l is assumed to be sorted in ascending order
+[(i[x(i)//2]+i[~x(i)//2])/2 # This line computes the median...
for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])] # ...for each of these lists (the first half, the overall list, and the second half)
# The (x:=len) is an assignment expression from Python 3.8.
# It assigns the len function to the variable x but also returns len.
# Therefore, x can be used as len to save a byte (yes, just one byte)
New contributor
$endgroup$
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
add a comment |
$begingroup$
C (gcc), 123 bytes
Assumes a list sorted in ascending order.
Outputs in order: min, Q1, Q2, Q3, max.
#define M(K,x)(K[(x-1)/2]+K[x/2])/2.
f(L,n,m)int*L;{m=n-n/2;printf("%d %f %f %f %d",*L,M(L,m),M(L,n),M((L+n/2),m),L[n-1]);}
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 66 bytes
lambda l:[(l[~-i//4]+l[-~i//4])/2for i in[1,n:=len(l),~n-n,~n,-3]]
Try it online!
Input and output are in ascending order.
$endgroup$
add a comment |
$begingroup$
R, 7 bytes
fivenum
Try it online!
Obvious cheeky answer. ;-)
Interestingly, fivenum(x)
is not equivalent to summary(x)
even when x
is numeric, as the quantiles are computed differently: fivenum
averages at discontinuities, whereas summary
interpolates. You can force summary
to behave like fivenum
with the option quantile.type=2
, but this is still longer than
R, 28 bytes
quantile(scan(),(0:4)/4,t=2)
Try it online!
which computes the quantiles of order 0 (min), 0.25 (Q1), 0.5 (median), 0.75 (Q3) and 1 (max). The t=2
specifies how quantiles are defined: there are 9 possible types, and the challenge definition corresponds to type 2. Note that the source code of the fivenum
built-in is very different (and much longer), for reasons I do not understand.
$endgroup$
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: "200"
};
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
});
}
});
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%2fcodegolf.stackexchange.com%2fquestions%2f188078%2ffivenum-and-a-little-bit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 13 bytes
œs2a;WÆṁ;Ḣ;Ṫ
Try it online!
Order: [Q1, Q3, Q2/med, min, max]
.
$endgroup$
add a comment |
$begingroup$
Jelly, 13 bytes
œs2a;WÆṁ;Ḣ;Ṫ
Try it online!
Order: [Q1, Q3, Q2/med, min, max]
.
$endgroup$
add a comment |
$begingroup$
Jelly, 13 bytes
œs2a;WÆṁ;Ḣ;Ṫ
Try it online!
Order: [Q1, Q3, Q2/med, min, max]
.
$endgroup$
Jelly, 13 bytes
œs2a;WÆṁ;Ḣ;Ṫ
Try it online!
Order: [Q1, Q3, Q2/med, min, max]
.
answered 7 hours ago
Erik the OutgolferErik the Outgolfer
34.5k4 gold badges30 silver badges108 bronze badges
34.5k4 gold badges30 silver badges108 bronze badges
add a comment |
add a comment |
$begingroup$
MATL, 18 bytes
tno?t.5Xqh]5:q4/Xq
Output order is increasing, as in the test cases.
Try it online! Or verify all test cases.
Explanation
MATL, like MATLAB, computes quantiles using linear interpolation (just as specified in the challenge for the median). To achieve the required behaviour for the first and third quartiles, it suffices to repeat the median if the length of the input is odd. Then the results are just the 0, .25, .5, .75 and 1 quantiles.
t % Implicit input: numeric row array. Duplicate
no % Length, parity
? % If not zero (that is, if input length is odd)
.5 % Push .5
Xq % .5-quantile: median
h % Concatenate horizontall
] % End
5:q % Push [0 1 2 3 4]
4/ % Divide by 4, element-wise: gives [0 .25 .5 .75 1]
Xq % [0 .25 .5 .75 1]-quantiles. Implicit display
$endgroup$
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.[1, 3, 3, 4]
). :D
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
add a comment |
$begingroup$
MATL, 18 bytes
tno?t.5Xqh]5:q4/Xq
Output order is increasing, as in the test cases.
Try it online! Or verify all test cases.
Explanation
MATL, like MATLAB, computes quantiles using linear interpolation (just as specified in the challenge for the median). To achieve the required behaviour for the first and third quartiles, it suffices to repeat the median if the length of the input is odd. Then the results are just the 0, .25, .5, .75 and 1 quantiles.
t % Implicit input: numeric row array. Duplicate
no % Length, parity
? % If not zero (that is, if input length is odd)
.5 % Push .5
Xq % .5-quantile: median
h % Concatenate horizontall
] % End
5:q % Push [0 1 2 3 4]
4/ % Divide by 4, element-wise: gives [0 .25 .5 .75 1]
Xq % [0 .25 .5 .75 1]-quantiles. Implicit display
$endgroup$
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.[1, 3, 3, 4]
). :D
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
add a comment |
$begingroup$
MATL, 18 bytes
tno?t.5Xqh]5:q4/Xq
Output order is increasing, as in the test cases.
Try it online! Or verify all test cases.
Explanation
MATL, like MATLAB, computes quantiles using linear interpolation (just as specified in the challenge for the median). To achieve the required behaviour for the first and third quartiles, it suffices to repeat the median if the length of the input is odd. Then the results are just the 0, .25, .5, .75 and 1 quantiles.
t % Implicit input: numeric row array. Duplicate
no % Length, parity
? % If not zero (that is, if input length is odd)
.5 % Push .5
Xq % .5-quantile: median
h % Concatenate horizontall
] % End
5:q % Push [0 1 2 3 4]
4/ % Divide by 4, element-wise: gives [0 .25 .5 .75 1]
Xq % [0 .25 .5 .75 1]-quantiles. Implicit display
$endgroup$
MATL, 18 bytes
tno?t.5Xqh]5:q4/Xq
Output order is increasing, as in the test cases.
Try it online! Or verify all test cases.
Explanation
MATL, like MATLAB, computes quantiles using linear interpolation (just as specified in the challenge for the median). To achieve the required behaviour for the first and third quartiles, it suffices to repeat the median if the length of the input is odd. Then the results are just the 0, .25, .5, .75 and 1 quantiles.
t % Implicit input: numeric row array. Duplicate
no % Length, parity
? % If not zero (that is, if input length is odd)
.5 % Push .5
Xq % .5-quantile: median
h % Concatenate horizontall
] % End
5:q % Push [0 1 2 3 4]
4/ % Divide by 4, element-wise: gives [0 .25 .5 .75 1]
Xq % [0 .25 .5 .75 1]-quantiles. Implicit display
edited 7 hours ago
answered 7 hours ago
Luis MendoLuis Mendo
76.9k8 gold badges95 silver badges300 bronze badges
76.9k8 gold badges95 silver badges300 bronze badges
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.[1, 3, 3, 4]
). :D
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
add a comment |
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.[1, 3, 3, 4]
). :D
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.
[1, 3, 3, 4]
). :D$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
"if the length of the input is odd, or equivalently if the median appears in the input" I disagree with this part (e.g.
[1, 3, 3, 4]
). :D$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
@EriktheOutgolfer Solved now. Thanks again!
$endgroup$
– Luis Mendo
7 hours ago
add a comment |
$begingroup$
Python 3.8, 97 bytes
lambda l:[l[0],l[-1]]+[(i[x(i)//2]+i[~x(i)//2])/2for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])]
This assumes that the input list is sorted in ascending order. f
is the function to return the 5-number summary.
The 5-number summary is in the order: ${min, max, Q1, Q2,Q3}$
I took off a few bytes by taking some hints from FlipTack's answer to Compute the Median.
Try it online!
How does it work?
lambda l:
[l[0],l[-1]] # The minimum and maximum, because l is assumed to be sorted in ascending order
+[(i[x(i)//2]+i[~x(i)//2])/2 # This line computes the median...
for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])] # ...for each of these lists (the first half, the overall list, and the second half)
# The (x:=len) is an assignment expression from Python 3.8.
# It assigns the len function to the variable x but also returns len.
# Therefore, x can be used as len to save a byte (yes, just one byte)
New contributor
$endgroup$
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
add a comment |
$begingroup$
Python 3.8, 97 bytes
lambda l:[l[0],l[-1]]+[(i[x(i)//2]+i[~x(i)//2])/2for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])]
This assumes that the input list is sorted in ascending order. f
is the function to return the 5-number summary.
The 5-number summary is in the order: ${min, max, Q1, Q2,Q3}$
I took off a few bytes by taking some hints from FlipTack's answer to Compute the Median.
Try it online!
How does it work?
lambda l:
[l[0],l[-1]] # The minimum and maximum, because l is assumed to be sorted in ascending order
+[(i[x(i)//2]+i[~x(i)//2])/2 # This line computes the median...
for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])] # ...for each of these lists (the first half, the overall list, and the second half)
# The (x:=len) is an assignment expression from Python 3.8.
# It assigns the len function to the variable x but also returns len.
# Therefore, x can be used as len to save a byte (yes, just one byte)
New contributor
$endgroup$
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
add a comment |
$begingroup$
Python 3.8, 97 bytes
lambda l:[l[0],l[-1]]+[(i[x(i)//2]+i[~x(i)//2])/2for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])]
This assumes that the input list is sorted in ascending order. f
is the function to return the 5-number summary.
The 5-number summary is in the order: ${min, max, Q1, Q2,Q3}$
I took off a few bytes by taking some hints from FlipTack's answer to Compute the Median.
Try it online!
How does it work?
lambda l:
[l[0],l[-1]] # The minimum and maximum, because l is assumed to be sorted in ascending order
+[(i[x(i)//2]+i[~x(i)//2])/2 # This line computes the median...
for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])] # ...for each of these lists (the first half, the overall list, and the second half)
# The (x:=len) is an assignment expression from Python 3.8.
# It assigns the len function to the variable x but also returns len.
# Therefore, x can be used as len to save a byte (yes, just one byte)
New contributor
$endgroup$
Python 3.8, 97 bytes
lambda l:[l[0],l[-1]]+[(i[x(i)//2]+i[~x(i)//2])/2for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])]
This assumes that the input list is sorted in ascending order. f
is the function to return the 5-number summary.
The 5-number summary is in the order: ${min, max, Q1, Q2,Q3}$
I took off a few bytes by taking some hints from FlipTack's answer to Compute the Median.
Try it online!
How does it work?
lambda l:
[l[0],l[-1]] # The minimum and maximum, because l is assumed to be sorted in ascending order
+[(i[x(i)//2]+i[~x(i)//2])/2 # This line computes the median...
for i in(l[:~((x:=len)(l)//2-1)],l,l[x(l)//2:])] # ...for each of these lists (the first half, the overall list, and the second half)
# The (x:=len) is an assignment expression from Python 3.8.
# It assigns the len function to the variable x but also returns len.
# Therefore, x can be used as len to save a byte (yes, just one byte)
New contributor
edited 4 hours ago
New contributor
answered 5 hours ago
MaxwellMaxwell
4118 bronze badges
4118 bronze badges
New contributor
New contributor
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
add a comment |
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
it's fine to use a function that computes the median; that submission would no longer by Python (3?), but "Python + statistics package" or similar.
$endgroup$
– Giuseppe
5 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
$begingroup$
@Giuseppe Well, I ended up getting it shorted by not using the statistics package, so it is sort of a non-issue now.
$endgroup$
– Maxwell
4 hours ago
add a comment |
$begingroup$
C (gcc), 123 bytes
Assumes a list sorted in ascending order.
Outputs in order: min, Q1, Q2, Q3, max.
#define M(K,x)(K[(x-1)/2]+K[x/2])/2.
f(L,n,m)int*L;{m=n-n/2;printf("%d %f %f %f %d",*L,M(L,m),M(L,n),M((L+n/2),m),L[n-1]);}
Try it online!
$endgroup$
add a comment |
$begingroup$
C (gcc), 123 bytes
Assumes a list sorted in ascending order.
Outputs in order: min, Q1, Q2, Q3, max.
#define M(K,x)(K[(x-1)/2]+K[x/2])/2.
f(L,n,m)int*L;{m=n-n/2;printf("%d %f %f %f %d",*L,M(L,m),M(L,n),M((L+n/2),m),L[n-1]);}
Try it online!
$endgroup$
add a comment |
$begingroup$
C (gcc), 123 bytes
Assumes a list sorted in ascending order.
Outputs in order: min, Q1, Q2, Q3, max.
#define M(K,x)(K[(x-1)/2]+K[x/2])/2.
f(L,n,m)int*L;{m=n-n/2;printf("%d %f %f %f %d",*L,M(L,m),M(L,n),M((L+n/2),m),L[n-1]);}
Try it online!
$endgroup$
C (gcc), 123 bytes
Assumes a list sorted in ascending order.
Outputs in order: min, Q1, Q2, Q3, max.
#define M(K,x)(K[(x-1)/2]+K[x/2])/2.
f(L,n,m)int*L;{m=n-n/2;printf("%d %f %f %f %d",*L,M(L,m),M(L,n),M((L+n/2),m),L[n-1]);}
Try it online!
answered 2 hours ago
gastropnergastropner
2,5601 gold badge6 silver badges13 bronze badges
2,5601 gold badge6 silver badges13 bronze badges
add a comment |
add a comment |
$begingroup$
Python 3.8 (pre-release), 66 bytes
lambda l:[(l[~-i//4]+l[-~i//4])/2for i in[1,n:=len(l),~n-n,~n,-3]]
Try it online!
Input and output are in ascending order.
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 66 bytes
lambda l:[(l[~-i//4]+l[-~i//4])/2for i in[1,n:=len(l),~n-n,~n,-3]]
Try it online!
Input and output are in ascending order.
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 66 bytes
lambda l:[(l[~-i//4]+l[-~i//4])/2for i in[1,n:=len(l),~n-n,~n,-3]]
Try it online!
Input and output are in ascending order.
$endgroup$
Python 3.8 (pre-release), 66 bytes
lambda l:[(l[~-i//4]+l[-~i//4])/2for i in[1,n:=len(l),~n-n,~n,-3]]
Try it online!
Input and output are in ascending order.
answered 2 hours ago
xnorxnor
97.1k19 gold badges200 silver badges460 bronze badges
97.1k19 gold badges200 silver badges460 bronze badges
add a comment |
add a comment |
$begingroup$
R, 7 bytes
fivenum
Try it online!
Obvious cheeky answer. ;-)
Interestingly, fivenum(x)
is not equivalent to summary(x)
even when x
is numeric, as the quantiles are computed differently: fivenum
averages at discontinuities, whereas summary
interpolates. You can force summary
to behave like fivenum
with the option quantile.type=2
, but this is still longer than
R, 28 bytes
quantile(scan(),(0:4)/4,t=2)
Try it online!
which computes the quantiles of order 0 (min), 0.25 (Q1), 0.5 (median), 0.75 (Q3) and 1 (max). The t=2
specifies how quantiles are defined: there are 9 possible types, and the challenge definition corresponds to type 2. Note that the source code of the fivenum
built-in is very different (and much longer), for reasons I do not understand.
$endgroup$
add a comment |
$begingroup$
R, 7 bytes
fivenum
Try it online!
Obvious cheeky answer. ;-)
Interestingly, fivenum(x)
is not equivalent to summary(x)
even when x
is numeric, as the quantiles are computed differently: fivenum
averages at discontinuities, whereas summary
interpolates. You can force summary
to behave like fivenum
with the option quantile.type=2
, but this is still longer than
R, 28 bytes
quantile(scan(),(0:4)/4,t=2)
Try it online!
which computes the quantiles of order 0 (min), 0.25 (Q1), 0.5 (median), 0.75 (Q3) and 1 (max). The t=2
specifies how quantiles are defined: there are 9 possible types, and the challenge definition corresponds to type 2. Note that the source code of the fivenum
built-in is very different (and much longer), for reasons I do not understand.
$endgroup$
add a comment |
$begingroup$
R, 7 bytes
fivenum
Try it online!
Obvious cheeky answer. ;-)
Interestingly, fivenum(x)
is not equivalent to summary(x)
even when x
is numeric, as the quantiles are computed differently: fivenum
averages at discontinuities, whereas summary
interpolates. You can force summary
to behave like fivenum
with the option quantile.type=2
, but this is still longer than
R, 28 bytes
quantile(scan(),(0:4)/4,t=2)
Try it online!
which computes the quantiles of order 0 (min), 0.25 (Q1), 0.5 (median), 0.75 (Q3) and 1 (max). The t=2
specifies how quantiles are defined: there are 9 possible types, and the challenge definition corresponds to type 2. Note that the source code of the fivenum
built-in is very different (and much longer), for reasons I do not understand.
$endgroup$
R, 7 bytes
fivenum
Try it online!
Obvious cheeky answer. ;-)
Interestingly, fivenum(x)
is not equivalent to summary(x)
even when x
is numeric, as the quantiles are computed differently: fivenum
averages at discontinuities, whereas summary
interpolates. You can force summary
to behave like fivenum
with the option quantile.type=2
, but this is still longer than
R, 28 bytes
quantile(scan(),(0:4)/4,t=2)
Try it online!
which computes the quantiles of order 0 (min), 0.25 (Q1), 0.5 (median), 0.75 (Q3) and 1 (max). The t=2
specifies how quantiles are defined: there are 9 possible types, and the challenge definition corresponds to type 2. Note that the source code of the fivenum
built-in is very different (and much longer), for reasons I do not understand.
answered 22 mins ago
Robin RyderRobin Ryder
2,4714 silver badges23 bronze badges
2,4714 silver badges23 bronze badges
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
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%2fcodegolf.stackexchange.com%2fquestions%2f188078%2ffivenum-and-a-little-bit%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
$begingroup$
Suggested test case:
[1 3 3 4]
; and maybe[1 3 3 3 4]
$endgroup$
– Luis Mendo
7 hours ago
$begingroup$
I changed the wording a little bit to clarify what the Q1 and Q3 halves are if the input has an odd length. Feel free to edit if this isn't accurate.
$endgroup$
– Erik the Outgolfer
6 hours ago
$begingroup$
@EriktheOutgolfer much better, thank you. I wasn't happy about it in the Sandbox but figured it was good enough to post.
$endgroup$
– Giuseppe
5 hours ago