| Bytes | Lang | Time | Link |
|---|---|---|---|
| 044 | JavaScript Node.js | 250808T132234Z | Fhuvi |
| 052 | Nim | 250727T050138Z | janAkali |
| 027 | JavaScript V8 | 250727T000407Z | Steve Be |
| 009 | APLDyalog Unicode | 250726T215120Z | Mat_rdv |
| 006 | Vyxal 3 | 250726T085347Z | Themooni |
| 003 | Jelly | 220821T173650Z | Unrelate |
| 003 | Nekomata + e | 250725T055920Z | alephalp |
| 004 | Uiua | 241123T194849Z | ErikDaPa |
| 087 | Regenerate c | 250130T223323Z | DLosc |
| 224 | Regenerate | 250128T171007Z | Unrelate |
| 032 | Zsh | 241119T234634Z | roblogic |
| 025 | Bash +coreutils | 241120T000610Z | roblogic |
| 006 | Japt | 180131T104208Z | Shaggy |
| 022 | Arturo | 230703T203033Z | chunes |
| 004 | Jelly | 240603T005958Z | DLosc |
| 058 | Scala 3 | 231125T083203Z | 138 Aspe |
| 022 | TIBASIC | 231122T153413Z | Youserna |
| 028 | GNU sed E | 231122T173824Z | Philippo |
| 022 | Ly | 231122T090706Z | cnamejj |
| 006 | Uiua | 231122T004155Z | chunes |
| 004 | Thunno 2 | 230721T060524Z | The Thon |
| 004 | Vyxal | 230704T130920Z | lyxal |
| 008 | MATL | 160412T090231Z | Luis Men |
| 037 | Haskell | 160415T202025Z | xnor |
| 017 | Regex Perl / PCRE / Java / Boost / Pythonregex / Ruby | 220821T090530Z | Deadcode |
| 040 | Prolog SWI | 220903T160650Z | naffetS |
| 030 | Ruby | 220903T172657Z | naffetS |
| 004 | Brachylog | 190813T075022Z | Unrelate |
| 083 | Desmos | 220824T052704Z | Aiden Ch |
| 010 | K ngn/k | 220821T110856Z | ngn |
| 004 | Vyxal | 220821T103235Z | Deadcode |
| 054 | sed | 220821T195619Z | Jiří |
| 005 | Jelly | 220821T080925Z | DLosc |
| 086 | brev | 220821T081050Z | Sandra |
| 005 | BQN | 220821T070937Z | DLosc |
| 158 | Oracle SQL | 190815T160920Z | Dr Y Wit |
| 017 | APL Dyalog | 180131T135519Z | Uriel |
| 018 | Wolfram Language Mathematica | 180131T041422Z | Misha La |
| 020 | Perl 5 | 160412T155723Z | msh210 |
| 085 | Lua | 160412T092448Z | Katenkyo |
| 053 | Racket | 160416T002318Z | cat |
| 071 | C | 160413T085051Z | mIllIbyt |
| 017 | Convex | 160414T140428Z | GamrCorp |
| 119 | C# | 160414T130016Z | auhmaan |
| 038 | R | 160412T005116Z | Alex A. |
| 156 | Java | 160412T062006Z | JamesENL |
| 055 | Python | 160411T234153Z | orlp |
| 111 | C# | 160413T143442Z | downrep_ |
| 008 | J | 160413T001115Z | Dennis |
| 022 | Factor | 160412T192817Z | cat |
| 011 | MATL | 160412T013830Z | Suever |
| 004 | 05AB1E | 160412T044309Z | Adnan |
| 017 | Retina | 160412T081641Z | Martin E |
| 005 | Jelly | 160411T234021Z | Dennis |
| 033 | Python 3 | 160412T000313Z | Dennis |
| 026 | Mathematica | 160412T041004Z | feersum |
| 035 | Julia | 160412T022522Z | Dennis |
| 005 | Pyth | 160412T010809Z | izzyg |
| 023 | Ruby | 160412T005339Z | Value In |
| 044 | Haskell | 160412T000948Z | nimi |
| 069 | JavaScript ES6 | 160411T235237Z | Conor O& |
| 027 | JavaScript ES6 | 160411T235903Z | Neil |
| 007 | Pyth | 160411T231802Z | Maltysen |
JavaScript (Node.js), 44 bytes
I tried to achieve the shortest possible JS solution without using regex.
Takes a list of numbers as parameter
l=>!l.some(c=>a!=c&o[c]==(o[a=c]=1),o=[a=0])
We verify if there aren't any elements that are "out of place".
a is the previous element, and o is the memory of all previously encountered numbers. If it contains a value 1 at the corresponding index, it means we've aready seen this number.
The "out of place" numbers are those that are different from their previous element, but have already been seen before.
Nim, 52 bytes
import re
proc(s=""):bool=0<s.find re"(.)(?!\1).*\1"
Explanation:
Regex for two non-consecutive digits using negative look-ahead (?!)
APL(Dyalog Unicode), 9 bytes SBCS
∧/2≤/⍳⍨∘,
Explanation
, Ravel
∘ Bind (preprocess)
∘, turns scalars into 1-element vectors, does nothing otherwise
⍨ Selfie (f⍨ X ←→ X f X)
⍳ Indices of
⍳⍨ vector of indices of the first occurrences;
it's non-decreasing ←→ no older element occurs after a newer one
←→ all elements are grouped in contiguous blocks
/ (n-wise) Reduction
≤
2≤/ checks each pair of consequtive elements for non-decreasing
(an 1-element vector turns into the empty vector ⍬)
/ Reduction
∧ And
∧/ all (∧/⍬ ←→ 1 by definiton)
∧/2≤/ whether a sequence is non-decreasing
∧/2≤/⍳⍨∘, solution
Vyxal 3, 6 bytes
S⎂※⎂L=
S⎂※⎂L=
# bottom of stack has infinite copies of input
S # sort one copy
⎂ ⎂ # to both the top 2 copies:
※ # group by consecutives
L # length of the resulting array
= # are both lengths equal?
💎
Created with the help of Luminespire.
<script type="vyxal3">
S⎂※⎂L=
</script>
<script>
args=[["3"],["51"],["44999911"],["123456789"],["222222222"],["818"],["8884443334"],["4545"],["554553"],["1234567891"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Jelly, \$_4\$ 3 bytes
nÞƑ
Semi-based on several of DLosc's 5-byters.
Ƒ The input is not changed by
Þ sorting its elements by
n the list of, for each element, if it is not equal to that element.
Kind of like cutting the F out of ¹ƙ`F⁼, but also kind of like cutting the `ṢƑ out of iⱮ`ṢƑ. Compared to the previous i@ÞƑ, it simply saves an @ by using lexicographic comparisons with a dyad that doesn't discriminate between its left and right argument instead of numeric comparisons with one that does--the first index of an element is the first position at which its inequality-vector has a zero, so one element has a greater first index than another iff its inequality-vector is all ones up to the position where the other's has its first zero.
It's been almost 3 years, and ðƙF⁼ still makes me so sad...!
Nekomata + -e, 3 bytes
Y$ů
Y$ů
Y Run length encoding; returns a list of elements and a list of counts
$ Swap to take the list of elements
ů Check that they are unique
Uiua, 4 bytes
≍⍆.⊛
Explanation:
≍⍆.⊛
⊛ => classify by first index of each unique value
⍆. => duplicate and sort ascending
≍ => exact list?
Regenerate -c, 103 94 87 bytes
((${$2/10}!$~1)($4!)(${$2%10})($0{$3-$4}!(${$6*$7}!1)(${$4*6+5})0{$6-$6/$7*$7-1})){#~1}
Outputs 0 for falsey, 1 for truthy. As of this writing, ATO doesn't have the -c flag yet, so the program there outputs nothing for falsey and a string of digits for truthy. Attempt This Online!
Explanation
Inspired by Unrelated String's answer, I thought of a different way to track whether a digit has been seen already: mapping each digit to a different prime number and storing their product. Then I realized that the numbers only needed to be coprime, which saved even more bytes.
(...){#~1}
Repeat the following a number of times equal to the length of the input:
(${$2/10}!$~1)
Group 2 stores the input number and removes one digit at a time from the right end. If group 2 has been matched before, ${$2/10} matches the previous value int-divided by 10; if not (which is only the case on the first iteration), $~1 matches the input.
($4!)
Group 3 stores the previous digit. It matches the previous value of group 4, or empty string if group 4 has not matched before.
(${$2%10})
Group 4 stores the current digit, i.e. the rightmost digit of the current number in group 2.
($0{$3-$4}!...)
Group 5 starts by testing if the difference between group 3 and group 4 is zero, i.e. if we have two consecutive identical digits. If so, we repeat the nonexistent group 0 zero times; this succeeds, matching empty string, and we proceed to the next iteration. If not, trying to match group 0 (or repeat it a negative number of times) fails, and we proceed to the alternate branch, where we process a new digit:
(${$6*$7}!1)
Group 6 stores the product of coprimes. If groups 6 and 7 have been matched before, ${$6*$7} multiplies them and sets that as the new value of group 6. If not, we use 1 as the initial value.
(${$4*6+5})
Group 7 stores the coprime number representing the current digit. We map the value of group 4, a digit between 1 and 9, to the corresponding number from this arithmetic progression: 11, 17, 23, 29, 35, 41, 47, 53, 59. All of them are prime except 35, which doesn't have any of the others as factors.
0{$6-$6/$7*$7-1}
Now we check whether the current digit has been seen before. If it has, we know that it wasn't just now because the previous digit is different, which means we want the match to fail.
$6/$7*$7 int-divides the product of coprimes in group 6 by the most recent coprime in group 7 and then multiplies it by this coprime again. If the current digit has been seen before, the division is exact, and multiplying gives the original value of group 6 back; otherwise, the division truncates the result, and multiplying gives a number smaller than the original value.
0{$6-...-1} subtracts the above quantity from group 6 and then subtracts 1. This results in -1 if the current digit has been seen before and a nonnegative number if it has not. We then use that number as a repetition count for an arbitrary character. If the number is negative, the regex fails, outputting 0 successful matches. If there are no failures in any iteration, the regex outputs 1 successful match.
Regenerate, 323 224 bytes
((0{#2/10}!0{$~1})(0{#2%10})(0$4!)((0$6!$4)$0{#3}|(0$7!$4)$0{#3-1}|(0$8!$4)$0{#3-2}|(0$9!$4)$0{#3-3}|(0$10!$4)$0{#3-4}|(0$11!$4)$0{#3-5}|(0$12!$4)$0{#3-6}|(0$13!$4)$0{#3-7}|(0$14!$4)$0{#3-8}|(0$15!$4)$0{#3-9})$0{#5-#4}){#~1}
Talk about the wrong tool for the job! Outputs 0 (with a considerable and varying number of leading zeroes) for truthy and nothing for falsy. Also hits the Python interpreter's recursion limit for any input greater than 987, so:
Regenerate, 320 279 221 bytes
((${$2/10}!$~1)(${$2%10})(9$4!)((9$6!$4)$0{$3}|(9$7!$4)$0{$3-1}|(9$8!$4)$0{$3-2}|(9$9!$4)$0{$3-3}|(9$10!$4)$0{$3-4}|(9$11!$4)$0{$3-5}|(9$12!$4)$0{$3-6}|(9$13!$4)$0{$3-7}|(9$14!$4)$0{$3-8}|(9$15!$4)$0{$3-9})$0{#5-#4}){#~1}
Uses much more performant decimal arithmetic at the expense of not having a (nominally) consistent truthy output, instead printing a bunch of prefixes of the input smashed together with a bunch of 9s. Structurally identical otherwise.
Explanation:
( ... ){#~1}
Repeat as many times as there are digits in the input...
(${$2/10}!$~1)
Initialize group 2 to the input, or floor divide it by 10 on subsequent loops.
(${$2%10})
Isolate the last digit of group 2 and save it as group 3.
(9$4!)
Initialize group 4 to an empty string, or prepend a 9 to it on subsequent loops.
( ... | ... ... ... | ... )$0{#5-#4}
Try the following schema, manually instantiated for each digit d from 0 to 9 and corresponding group number g from 6 to 15, and save whichever instance/branch succeeds in group 5; repeat group 0 (nonexistent--fails unless 0 repetitions) by the difference between its length and the length of group 4:
(9$g!$4)$0{$3-d}
Initialize group g to the current value of group 4, or prepend a 9 to it if it already has a value (i.e. this branch has already succeeded). Fail this branch if group 3 is not equal to d.
Zsh, 61 41 32 bytes
for i (${(us::)1})1=${1/$i##};$1
Try it online!
41bytes
61bytes
Returns nothing if truthy/ all together. Error if some digits are not contiguous.
Bash +coreutils, 60 25 bytes
fold -1|uniq|sort|uniq -d
Returns nothing if truthy/contiguous, a number (from the repeated block) if not.
Japt, 9 6 bytes
Takes input as a digit array
òÎÍeUü
òÎÍeUü :Implicit input of digit array U
ò :Partition between elements where
Î : Signs of differences are truthy (not 0)
Í :Sort
e :Is equal to
Uü : Sort & group U
Arturo, 28 22 bytes
$=>[=unique<=squeeze&]
$=>[ ; a function where input is assigned to &
squeeze& ; squeeze the input; e.g. [1 1 2 1 1 1 3 3] -> [1 2 1 3]
<= ; duplicate
unique ; remove duplicates
= ; are they equal?
] ; end function
Jelly, 4 bytes
ĠIỊȦ
Explanation
An independently derived variation on Dennis's 5-byte answer:
ĠIỊȦ
Ġ Group indices 1..N based on their values in the input list
I Get the difference of each pair of adjacent values in those lists
Ị For each difference, 1 if it is <= 1, 0 if it is greater than 1
Ȧ 0 if the nested list contains any 0s, 1 otherwise
Scala 3, 58 bytes
A port of @JamesENL's Java answer in Scala.
Golfed version. Attempt This Online!
x=> !new Regex("(.)(?!\\1).*\\1").findFirstIn(x).isDefined
Ungolfed version. Attempt This Online!
import scala.util.matching.Regex
object Main {
def main(args: Array[String]): Unit = {
val pattern = new Regex("(.)(?!\\1).*\\1")
val testCases = List(
("3", true),
("51", true),
("44999911", true),
("123456789", true),
("222222222222222222222", true),
("818", false),
("8884443334", false),
("4545", false),
("554553", false),
("1234567891", false)
)
testCases.foreach { case (input, expected) =>
val result = !pattern.findFirstIn(input).isDefined
assert(result == expected, s"Test case '$input' failed, expected $expected but got $result")
println(s"Test case '$input' passed")
}
}
}
TI-BASIC, 24 22 bytes
Prompt A
sum(not(∆List(ʟA
SortA(ʟA
Ans=sum(not(∆List(ʟA
Takes input as a list ot integers.
-2 bytes thanks to MarcMush.
GNU sed -E, 32 28 bytes
Thank you, Jiří, for golfing off four bytes!
In the first line, remove all duplicate characters to avoid false positives, then check whether there is still a character twice. Output F for Falsy or T for Truthy:
s/(.)\1*/\1/g
/(.).+\1/cF
cT
Even without -E, it would only be 32 bytes, by far better than the current sed solution.
Ly, 22 bytes
&i[s=[ppl=]pl~[!u;]p]1
This one isn't that all short, but as far as I can tell it works differently from the other programs posted. It deletes all "grouped" digits, then searches the remaining digits for the digit it just deleted. If if finds another one, 0 is printed and the code exits. If the stack is exhausted, the default 1 output is printed.
&i - read all the digits as codepoints
[ ] - loop until the stack is empty
s - save current digit
= - is the next digit the same?
[ ] - yes, then loop deleting that digit
p - delete previous test results
p - delete digit we just checked
l - load the current digit
= - does the next one match?
p - delete loop/iterator var
l - load the current digit again
~ - is there another of those digits?
[ ] - yes, then time to exit...
!ul; - push "0", print and exit
p - print test result variable
1 - push "1" for truthy result
- stack prints on exit
Uiua, 6 bytes
≍⊝.⊜⊢.
≍⊝.⊜⊢.
⊜⊢. # get the first digit in each like contiguous group
. # duplicate
⊝ # deduplicate
≍ # do they match?
Thunno 2, 4 bytes
ñdḞ⁼
Input as a digit list.
Thunno 2, 5 bytes
çµUUạ
Input as an integer.
Explanation
ñdḞ⁼ # Implicit input
ñd # Group identical items together
Ḟ # Flatten the list
⁼ # Equals the input?
çµUUạ # Implicit input
ç # Pair apply on the input:
µU # Connected uniquify
U # Uniquify
ạ # Are they the same?
# Implicit output
Vyxal, 4 bytes
)ġf⁼
Takes input as a list of singleton strings. Happens to be a different algorithm to the existing vyxal answer.
Explained
)ġf⁼
)ġ # Group by the identity function, by order of first appearance.
f # Flatten the list
⁼ # Does that equal the input?
💎
MATL, 8 bytes
t!=tXSP=
The output is an array containing only ones for truthy, or an array containing at least one zero for falsey.
Explanation
Consider the input 22331, which satisfies the condition. Testing if each character equals each other gives the 2D array
1 1 0 0 0
1 1 0 0 0
0 0 1 1 0
0 0 1 1 0
0 0 0 0 1
The final result should be truthy if the rows of that array (with rows considered as atomic) are in decreasing lexicographical order. For comparison, input 22321 gives the array
1 1 0 1 0
1 1 0 1 0
0 0 1 0 0
1 1 0 1 0
0 0 0 0 1
in which the rows are not in order.
t! % Take string input. Duplicate and tranpose
= % Test for equality, element-wise with broadcast: gives a 2D array
% that contains 0 or 1, for all pairs of characters in the input
t % Duplicate
XS % Sort rows (as atomic) in increasing order
P % Flip vertically to obtain decreasing order
= % Test for equality, element-wise
Haskell, 37 bytes
f l=(==)=<<scanl1 min$(<$>l).(==)<$>l
Uses the same approach as Luis Mendo's MATL answer: creates a vector for each entry which indices equal it, and checks that the result is sorted in decreasing order.
(<$>l).(==)<$>l is shorter version of [map(==a)l|a<-l]. The function (<$>l).(==) that takes a to map(==a)l is mapped onto l.
scanl1 min takes the cumulative smallest elements of l, which equals the original only if l is reverse-sorted. (==)=<< checks if the list is indeed invariant under this operation.
A recursive strategy also gave 37 bytes:
f(h:t)=t==[]||f t>(elem h t&&h/=t!!0)
This checks each suffix to see if its first element doesn't appear in the remainder, excusing cases where the first two elements are equal as part of a contiguous block.
Other approaches:
42 bytes
f l=and[all(/=a)t|a:b:t<-scanr(:)[]l,a/=b]
43 bytes
import Data.List
((==)<*>nub).map nub.group
Regex (Perl / PCRE / Java / Boost / Pythonregex / Ruby), 17 bytes
^(?!(.)+\1*+.+\1)
Try it online! - Perl
Try it online! - PCRE
Try it online! - Java
Try it online! - Boost
Try it online! - Python import regex
Try it online! - Ruby
^ # Anchor to start of string
(?! # Assert that the following can't match:
(.)+ # Skip any number of characters (minimum zero), then capture
# the next character in \1
\1*+ # Skip all subsequent occurrences of \1, and lock in that match
# using a possessive quantifier.
.+ # Skip at least one character, or as many as necessary to make the
# following match:
\1 # Match the character captured in \1
)
Regex (Perl / PCRE / Java / Boost / Pythonregex / Ruby), 11 bytes
(.)\1*+.+\1
Returns a match for false, and a non-match for true.
Try it online! - Perl
Try it online! - PCRE
Try it online! - Java
Try it online! - Boost
Try it online! - Python import regex
Try it online! - Ruby
This beats the regex in the previous Java answer, Perl answer, and Ruby answer by 2 bytes.
(.) # \1 = a character anywhere in the string
\1*+ # Skip all subsequent occurrences of \1, and lock in that match using a
# possessive quantifier.
.+ # Skip at least one character, or as many as necessary to make the
# following match:
\1 # Match the character captured in \1
Regex (ECMAScript (or better) / Python / .NET), 13 bytes
(.)(?!\1).+\1
Returns a match for false, and a non-match for true.
Try it online! - ECMAScript
Try it online! - Perl
Try it online! - PCRE
Try it online! - Java
Try it online! - Boost
Try it online! - Python
Try it online! - Python import regex
Try it online! - Ruby
Try it online! - .NET
\$\large\textit{Anonymous functions}\$
Ruby, 21 bytes
->s{/(.)\1*+.+\1/!~s}
JavaScript (ES6), 27 bytes
s=>!/(.)(?!\1).+\1/.test(s)
Included for completeness; already done in Neil's answer.
PowerShell, 29 bytes
!($args-match'(.)(?!\1).+\1')
Julia v0.7+, 30 bytes
s->!occursin(r"(.)\1*+.+\1",s)
Attempt This Online! / Try it online!
Julia v1.3+, 28 bytes
s->count(r"(.)\1*+.+\1",s)<1
-2 bytes compared to v0.7+, thanks to MarcMush
R, 32 bytes
\(s)!grepl('(.)\\1*+.+\\1',s,,1)
Beaten by Alex A.'s non-regex answer by 2 bytes, or by 6 bytes with pajonk's improvement.
Java, 33 bytes
s->!s.matches("(.)+\\1*+.+\\1.*")
PHP, 39 bytes
fn($s)=>!preg_match('/(.)\1*+.+\1/',$s)
\$\large\textit{Full programs}\$
Perl -p, 17 bytes (18 bytes including flag)
$_=!/(.)\1*+.+\1/
Retina 0.8.2, 17 bytes
M`(.)(?!\1).+\1
0
Included for completeness; already done in Martin Ender's answer.
Retina 1.0, 17 bytes
C`(.)(?!\1).+\1
0
Pip, 19 18 bytes
`(.)(?!\1).+\1`NIa
Thanks to DLosc.
Prolog (SWI), 40 bytes
\[A|S]:-S=[];(S=[A|_];\+member(A,S)),\S.
-3 thanks to Jo King
Similar to xnor's second Haskell answer.
This won't work on TIO because it is too old of a version, and it's longer, but I think it's more interesting:
Prolog (SWI), 57 bytes
\A:-clumped(A,B),maplist([X-_,X]>>!,B,C),list_to_set(A,C).
Ruby, 30 bytes
->s{(s*'').squeeze==s.uniq*''}
Longer than using regex, but uses a builtin that doesn't need to exist.
squeeze is connected-uniquify. It's on strings, but not lists, and uniq is on lists, and not strings...
Brachylog, 5 4 bytes
≡ᵍc?
-1 thanks to DLosc
Takes input as a list of digits through the input variable, and succeeds or fails as output.
≡ᵍ The input with identical elements grouped in order of first appearance
c with the groups concatenated
? is the input.
Desmos, 83 bytes
A=[1...l.length][l=i]
L=join(A.min-1,A)
f(l)=[0^{max(L[2...]-L-1)}fori=l[1...]].min
K (ngn/k), 11 10 bytes
-1 byte thanks to @ovs
~/,/'='^:\
^: test for spaces - always returns 0s, as the input consists only of digits. The : here means monadic.
^:\ repeat until convergence. This will return a pair of the original argument and a list of 0s of the same length as the argument.
=' group each. To "group" a list means to make a dictionary that maps each distinct element of the list to the positions where it occurs.
,/' raze each, i.e. ignore the keys and concatenate the values. For the list of all 0s this will result in 0,1,...,length-1. For the other list, it will depend on the presence of non-continuous blocks - if there aren't any, the result will be 0,1,...,length-1 too.
~/ do they match?
Vyxal, 4 bytes
vḟÞṠ
vḟ # Index of B in A, vectorized (B will be each individual character in the
# string / item in the list, and A will be the string/list itself)
ÞṠ # Is the result sorted?
Answers that use this method, in chronological order:
- Dennis – Python, 33 bytes
- isaacg – Pyth, 5 bytes
- Dennis - Julia, 30 bytes
- Dennis - J, 8 bytes
- DLosc – BQN, 5 bytes
- DLosc – Jelly, 5 bytes
- Unrelated String – Jelly, 4 bytes
Vyxal, 5 bytes
₌ÞǓU=
₌ # Parallel Apply - apply the following two elements to the input and keep
# both results on the stack.
ÞǓ # Connected Uniquify - Remove occurences of adjacent duplicates.
U # Uniquify - Remove all duplicates.
= # Are the two results equal?
Turned out to be a port of Adnan's 05AB1E answer.
Jelly, 5 bytes
iⱮ`ṢƑ
Takes a list of integers. Try it online!
Explanation
Port of my BQN solution.
iⱮ`ṢƑ
i First index of right argument in left argument
Ɱ mapped over right argument
` using the single argument as both left and right arguments
Ƒ The result is equal to
Ṣ itself sorted
Here's some other 5-byte solutions (see also Dennis's 5-byte solution):
ĠṢFṢƑ
Ġ Group indices by their value in the list
Ṣ Sort list of lists of indices
F Flatten
Ƒ The result is equal to
Ṣ itself sorted
ĠṢF⁼J
Ġ Group indices by their value in the list
Ṣ Sort list of lists of indices
F Flatten
⁼ The result is equal to
J Range from 1 up to len of original argument
¹ƙ`F⁼
¹ƙ` Sort into buckets of identical items, ordered by first occurrence
F Flatten
⁼ The result is equal to the original list
brev, 86 bytes
(fn(with(call-table)((as-list(c every(fn(or(eq?(it'l)x)(it x(not(it(it'l x))))))))x)))
or 79 if it doesn't have to reset its memory between each call, i.e. doesn't have to be idempotent:
(with(call-table)(as-list(c every(fn(or(eq?(it'l)x)(it x(not(it(it'l x)))))))))
BQN, 9 5 bytes
≡⟜∧⊐˜
Anonymous tacit function. Takes a string or a list of integers. Try it at BQN online!
Explanation
Inspired by Dennis's J answer, though the details are a bit different.
Given two lists, ⊐ works as follows: for each element of its right argument, it finds the first index of that value in its left argument.
We're using ⊐˜, which passes the same list as both left and right arguments. The result is a list, for each digit, of the index of that digit's first occurrence.
If the digits are grouped together, these indices will occur in ascending order. However, if some copies of digit A are separated by digit B, B's (larger) index will come between the copies of A's (smaller) index.
≡⟜∧⊐˜
⊐˜ First index of each digit in the argument list
≡⟜ The result is identical to
∧ The result, sorted ascending
Oracle SQL, 158 bytes
select decode(count(*),count(unique y),1,0)from(select regexp_substr(x,'(.)\1*',1,level,'',1)y from t connect by regexp_substr(x,'(.)\1*',1,level)is not null)
It works with an assumption that input data is stored in a table t(x), e.g.
with t(x) as (select '44999911' from dual)
Wolfram Language (Mathematica), 18 bytes
Gather@#==Split@#&
Gather gathers a list into sublists of identical elements, and Split splits a list into sublists of consecutive identical elements. They give the same result if and only if each value appears in only one contiguous block.
Perl 5, 20 bytes
19, plus 1 for -pe instead of -e.
$_=!/(.)(?!\1).+\1/
Lua, 107 94 85 Bytes
13 bytes saved thanks to @LeakyNun
At least, it beats Java :D. Lua sucks at manipulating strings, but I think it is good enough :).
It takes its input as a command-line argument, and outputs Now outputs using its exit code. Exit code 0 for truthy, and 1 for falsy1 for truthy cases and false for falsy ones.
o=os.exit;(...):gsub("(.)(.+)%1",function(a,b)if 0<#b:gsub(a,"")then o(1)end end)o(0)
Ungolfed
Be care, there's two magic-variables called ..., the first one contains the argument of the program, the second one is local to the anonymous function and contains its parameters
o=os.exit; -- ; mandatory, else it would exit instantly
(...):gsub("(.)(.+)%1", -- iterate over each group of the form x.*x and apply an anonymous
function(a,b) -- function that takes the captured group as parameters
if 0<#b:gsub(a,"") -- if the captured group (.+) contain other character than
then -- the one captured by (.)
o(1) -- exit with falsy
end
end)
o(0) -- exit with truthy, reached only when the string is okay
Racket, 53 bytes
The dumb, simple version.
(λ(s)(let([s(string->list s)])(eq?(sort s char<?)s)))
Ungolfed:
(define (lame-all-together s)
(let ([s (string->list s)])
(eq? (sort s char<?) s)))
Racket, 86 bytes
Here's the version implementing @xnor's comment about more efficient ways to do this.
(λ(s)(let([s(string->list(regexp-replace#px"(.)\\1+"s"\\1"))])(eq?(sort s char<?)s)))
Ungolfed:
(define (all-together s)
(let ([s (string->list (regexp-replace #px"(.)\\1+" s "\\1"))])
(eq? (sort s char<?) s )))
Okay, this may actually just shift the weight of computation from the sort function to regexp-replace, but it was an interesting solution. Basically, it removes runs of duplicate characters first (see here), then tests if the remaining length-1 runs are in sorted fashion.
C, 74 73 71 bytes
Shaved one three byte thanks to @xsot!
a[99],c,m;main(d){for(;~c;m|=c^d&&a[d=c]++)c=getchar();putchar(48+!m);}
C#, 119 bytes
bool m(String s){for(int i=0;i<9;i++){if(new Regex(i.ToString()+"+").Matches(s).Count>1){return false;}}return true;}
Ungolfed
bool m(String s) {
for(int i=0;i<9;i++) {
if(new Regex(i.ToString() + "+").Matches(s).Count > 1) {
return false;
}
}
return true;
}
R, 66 48 46 43 38 bytes
function(s)!any(duplicated(rle(s)$v))
This is a function that accepts the input as a vector of digits and returns a boolean. To call it, assign it to a variable.
Not the shortest but I thought it was a fun approach. We run length encode the input and extract the values. If the list of values contains duplicates then return FALSE, otherwise return TRUE.
Saved 20 bytes thanks to MickyT, 3 thanks to Albert Masclans, and 5 thanks to mnel!
Java, 161 156 bytes
Shamelessly stealing borrowing the regex from this answer because I started out trying to do this with arrays and math manipulation, but it got hideously complex, and regex is as good a tool as any for this problem.
import java.util.regex.*;public class a{public static void main(String[] a){System.out.println(!Pattern.compile("(.)(?!\\1).*\\1").matcher(a[0]).find());}}
Ungolfed:
import java.util.regex.*;
public class a {
public static void main(String[] args) {
System.out.println(!Pattern.compile("(.)(?!\\1).*\\1").matcher(args[0]).find());
}
Laid out like a sensible Java person:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class {
public static void main(String[] args) {
Pattern p = Pattern.compile("(.)(?!\\1).*\\1");
Matcher m = p.matcher(args[0]);
System.out.println(!m.find());
}
}
Python, 56 55 bytes
a=lambda s:~(s[0]in s.lstrip(s[0]))&a(s[1:])if s else 1
C# 111 bytes
bool f(string s){for(int i=0;i<s.Length-1;i++)if(s[i]!=s[i+1]&&s.LastIndexOf(s[i])!=i)return 1==2;return true;}
old strategy 131 bytes
bool s(string i){var f=Regex.Matches(i,@"([0-9])\1{0,}").Cast<Match>().Select(m=>m.Value[0]);return f.Distinct().SequenceEqual(f);}
first golf i think i did ok in
J, 8 bytes
-:]/:i.~
Test it with J.js.
How it works
-:]/:i.~ Monadic verb. Argument: y (list of digits)
i.~ Find the index in y of each d in y.
]/: Sort y by the indices.
-: Match; compare the reordering with the original y.
Factor, 22 bytes
[ dup natural-sort = ]
Does what it says on the tin. As an anonymouse function, you should call this, or make it a : word ;.
MATL, 13 11 bytes
u"G@=fd2<vA
Thanks to Luis Mendo for saving two bytes!
Explanation
% Grab the input implicitly
u % Find the unique characters
" % For each of the unique characters
G % Grab the input again
@= % Determine which chars equal the current char
f % Find the locations of these characters
d % Compute the difference between the locations
2< % Find all index differences < 2 (indicating consecutive chars)
v % Vertically concatenate all stack contents
A % Ensure that they are all true
% Implicit end of the for loop
05AB1E, 4 bytes
Code:
Ô¹ÙQ
Explanation:
Ô # Push connected uniquified input. E.g. 111223345565 would give 1234565.
¹ # Push input again.
Ù # Uniquify the input. E.g. 111223345565 would give 123456.
Q # Check if equal, which yields 1 or 0.
Uses CP-1252 encoding.
Retina, 17 bytes
M`(.)(?!\1).+\1
0
Try it online! (Slightly modified to run all test cases at once.)
The first regex matches digits which are separated by other digits, so we get a 0 for valid inputs and anywhere between 1 and 9 for invalid inputs (due to the greediness of the the .+, we can't get more than n-1 matches for n different digits).
To invert the truthiness of the result, we count the number of 0s, which is 1 for valid inputs and 0 for invalid ones.
Jelly, 5 bytes
ĠIFPỊ
How it works
ĠIFPỊ Main link. Input: n (list of digits or integer)
Ġ Group the indices of n by their corresponding values, in ascending order.
For 8884443334, this yields [[7, 8, 9], [4, 5, 6, 10], [1, 2, 3]].
I Increments; compute the all differences of consecutive numbers.
For 8884443334, this yields [[1, 1], [1, 1, 4], [1, 1]].
F Flatten the resulting 2D list of increments.
P Product; multiply all increments.
Ị Insignificant; check if the product's absolute value is 1 or smaller.
Python 3, 38 34 33 bytes
lambda s:s==sorted(s,key=s.index)
This expects a list of digits or singleton strings as argument. Test it on Ideone.
Thanks to @xsot for golfing off 4 bytes!
Thanks to @immibis for golfing off 1 byte!
Mathematica, 26 bytes
0<##&@@Sort[#&@@@Split@#]&
Julia, 35 bytes
s->issorted(s,by=x->findfirst(s,x))
For whatever reason, sort does not take a string, but issorted does...
Pyth, 6 5 bytes
1 bytes thanks to FryAmTheEggman
SIxLQ
Inspired by the Python solution here.
Explanation:
SIxLQ
xLQ Map each element in the input to its index in the input. Input is implicit.
SI Check whether this list is sorted.
Ruby, 23 bytes
Anonymous function. Accepts a string. Regex strat.
->n{/(.)(?!\1).*\1/!~n}
Regex breakdown
/(.)(?!\1).*\1/
(.) # Match a character and save it to group 1
(?!\1) # Negative lookahead, match if next character isn't
# the same character from group 1
.* # Any number of matches
\1 # The same sequence as group 1
!~ means if there are no matches of the regex within the string, return true, and otherwise return false.
Haskell, 44 bytes
import Data.List
((==)<*>nub).map head.group
Usage example: ((==)<*>nub).map head.group $ "44999911" -> True.
A non-pointfree version:
f x = q == nub q -- True if q equals q with duplicates removed
where
q = map head $ group x -- group identical digits and take the first
-- e.g. "44999911" -> ["44","9999","11"] -> "491"
-- e.g "3443311" -> ["3","44","33","11"] -> "3431"
JavaScript ES6, 71 69 bytes
h=y=>y.match(/(.)\1*/g);x=>h((u=h(x)).sort().join``).length==u.length
Or, equivalently:
x=>((u=x.match(r=/(.)\1*/g)).sort().join``).match(r).length==u.length
x=>(h=y=>y.match(/(.)\1*/g))((u=h(x)).sort().join``).length==u.length
Golfing in progress.
Verify test cases
var truthy = `3
51
44999911
123456789
222222222222222222222`.split `
`;
var falsey = `818
8884443334
4545
554553
1234567891`.split `
`;
var Q = x => ((u = x.match(r = /(.)\1*/g)).sort().join ``).match(r).length == u.length;
truthy.concat(falsey).forEach(e => {
t = document.createTextNode(`${e} => ${Q(e)}`);
o.appendChild(t);
o.appendChild(document.createElement("br"));
});
* {
font-family: Consolas, monospace;
}
<div id=o></div>
JavaScript (ES6), 27 bytes
s=>!/(.)(?!\1).*\1/.test(s)
Uses negative lookahead to look for two non-contiguous digits. If at least two such digits exist, then they can be chosen so that the first digit precedes a different digit.