Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
429 PurelyFunctional.tv Newsletter

NOTE: If you're looking for issue 430's gist, go here: https://gist.github.com/ericnormand/fb8d0356a5ff7d898707012a97975ec7

Headline Scroller

A local news station needs you to program their LED scroller screen. It will scroll headlines from the right side to the left side all day. Write a function that takes a headline (string) and the number of characters in the LED screen (integer) and returns a list of the different refreshes of the screen.

The first refresh should be a string of all spaces to clear the screen:

(nth (scroller "HEADLINE" 10) 0) ;=> "          "

It should be a number of spaces equal to the width of the screen (in this case, 10).

The second refresh should have the first letter after 9 spaces:

(nth (scroller "HEADLINE" 10) 1) ;=> "         H"

Then:

(nth (scroller "HEADLINE" 10) 2) ;=> "        HE"

The full output:

(scroller "HEADLINE" 10) => ("          "
                             "         H"
                             "        HE"
                             "       HEA"
                             "      HEAD"
                             "     HEADL"
                             "    HEADLI"
                             "   HEADLIN"
                             "  HEADLINE"
                             " HEADLINE "
                             "HEADLINE  "
                             "EADLINE   "
                             "ADLINE    "
                             "DLINE     "
                             "LINE      "
                             "INE       "
                             "NE        "
                             "E         "
                             "          ")

The scroller should end with a string of 10 spaces again.

Thanks to this site for the problem idea, where it is rated Very Hard in Ruby. The problem has been modified.

Please submit your solutions as comments on this gist.

To subscribe: https://purelyfunctional.tv/newsletter/

@steffan-westcott
Copy link

steffan-westcott commented Jun 1, 2021

(defn scroller [s width]
  (let [blank (apply str (repeat width " "))
        text (str blank s blank)]
    (map #(subs text % (+ % width)) (-> s count (+ width 1) range))))

@KingCode
Copy link

KingCode commented Jun 1, 2021

(defn scroller [txt width]
  (let [padding (-> width (repeat " "))
        banner (-> [] (into padding) (into txt) (into padding))
        len (count txt)
        rows (-> len (* 2) ;; one row for each dis/appearing char. 
                  (+ (- width len)) ;; + while fully visible
                  inc)]
    (->> (range rows) 
         (map #(->> banner (drop %) (take width) (apply str))))))

@vpetruchok
Copy link

vpetruchok commented Jun 1, 2021

(defn scroller [s width]
  (let [xs (concat (repeat width \space) s)
        ys (cycle xs)]
    (for [l (range (+ (count s) width 1))]
      (apply str (->> ys
                  (drop l)
                  (take width))))))

@diavoletto76
Copy link

diavoletto76 commented Jun 1, 2021

(defn padded-headline [h n]
  (let [spaces (clojure.string/join "" (repeat n \space))]
    (str spaces h spaces)))

(defn scroller [h n]
  (let [ph (padded-headline h n)]
    (->> (range (+ 3 (* 2 (count h))))
         (map (fn [x] (clojure.string/join "" (take n (drop x ph))))))))

@mchampine
Copy link

mchampine commented Jun 2, 2021

(defn scroller [hl n]
  (let [ps (partition n 1 (cycle (concat (repeat n " ") hl)))]
    (take (inc (+ (count hl) n)) (map (partial apply str) ps))))

@bgrabow
Copy link

bgrabow commented Jun 2, 2021

(defn scroller
  [h n]
  (->> (apply str (concat (repeat n " ") h (repeat n " ")))
      (partition n 1)
      (map #(apply str %))))

@arthurulacerda
Copy link

arthurulacerda commented Jun 3, 2021

(defn scroller
  [st leds]
  (map (fn [frame]
         (subs (apply str (concat (repeat leds " ") st (repeat leds " "))) 
               frame 
               (+ frame leds)))               
       (range 0 (+ (count st) leds 1))))

@ndonolli
Copy link

ndonolli commented Jun 3, 2021

(defn scroller [text size]
 (let [pad (repeat size " ")] 
  (->> (concat pad text pad)
       (partition size 1)
       (map (partial apply str)))))  

@alex-gerdom
Copy link

alex-gerdom commented Jun 3, 2021

(defn scroller [s width]
   (let [blanked (apply str (repeat width " "))
         combined (concat blanked s blanked)]
     (->> combined
          (partition width 1)
          (map (partial apply str)))))

@sztamas
Copy link

sztamas commented Jun 9, 2021

(defn scroller [headline width]
  (let [cleared-screen  (repeat width " ")
        padded-headline (apply str (concat cleared-screen headline cleared-screen))]
    (->> (range (+ (count headline) width 1))
         (map #(subs padded-headline % (+ % width))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment