Last active
March 14, 2019 09:25
-
-
Save ak-ymst/be1f8122efd0c1fc90e354d99002623e to your computer and use it in GitHub Desktop.
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
-- Q0 | |
(defun* q0 (a &optional (acc "")) | |
(if (string= a "") | |
acc | |
(q0 (subseq a 0 -1) (concat acc (subseq a -1))) | |
) | |
) | |
-- Q1 | |
(defun* q1(a &optional (acc "")) | |
(cond | |
((= (length a) 0) acc) | |
((= (length a) 1) (concat acc a)) | |
(t (q1 (subseq a 2) (concat acc (car-string a)))) | |
) | |
) | |
-- Q2 | |
(defun* q2(a b &optional (acc "")) | |
(if (string= a "") | |
acc | |
(kaban b (cdr-string a) (concat acc(car-string a))) | |
) | |
) | |
-- Q3 | |
(defun q3 (src) | |
(mapcar (function (lambda(x) (length x))) (split-string (replace-regexp-in-string "[,.]" "" src))) | |
) | |
-- Q4 | |
(defun* q4(elements lengths index acc) | |
(cond | |
((= (length elements) 0) acc) | |
(t | |
(puthash index (subseq (car elements) 0 (car lengths)) acc) | |
(q4 (cdr elements) (cdr lengths) (+ index 1) acc) | |
) | |
) | |
) | |
(defun Q4(elements) (q4 (split-string elements) '(1 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 2 2 1 2) 0 (make-hash-table :test 'equal))) | |
(Q4 "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.") | |
(defun* sublist(src rest &optional (acc '())) | |
(cond | |
((= rest 0) (reverse acc)) | |
((= (length src) 0) (reverse acc)) | |
(t (sublist (cdr src) (- rest 1) (push (car src) acc))) | |
) | |
) | |
(defun string-to-list(src) | |
(reverse (cdr (reverse (cdr (split-string src ""))))) | |
) | |
(defun ngram(src size acc) | |
(cond | |
((< (length src) size) (reverse acc)) | |
(t (ngram (cdr src) size (push (sublist src size) acc))) | |
) | |
) | |
(defun char-ngram(stc size) | |
(mapcar (lambda(x) (mapconcat 'identity x "")) (ngram (string-to-list (replace-regexp-in-string "[ ]" "" str)) size '())) | |
) | |
(defun word-ngram(str size) | |
(ngram (split-string str) size '()) | |
) | |
(defun Q5(str) | |
(setq result '()) | |
(push (char-ngram str 2) result) | |
(push (word-ngram str 2) result) | |
(reverse result) | |
) | |
(Q5 "I am an NLPer") | |
(defun* string-uniq(x &optional (acc '())) | |
(cond ((= (length x) 0) (reverse acc)) | |
((member (car x) acc) (string-uniq (cdr x) acc) ) | |
(t (string-uniq (cdr x) (cons (car x) acc)) | |
) | |
) | |
) | |
(defun* string-union(x y) | |
(string-uniq (append x y)) | |
) | |
(string-union'("a" "b" "c") '("A" "b" "C")) | |
(defun* string-intersection(x y &optional (acc '())) | |
(cond ((= (length y) 0) acc) | |
((member (car y) x) (string-intersection x (cdr y) (cons (car y) acc) )) | |
(t (string-intersection x (cdr y) acc)) | |
) | |
) | |
(defun Q6(command x y) | |
(cond ((string= (upcase command) "AND") (string-intersection (char-ngram x 2) (char-ngram y 2))) | |
((string= (upcase command) "OR") (string-union (char-ngram x 2) (char-ngram y 2))) | |
(t "unsupported command!") | |
) | |
) | |
Q6 | |
(Q6 "and" "paraparaparadise" "paragraph") | |
(defun* Q7(f &rest list) | |
(eval (cons 'format (cons f list))) | |
) | |
(Q7 "Hello %s" "world") | |
(Q7 "Hello %s %s" "world" "!!") | |
-- 08. 暗号文 | |
-- 与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ. | |
-- * 英小文字ならば(219 - 文字コード)の文字に置換 | |
-- * その他の文字はそのまま出力 | |
(string-to-char "A") | |
65 | |
(string-to-char "Z") | |
90 | |
(string-to-char "a") | |
97 | |
(string-to-char "z") | |
122 | |
(defun lower?(x) | |
(and (> (string-to-char x) 96) (< (string-to-char x) 123)) | |
) | |
lower\? | |
(defun encrypt-char(x) | |
(if (lower? x) (char-to-string (- 219 (string-to-char x))) x) | |
) | |
encrypt-char | |
(defun encrypt-string(str) | |
(mapconcat (lambda(x) (encrypt-char x)) (string-to-list str) "") | |
) | |
(encrypt-string "aBc") | |
(defun Q8(command target) | |
(cond ((string= command :encrypt) (encrypt-string target)) | |
((string= command :decrypt) (encrypt-string target)) | |
(t "unsupported command!") | |
) | |
) | |
Q8 | |
(Q8 :decrypt (Q8 :encrypt "abcdwxyz")) | |
--- 09. Typoglycemia | |
--- スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ. | |
--- ただし,長さが4以下の単語は並び替えないこととする. | |
--- 適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")を与え,その実行結果を確認せよ. | |
(defun need-suffle?(x) | |
(> (length x) 4) | |
) | |
need-suffle\? | |
(need-suffle? "abcde") | |
(random 0 10) | |
(nthcdr 2 '("a" "b" "c" "d")) | |
(delete "b" '("a" "b" "c" "b")) | |
(random (length '("a" "b"))) | |
(defun n-shift(n list acc) | |
) | |
(defun* delete-by-index(n list &optional (acc '())) | |
(cond ((= (length list) 0) (reverse acc)) | |
((= n 0) (append (reverse acc) (cdr list))) | |
(t (delete-by-index (- n 1) (cdr list) (cons (car list) acc))) | |
) | |
) | |
(delete-by-index 4 '("a" "b" "c" "d") '()) | |
(delete-by-index 0 '("a" "b" "c" "d")) | |
(setq tmp '()) | |
nil | |
(remove-duplicates '("a" "b" "a" "c" "A") :test 'string=) | |
("b" "a" "c" "A") | |
(random 2) | |
(defun random-range(min max) | |
(+ (random (- max min)) min) | |
) | |
random-range | |
(defun* suffle-chars(n list acc) | |
(cond | |
((= (length list) 2) (append (reverse acc) list) ) | |
(t (suffle-chars (random-range 0 (- (length list) 2) ) (delete-by-index n list) (cons (nth n list) acc))) | |
) | |
) | |
suffle-chars | |
(suffle-chars 0 '("a" "b" "c" "d" "e") '()) | |
(defun suffle-word(word) | |
(if (need-suffle? word) | |
(apply 'concat (suffle-chars 0 (string-to-list word) '())) | |
word | |
) | |
) | |
(defun Q9(line) | |
(mapconcat (lambda(word) (suffle-word word)) (split-string line) " ") | |
) | |
(Q9 "AbcdefghijZ") | |
(Q9 "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .") | |
(defun read-file-by-line(path) | |
(split-string | |
(with-temp-buffer | |
(insert-file-contents path) | |
(buffer-substring-no-properties (point-min) (point-max)) | |
) | |
"\n" | |
) | |
) | |
read-file-by-line | |
(defun Q10(path) | |
(length (read-file-by-line path)) | |
) | |
(Q10 "./hightemp.txt") | |
> wc -l ./hightemp.txt | |
(defun Q11(path) | |
(mapcar (lambda(str) (replace-regexp-in-string "[\t]" " " str) ) (read-file-by-line path)) | |
) | |
Q11 | |
(Q11 "./hightemp.txt") | |
> sed "s/\t/ /g" ./hightemp.txt | |
(defun select-nth(n list) | |
(mapcar (lambda(record) (nth n record )) list) | |
) | |
select-nth | |
(defun file-to-matrix(path sep) | |
(mapcar (lambda(str) (split-string str sep)) (read-file-by-line path)) | |
) | |
file-to-matrix | |
(defun write-file-list(file list) | |
(with-temp-buffer | |
(mapcar (lambda(element) (insert element "\n") ) list) | |
(write-file file) | |
) | |
) | |
write-file-list | |
(defun Q12(path) | |
(write-file-list (format "~/column%d.txt" (+ 0 1)) (select-nth 0 (file-to-matrix path "\t"))) | |
(write-file-list (format "~/column%d.txt" (+ 1 1)) (select-nth 1 (file-to-matrix path "\t"))) | |
t | |
) | |
Q12 | |
(Q12 "./hightemp.txt") | |
(defun chain(x y acc) | |
(cond ((= (length x) 0) (reverse acc)) | |
(t (chain (cdr x) (cdr y) (cons (concat (car x) "\t" (car y)) acc))) | |
) | |
) | |
chain | |
(defun Q13(one other dest) | |
(write-file-list | |
dest | |
(chain (read-file-by-line one) (read-file-by-line other) '()) | |
) | |
) | |
Q13 | |
(Q13 "~/column1.txt" "~/column2.txt" "~/concated_list.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment