Smoothing a pair of arrays
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Input
Two lists with binary values (list_a, list_b)
of equal length=>3.
Output
Two lists with binary values (smoothed_a, smoothed_b)
with minimal Forward Difference while keeping the same sum before and after processing sum(list_a) + sum(list_b) == sum(smoothed_a) + sum(smoothed_b)
.
Forward Difference being the absolute difference between items. For example:
[0, 1, 0]
would besum(abs([1, -1])) => 2
[1, 1, 1]
would besum(abs([0, 0])) => 0
Illustrative test cases
Formatted as (list_a, list_b) => (smoothed_a, smoothed_b)
([0, 0, 1, 0, 0],
[1, 1, 0, 1, 1])
=>
([0, 0, 0, 0, 0],
[1, 1, 1, 1, 1])
The sum of both arrays pairs is 5. The forward difference was minimized to 0.
([1, 0, 1, 0, 1],
[0, 0, 0, 1, 0])
=>
([0, 1, 1, 1, 1],
[0, 0, 0, 0, 0])
The sum of both arrays pairs is 4. The forward difference was minimized to 1.
Test cases
Formatted as (list_a, list_b): forward_diff => (smoothed_a, smoothed_b): min_diff
where (smoothed_a, smoothed_b)
are potential solutions.
([0, 0, 1],
[0, 1, 0]): 3
([0, 0, 0],
[0, 1, 1]): 1
([1, 1, 1],
[0, 0, 0]): 0
([1, 1, 1],
[0, 0, 0]): 0
([0, 1, 0],
[1, 0, 1]): 4
([0, 0, 0],
[1, 1, 1]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 1, 1],
[0, 0, 0, 1]): 3
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 0, 1],
[0, 0, 0, 1]): 2
([1, 1, 1, 0],
[0, 0, 0, 0]): 1
([1, 1, 0, 1],
[1, 0, 1, 0]): 3
([1, 1, 1, 1],
[1, 0, 0, 0]): 1
code-golf array-manipulation
New contributor
add a comment |Â
up vote
2
down vote
favorite
Input
Two lists with binary values (list_a, list_b)
of equal length=>3.
Output
Two lists with binary values (smoothed_a, smoothed_b)
with minimal Forward Difference while keeping the same sum before and after processing sum(list_a) + sum(list_b) == sum(smoothed_a) + sum(smoothed_b)
.
Forward Difference being the absolute difference between items. For example:
[0, 1, 0]
would besum(abs([1, -1])) => 2
[1, 1, 1]
would besum(abs([0, 0])) => 0
Illustrative test cases
Formatted as (list_a, list_b) => (smoothed_a, smoothed_b)
([0, 0, 1, 0, 0],
[1, 1, 0, 1, 1])
=>
([0, 0, 0, 0, 0],
[1, 1, 1, 1, 1])
The sum of both arrays pairs is 5. The forward difference was minimized to 0.
([1, 0, 1, 0, 1],
[0, 0, 0, 1, 0])
=>
([0, 1, 1, 1, 1],
[0, 0, 0, 0, 0])
The sum of both arrays pairs is 4. The forward difference was minimized to 1.
Test cases
Formatted as (list_a, list_b): forward_diff => (smoothed_a, smoothed_b): min_diff
where (smoothed_a, smoothed_b)
are potential solutions.
([0, 0, 1],
[0, 1, 0]): 3
([0, 0, 0],
[0, 1, 1]): 1
([1, 1, 1],
[0, 0, 0]): 0
([1, 1, 1],
[0, 0, 0]): 0
([0, 1, 0],
[1, 0, 1]): 4
([0, 0, 0],
[1, 1, 1]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 1, 1],
[0, 0, 0, 1]): 3
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 0, 1],
[0, 0, 0, 1]): 2
([1, 1, 1, 0],
[0, 0, 0, 0]): 1
([1, 1, 0, 1],
[1, 0, 1, 0]): 3
([1, 1, 1, 1],
[1, 0, 0, 0]): 1
code-golf array-manipulation
New contributor
So, if I understand right, "forward difference" means $sum_i | x_i - x_i+1 |$?
â Rogem
30 mins ago
@Rogem that is correct
â Seanny123
13 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Input
Two lists with binary values (list_a, list_b)
of equal length=>3.
Output
Two lists with binary values (smoothed_a, smoothed_b)
with minimal Forward Difference while keeping the same sum before and after processing sum(list_a) + sum(list_b) == sum(smoothed_a) + sum(smoothed_b)
.
Forward Difference being the absolute difference between items. For example:
[0, 1, 0]
would besum(abs([1, -1])) => 2
[1, 1, 1]
would besum(abs([0, 0])) => 0
Illustrative test cases
Formatted as (list_a, list_b) => (smoothed_a, smoothed_b)
([0, 0, 1, 0, 0],
[1, 1, 0, 1, 1])
=>
([0, 0, 0, 0, 0],
[1, 1, 1, 1, 1])
The sum of both arrays pairs is 5. The forward difference was minimized to 0.
([1, 0, 1, 0, 1],
[0, 0, 0, 1, 0])
=>
([0, 1, 1, 1, 1],
[0, 0, 0, 0, 0])
The sum of both arrays pairs is 4. The forward difference was minimized to 1.
Test cases
Formatted as (list_a, list_b): forward_diff => (smoothed_a, smoothed_b): min_diff
where (smoothed_a, smoothed_b)
are potential solutions.
([0, 0, 1],
[0, 1, 0]): 3
([0, 0, 0],
[0, 1, 1]): 1
([1, 1, 1],
[0, 0, 0]): 0
([1, 1, 1],
[0, 0, 0]): 0
([0, 1, 0],
[1, 0, 1]): 4
([0, 0, 0],
[1, 1, 1]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 1, 1],
[0, 0, 0, 1]): 3
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 0, 1],
[0, 0, 0, 1]): 2
([1, 1, 1, 0],
[0, 0, 0, 0]): 1
([1, 1, 0, 1],
[1, 0, 1, 0]): 3
([1, 1, 1, 1],
[1, 0, 0, 0]): 1
code-golf array-manipulation
New contributor
Input
Two lists with binary values (list_a, list_b)
of equal length=>3.
Output
Two lists with binary values (smoothed_a, smoothed_b)
with minimal Forward Difference while keeping the same sum before and after processing sum(list_a) + sum(list_b) == sum(smoothed_a) + sum(smoothed_b)
.
Forward Difference being the absolute difference between items. For example:
[0, 1, 0]
would besum(abs([1, -1])) => 2
[1, 1, 1]
would besum(abs([0, 0])) => 0
Illustrative test cases
Formatted as (list_a, list_b) => (smoothed_a, smoothed_b)
([0, 0, 1, 0, 0],
[1, 1, 0, 1, 1])
=>
([0, 0, 0, 0, 0],
[1, 1, 1, 1, 1])
The sum of both arrays pairs is 5. The forward difference was minimized to 0.
([1, 0, 1, 0, 1],
[0, 0, 0, 1, 0])
=>
([0, 1, 1, 1, 1],
[0, 0, 0, 0, 0])
The sum of both arrays pairs is 4. The forward difference was minimized to 1.
Test cases
Formatted as (list_a, list_b): forward_diff => (smoothed_a, smoothed_b): min_diff
where (smoothed_a, smoothed_b)
are potential solutions.
([0, 0, 1],
[0, 1, 0]): 3
([0, 0, 0],
[0, 1, 1]): 1
([1, 1, 1],
[0, 0, 0]): 0
([1, 1, 1],
[0, 0, 0]): 0
([0, 1, 0],
[1, 0, 1]): 4
([0, 0, 0],
[1, 1, 1]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 1, 1],
[0, 0, 0, 1]): 3
([1, 1, 1, 1],
[0, 0, 0, 0]): 0
([1, 0, 0, 1],
[0, 0, 0, 1]): 2
([1, 1, 1, 0],
[0, 0, 0, 0]): 1
([1, 1, 0, 1],
[1, 0, 1, 0]): 3
([1, 1, 1, 1],
[1, 0, 0, 0]): 1
code-golf array-manipulation
code-golf array-manipulation
New contributor
New contributor
edited 1 hour ago
New contributor
asked 2 hours ago
Seanny123
1164
1164
New contributor
New contributor
So, if I understand right, "forward difference" means $sum_i | x_i - x_i+1 |$?
â Rogem
30 mins ago
@Rogem that is correct
â Seanny123
13 mins ago
add a comment |Â
So, if I understand right, "forward difference" means $sum_i | x_i - x_i+1 |$?
â Rogem
30 mins ago
@Rogem that is correct
â Seanny123
13 mins ago
So, if I understand right, "forward difference" means $sum_i | x_i - x_i+1 |$?
â Rogem
30 mins ago
So, if I understand right, "forward difference" means $sum_i | x_i - x_i+1 |$?
â Rogem
30 mins ago
@Rogem that is correct
â Seanny123
13 mins ago
@Rogem that is correct
â Seanny123
13 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
Jelly, 3 bytes
Fá¹¢á¹Â
Try it online!
Explanation:
Flatten the list
á¹¢ort the flattened list
á¹Âold the sorted flatten list in the shape of the input.
add a comment |Â
up vote
1
down vote
Husk, 3 bytes
ýOã
Try it online!
ýOã â Full program. Takes a list of two lists from the first CLA, outputs to STDOUT.
ã â Flatten (i.e. concatenate the contents of the 2 lists together).
O â Sort.
ý â Divide list in two halves (i.e. partition it into two equal-length sublists).
add a comment |Â
up vote
0
down vote
Python 2, 51 bytes
def f(a,b):s=sorted(a+b);l=len(a);print s[:l],s[l:]
Try it online!
Function that prints to STDOUT.
add a comment |Â
up vote
0
down vote
C gcc 32bit, 37 bytes
f(a,n)qsort(a,n+n,4,"YXZx8Bx00+x02QQQxC3");
struct
int a[5], b[5];
test;
int main()
test.a[0] = test.a[2] = test.b[1] = 1;
f(&test, 5);
printf ("%d%d%d%d%dn", test.a[0], test.a[1], test.a[2], test.a[3], test.a[4]);
printf ("%d%d%d%d%dn", test.b[0], test.b[1], test.b[2], test.b[3], test.b[4]);
return 0;
Written non-ASCII to x??
so se won't eat
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Jelly, 3 bytes
Fá¹¢á¹Â
Try it online!
Explanation:
Flatten the list
á¹¢ort the flattened list
á¹Âold the sorted flatten list in the shape of the input.
add a comment |Â
up vote
2
down vote
Jelly, 3 bytes
Fá¹¢á¹Â
Try it online!
Explanation:
Flatten the list
á¹¢ort the flattened list
á¹Âold the sorted flatten list in the shape of the input.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Jelly, 3 bytes
Fá¹¢á¹Â
Try it online!
Explanation:
Flatten the list
á¹¢ort the flattened list
á¹Âold the sorted flatten list in the shape of the input.
Jelly, 3 bytes
Fá¹¢á¹Â
Try it online!
Explanation:
Flatten the list
á¹¢ort the flattened list
á¹Âold the sorted flatten list in the shape of the input.
answered 49 mins ago
DJMcSpookemâ¦
40.3k11142307
40.3k11142307
add a comment |Â
add a comment |Â
up vote
1
down vote
Husk, 3 bytes
ýOã
Try it online!
ýOã â Full program. Takes a list of two lists from the first CLA, outputs to STDOUT.
ã â Flatten (i.e. concatenate the contents of the 2 lists together).
O â Sort.
ý â Divide list in two halves (i.e. partition it into two equal-length sublists).
add a comment |Â
up vote
1
down vote
Husk, 3 bytes
ýOã
Try it online!
ýOã â Full program. Takes a list of two lists from the first CLA, outputs to STDOUT.
ã â Flatten (i.e. concatenate the contents of the 2 lists together).
O â Sort.
ý â Divide list in two halves (i.e. partition it into two equal-length sublists).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Husk, 3 bytes
ýOã
Try it online!
ýOã â Full program. Takes a list of two lists from the first CLA, outputs to STDOUT.
ã â Flatten (i.e. concatenate the contents of the 2 lists together).
O â Sort.
ý â Divide list in two halves (i.e. partition it into two equal-length sublists).
Husk, 3 bytes
ýOã
Try it online!
ýOã â Full program. Takes a list of two lists from the first CLA, outputs to STDOUT.
ã â Flatten (i.e. concatenate the contents of the 2 lists together).
O â Sort.
ý â Divide list in two halves (i.e. partition it into two equal-length sublists).
answered 13 mins ago
Mr. Xcoder
31k758195
31k758195
add a comment |Â
add a comment |Â
up vote
0
down vote
Python 2, 51 bytes
def f(a,b):s=sorted(a+b);l=len(a);print s[:l],s[l:]
Try it online!
Function that prints to STDOUT.
add a comment |Â
up vote
0
down vote
Python 2, 51 bytes
def f(a,b):s=sorted(a+b);l=len(a);print s[:l],s[l:]
Try it online!
Function that prints to STDOUT.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2, 51 bytes
def f(a,b):s=sorted(a+b);l=len(a);print s[:l],s[l:]
Try it online!
Function that prints to STDOUT.
Python 2, 51 bytes
def f(a,b):s=sorted(a+b);l=len(a);print s[:l],s[l:]
Try it online!
Function that prints to STDOUT.
answered 34 mins ago
Erik the Outgolfer
29.9k42899
29.9k42899
add a comment |Â
add a comment |Â
up vote
0
down vote
C gcc 32bit, 37 bytes
f(a,n)qsort(a,n+n,4,"YXZx8Bx00+x02QQQxC3");
struct
int a[5], b[5];
test;
int main()
test.a[0] = test.a[2] = test.b[1] = 1;
f(&test, 5);
printf ("%d%d%d%d%dn", test.a[0], test.a[1], test.a[2], test.a[3], test.a[4]);
printf ("%d%d%d%d%dn", test.b[0], test.b[1], test.b[2], test.b[3], test.b[4]);
return 0;
Written non-ASCII to x??
so se won't eat
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
add a comment |Â
up vote
0
down vote
C gcc 32bit, 37 bytes
f(a,n)qsort(a,n+n,4,"YXZx8Bx00+x02QQQxC3");
struct
int a[5], b[5];
test;
int main()
test.a[0] = test.a[2] = test.b[1] = 1;
f(&test, 5);
printf ("%d%d%d%d%dn", test.a[0], test.a[1], test.a[2], test.a[3], test.a[4]);
printf ("%d%d%d%d%dn", test.b[0], test.b[1], test.b[2], test.b[3], test.b[4]);
return 0;
Written non-ASCII to x??
so se won't eat
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
C gcc 32bit, 37 bytes
f(a,n)qsort(a,n+n,4,"YXZx8Bx00+x02QQQxC3");
struct
int a[5], b[5];
test;
int main()
test.a[0] = test.a[2] = test.b[1] = 1;
f(&test, 5);
printf ("%d%d%d%d%dn", test.a[0], test.a[1], test.a[2], test.a[3], test.a[4]);
printf ("%d%d%d%d%dn", test.b[0], test.b[1], test.b[2], test.b[3], test.b[4]);
return 0;
Written non-ASCII to x??
so se won't eat
C gcc 32bit, 37 bytes
f(a,n)qsort(a,n+n,4,"YXZx8Bx00+x02QQQxC3");
struct
int a[5], b[5];
test;
int main()
test.a[0] = test.a[2] = test.b[1] = 1;
f(&test, 5);
printf ("%d%d%d%d%dn", test.a[0], test.a[1], test.a[2], test.a[3], test.a[4]);
printf ("%d%d%d%d%dn", test.b[0], test.b[1], test.b[2], test.b[3], test.b[4]);
return 0;
Written non-ASCII to x??
so se won't eat
answered 26 mins ago
l4m2
3,8681431
3,8681431
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
add a comment |Â
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
Should probably mention that the input should be provided as a pointer to the first element of the list of lists.
â Rogem
20 mins ago
add a comment |Â
Seanny123 is a new contributor. Be nice, and check out our Code of Conduct.
Seanny123 is a new contributor. Be nice, and check out our Code of Conduct.
Seanny123 is a new contributor. Be nice, and check out our Code of Conduct.
Seanny123 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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174695%2fsmoothing-a-pair-of-arrays%23new-answer', 'question_page');
);
Post as a guest
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
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
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
So, if I understand right, "forward difference" means $sum_i | x_i - x_i+1 |$?
â Rogem
30 mins ago
@Rogem that is correct
â Seanny123
13 mins ago