-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg-exrc.lisp
293 lines (232 loc) · 6.92 KB
/
pg-exrc.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
;----- print list recursivly -----------;
(defun prnt_list_reverse(lst)
(unless (null lst)
(progn
(prnt_list_reverse (cdr lst))
(format t "~A " (car lst)))))
;CL-USER> (prnt_list_reverse '(1 2 3 4 5 6 7 8 9))
;9 8 7 6 5 4 3 2 1
;----- cptr 3 - qn2 -----------;
;; the input lists should be sets
(defun my-union(llst rlst)
(if(null llst) rlst
(get-union llst rlst)))
(defun get-union(llst rlst)
(if(null rlst) llst
(if(null (member (car rlst) llst))
(get-union (append llst (list(car rlst))) (cdr rlst))
(get-union llst (cdr rlst)))))
;----- cptr 3 - qn8 --------------;
;; Deepaks solution
(defun show-dots (list)
(if (null list)
(format t "nil)")
(progn
(format t "(~A . " (car list))
(show-dots (cdr list))
(format t ")"))))
;CL-USER> (show-dots '(a b c))
;(A . (B . (C . nil))))
;----- shortest path fun from book --------------;
(defun shortest-path(start end net)
(bfs end (list (list start)) net))
(defun bfs (end queue net)
(format t "~% Queue ~A" queue)
(if (null queue)
nil
(let ((path (car queue)))
(let ((node (car path)))
(if (eql node end)
(reverse path)
(bfs end
(append (cdr queue)
(new-paths path node net))
net))))))
(defun new-paths (path node net)
(mapcar #'(lambda (n)
(cons n path))
(cdr (assoc node net))))
;----- cptr 3 - qn 9 --------------;
(defun longest-path (start net)
(lbfs (list (list start)) net nil))
(defun lbfs (queue net long-paths)
(format t "~%Queue is ~A || ~A" queue long-paths)
(let ((path (car queue)))
(let ((node (car path)))
(if (null queue)
(print-longest-paths long-paths)
(progn
(if (null (assoc node net))
(setf long-paths (process-paths queue path long-paths))
(setf long-paths nil))
(lbfs (append (cdr queue) (new-paths path node net))
net long-paths))))))
(defun process-paths (queue path long-paths)
(remove path queue)
(if (> (length path) (length (car long-paths)))
(setf long-paths nil))
(append (list path) long-paths))
(defun print-longest-paths (long-paths)
(format t "~%~%Longest paths are ~A"
(mapcar #'(lambda (n)
(reverse n))
long-paths)))
;;; OUTPUT ;;;
;CL-USER> (setf net '((a b c) (b c f) (c d e) (d) (e d h g) (f)))
;((A B C) (B C F) (C D E) (D) (E D H G) (F))
;CL-USER> (longest-path 'a net)
;Queue is ((A))
;Queue is ((B A) (C A))
;Queue is ((C A) (C B A) (F B A))
;Queue is ((C B A) (F B A) (D C A) (E C A))
;Queue is ((F B A) (D C A) (E C A) (D C B A) (E C B A))
;Queue is ((D C A) (E C A) (D C B A) (E C B A))
;Queue is ((E C A) (D C B A) (E C B A))
;Queue is ((D C B A) (E C B A) (D E C A) (H E C A) (G E C A))
;Queue is ((E C B A) (D E C A) (H E C A) (G E C A))
;Queue is ((D E C A) (H E C A) (G E C A) (D E C B A) (H E C B A) (G E C B A))
;Queue is ((H E C A) (G E C A) (D E C B A) (H E C B A) (G E C B A))
;Queue is ((G E C A) (D E C B A) (H E C B A) (G E C B A))
;Queue is ((D E C B A) (H E C B A) (G E C B A))
;Queue is ((H E C B A) (G E C B A))
;Queue is ((G E C B A))
;Queue is NIL
;Longest paths are ((A B C E H) (A B C E G))
;-------------------;
(defun second-word(str)
(let ((p1 (+ (position #\ str) 1)))
(subseq str p1 (position #\ str :start p1))))
;CL-USER> (second-word "mahesh pandurang jadhav")
;"pandurang"
;-------remove-duplicate function pg 66------------;
(defun call-remove-duplicate(pstr)
(my-remove-duplicate pstr 0))
(defun my-remove-duplicate (str pos)
(let ((len (length str)))
(if (or (null str) (equal 1 len) (equal (+ pos 1) len))
str
(if (not (equal pos (position (char str pos) str :from-end t)))
(my-remove-duplicate
(concatenate 'string (subseq str 0 pos) (subseq str (+ pos 1) len))
pos)
(my-remove-duplicate
str
(+ pos 1))))))
;CL-USER> (call-remove-duplicate "abracadabra")
;1 abracadabra
;1 bracadabra
;1 racadabra
;1 acadabra
;2 cadabra
;1 cadabra
;2 cdabra
;1 cdabra
;2 cdbra
;2 cdbra
;"cdbra"
(defun my-remove-duplicate-2 (str)
(let ((stack) (result))
(dotimes (pos (length str))
(pushnew (char (reverse str) pos) stack))
(dolist (obj stack)
(setf result (concatenate 'string result (string (pop stack)))))
result))
;--------------------------------;
; 2.8.a (30);
(defun print-dots (int)
(do ((i 1 (+ i 1)))
((> i int) nil)
(format t ". ")))
(defun rs-print-dots (int)
(unless (zerop int)
(progn
(rs-print-dots (- int 1)))
(format t ". ")))
;--------------------------------;
; 2.8.b (30)
(defun init-count-a (lst)
(if (listp lst)
(count-a lst 0)))
(defun count-a (lst x)
(format t "~A ~A~%" lst x)
(if (null lst) ; stop
(format t "~A" x) ; return
(if (eql (car lst) 'a) ; small step
(count-a (cdr lst) (+ 1 x)) ; rest
(count-a (cdr lst) x))))
;--------------------------------;
;GCL 8.1
(defun anyoddp (lst)
(cond ((null lst) nil)
((oddp (car lst)) t)
(t (anyoddp (cdr lst)))))
;GCL 8.2
(defun anyoddp-if (lst)
(if (null lst) nil
(if (oddp (car lst)) t
(anyoddp-if (cdr lst)))))
;GCL 8.4
(defun laugh (n)
(cond ((zerop n) nil)
(t (cons 'ha (laugh (- n 1))))))
;GCL 8.5
(defun addup (lst)
(cond ((null lst) 0)
((eq (cdr lst) nil) lst)
(t (addup-n lst))))
(defun addup-n (lst)
(if (null (cdr lst)) (car lst)
(+ (car lst) (addup-n (cdr lst)))))
; GCL 8.6
(defun alloddp (lst)
(cond ((null lst) nil)
(t (alloddp-n lst))))
(defun alloddp-n (lst)
(cond ((null lst) t)
((oddp (car lst)) (alloddp-n (cdr lst)))
(t 'even)))
; GCL 8.9
(defun rec-nth (n lst)
(if (null lst) nil
(rec-nth2 0 n lst)))
(defun rec-nth2 (c n lst)
(if (null lst) nil
(if (eq c n) (car lst)
(rec-nth2 (+ 1 c) n (cdr lst)))))
;GCL 8.10
(defun add1 (x)
(+ x 1))
(defun sub1 (y)
(- y 1))
(defun rec-plus (x y)
(cond ((zerop y) x)
(t (rec-plus (add1 x) (sub1 y)))))
;GCL 8.10.n
(defun rec-plus-n (lst)
(if (null (cdr lst)) (car lst)
(rec-plus-n
(cons
(setf (car (cdr lst))
(rec-plus (car lst) (car (cdr lst))))
(cdr (cdr lst))))))
;; given an array having 1 duplicate value find duplicate value
;; (find-duplicate '(1 8 3 9 0 22 66 29 200 17 100 33 88 23 27 31 3))
;; (find-duplicate '(1 2 3 4 5 6))
;; (find-duplicate '())
(defun find-duplicate (arr)
(if (null arr)
nil
(let ((sorted (sort arr #'<)))
(let ((rst (cdr sorted)))
(recurse (car sorted) rst rst)))))
(defun recurse (ptr arr oarr)
(if (null oarr)
nil
(let ((rst (cdr oarr)))
(if (null arr)
(recurse (car oarr) rst rst)
(if (= ptr (car arr))
ptr
(if (< ptr (car arr))
(recurse ptr (cdr arr) oarr)
(recurse (car oarr) rst rst)))))))