DrRacket代写: CSC104 Project #1

; CSC104 Project #1
; Classical encryption project
; Due: 10 p.m., October 28
; (subject to class vote October 21)
;
; tools for reading from files and web pages
(require 2htdp/batch-io)
(require net/sendurl)
;
; This project involves two classical encryption algorithms.
; The techniques are called “classical” because they were made
; obsolete by encryption technology during the last 80 years.
; You can find out more by cutting-and-pasting the following
; into the interactions pane:
;
; (require net/sendurl)
; (send-url “https://en.wikipedia.org/wiki/Classical_cipher”)

; Your job:
; I have left several partly-completed parts of code for
; you to complete below. Everywhere you find ### there is some work
; for you to do. Check your work by running this file.
; When you have some, or all, of it working, submit it by going
; to:
;
; (require net/sendurl)
; (send-url “https://markus.teach.cs.toronto.edu/csc104-2016-09/”)
;
; If you worked in a group of (up to 3) stduents: select one person from
; your group to submit the file. Only that person should select “Create”
; from the “Group Information” section of “Assignment P1”. That person can
; then invite the other member(s) of the group to join their group. If you
; are joining someone’s group: do not click “Work alone” or “Create”.
; Instead, wait for your group’s creator’s invitation to appear under your
; “Group Information”, and then accept their invitation.

; ### fix the body of function ‘string-quotient’ below so that it works.
; You may need to look up ‘quotient’
(check-expect (string-quotient “thirty-two” “one”) 3)
; Here is a full-design check-expect to help you out
(check-expect (string-quotient “thirty-two” “one”)
(quotient
(string-length “thirty-two”)
(string-length “one”)))
; string-quotient : string string -> number
; Quotient of string1’s length divided by string2’s length
(define (string-quotient string1 string2)
0)

(check-expect (string-multi-append 3 “one”)
“oneoneone”)
; Here’s a partial design check-expect:
(check-expect (string-multi-append 3 “one”)
(apply
string-append
(list “one” “one” “one”)))
; ### Change the check-expect below into a
; full-design check-expect for
; ‘string-multi-append’. The full-design check-expect
; should be easy to change into a function definition by
; simply changing ‘check-expect’->’define’ and replacing
; function arguments by place-holders.
; You may want to look up ‘make-list’
(check-expect (string-multi-append 3 “one”)
“oops”)
; ### Now mimic the check-expect you just wrote to fix
; the body of the function definition for
; ‘string-append-multi’ below.
;
; string-multi-append : number string -> string
; Append a-number copies of a-string
(define (string-multi-append a-number a-string)
“oopsoops”)

(check-expect (secret-subtext “abra” “cadabra”)
“abraabr”)
; ### Change the broken check-expect below into a
; full-design check-expect for ‘secret-subtext’
; Remember, this should have nearly the form of the function
; definition, but have actual values where the function definition
; has placeholders
(check-expect (secret-subtext “abra” “cadabra”)
“gosh…”)
; secret-subtext : string string -> string
; Concatenate enough copies of secret to make
; same length as text
(define (secret-subtext secret text)
(substring
(string-multi-append
(add1 (string-quotient text secret))
secret)
0 (string-length text)))

(check-expect (compute-mod-128 + 70 70) 12)
; Here is a full-design check-expect:
(check-expect (compute-mod-128 + 70 70)
(modulo
(+ 70 70)
128))
; ### Use the full-design check-expect above to fix
; the body of ‘compute-mod-128’ below
;
; compute-mod-128 : function number number -> number
; The remainder after dividing by 128 of
; (operator number1 number2)
(define (compute-mod-128 operator number1 number2)
42)

(check-expect (compute-text-mod-128 + “ABC” “ppp”)
“123”)
(check-expect (compute-text-mod-128 – “123” “ppp”)
“ABC”)
; full-design check-expect for compute-text-mod-128
(check-expect (compute-text-mod-128 + “ABC” “ppp”)
(list->string
(map integer->char
(map compute-mod-128
(make-list (string-length “ABC”) +)
(map char->integer (string->list “ABC”))
(map char->integer (string->list “ppp”))))))
; ### Fix the broken body of ‘compute-text-mod-128’ by mimicking
; the full-design check-expect above.
; compute-text-mod-128 : string string -> string
; Compute string characters, take remainder after dividing by
; 128, to generate new string characters. Strings must be
; the same length.
(define (compute-text-mod-128 operator string1 string2)
“X”)

(check-expect (vigenere-encode “ABC” “ppp”) “123”)
; encode : string string -> string
; Encode text using secret.
(define (vigenere-encode text secret)
(compute-text-mod-128
+
text
(secret-subtext secret text)))

(check-expect (vigenere-decode “123” “ppp”) “ABC”)
; decode : string string -> string
; Decode text using secret.
(define (vigenere-decode text secret)
(compute-text-mod-128

text
(secret-subtext secret text)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; transposition cipher

(check-expect (inverse-mod 31 23)
7)
; inverse-mod : number number -> number
; inverse multiplicative inverse by p mod n+1
(define (inverse-mod p n)
(add1
(one-position
(prime-permutation p n))))

(check-expect (one-position (list 0 1 2)) 1)
; one-position : list -> number
; The position of the first one in a list
(define (one-position a-list)
; ### fix local function p so that it says whether
; there is a 1 at position n in a-list
(local [(define (p n) #true)]
(first (filter p (range 0 (length a-list) 1)))))

(check-expect (position-list->char-list
(list 0 2 1)
(list #\A #\B #\C))
(list #\A #\C #\B))
; position-list->char-list
;
(define (position-list->char-list L1 L2)
; ### Fix function ‘char-at’ so that it returns
; the character at position n in list L2
(local [(define (char-at n) #\A)]
(map char-at L1)))

(check-expect (scramble-string
(list 0 2 1) “ABC”)
“ACB”)
; ### Fix the broken check-expect below so that it is a
; full-design check-expect with the same structure
; as the function definition for scramble-string
(check-expect (scramble-string (list 2 0 1) “ABC”)
“ABC”)
;
; scramble-string : list string -> string
; scramble characters in a-string to positions
; in a-list
(define (scramble-string a-list a-string)
(list->string
(position-list->char-list
a-list (string->list a-string))))

(check-expect (p-scramble 37 “ABCD”) “BDAC”)
; ### Fix the broken check-expect below so that it is a
; full-design check-expect with the same structure as
; the function definition for p-scramble.
(check-expect (p-scramble 37 “ABCD”)
“ABCD”)
;
; p-scamble-string : number string -> string
; scramble a-string using a-prime
(define (p-scramble a-prime a-string)
(scramble-string
(map
sub1
(prime-permutation a-prime (string-length a-string))) a-string))

(check-expect (p-unscramble 37 “BDAC”) “ABCD”)
; p-unscramble : number string -> string
; unscramble a-string that has been scrambled usig a-prime
(define (p-unscramble a-prime a-string)
(p-scramble
(inverse-mod a-prime (string-length a-string))
a-string))

; prime-permutation : number number -> list
; list number from 1 to a-number permutated
; by multiplication by prime
(define (prime-permutation prime a-number)
(local [(define (mult-prime n)
(remainder (* prime n) (add1 a-number)))]
(map mult-prime (range 1 (add1 a-number) 1))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Try out encryption on Alice in Wonderland
;;
;; I’ve provided a text file containing Lewis Carrol’s “Alice in Wonderland” called
;; alice30.txt
;;
;; You can read this in as a *very* large string, encode and decode it.
;;
;; Cut-and-paste the expressions below into the interactions pane to see how they work.
;; Remember the strings are *very* large, so you should examine them using substring
;;
;; Notice that scrambling with 37 won’t work for strings that are multiples of 37. In
;; those cases, you will have to find, use, and share another prime number.

;(define ALICE (read-file “alice30.txt”))
;(substring ALICE 0 400)
;(define ALICE-VIGENERE-ENCODED (vigenere-encode ALICE “Where is Waldo?”))
;(substring ALICE-VIGENERE-ENCODED 0 400)
;(define ALICE-VIGENERE-DECODED
; (vigenere-decode ALICE-VIGENERE-ENCODED “Where is Waldo?”))
;(substring ALICE-VIGENERE-DECODED 0 400)
;(define ALICE-SCRAMBLED (p-scramble 37 ALICE))
;(substring ALICE-SCRAMBLED 0 400)
;(define ALICE-UNSCRAMBLED (p-unscramble 37 ALICE-SCRAMBLED))
;(substring ALICE-UNSCRAMBLED 0 400)