schemeracket

Upside down text


How would you design a program that will take in a string of lower case letters and produce the string upside down?

so if I type in home

i get ǝɯoɥ upside down.

I've tried looking for in the book to get started, but nothing.


Solution

  • Try this, a bit of a brute-force approach but works quite well for uppercase, lowercase and number characters - all other characters are presented just as they come:

    (define upside-map '#hash(
      (#\a . #\ɐ) (#\b . #\q) (#\c . #\ɔ) (#\d . #\p) (#\e . #\ǝ) (#\f . #\ɟ)
      (#\g . #\ƃ) (#\h . #\ɥ) (#\i . #\ı) (#\j . #\ɾ) (#\k . #\ʞ) (#\l . #\ן)
      (#\m . #\ɯ) (#\n . #\u) (#\o . #\o) (#\p . #\d) (#\q . #\b) (#\r . #\ɹ)
      (#\s . #\s) (#\t . #\ʇ) (#\u . #\n) (#\v . #\ʌ) (#\w . #\ʍ) (#\x . #\x)
      (#\y . #\ʎ) (#\z . #\z) (#\A . #\∀) (#\B . #\𐐒) (#\C . #\Ɔ) (#\D . #\◖)
      (#\E . #\Ǝ) (#\F . #\Ⅎ) (#\G . #\⅁) (#\H . #\H) (#\I . #\I) (#\J . #\s)
      (#\K . #\⋊) (#\L . #\˥) (#\M . #\W) (#\N . #\N) (#\O . #\O) (#\P . #\Ԁ)
      (#\Q . #\Ό) (#\R . #\ᴚ) (#\S . #\S) (#\T . #\⊥) (#\U . #\∩) (#\V . #\Λ)
      (#\W . #\M) (#\X . #\X) (#\Y . #\⅄) (#\Z . #\Z) (#\0 . #\0) (#\1 . #\Ɩ)
      (#\2 . #\ᄅ) (#\3 . #\Ɛ) (#\4 . #\ㄣ) (#\5 . #\ϛ) (#\6 . #\9) (#\7 . #\ㄥ)
      (#\8 . #\8) (#\9 . #\6)))
    
    (define (flip-string str)
      (list->string
       (map (lambda (c)
              (hash-ref upside-map c (const c)))
            (reverse (string->list str)))))
    

    For example:

    (flip-string "Hello World")
    => "pןɹoM oןןǝH"
    

    For reference, I used this conversion table taken from Wikipedia.