Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Last active June 14, 2021 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnormand/04bb706198c23b8554aaf76c4edada7b to your computer and use it in GitHub Desktop.
Save ericnormand/04bb706198c23b8554aaf76c4edada7b to your computer and use it in GitHub Desktop.
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/

@arthurulacerda
Copy link

(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

(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