Skip to content

Instantly share code, notes, and snippets.

@takikawa
Last active August 29, 2015 14:03
Show Gist options
  • Save takikawa/d55480f2f83a22d101c4 to your computer and use it in GitHub Desktop.
Save takikawa/d55480f2f83a22d101c4 to your computer and use it in GitHub Desktop.
#lang racket
;; 1. Get the HTML source of a web page
(require net/url)
(port->string (get-pure-port (string->url "http://www.racket-lang.org")))
#lang racket
;; 3. Decimal digit length of 42!
(require math)
(string-length (number->string (factorial 42)))
#lang web-server/insta
;; 4. Set up an HTTP server that returns the current timestamp
(require racket/date)
(define (start req)
(response/output
(λ (op) (display (date->string (current-date)) op))))
#lang racket
;; 5. Split a string on dashes, reverse the order of the elements and join them using slashes
(string-join (reverse (string-split "1969-07-20" "-")) "/")
#lang racket
;; 6. Convert all JPG files in the current directory to PNG format
(require racket/draw)
(for ([file (directory-list (current-directory))]
#:when (file-exists? file)
#:when (equal? (filename-extension file) #"jpg"))
(call-with-input-file file
(λ (in)
(send (read-bitmap in) save-file (path-replace-suffix file ".png") 'png))))
#lang racket
;; 7. Sum of the first 64 primes
(require math)
(sum (next-primes 0 64))
#lang racket
;; 8. Extract a Unix format timestamp from the 5th to 8th byte of a byte array given in hex
(require file/sha1)
(seconds->date
(integer-bytes->integer
(hex-string->bytes "CAFEBABE4422334400FF")
#t #t 4 8))
#lang racket/gui
;; 9. Show a PNG image retrieved from a URL
;; Note: run in DrRacket
(require net/url)
(call/input-url (string->url "http://www.racket-lang.org/logo-and-text.png")
get-pure-port
(λ (in) (read-bitmap in 'png)))
#lang racket
;; 10. Verify that a fraction is close to ∏
;; Note: not quite the same, but similar
(require math)
(< (absolute-error (/ 355 113) pi) 0.00001)
#lang racket
;; 11. Test whether one set is included in another one
(subset? (set 'f 'd 'b) (set 'a 'b 'c 'd 'e 'f))
#lang racket
;; 13. Average of the prime factors of 2^32 — 1
(require math)
(mean (prime-divisors (sub1 (expt 2 32))))
#lang racket
(require math)
(stddev '(2 4 4 4 5 5 7 9) #:bias #t)
#lang racket
;; 18. Save the HTML source of a web page to a file
(require net/url)
(copy-port (get-pure-port (string->url "http://www.racket-lang.org"))
(open-output-file "page.html"))
#lang racket
;; 19. Basic grep on a file using a regular expression
(for/list ([line (port->lines (open-input-file "/tmp/foo.txt"))]
#:when (regexp-match? #rx"^.*.jpg" line))
line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment