4Clojure: Recent Problemshttp://4clojure.com/problemsRecent problems at 4Clojure.comhttp://4clojure.com/problem/195Parentheses... Again<p>In a family of languages like Lisp, having balanced parentheses is a defining feature of the language. Luckily, Lisp has almost no syntax, except for these "delimiters" -- and that hardly qualifies as "syntax", at least in any useful computer programming sense.</p>
<p>It is not a difficult exercise to find all the combinations of well-formed parentheses if we only have N pairs to work with. For instance, if we only have 2 pairs, we only have two possible combinations: "()()" and "(())". Any other combination of length 4 is ill-formed. Can you see why? </p>
<p>Generate all possible combinations of well-formed parentheses of length <code>2n</code> (n pairs of parentheses).
For this problem, we only consider '(' and ')', but the answer is similar if you work with only {} or only [].</p>
<p>There is an interesting pattern in the numbers!</p>http://4clojure.com/problem/178Best Hand<p>Following on from <a href="http://www.4clojure.com/problem/128">Recognize Playing Cards</a>, determine the best poker hand that can be made with five cards. The hand rankings are listed below for your convenience.</p>
<ol>
<li>Straight flush: All cards in the same suit, and in sequence</li>
<li>Four of a kind: Four of the cards have the same rank</li>
<li>Full House: Three cards of one rank, the other two of another rank</li>
<li>Flush: All cards in the same suit</li>
<li>Straight: All cards in sequence (aces can be high or low, but not both at once)</li>
<li>Three of a kind: Three of the cards have the same rank</li>
<li>Two pair: Two pairs of cards have the same rank</li>
<li>Pair: Two cards have the same rank</li>
<li>High card: None of the above conditions are met</li>
</ol>http://4clojure.com/problem/177Balancing BracketsWhen parsing a snippet of code it's often a good idea to do a sanity check to see if all the brackets match up. Write a function that takes in a string and returns truthy if all square [ ] round ( ) and curly { } brackets are properly paired and legally nested, or returns falsey otherwise.http://4clojure.com/problem/173Intro to Destructuring 2Sequential destructuring allows you to bind symbols to parts of sequential things (vectors, lists, seqs, etc.): <a href="http://clojure.org/special_forms#Special Forms--(let [bindings* ] exprs*)">(let [bindings* ] exprs*)</a>
Complete the bindings so all let-parts evaluate to 3.http://4clojure.com/problem/171IntervalsWrite a function that takes a sequence of integers and returns a sequence of "intervals". Each interval is a a vector of two integers, start and end, such that all integers between start and end (inclusive) are contained in the input sequence.http://4clojure.com/problem/168Infinite Matrix<p>
In what follows, <code>m</code>, <code>n</code>, <code>s</code>, <code>t</code>
denote nonnegative integers, <code>f</code> denotes a function that accepts two
arguments and is defined for all nonnegative integers in both arguments.
</p>
<p>
In mathematics, the function <code>f</code> can be interpreted as an infinite
<a href="http://en.wikipedia.org/wiki/Matrix_%28mathematics%29">matrix</a>
with infinitely many rows and columns that, when written, looks like an ordinary
matrix but its rows and columns cannot be written down completely, so are terminated
with ellipses. In Clojure, such infinite matrix can be represented
as an infinite lazy sequence of infinite lazy sequences,
where the inner sequences represent rows.
</p>
<p>
Write a function that accepts 1, 3 and 5 arguments
<ul>
<li>
with the argument <code>f</code>, it returns the infinite matrix <b>A</b>
that has the entry in the <code>i</code>-th row and the <code>j</code>-th column
equal to <code>f(i,j)</code> for <code>i,j = 0,1,2,...</code>;</li>
<li>
with the arguments <code>f</code>, <code>m</code>, <code>n</code>, it returns
the infinite matrix <b>B</b> that equals the remainder of the matrix <b>A</b>
after the removal of the first <code>m</code> rows and the first <code>n</code> columns;</li>
<li>
with the arguments <code>f</code>, <code>m</code>, <code>n</code>, <code>s</code>, <code>t</code>,
it returns the finite s-by-t matrix that consists of the first t entries of each of the first
<code>s</code> rows of the matrix <b>B</b> or, equivalently, that consists of the first s entries
of each of the first <code>t</code> columns of the matrix <b>B</b>.</li>
</ul>
</p>http://4clojure.com/problem/166ComparisonsFor any orderable data type it's possible to derive all of the basic comparison operations (<, ≤, =, ≠, ≥, and >) from a single operation (any operator but = or ≠ will work). Write a function that takes three arguments, a <var>less than</var> operator for the data and two items to compare. The function should return a keyword describing the relationship between the two items. The keywords for the relationship between <var>x</var> and <var>y</var> are as follows:
<ul>
<li><var>x</var> = <var>y</var> → :eq</li>
<li><var>x</var> > <var>y</var> → :gt</li>
<li><var>x</var> < <var>y</var> → :lt</li>
</ul>http://4clojure.com/problem/164Language of a DFAA <a href="http://en.wikipedia.org/wiki/Deterministic_finite_automaton">deterministic finite automaton (DFA)</a> is an abstract machine that recognizes a <a href=" http://en.wikipedia.org/wiki/Regular_language">regular language</a>. Usually a DFA is defined by a 5-tuple, but instead we'll use a map with 5 keys:
<ul>
<li><var>:states</var> is the set of states for the DFA.</li>
<li><var>:alphabet</var> is the set of symbols included in the language recognized by the DFA. </li>
<li><var>:start</var> is the start state of the DFA. </li>
<li><var>:accepts</var> is the set of accept states in the DFA. </li>
<li><var>:transitions</var> is the transition function for the DFA, mapping <var>:states</var> ⨯ <var>:alphabet</var> onto <var>:states</var>.</li>
</ul>
Write a function that takes as input a DFA definition (as described above) and returns a sequence enumerating all strings in the language recognized by the DFA.
Note: Although the DFA itself is finite and only recognizes finite-length strings it can still recognize an infinite set of finite-length strings. And because stack space is finite, make sure you don't get stuck in an infinite loop that's not producing results every so often!http://4clojure.com/problem/162Logical falsity and truthIn Clojure, only nil and false represent the values of logical falsity in conditional tests - anything else is logical truth.http://4clojure.com/problem/161Subset and SupersetSet A is a subset of set B, or equivalently B is a superset of A, if A is "contained" inside B. A and B may coincide.http://4clojure.com/problem/158DecurryWrite a function that accepts a curried function of unknown arity <i>n</i>. Return an equivalent function of <i>n</i> arguments.
<br/>
You may wish to read <a href="http://en.wikipedia.org/wiki/Currying">this</a>.http://4clojure.com/problem/157Indexing SequencesTransform a sequence into a sequence of pairs containing the original elements along with their index.http://4clojure.com/problem/156Map DefaultsWhen retrieving values from a map, you can specify default values in case the key is not found:<br/><br/>(= 2 (:foo {:bar 0, :baz 1} 2))<br/><br/>However, what if you want the map itself to contain the default values? Write a function which takes a default value and a sequence of keys and constructs a map.http://4clojure.com/problem/153Pairwise Disjoint Sets
<p>
Given a set of sets, create a function which returns <code>true</code>
if no two of those sets have any elements in common<sup>1</sup> and <code>false</code> otherwise.
Some of the test cases are a bit tricky, so pay a little more attention to them.
</p>
<p>
<sup>1</sup>Such sets are usually called <i>pairwise disjoint</i> or <i>mutually disjoint</i>.
</p>
http://4clojure.com/problem/152Latin Square Slicing<p>
A <a href="http://en.wikipedia.org/wiki/Latin_square">Latin square</a> of order <code>n</code>
is an <code>n x n</code> array that contains <code>n</code> different elements,
each occurring exactly once in each row, and exactly once in each column.
For example, among the following arrays <i>only the first one</i> forms a Latin square:
<pre>
A B C A B C A B C
B C A B C A B D A
C A B C A C C A B
</pre>
</p>
<p>
Let <code>V</code> be a vector of such vectors<sup>1</sup> that they may differ in length<sup>2</sup>.
We will say that an arrangement of vectors of <code>V</code> in consecutive rows
is an <em>alignment (of vectors) of</em> <code>V</code>
if the following conditions are satisfied:
<ul>
<li>All vectors of <code>V</code> are used.</li>
<li>Each row contains just one vector.</li>
<li>The order of <code>V</code> is preserved.</li>
<li>All vectors of maximal length are horizontally aligned each other.</li>
<li>If a vector is not of maximal length then all its elements are aligned
with elements of some <a href="http://clojuredocs.org/clojure_core/clojure.core/subvec">subvector</a>
of a vector of maximal length.</li>
</ul>
Let <code>L</code> denote a Latin square of order 2 or greater.
We will say that <code>L</code> <em>is included</em> in <code>V</code> or
that <code>V</code> <em>includes</em> <code>L</code>
iff there exists an alignment of <code>V</code>
such that contains a subsquare that is equal to <code>L</code>.
</p>
<p>
For example, if <code>V</code> equals <code>[[1 2 3][2 3 1 2 1][3 1 2]]</code>
then there are nine alignments of <code>V</code> (brackets omitted):
<pre>
1 2 3
1 2 3 1 2 3 1 2 3
A 2 3 1 2 1 2 3 1 2 1 2 3 1 2 1
3 1 2 3 1 2 3 1 2
1 2 3 1 2 3 1 2 3
B 2 3 1 2 1 2 3 1 2 1 2 3 1 2 1
3 1 2 3 1 2 3 1 2
1 2 3 1 2 3 1 2 3
C 2 3 1 2 1 2 3 1 2 1 2 3 1 2 1
3 1 2 3 1 2 3 1 2
</pre>
Alignment <b>A1</b> contains Latin square <code>[[1 2 3][2 3 1][3 1 2]]</code>,
alignments <b>A2, A3, B1, B2, B3</b> contain no Latin squares, and alignments
<b>C1, C2, C3</b> contain <code>[[2 1][1 2]]</code>.
Thus in this case <code>V</code> includes one Latin square of order 3
and one of order 2 which is included three times.
</p>
<p>
Our aim is to implement a function which accepts a vector of vectors <code>V</code> as an argument,
and returns a map which keys and values are integers.
Each key should be the order of a Latin square included in <code>V</code>,
and its value a count of <i>different</i> Latin squares of that order included in <code>V</code>.
If <code>V</code> does not include any Latin squares an empty map should be returned.
In the previous example the correct output of such a function is {3 1, 2 1} and <i>not</i> {3 1, 2 3}.
</p>
<p>
<sup>1</sup> Of course, we can consider sequences instead of vectors.
<br />
<sup>2</sup> Length of a vector is the number of elements in the vector.
</p>http://4clojure.com/problem/150Palindromic Numbers<p>A palindromic number is a number that is the same when written forwards or backwards (e.g., 3, 99, 14341).</p>
<p>Write a function which takes an integer <code>n</code>, as its only argument, and returns an increasing lazy sequence of all palindromic numbers that are not less than <code>n</code>.</p>
<p>The most simple solution will exceed the time limit!</p>http://4clojure.com/problem/148The Big Divide<p>Write a function which calculates the sum of all natural numbers under <i>n</i> (first argument) which are evenly divisible by at least one of <i>a</i> and <i>b</i> (second and third argument). Numbers <i>a</i> and <i>b</i> are guaranteed to be <a href="http://en.wikipedia.org/wiki/Coprime">coprimes</a>.</p>
<p>Note: Some test cases have a very large <i>n</i>, so the most obvious solution will exceed the time limit.</p>http://4clojure.com/problem/147Pascal's Trapezoid<p>Write a function that, for any given input vector of numbers, returns an infinite lazy sequence of vectors, where each next one is constructed from the previous following the rules used in <a href="http://en.wikipedia.org/wiki/Pascal's_triangle">Pascal's Triangle</a>. For example, for [3 1 2], the next row is [3 4 3 2].</p>
<p>Beware of arithmetic overflow! In clojure (since version 1.3 in 2011), if you use an arithmetic operator like + and the result is too large to fit into a 64-bit integer, an exception is thrown. You can use +' to indicate that you would rather overflow into Clojure's slower, arbitrary-precision bigint.</p>http://4clojure.com/problem/146Trees into tables<p>Because Clojure's <code>for</code> macro allows you to "walk" over multiple sequences in a nested fashion, it is excellent for transforming all sorts of sequences. If you don't want a sequence as your final output (say you want a map), you are often still best-off using <code>for</code>, because you can produce a sequence and feed it into a map, for example.</p>
<p>For this problem, your goal is to "flatten" a map of hashmaps. Each key in your output map should be the "path"<sup>1</sup> that you would have to take in the original map to get to a value, so for example <code>{1 {2 3}}</code> should result in <code>{[1 2] 3}</code>. You only need to flatten one level of maps: if one of the values is a map, just leave it alone.</p>
<p><sup>1</sup> That is, <code>(get-in original [k1 k2])</code> should be the same as <code>(get result [k1 k2])</code></p>http://4clojure.com/problem/145For the winClojure's <a href="http://clojuredocs.org/clojure_core/clojure.core/for">for</a> macro is a tremendously versatile mechanism for producing a sequence based on some other sequence(s). It can take some time to understand how to use it properly, but that investment will be paid back with clear, concise sequence-wrangling later. With that in mind, read over these <code>for</code> expressions and try to see how each of them produces the same result.