Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Is it a valid chemical?

Non-metals typically* have a fixed number of covalent bonds in every chemical they are part of. Given the number of bonds every element requires, output whether it's possible to construct a single molecule from the given chemicals, such that every atom has it's desired number of bonds.

In other words, output if it's possible to construct a connected undirected multigraph without self-loops so that each node has the number of edges equal to the corresponding number listed in the input.

Test Cases

Falsey:

  • [1] A single element has nothing to bond with
  • [4] Atoms can not bond with themselves
  • [1, 1, 1, 1] Each atom is saturated after single bond, so it's impossible to connect all 4 of them together. It can form 2 pairs but not a single molecule.
  • [0, 0] Two separate helium "molecules"
  • [1, 3]
  • [2, 3]
  • [1, 1, 1, 1, 1, 3]

Truthy:

  • [1, 1] (Hydrogen Gas: H-H → 1-1)
  • [4, 2, 2] (Carbon Dioxide: O=C=O → 2=4=2)
  • [3, 3] (Nitrogen Gas: N≡N → 3≡3)
  • [2, 2, 2] (Ozone*: \$\newcommand{\tripow}[3]{ _{#1}{\stackrel{#2}{\triangle}}_{#3}} \tripow{O}{O}{O}\$\$\tripow{2}{2}{2}\$)
  • [0] (Helium: He → 0)
  • [3, 2, 1] (Nitroxyl: O=N-H → 2=3-1)
  • [3, 3, 3, 3] (Tetrazete: \$\begin{array}{ccccccc}N&-&N& &3&-&3\\\ ||& &||& → &||& &||\\\ N&-&N& &3&-&3\end{array}\$)
  • [4, 1, 1, 1, 1, 2, 2, 2]
  • [4, 2, 2, 1, 1]
  • [3, 3, 2, 1, 1]

Here are the Falsey/Truthy test cases as two lists of lists:

[[1], [4], [1, 1, 1, 1], [0, 0], [1, 3], [2, 3], [1, 1, 1, 1, 1, 3]]
[[1, 1], [4, 2, 2], [3, 3], [2, 2, 2], [0], [3, 2, 1], [3, 3, 3, 3], [4, 1, 1, 1, 1, 2, 2, 2], [4, 2, 2, 1, 1], [3, 3, 2, 1, 1]]

Clarifications

  • You may assume the input is non-empty and all values are positive or zero.
  • You may choose any 2 distinct values for truthy or falsy, or output truthy/falsy using your language's convention (swapping is allowed). See the tag wiki

*I've been told real chemistry is a lot more complex than this, but you can ignore the complexities in this challenge.

Answer*

Draft saved
Draft discarded
Cancel

AltStyle によって変換されたページ (->オリジナル) /