-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAst1_AC.js
280 lines (254 loc) · 7.6 KB
/
Ast1_AC.js
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
/*
* Anthony Courchesne
* 260688650
* COMP302 - Assignment 1
* Prof. C. Verbrugge
*
* 19 Septembre 2016
*/
function cons(a,b) {
return function (selector) {
if (selector=='areyoualist?')
return 'yesIam';
return selector(a,b);
};
}
function car(list) {
function carHelper(a,b) {
return a;
}
return list(carHelper);
}
function cdr(list) {
function cdrHelper(a,b) {
return b;
}
return list(cdrHelper);
}
function isList(thing) {
if (typeof(thing)!='function')
return false;
try {
if (thing('areyoualist?')=='yesIam')
return true;
} catch(e) {
}
return false;
}
function show(list) {
var sval;
if (list==null)
sval = '()';
else if (isList(list))
sval = '('+ show(car(list)) +' '+show(cdr(list))+')';
else
sval = String(list);
return sval;
}
//-----------Question 1---------------
console.log("test1");
function cXXXr(ads){
if(ads.length==1){
if(ads == 'a'){
return function (list){
return car(list);
}
}
if(ads == 'd'){
return function (list){
return cdr(list);
}
}
}
if(ads.charAt(0)=='a'){
return function (list){
return car(cXXXr(ads.slice(1,ads.length))(list));
}
}
if(ads.charAt(0)=='d'){
return function (list){
return cdr(cXXXr(ads.slice(1,ads.length))(list));
}
}
}
/*var ls = cons('v',cons('w',cons('x',cons('y',cons('z',null)))));
var cadr = cXXXr('ad');
var caddr = cXXXr('add');
document.writeln("cadr: "+cadr(ls));
document.writeln("caddr: "+caddr(ls));
document.write(cXXXr("adddd")(ls));
*/
//---------------Question 2 ------------------
//this function find a string then returns its path
//Debth-first search
function makeXXX(tree,s){
var left = car(tree);
var right = cdr(tree);
if(!isList(left)){
if(left===s){ //left is the searched string
return "a";
}
}else{ //left is a list
var result = makeXXX(left,s);
if(result!=null){ //string found in left
return result+"a";
}
}
//string not found in left side
if(isList(right)){
var result = makeXXX(right,s);
if(result!=null){ //string found in right
return result+"d";
}
}
//right is null
return null;
}
//var t = rndTree(0.6);
//document.write(show(t.tree));
//document.write("<BR><BR>");
/*
document.write("Target: "+t.target);
document.write("<BR><BR>");
document.write(makeXXX(t.tree,t.target));
document.write("<BR><BR>");
document.write(cXXXr(makeXXX(t.tree,t.target))(t.tree));
*/
//-------------Question 3------------------------------
function wohs(param){
function getLeft(dStr){
//this returns the first part of the list
function getLeftHelper(str,i,pStack){
var character = str.charAt(i);
if(character != '(' && i==0){
//Left part is a string, return it
return str.slice(0,5);
}
if(pStack==0 && i!=0){
return "";
}
if(character=='(')
return character+""+getLeftHelper(str,i+1,pStack+1);
if(character==')')
return character+""+getLeftHelper(str,i+1,pStack-1);
return character+""+getLeftHelper(str,i+1,pStack);
}
return(getLeftHelper(dStr.slice(1,dStr.length-1),0,0));
}
function getRight(dStr){
//this returns the second part of the list
slicedString = dStr.slice(1,dStr.length-1);
return slicedString.slice(getLeft(dStr).length+1);
}
//The core of the function check if an element is null or a string, return it;
// else parse it then return is
var leftPart = getLeft(param);
var rightPart = getRight(param);
var leftConstruct;
var rightConstruct;
if(leftPart.charAt(0)=='('){
leftConstruct = wohs(leftPart); //the left part is a list so parse it
}else{
leftConstruct = leftPart; //The left part is a string so return it like it is
}
if(rightPart=="()"){
rightConstruct = null; //The right part is null so return null
}else{
rightConstruct = wohs(rightPart); //The right part is a list so parse it
}
return cons(leftConstruct,rightConstruct);
}
/*var t = rndTree(0.6);
var display = show(t.tree)
document.write(display);
document.write("<BR><BR>");
document.write("Left: "+getLeft(display));
document.write("<BR><BR>");
document.write("Right: "+getRight(display));
document.write("<BR><BR>");
document.write("Answer:");
document.write("<BR><BR>");
document.write(show(wohs(display)));*/
//-----Question 4----------
function partition(dList){
function buildList(list,func){ //this function take as params the list and a function, returns a list where this function is true
if(list==null){
return null
}
if(func(car(list))==true){ //if current element yield true, return it in the outputlist
return (cons(car(list),buildList(cdr(list),func)));
}
//current element yield false, omit it
return buildList(cdr(list),func);
}
function listOfLists(args){ //this function take as params the array arguments and return list of lists
function listOfListsHelper(args,i){ //i should start at 1
if(args[i]==undefined)
return null;
return cons(buildList(args[0],args[i]),buildList(args[0],args[i+1]));
}
return listOfListsHelper(args,1);
}
return (listOfLists(arguments));
}
/*
function isEven(param){
if(param%2==0)
return true;
return false;
}
function containsA(param){
if(typeof(param)!="string")
return false;
if(param.charAt(0)=='a')
return true;
return false;
}
var theList = cons(2,cons(3,cons(4,cons('abc',cons(9,cons('4ctrsd',cons(12,cons('atefa45r',null))))))));
console.log(show(partition(theList,isEven,containsA)));*/
//-----Question 5-------------
function constructAA(){
return null;
}
function addAA (aa, key, value){
var ls = cons(key,value);
var search = getValueAA(aa, key)
if(search == null){ //if the value was not found
return cons(ls,aa)
}else{ //The value was found somewhere, overwrite it
var leftPart = car(aa);
if(car(leftPart)==key){ //This is the key we were looking for
return cons(cons(key,value),cdr(aa)); //TODO debug
}else{
return cons(leftPart,addAA(cdr(aa),key,value));
}
}
}
function getValueAA (aa, key){
if(!isList(aa)) //Base case
return null;
var leftPart = car(aa); //Store the left part of aa for optimization
if(car(leftPart) == key){ //Check if the key is the right one
return cdr(leftPart); //return its value
}else{
return getValueAA(cdr(aa),key); //If it wasnt found, recursively search the list
}
}
function showAA (aa){
if(!isList(aa)){
return "";
}
return showAA(cdr(aa))+car(car(aa)) +":"+ cdr(car(aa))+"\n";
}
/*var assarr = constructAA();
assarr = addAA(assarr,"first",1);
assarr = addAA(assarr,"Second",2);
assarr = addAA(assarr,"Third",3);
assarr = addAA(assarr, "Fourth",4);
assarr = addAA(assarr, "Fourth",2);
//var a = getValueAA (assarr5, "first");
//assarr5 = addAA(assarr, "Third", "This is not 3");
console.log(showAA(assarr));
console.log(showAA( addAA ( addAA ( constructAA () , "name" , "clark"), "age",1000)));
*/