Created
September 30, 2011 23:23
-
-
Save Josh-Tilles/1255304 to your computer and use it in GitHub Desktop.
pseudocode and data definitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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))) | |
inp) | |
(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))] | |
[else | |
(aux (rest lst) total)]))) | |
(define (count-letters lst) | |
(foldl (λ (elem total) (if (char? elem) | |
(add1 total) | |
total)) | |
0 lst)) | |
(define initial-queue (list #\A #\B #\C #\D #\E #\F #\G #\H)) | |
(provide/contract | |
[initial-queue (listof char?)] | |
[symbol->char (-> single-symbol? char?)] | |
[input->assoc-list (-> valid-input? | |
(listof instruction/c))] | |
[tip-the-queue (-> list? pair? | |
list?)]) | |
(provide count-numbers | |
count-letters) | |
(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)) | |
(define-values | |
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
(input->assoc-list | |
;; (port->list))) | |
(for/list ([datum (in-port)]) | |
(if (number? datum) | |
datum | |
(symbol->char datum))))) | |
(define answer | |
(foldl (λ (instruction Q) | |
(tip-the-queue Q instruction)) | |
initial-queue | |
alphabet-gods-decree)) | |
(printf "The original queue was ~a" initial-queue) | |
(newline) | |
(printf "According to the will of the alphabet gods, the new-- and most assuredly IMPROVED-- queue is ~a" | |
answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require rackunit | |
"Queue-Tip.rkt" | |
#;"Queue-Tip_IO.rkt") | |
; could consider require/expose | |
(define-test-suite 3AB5 | |
"Tests for if the list of instructions is (3 A B 5)" | |
(test-begin | |
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TranSlayTor | |
attr_accessor :content | |
end | |
class TextObj | |
attr_accessor :textType, :pos, :content | |
end | |
# 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