4Clojure: Recent Problemshttp://4clojure.com/problems<![CDATA[Recent problems at 4Clojure.com]>http://4clojure.com/problem/166ComparisonsFor any orderable data type it's possible to derive all of the basic comparison operations (&lt;, &le;, =, &ne;, &ge;, and &gt;) from a single operation (any operator but = or &ne; 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> &rarr; :eq</li> <li><var>x</var> &gt; <var>y</var> &rarr; :gt</li> <li><var>x</var> &lt; <var>y</var> &rarr; :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> &#x2a2f <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 representing 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 TrapezoidWrite 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].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.http://4clojure.com/problem/144OscilrateWrite an oscillating iterate: a function that takes an initial value and a variable number of functions. It should return a lazy sequence of the functions applied to the value in order, restarting from the first function after it hits the end.http://4clojure.com/problem/143dot productCreate a function that computes the <a href="http://en.wikipedia.org/wiki/Dot_product#Definition">dot product</a> of two sequences. You may assume that the vectors will have the same length.http://4clojure.com/problem/141Tricky card games<p> In <a href="http://en.wikipedia.org/wiki/Trick-taking_game">trick-taking card games</a> such as bridge, spades, or hearts, cards are played in groups known as "tricks" - each player plays a single card, in order; the first player is said to "lead" to the trick. After all players have played, one card is said to have "won" the trick. How the winner is determined will vary by game, but generally the winner is the highest card played <i>in the suit that was led</i>. Sometimes (again varying by game), a particular suit will be designated "trump", meaning that its cards are more powerful than any others: if there is a trump suit, and any trumps are played, then the highest trump wins regardless of what was led. </p> <p> Your goal is to devise a function that can determine which of a number of cards has won a trick. You should accept a trump suit, and return a function <code>winner</code>. Winner will be called on a sequence of cards, and should return the one which wins the trick. Cards will be represented in the format returned by <a href="/problem/128/">Problem 128, Recognize Playing Cards</a>: a hash-map of <code>:suit</code> and a numeric <code>:rank</code>. Cards with a larger rank are stronger. </p>http://4clojure.com/problem/140Veitch, Please!Create a function which accepts as input a boolean algebra function in the form of a set of sets, where the inner sets are collections of symbols corresponding to the input boolean variables which satisfy the function (the inputs of the inner sets are conjoint, and the sets themselves are disjoint... also known as canonical minterms). Note:&nbsp;capitalized symbols represent truth, and lower-case symbols represent negation of the inputs. Your function must return the minimal function which is logically equivalent to the input. </br></br> PS &mdash; You may want to give this a read before proceeding: <a href="http://en.wikipedia.org/wiki/K_map">K-Maps</a> </br> PPS &mdash; If you're interested in logic programming more generally, you should also check out: <a href="https://github.com/clojure/core.logic">core.logic</a>http://4clojure.com/problem/138Squares SquaredCreate a function of two integer arguments: the start and end, respectively. You must create a vector of strings which renders a 45&deg; rotated square of integers which are successive squares from the start point up to and including the end point. If a number comprises multiple digits, wrap them around the shape individually. If there are not enough digits to complete the shape, fill in the rest with asterisk characters. The direction of the drawing should be clockwise, starting from the center of the shape and working outwards, with the initial direction being down and to the right.http://4clojure.com/problem/137Digits and basesWrite a function which returns a sequence of digits of a non-negative number (first argument) in numerical system with an arbitrary base (second argument). Digits should be represented with their integer values, e.g. 15 would be [1 5] in base 10, [1 1 1 1] in base 2 and [15] in base 16.