[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 order they appear in s.

1. (= (__ #(> % 5) [1 3 6 8]) {false [1 3], true [6 8]})

2. (= (__ #(apply / %) [[1 2] [2 4] [4 6] [3 6]])  {1/2 [[1 2] [2 4] [3 6]], 2/3 [[4 6]]})

3. (= (__ count [[1] [1 2] [3] [1 2 3] [2 3]]) {1 [[1] [3]], 2 [[1 2] [2 3]], 3 [[1 2 3]]})

限制:不能用“group-by”

题解:长度分61

(fn[f s](apply merge-with concat (map (fn [x] (hash-map (f x) [x])) s)))

中间过程测试:

1。生成hash-map:

user=> (map #(hash-map % 0) [1 2 3 4])

;({1 0} {2 0} {3 0} {4 0})

2. 测试merge-with

user=>(merge-with concat {false [1]} {false [2]} {true [3]} {true [4]})

;{true (3 4), false (1 2)}

3. 合并

user=>(merge-with concat (map #(hash-map ((fn[x](> x 5)) %) [%]) [1 2 3 4 5 6 7 8]))

;({false [1]} {false [2]} {false [3]} {false [4]} {false [5]} {true [6]} {true [7]} {true [8]})

有点问题,少了个东西

user=>(apply merge-with concat (map #(hash-map ((fn[x](> x 5)) %) [%]) [1 2 3 4 5 6 7 8]))

;{true (6 7 8), false (1 2 3 4 5)}

4. 整理成匿名函数,和题目要求的参数格式:

(fn[f s](apply merge-with concat (map (fn [x] (hash-map (f x) [x])) s)))

时间: 2024-10-11 15:50:47

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

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

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 //

洛谷1613跑路解题记录

题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的空间跑路器,每秒钟可以跑2^k千米(k是任意自然数).当然,这个机器是用longint存的,所以总跑路长度不能超过maxlongint千米.小A的家到公司的路可以看做一个有向图,小A家为点1,公司为点n,每条边长度均为一千米.小A想每天能醒地尽量晚,所以让你帮他算算,他最少需要几秒才能到公司.数据保证1到n至少有一条