[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, the mapping(s) from the latter (left-to-right) should be combined with the mapping in the result by calling (f val-in-result val-in-latter)

(= (__ * {:a 2, :b 3, :c 4} {:a 2} {:b 2} {:c 5})  {:a 4, :b 6, :c 20})

(= (__ - {1 10, 2 20} {1 3, 2 10, 3 15}) {1 7, 2 10, 3 15})

(= (__ concat {:a [3], :b [6]} {:a [4 5], :c [8 9]} {:b [7]}) {:a [3 4 5], :b [6 7], :c [8 9]})

特殊限制:禁用merge-with

解(长度:100)

(fn [f & x] (reduce   (fn [m v]  (let [k (first v) w (last v)]    (if (m k)      (assoc m k (f (m k) w))      (assoc m k w)))) {} (apply concat x)))
中间过程:经历了2个版本,第一个版本比较差,后面这个比较好(短)。基本思路是用reduce两两合并,输入方式是(apply concat x),效果:(apply concat ‘({:a 2, :b 3, :c 4} {:a 2} {:b 2} {:c 5}));([:a 2] [:b 3] [:c 4] [:a 2] [:b 2] [:c 5])因此第一个reduce合并的想法是,vector of vector 与 vector 合并:(defn my-com [f x y]  (let [u (last x) v (first u) w (first y)]    (if (= v w)      (sort-by  #(first %) (conj (butlast x) [v (f (last u) (last y))]))      (sort-by  #(first %) (conj x y)))))效果:(这里需要有序的输入)user=> (my-com + [] [:a 1]);([:a 1]) user=> (my-com + [[:a 3] [:b 4]] [:b 1]);([:a 3] [:b 5])
最后配合reduce:(reduce (partial my-com +) nil (sort-by #(first %) (concat {:a 2:b 3} {:a 2 :b 3 :c 4}))); ([:a 4] [:b 6] [:c 4]); 以及按要求输出成{:a 4, :b 3, :c 4},(into (hash-map) (reduce (partial my-com *) nil (sort-by #(first %) (concat{:a 2, :b 3, :c 4} {:a 2} {:b 2} {:c 5}))));{:a 4, :b 6, :c 20}
然后是所有函数匿名以及整合。 但是这个方法不好,因为中间通过vector的形式合并,又需要有序,因此立刻想到reduce是hash-map与vector的合并:(defn my-com2 [f m v]  (let [k (first v) w (second v)]    (if (m k)      (assoc m k (f (m k) w))      (assoc m k w))))user=>(my-com2 + {:a 2 :b 3} [:a 1]);{:a 3, :b 3}user=>(reduce (partial my-com2 *) {} (apply concat ‘({:a 2, :b 3, :c 4} {:a2} {:b 2} {:c 5}))); {:c 20, :b 6, :a 4}
这次,就不需要对输出进行转换了,因为直接是目标格式的大map,最终整合,匿名时候发现,my-com2的匿名形式不需要f这个参数,因为在外层的fn[f & x]已经有了f,可以直接用:
(fn [f & x] (reduce   (fn [m v]  (let [k (first v) w (last v)]    (if (m k)      (assoc m k (f (m k) w))      (assoc m k w)))) {} (apply concat x)))
为了尽可能缩短代码,把second 换成了last,这个纯粹是为了题目,其实不是好的做法。
时间: 2024-10-01 22:56:59

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

[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]解题记录-#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]解题记录-#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]解题记录-#73

Difficulty: Hard Topics: game A 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

[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{