[4Clojure]解题记录-#73

Difficulty: Hard
Topics: game

tic-tac-toe board is represented by a two dimensional vector. X is represented by :x, O is represented by :o, and empty is represented by :e. A player wins by placing three Xs or three Os in a horizontal, vertical, or diagonal row. Write a function which analyzes a tic-tac-toe board and returns :x if X has won, :o if O has won, and nil if neither player has won.

(= nil (__ [[:e :e :e]

[:e :e :e]

[:e :e :e]]))

(= :x (__ [[:x :e :o]

[:x :e :e]

[:x :e :o]]))

(= :o (__ [[:e :x :e]

[:o :o :o]

[:x :e :x]]))

(= :x (__ [[:x :e :e]

[:o :x :e]

[:o :e :x]]))

(= :o (__ [[:x :e :o]

[:x :o :e]

[:o :e :x]]))

(= nil (__ [[:x :o :x]

[:x :o :x]

[:o :x :o]]))

解长度:178
(fn [m]
  (let [a (concat (apply map vector m) (seq m)
         ((fn [x] (let [g #(get-in x %) d (g [1 1])] 
              (seq [[(g [0 0]) d (g [2 2])] [(g [0 2]) d (g [2 0])]]))) m))       h (fn [k] (some #(every? #{k} %) a))]
    (if (h :x)
     :x 
         (if (h :o)
       :o

             nil))))

基本思路,先将输入产生8个vector,3横,3纵以及两个对角线,所以

(apply map vector m)就是生成3纵; (seq m)就是3横, 
((fn  cro [x] (let [g #(get-in x %) d (g [1 1])] (seq [[(g [0 0]) d (g [2 2])] [(g [0 2]) d (g [2 0])]]))) m)) 就是两个对角线。

user=>(def input[[:x :o :x]                 [:x :o :x]                  [:o :x :o]])user=>(apply map vector input); ([:x :x :o] [:o :o :x] [:x :x :o])user=>(seq input); ([:x :o :x] [:x :o :x] [:o :x :o])user=>(cro input); ([:x :o :o] [:x :o :o])user=>(some  #(every? #{:x} %) ‘([:x :x :o] [:o :o :x] [:x :x :o])); niluser=>(some  #(every? #{:x} %) ‘([:x :x :x] [:o :o :x] [:x :x :o])); trueuser=>(some  #(every? #{:x} %) ‘([:x :x :o] [:o :o :o] [:x :x :o])); nil
最后整理,函数匿名,已经尽可能缩短字数。
时间: 2024-08-10 19:14:17

[4Clojure]解题记录-#73的相关文章

[4Clojure]解题记录-#80

Perfect Numbers Difficulty: Medium Topics:   A number is "perfect" if the sum of its divisors equal the number itself. 6 is a perfect number because 1+2+3=6. Write a function which returns true for perfect numbers and false otherwise. (= (__ 6)

[4Clojure]解题记录-#79

Triangle Minimal Path Difficulty: Hard Topics: graph-theory Write a function which calculates the sum of the minimal path through a triangle. The triangle is represented as a collection of vectors. The path should start at the top of the triangle and

[4Clojure]解题记录-#77

Anagram Finder Difficulty: Medium Topics:   Write a function which finds all the anagrams in a vector of words. A word x is an anagram of word y if all the letters in x can be rearranged in a different order to form y. Your function should return a s

[4Clojure]解题记录-#78

Reimplement Trampoline Difficulty: Medium Topics: core-functions Reimplement the function described in "Intro to Trampoline". (= (letfn [(triple [x] #(sub-two (* 3 x))) (sub-two [x] #(stop?(- x 2))) (stop? [x] (if (> x 50) x #(triple x)))]  (

[4Clojure]解题记录-#67

Difficulty: Medium Topics: primes Write a function which returns the first x number of prime numbers. (= (__ 2) [2 3]) (= (__ 5) [2 3 5 7 11]) (= (last (__ 100)) 541) 提交长度:Code Golf Score: 120 (fn [x] (take x (filter (fn [n] (if (= 2 n) true (empty? 

[4Clojure]解题记录-#69

Difficulty: Medium Topics: core-functions Write a function which takes a function f and a variable number of maps. Your function should return a map that consists of the rest of the maps conj-ed onto the first. If a key occurs in more than one map, t

[4Clojure]解题记录-#63

题目 Difficulty: Easy Topics: core-functions Given a function f and a sequence s, write a function which returns a map. The keys should be the values of f applied to each item in s. The value at each key should be a vector of corresponding items in the

Pwnable中的passcode解题记录:

Pwnable中的passcode解题记录: 看下源码: 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void login(){ 5 int passcode1; 6 int passcode2; 7 8 printf("enter passcode1 : "); 9 scanf("%d", passcode1); //这里没有加取地址符号 10 fflush(stdin); 11 12 //

AC自动机解题记录

1.HDU 2222 Keywords Search 模板题 1 #include <bits/stdc++.h> 2 #define fir first 3 #define sec second 4 #define EPS 1e-12 5 using namespace std; 6 7 typedef long long LL; 8 typedef pair<int , int > pii; 9 const int MAXN=5e5+5; 10 11 struct Trie{