33
\$\begingroup\$

Introduction:

I think we've all heard of it, but here a very brief summary: Noah gathered two of every species of animal on the planet, male and female, to save in his Ark during a great flood. The actual quote from the Bible is:

Genesis 7:2-3
You must take with you seven of every kind of clean animal, the male and its mate, two of every kind of unclean animal, the male and its mate, and also seven of every kind of bird in the sky, male and female, to preserve their offspring on the face of the earth.
source

But for the sake of this challenge we will ignore the clean/unclean part and the part where he took seven of each animal. This challenge is only about this part:

two of every kind of unclean animal, the male and its mate

Challenge:

Input:

You are given a list of positive integers (in random order).

Output:

Two distinct values indicating whether it's a 'List of Noah' or not. This doesn't necessary have to be a truthy/falsey value, so could also be 0/1 in Java/C#, or 'A'/'B' in any language, to give some examples.

When is a list a 'List of Noah'? When there are exactly two of every integer in the list.

Challenge rules:

  • I/O is flexible. Input can be a list/array/stream of integers/floats/strings, or read one by one from STDIN. Output can be any two distinct values, returned from a function or output to STDOUT / a file.
  • The integers in the input-list are in random order, and are guaranteed to be positive within the range \$1\leq n\leq100000\$.
  • The input-list is guaranteed to be non-empty.
  • Having an integer a multiple of two times present above 2 (i.e. 4, 6, 8, etc.) will be falsey. I.e. [6,4,4,6,4,7,4,7] is falsey, although you could still create equal pairs like this: [[4,4],[4,4],[6,6],[7,7]].

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (i.e. TIO).
  • Also, adding an explanation for your answer is highly recommended.

Test cases:

Truthy:
[7,13,9,2,10,2,4,10,7,13,4,9]
[1,2,3,1,2,3]
[10,100,1000,1,100,10,1000,1]
[123,123]
[8,22,57189,492,22,57188,8,492,57188,57189,1,1]

Falsey:
[6,4,4,6,4,7,4,7]
[2,2,2,2,2,2]
[5,1,4,5,1,1,4]
[77,31,5,31,80,77,5,8,8]
[1,2,3,2,1]
[44,4,4]
[500,30,1]
[1,2,1,1]
[2,4,6,4,4,4]
[2,23,34,4]
[2,23,3,3,34,4]
\$\endgroup\$
1
  • 15
    \$\begingroup\$ And in Quran also; Surah Al-Mumenoon, Verse 27: So We inspired him (with this message): "Construct the Ark within Our sight and under Our guidance: then when comes Our Command, and the fountains of the earth gush forth, take thou on board pairs of every species, male and female, and thy family- except those of them against whom the Word has already gone forth: And address Me not in favour of the wrong-doers; for they shall be drowned (in the Flood). (Yusuf Ali) \$\endgroup\$
    – Ishaq Khan
    Commented Jul 8, 2019 at 10:12

68 Answers 68

1 2
3
1
\$\begingroup\$

PHP, 58 bytes

for(;$n=$argv[++$i];$a[$n]++);echo min($a)==2&&max($a)==2;

Each number in the list is passed as an argument.

Outputs 1 for truthy and nothing for falsey.

Try it online!

Tests

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

Arturo, 24 bytes

$=>[[]=--values tally&2]

Try it!

$=>[        ; a function where input is assigned to &
    tally&  ; create a dictionary of counts of elements in input
    values  ; get just the counts
    --   2  ; remove all the twos
    []=     ; is it the empty list?
]           ; end function
\$\endgroup\$
1
\$\begingroup\$

Desmos, 40 bytes

f(L)=0^{[L[L=i].length-2fori=L]^2.total}

Try It On Desmos!

Try It On Desmos! - Prettified

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

Vyxal 3, 4 bytes

SUu₌

Try it Online!

Sort, uninterleave, uniquify, check if equal

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

Uiua, 8 bytes

/×=2⊕⧻⊛.

Test pad

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

Awk, 44 bytes

{++k[$0]}END{for(i in k)a=a||k[i]-2;print a}

Try it online!

0 means it is a list of Noah, 1 means that it is not a list of Noah.

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

Perl 5 -pl, 31 bytes

$k{$_}++}{$_=!grep$_-2,values%k

Try it online!

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

AWK, 36 32 bytes

++_[$0]-2{i++}END{print NR/i==2}

Try it online!

  • -4 bytes, print value of NR/i==2 assessment instead of using ternary operator.

Will print 1 for List of Noah and 0 for not.

\$\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.