Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active October 18, 2024 22:00
Show Gist options
  • Save PEZ/bdbcf005bcae10b15531ebe3a7d0be9c to your computer and use it in GitHub Desktop.
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
(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!
@StanTheBear
Copy link

#(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?

Copy link

ghost commented Nov 2, 2023

(def __ #(loop [el (seq %)
                res ""]
           (if (nil? el)
             (str res)
             (recur (next el)
                    (if (Character/isUpperCase (first el))
                      (str res (first el))
                      res)))))

@jsebdev
Copy link

jsebdev commented May 27, 2024

(def __ (fn [s]
          (->> s
               (filter (fn [c] (Character/isUpperCase c)))
               (apply str)
               )))

@oezg
Copy link

oezg commented Oct 18, 2024

(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