55
\$\begingroup\$

In an earlier challenge I asked code golfers to produce strings which copy each character in a string. For example:

TThhiiss  iiss  ddoouubbllee  ssppeeaakk!!

This challenge is simply to detect if some text meets the definition of a double speak string.

  • There is an even number of characters.
  • When split into pairs, every pair consists of two of the same character.

The challenge

  • It's code golf, do it in few bytes.
  • Use any language you choose.
  • Please include a link to an online interpreter.
  • The code will accept some text.
  • For simplicity, the input will only consist of printable ASCII characters
  • It will return an indication of whether or not the input is double speak. It could be:
  • A boolean
  • Strings ('true', 'false', 'yes', 'no' etc)
  • Integers 0 or 1

Test Cases:

input -> output
aba -> false
aab -> false
abba -> false
aabb -> true
aaabb -> false
tthhiiss -> true
ttthhhiiisss -> false
\$\endgroup\$
12
  • 9
    \$\begingroup\$ May we error on inputs of length < 2? \$\endgroup\$
    – cole
    Commented Aug 6, 2019 at 16:06
  • 4
    \$\begingroup\$ Suggested test case: abba which should be falsey \$\endgroup\$
    – Giuseppe
    Commented Aug 6, 2019 at 16:29
  • 3
    \$\begingroup\$ Suggested test case: aabbbb which should be truthy \$\endgroup\$ Commented Aug 6, 2019 at 17:30
  • 2
    \$\begingroup\$ @val Well, I'm not going to argue with standard I/O \$\endgroup\$
    – AJFaraday
    Commented Aug 7, 2019 at 8:27
  • 4
    \$\begingroup\$ What about the empty string? \$\endgroup\$
    – PieCot
    Commented Aug 7, 2019 at 21:04

102 Answers 102

1 2 3
4
1
\$\begingroup\$

Oracle SQL, 47 bytes

select*from t where regexp_like(x,'^((.)\2)+$')

It works with an assumption that input data is stored in a table t(x), e.g.

with t(x) as (select 'hheelllloo' from dual)

Returns either original string or no rows.

\$\endgroup\$
1
\$\begingroup\$

Rust, 61 58 39 bytes

|s:&[u8]|s.chunks(2).all(|n|n[0]==n[1])

Try it online!

This is a closure that takes the input as a byte slice (b prefix on a string), e.g.

println!("{}", f(b"aabb"));
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Does not work for b"aab", unless the panic is intended. \$\endgroup\$
    – leo848
    Commented Oct 13, 2023 at 4:35
1
\$\begingroup\$

Swift, 109 bytes

func a(b:String){let c=Array(b),l=c.count;var d=0,i=0;while i<l-1{if c[i]==c[i+1]{d+=1};i+=2;};print(d*2==l)}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C#, 190 bytes

public class P{public static void Main(string[]a){bool d=true;if(a[0].Length%(double)2>0)d=false;else for(int i=0;i<a[0].Length;i+=2){d=a[0][i]==a[0][i+1]?d:false;}System.Console.Write(d);}}

Try Online

\$\endgroup\$
1
\$\begingroup\$

Clojure, 48 bytes

(fn[x](every? #(apply = %)(partition 2 2[\0]x)))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ruby -n, 21 14+1 = 15 bytes

p~/^((.)\2)*$/

Try it online!

\$\endgroup\$
1
\$\begingroup\$

GolfScript, 7 bytes

2/zip~=

Try it online!

Explanation

2/      # Split input into parts of 2,            e.g. tt,ee,ss,tt
  zip   # Read the list horizontally,         yielding test, test
     ~  # Put the two strings onto the stack, yielding test,test
      = # Check whether they are equal,       yielding 1

# False case:
#  tteesst
#->tt,ee,ss,t
#->test,tes
#->0
```
\$\endgroup\$
1
\$\begingroup\$

Perl 5 + -pF/^((.)\2)+$/, 7 bytes

Outputs 0 for falsy and 2 for truthy.

$_=@F^1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

SM83, 7 bytes

Input in hl. Output is in z flag. All other flags are off.

2A B7 C8 BE 23 28 F9 C9
isdbl:
 ld a,(hl+)                 // 2A       load first byte and inc
 or a                       // B7       test if zero
 ret z                      // C8       return if at end (with z on)
 xor (hl)                   // BE       test sameness
 inc hl                     // 23       inc again (does not change flags)
 jr z,isdbl                 // 28 F9    loop if same
 ret                        // C9       else return
\$\endgroup\$
1
\$\begingroup\$

C++, 69 bytes

a solution which would put myself between this and this.

EDIT: you, people, are soooo clever. I love yall

[](string a){int c=0,d=1,k;for(int i:a)k=c++%2?d*=k==i,k:i;return d;}
\$\endgroup\$
3
  • \$\begingroup\$ @ceilingcat if you mean the one in the ranged for statement, I believe I cannot as the following if-else statement is not reducible to a single semicolon. \$\endgroup\$ Commented Jul 16, 2021 at 4:06
  • \$\begingroup\$ @ceilingcat thank you, that works! \$\endgroup\$ Commented Jul 18, 2021 at 12:06
  • 1
    \$\begingroup\$ 78 bytes by using a single return. \$\endgroup\$
    – ovs
    Commented Jul 18, 2021 at 12:14
1
\$\begingroup\$

Pyth, 7 bytes

q{Q.-Q{

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Rockstar, 108 97 88 69 bytes

listen to S
cut S
D's 1
while D and S
let D be roll S's roll S

say D

Try it here (Code will need to be pasted in)

listen to S       :Read input string into variable S
cut S             :Split into an array
D's 1             :Initialise D as 1
while D and S     :While D is truthy and S is not empty
let D be          :  Reassign to D
  roll S          :    Pop the first element from S
  's              :    Check equality with
  roll S          :    Pop the first element from S
                  :End while loop
say D             :Output D
\$\endgroup\$
2
  • \$\begingroup\$ I quite like the output of 'mysterious' if it's false. \$\endgroup\$
    – AJFaraday
    Commented Sep 24, 2020 at 14:24
  • \$\begingroup\$ Odd, @AJFaraday, could've sworn I has it outputting 0 for falsey. Must've changed something before posting without realising. Anyway, long's you're OK with that, I'll leave it as-is. Could save some more by looping forever with no output if the result is truthy but that would probably be pushing things! \$\endgroup\$
    – Shaggy
    Commented Sep 24, 2020 at 14:29
1 2 3
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.