Smoothing a pair of arrays

The name of the pictureThe name of the pictureThe name of the pictureClash 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 be sum(abs([1, -1])) => 2


  • [1, 1, 1] would be sum(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









share|improve this question









New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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














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 be sum(abs([1, -1])) => 2


  • [1, 1, 1] would be sum(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









share|improve this question









New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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












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 be sum(abs([1, -1])) => 2


  • [1, 1, 1] would be sum(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









share|improve this question









New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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 be sum(abs([1, -1])) => 2


  • [1, 1, 1] would be sum(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






share|improve this question









New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago





















New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Seanny123

1164




1164




New contributor




Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Seanny123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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










  • @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










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.





share|improve this answer



























    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).





    share|improve this answer



























      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.






      share|improve this answer



























        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






        share|improve this answer




















        • 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










        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: "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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        Seanny123 is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        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






























        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.





        share|improve this answer
























          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.





          share|improve this answer






















            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.





            share|improve this answer













            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.






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 49 mins ago









            DJMcSpookem♦

            40.3k11142307




            40.3k11142307




















                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).





                share|improve this answer
























                  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).





                  share|improve this answer






















                    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).





                    share|improve this answer













                    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).






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 13 mins ago









                    Mr. Xcoder

                    31k758195




                    31k758195




















                        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.






                        share|improve this answer
























                          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.






                          share|improve this answer






















                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 34 mins ago









                            Erik the Outgolfer

                            29.9k42899




                            29.9k42899




















                                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






                                share|improve this answer




















                                • 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














                                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






                                share|improve this answer




















                                • 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












                                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






                                share|improve this answer












                                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







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                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
















                                • 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










                                Seanny123 is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                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.













                                 


                                draft saved


                                draft discarded














                                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













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                Installing NextGIS Connect into QGIS 3?

                                One-line joke