Skip to content

Instantly share code, notes, and snippets.

Last active June 14, 2021 15:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
429 Newsletter

NOTE: If you're looking for issue 430's gist, go here:

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"


(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:

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

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
    (->> (range rows) 
         (map #(->> banner (drop %) (take width) (apply str))))))

Copy link

(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))))))

Copy link

(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))))))))

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

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 %))))

Copy link

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

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

Copy link

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

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