Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
pseudocode and data definitions
#lang racket
; for symbol->char contract
(define (single-symbol? sym)
(= 1 (string-length (symbol->string sym))))
(define (each-unique? lst)
(= (length lst)
(length (remove-duplicates lst))))
; could make a struct later if data type gets more complex
(define instruction/c (cons/c char? number?))
; for contracts
(define (valid-char? chr)
(and (char? chr)
(memq chr initial-queue)))
; for contracts
(define (valid-num? num)
(and (number? num)
[num . < . (length initial-queue)]))
; for contracts
(define (valid-input? inp)
(and (list? inp)
(= (count-numbers inp) (count-letters inp))
(andmap (λ (datum) (if (char? datum)
(valid-char? datum)
(valid-num? datum)))
(each-unique? inp)))
; count-numbers : list -> number
(define (count-numbers lst)
(let aux ([lst lst] [total 0])
(cond [(empty? lst) total]
[(number? (first lst))
(aux (rest lst) (add1 total))]
(aux (rest lst) total)])))
(define (count-letters lst)
(foldl (λ (elem total) (if (char? elem)
(add1 total)
0 lst))
(define initial-queue (list #\A #\B #\C #\D #\E #\F #\G #\H))
[initial-queue (listof char?)]
[symbol->char (-> single-symbol? char?)]
[input->assoc-list (-> valid-input?
(listof instruction/c))]
[tip-the-queue (-> list? pair?
(provide count-numbers
(define (symbol->char sym)
(string-ref (symbol->string sym) 0))
(define (input->assoc-list vals)
(local [(define (extract-next upcoming [seen '()] #:type identifier?)
(let ([next (first upcoming)])
(if (identifier? next)
(values next (append (reverse seen) (rest upcoming)))
(extract-next (rest upcoming) (cons next seen) #:type identifier?))))]
(cond [(empty? vals) '()]
[(number? (first vals)) (define-values [ltr inp] (extract-next #:type char? (rest vals)))
(cons (cons ltr (first vals))
(input->assoc-list inp))]
[else ;char?
(define-values [num inp] (extract-next #:type number? (rest vals)))
(cons (cons (first vals) num)
(input->assoc-list inp))])))
(define (tip-the-queue q instr)
(local [(define chr (car instr))
(define pos (cdr instr))
(front back) (split-at q (add1 pos)#;"Add1 because otherwise this inserts BEFORE pos"))]
(set! front (remq chr front))
(set! back (remq chr back))
(append front (cons chr back))))
#lang racket
(require "Queue-Tip.rkt")
; set current-input-port to a particular string OR comment all out to read from user
(current-input-port (open-input-string "3 A B 5"))
;(current-input-port (open-input-string "A H 1 6 7 E"))
;(current-input-port (open-input-string "A 1 A 2"))
;(current-input-port (open-input-string "A 1 B 3 C"))
(define alphabet-gods-decree
;; (port->list)))
(for/list ([datum (in-port)])
(if (number? datum)
(symbol->char datum)))))
(define answer
(foldl (λ (instruction Q)
(tip-the-queue Q instruction))
(printf "The original queue was ~a" initial-queue)
(printf "According to the will of the alphabet gods, the new-- and most assuredly IMPROVED-- queue is ~a"
#lang racket
(require rackunit
; could consider require/expose
(define-test-suite 3AB5
"Tests for if the list of instructions is (3 A B 5)"
(define lst '(3 #\A #\B 5))
(check = 2 (count-letters lst))
#;(check-equal? (input->assoc-list '(3 #\A #\B 5))
'((#\A . 3) (#\B . 5)))
;(check-equal? 3 (count-letters '(#\A #\H 1 6 7 #\E)))
;(check-equal? (input->assoc-list '(#\A #\H 1 6 7 #\E))
; '((#\A . 1) (#\H . 6) (#\E . 7)))
class TranSlayTor
attr_accessor :content
class TextObj
attr_accessor :textType, :pos, :content
# data definitions
# textTypeIdentifier are bits of regexes, and these bits of regexes
# should be manipulatable/composable by CONSECUTIVE and UNIQUE
character = /[A-Za-z]/ # N.B. only one.
vowel = /[aeiou]/i
consonant = "" # AND character NOT vowel;
number = /\d+/ # if given x100x it needs to be greedy and get 100; not 10.
# UPDATE after trying in IRb, it seems to be greedy by default
word =
# character ==> \w ==> ([A-Z][a-z])
# @TODO should character be regarded only as a letter?
# vowel ==> (aeiou) y?
# number ==> <digit>+
# digit ==> \d ==> [0-9]
# word ==> \b+\w+\b+ ==>
# line IS ^.*$ ==> <word>*<newline>
# textType = character | consonant | vowel | number | word | line
# textObj IS at least a position, perhaps bound with the type and content
# consecutive is a function combinator
# consecutive(textType, inputStr)
# unique(textType, inputStr)
# replace_with(textObj, inputStr)
# @IDEA: a function that takes an inputStr of any length and returns all of the symbols that it validates
# plural?(inputChunk)
# if substring to one before end is a valid symbol, and final is "s", then TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.