| Bytes | Lang | Time | Link |
|---|---|---|---|
| 038 | Uiua | 240602T022935Z | Joao-3 |
| 093 | R | 231003T090733Z | Evargalo |
| 162 | Scala | 231002T233857Z | 138 Aspe |
| 010 | Brachylog | 231002T231750Z | DLosc |
| 053 | Curry PAKCS | 220408T025138Z | alephalp |
| 005 | 05AB1E | 220314T074245Z | Kevin Cr |
| 004 | Vyxal | 220314T042307Z | emanresu |
| 006 | Jelly | 220313T180215Z | Jonathan |
| 278 | C++ gcc | 220317T050818Z | Spectral |
| 088 | Python 3 | 220313T185352Z | solid.py |
| 053 | Ruby | 220314T090202Z | G B |
| 073 | PARI/GP | 220314T061926Z | zoomlogo |
| 090 | JavaScript ES6 | 220313T211111Z | Arnauld |
| 035 | BQN | 220313T181739Z | ovs |
| 059 | Factor + grouping.extras math.combinatorics | 220313T202504Z | chunes |
| 069 | Haskell | 220313T201821Z | AZTECCO |
| 030 | J | 220313T200528Z | Jonah |
Uiua, 38 bytes
▽⊸≡(/×≡/≠◫2)∧(☇1⊞⍜⊙↻⊂¤⊙(⇡+1⊡1△.))⊙[[]]
You can optionally add ◴ deduplicate to generate the solution without duplicates.
R, 93 bytes
\(v,m=permute::allPerms(v))for(i in 1:nrow(m)){l=v[m[i,]];if(all(l!=lag(l),na.rm=T))print(l)}
Prints in console, including duplicates:
v=c(0,-2,1,0) ; a(v)
[1] 0 -2 0 1
[1] 0 1 -2 0
[1] 0 1 0 -2
[1] -2 0 1 0
[1] -2 0 1 0
[1] 1 0 -2 0
[1] 1 0 -2 0
[1] 0 -2 0 1
[1] 0 -2 1 0
[1] 0 1 0 -2
[1] 0 1 -2 0
Scala, 162 bytes
Port of @G B's Ruby answer in Scala.
Golfed version. Try it online!
def u(l:List[Int])=l.permutations.filter(i).toList
def i(l:List[Int]):Boolean=l match{case Nil=>true case _::Nil=>true case x::y::xs=>if(x==y)false else i(y::xs)}
Ungolfed version. Try it online!
object Main {
def uniquePermutations(list: List[Int]): List[List[Int]] = {
list.permutations.filter(isUniqueConsecutive).toList
}
def isUniqueConsecutive(list: List[Int]): Boolean = {
list match {
case Nil => true
case _::Nil => true
case x::y::xs => if (x == y) false else isUniqueConsecutive(y::xs)
}
}
def main(args: Array[String]): Unit = {
println(uniquePermutations(List(0, 1, 1, 0)))
println(uniquePermutations(List(1, 1, 1)))
}
}
Brachylog, 10 bytes
p.{s₂=}ᶜ0∧
A generator predicate that takes a list through its input parameter and produces permutations (including duplicates) through its output parameter. Try it online!
Explanation
p.{s₂=}ᶜ0∧
p Get a permutation of the input list
. This is the output
{ }ᶜ Count the number of ways to:
s₂ Get a length-2 sublist
= whose elements are equal
0 Assert that the result is 0
∧ Don't try to unify 0 with the output
Curry (PAKCS), 53 bytes
[]#s=s
(x++a:y)#s|s==[]||a/=head s=(x++y)#(a:s)
(#[])
Curry supports non-deterministic operations which can return different values for the same input. This is a non-deterministic function whose different return values are the all complex permutations (including duplicates).
05AB1E, 5 bytes
œʒ¥ĀP
Includes duplicated items.
Try it online or verify all test cases.
Explanation:
œ # Get all permutations of the (implicit) input-list
ʒ # Filter it by:
¥ # Get the deltas/forward-differences
Ā # Check for each whether it's NOT 0
P # Product: check if all of them are truthy
# (after which the result is output implicitly)
¥Ā could alternatively be üÊ for the same byte-count:
ü # For each overlapping pair:
Ê # Check that they are not equal
Jelly, 6 bytes
Œ!IẠ$Ƈ
How?
Œ!IẠ$Ƈ - Link: list of numbers, A
Œ! - all permutations of A
Ƈ - filter keep those for which:
$ - last two links as a monad:
I - forward differences
Ạ - all non-zero?
C++ (gcc), 310 301 283 278 bytes
EDIT: -22 bytes thanks to ceilingcat and -1 byte from the label statement.
#include"bits/stdc++.h"
#define c std::cout<<
#define Q&a[0],&*end(a))
#define T;for(j=0;j<a.size()
int j,k;l(std::deque<int>a){auto p=[&]{c"["T;)c","+!j<<a[j++];c"]";};p();c"->";for(std::sort(Q;std::next_permutation(Q;){T-1;)if(a[j]==a[++j])goto e;c","+1/++k;p();e:;}k||c"[]";}
Python 3, 88 bytes
Thanks to user loopy walt for -7 bytes and user pxeger for -1 byte.
Returns a set of non-duplicate tuples. For the nothing case an empty set() is returned.
lambda l:{p for p in permutations(l)if p[:len([*groupby(p)])]==p}
from itertools import*
Python 3.10, 95 bytes
Thanks to user LeopardShark for -1 byte.
lambda l:{p for p in permutations(l)if all(i!=j for i,j in pairwise(p))}
from itertools import*
PARI/GP, 73 bytes
f(x)=r=[];forperm(vecsort(x),p,prod(i=2,#p,p[i]-p[i-1])&&r=concat(r,p));r
After a lot of head scratching and asking @alephalpha in chat, here it is.
JavaScript (ES6), 90 bytes
Returns a set of strings.
f=(a,p=[],s=new Set)=>a.map((v,i)=>v==p[0]||f(a.filter(_=>i--),[v,...p],s))+a?s:s.add(p+a)
Factor + grouping.extras math.combinatorics, 59 bytes
[ [ [ = ] 2clump-map vnone? ] filter-permutations members ]
[ ... ] filter-permutationsSelect the permutations of the input for which[ ... ]returns true.[ = ] 2clump-mapMap=to every two elements of a sequence with overlapping. e.g.{ 1 0 1 1 2 } [ = ] 2clump-map->{ f f t f }vnone?Returns true only if every element of a sequence isf.membersGet the unique elements of a sequence.
Haskell, 69 bytes
import Data.List
f=(filter$and.(zipWith(/=)<*>tail)).nub.permutations