Skip to main content
Commonmark migration
Source Link

#C++, 79 76 64 bytes

C++, 79 76 64 bytes

[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);}

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use XOR swap, which saves us having to include <algorithm> - or <utility> for std::swap).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo auto f=[](auto a,auto b){while(a<--b)a%2?++a:(*a^=*b,*b^=*a,*a^=*b);};

Demo

auto f=[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

Non-competitive answer

##Non-competitive answer TheThe natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}

#C++, 79 76 64 bytes

[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);}

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use XOR swap, which saves us having to include <algorithm> - or <utility> for std::swap).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo auto f=[](auto a,auto b){while(a<--b)a%2?++a:(*a^=*b,*b^=*a,*a^=*b);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

##Non-competitive answer The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}

C++, 79 76 64 bytes

[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);}

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use XOR swap, which saves us having to include <algorithm> - or <utility> for std::swap).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

Demo

auto f=[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

Non-competitive answer

The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}
-12 bytes by using XOR swap
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

#C++, 79 7676 64 bytes

#include<utility>
[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a(*a^=*b,*b^=*a,*a^=*b);}

That's 18 bytes for the include line, and 58 for the function itself.

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use std::swapXOR swap, as the extrawhich saves us having to include *<algorithm> are a net benefit compared to the longer name and include - or <algorithm><utility> versusfor <utility>std::swap).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo #includeauto f=[](auto a,auto b){while(a<--b)a%2?++a:(*a^=*b,*b^=*a,*a^=*b);};

auto f=[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

##Non-competitive answer The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}

#C++, 79 76 bytes

#include<utility>
[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);}

That's 18 bytes for the include line, and 58 for the function itself.

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use std::swap, as the extra * are a net benefit compared to the longer name and include - <algorithm> versus <utility>).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo #include

auto f=[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

##Non-competitive answer The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}

#C++, 79 76 64 bytes

[](auto a,auto b){while(a<--b)*a%2?*++a:(*a^=*b,*b^=*a,*a^=*b);}

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use XOR swap, which saves us having to include <algorithm> - or <utility> for std::swap).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo auto f=[](auto a,auto b){while(a<--b)a%2?++a:(*a^=*b,*b^=*a,*a^=*b);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

##Non-competitive answer The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}
Add the non-answer
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

#C++, 79 76 bytes

#include<utility>
[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);}

That's 18 bytes for the include line, and 58 for the function itself.

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use std::swap, as the extra * are a net benefit compared to the longer name and include - <algorithm> versus <utility>).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo #include

auto f=[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

##Non-competitive answer The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}

#C++, 79 76 bytes

#include<utility>
[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);}

That's 18 bytes for the include line, and 58 for the function itself.

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use std::swap, as the extra * are a net benefit compared to the longer name and include - <algorithm> versus <utility>).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo #include

auto f=[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

#C++, 79 76 bytes

#include<utility>
[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);}

That's 18 bytes for the include line, and 58 for the function itself.

This function accepts a pair of iterators (which must be random access iterators), and steadily moves them towards each other. When a points to an odd number, it is advanced. Otherwise, a points to an even number; b is decremented, and iter_swap'ed with a. (We use std::swap, as the extra * are a net benefit compared to the longer name and include - <algorithm> versus <utility>).

There are unnecessary swaps when b points to an even number, but we're golfing, not squeezing efficiency!

##Demo #include

auto f=[](auto a,auto b){while(a<--b)std::swap(*b,*a%2?*++a:*a);};

#include <array>
#include <iostream>
int main()
{
    auto a = std::array{ 3,2,2,5,2,1,2 };

    f(a.begin(),a.end());

    for (auto i: a)
        std::cout << i << " ";
    std::cout << std::endl;
}

##Non-competitive answer The natural C++ method is std::partition, but that comes out at 83 bytes:

#include<algorithm>
[](auto a,auto b){std::partition(a,b,[](auto x){return x&1;});}
-3 bytes; thanks @PeterCordes
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41
Loading
Shave a byte (thanks @O.O.Balance) but count the #include's newline
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41
Loading
Added demo
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41
Loading
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41
Loading