Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My answer is in Java, I am just getting into Python so I will update this answer when I get around to it.</p> <pre class="lang-java prettyprint-override"><code>boolean nand(boolean[] a) { boolean b = true if (a.length &gt; 1) { b = a[0] &amp;&amp; a[1]; if (a.length &gt; 2) for (int i = 2; i &lt; a.length; i++) b &amp;= a[i]; } return !b; } </code></pre> <p>Usage (Should return true):</p> <pre><code>nand(training_set[i][0]) == training_set[i][1] </code></pre> <h1>Edit</h1> <p>Over two years later, I have come back to this question to add to my answer...</p> <p>I have created an iterative and recursive solution in python. I have also attempted to code golf the recursive. I got it down to 106 bytes.</p> <h2>Implementations</h2> <h3>Iterative</h3> <pre class="lang-py prettyprint-override"><code>def nand(*a): if len(a) &gt; 1: b = a[0] and a[1] if len(a) &gt; 2: for i in range(2, len(a)): b = b and a[i] return not b return None </code></pre> <h3>Recursive</h3> <pre class="lang-py prettyprint-override"><code>def nand(*a): if len(a) &lt; 2: return None elif len(a) == 2: return not (a[0] and a[1]) else: return nand(*([a[0] and a[1]] + list(a[2:]))) </code></pre> <h3>Recursive (lambda)</h3> <pre class="lang-py prettyprint-override"><code>nand=lambda*a:None if len(a)&lt;2 else not(a[0]and a[1])if len(a)==2 else nand(*([a[0]and a[1]]+list(a[2:]))) </code></pre> <h2>Results</h2> <pre class="lang-py prettyprint-override"><code>print nand(True, True, True) # ¬(1 ∧ 1 ∧ 1) == 0 print nand(True, True, False) # ¬(1 ∧ 1 ∧ 0) == 1 print nand(True, False, True) # ¬(1 ∧ 0 ∧ 1) == 1 print nand(True, False, False) # ¬(1 ∧ 0 ∧ 0) == 1 print nand(False, True, True) # ¬(0 ∧ 1 ∧ 1) == 1 print nand(False, True, False) # ¬(0 ∧ 1 ∧ 0) == 1 print nand(False, False, True) # ¬(0 ∧ 0 ∧ 1) == 1 print nand(False, False, False) # ¬(0 ∧ 0 ∧ 0) == 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload