28
\$\begingroup\$

Given a list of non-negative integers, return whether or not that list is all the same number.

Rules

  • Input and output can be taken/given in any reasonable and convenient format
  • Truthy/Falsey values can be represented as any value of your choice as long as it's reasonable and relatively consistent (e.g. 1 for falsey and >= 2 for truthy is fine)
  • There will always be at least 1 item in the input list
  • The list items are guaranteed to be in the range [0, 9] (\$0 \le n \le 9\$)
  • Standard loopholes apply

This is code golf, so the shortest program in each language wins. I've made a community wiki answer for trivial answers, so feel free to submit potentially longer programs.

Test Cases

[1, 1, 1, 1, 1, 1, 1] -> True
[1, 2, 3, 4] -> False
[6, 9, 6, 9, 6] -> False
[6] -> True
[7, 7] -> True
[4, 2, 0] -> False
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Relevant: Default policy for ouput in decision problems (since this challenge doesn't specify the allowed options for the output]. Anyway, it would be better for the challenge to explicitly specify what is allowed and what isn't \$\endgroup\$
    – Luis Mendo
    Apr 21, 2021 at 9:08
  • \$\begingroup\$ @LuisMendo Input and output can be taken/given in any reasonable and convenient format, that's pretty standard for more trivial challenges \$\endgroup\$ Apr 21, 2021 at 12:50
  • 3
    \$\begingroup\$ @RedwolfPrograms Sure, but I'm not talking about format. I understand format as outputting string '3' instead of number 3; or producing the output via program exit code. What I mean is what options for output are allowed: two consistent values? Non-consistent truthy/falsy? One consistent value for truthy and any inconsistent values for falsy? \$\endgroup\$
    – Luis Mendo
    Apr 21, 2021 at 14:29

63 Answers 63

1 2
3
1
\$\begingroup\$

Arturo, 19 15 bytes

$->a->[]=a--a\0

Try it

$->a->      ; a function taking an argument a
    []=     ; is the empty block equal to...
    a--a\0  ; the input with the value of its first element removed everywhere?
\$\endgroup\$
1
\$\begingroup\$

T-SQL 75 bytes

declare @x table (y int) insert @x values...

select top 1 1 from @x a where not exists (select*from @x b where b.y!=a.y)

will return 1 for true, nothing for false

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

YASEPL, 45 bytes

=a=1'±comma=c®1=d¥0,1`1=e¥a,1}7,d,3!+}2,c`3!<

input list as numbers seperated by commas (like 5,16,24,26,29,12,1)

outputs the list's length if true, outputs the first occurrence of a different number if false

\$\endgroup\$
1 2
3

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.