Last active
October 18, 2024 22:00
-
-
Save PEZ/bdbcf005bcae10b15531ebe3a7d0be9c to your computer and use it in GitHub Desktop.
Get the Caps – Rich 4Clojure Problem 29 – See: https://github.com/PEZ/rich4clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns rich4clojure.easy.problem-029 | |
(:require [hyperfiddle.rcf :refer [tests]])) | |
;; = Get the Caps = | |
;; By 4Clojure user: dbyrne | |
;; Difficulty: Easy | |
;; Tags: [strings] | |
;; | |
;; Write a function which takes a string and returns a new | |
;; string containing only the capital letters. | |
(def __ :tests-will-fail) | |
(comment | |
) | |
(tests | |
(__ "HeLlO, WoRlD!") := "HLOWRD" | |
(__ "nothing") := | |
(__ "$#A(*&987Zf") := "AZ") | |
;; To participate, fork: | |
;; https://github.com/PEZ/rich4clojure | |
;; Post your solution below, please! |
#(apply
str (re-seq #"[A-Z]" %)))
Had to edit (tests 2 to
(__ "nothing") := "", cant think of any way to return no action for "" or nil?
(def __ #(loop [el (seq %)
res ""]
(if (nil? el)
(str res)
(recur (next el)
(if (Character/isUpperCase (first el))
(str res (first el))
res)))))
(def __ (fn [s]
(->> s
(filter (fn [c] (Character/isUpperCase c)))
(apply str)
)))
(defn filter-upper
"takes a string and returns a new string containing only the capital letters"
[s]
(apply str (re-seq #"[A-Z]" s)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(fn [s] (apply str (filter #(Character/isUpperCase %) s)))