-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradni.html
186 lines (177 loc) · 9.31 KB
/
radni.html
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
<!DOCTYPE html>
<html>
<head>
<title>Radni HTML</title>
<link rel="stylesheet" href="main.css">
<meta charset="utf-8" />
</head>
<body>
<div>
<div id="postavka" class="postavka">
<h1>Radni HTML</h1>
<h4>Strana za testiranje javascripta</h4>
<p>Consider a sequence u where <strong>u</strong> is defined as follows:</p>
<p>
<li>The number <strong>u(0) = 1</strong> is the first one in <strong>u</strong>.</li>
<li>For each <strong>x</strong> in <strong>u</strong>, then <strong>y = 2 * x + 1</strong> and <strong>z = 3 * x + 1</strong> must be in <strong>u</strong> too.</li>
<li>There are no other numbers in <strong>u</strong>.</li>
</p>
<p>Ex: <strong>u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]</strong>
1 gives 3 and 4, then 3 gives 7 and 10, 4 gives 9 and 13, then 7 gives 15 and 22 and so on...</p>
<p>Given parameter <strong>n</strong> the function <strong>dblLinear</strong> returns the element <strong>u(n)</strong> of the ordered (with <) sequence <strong>u</strong>.</p>
<ul>
<li>dbl_linear(10) should return 22</li>
</ul>
</div>
</div>
<div>
<div id="unos" class="unos">
Unesi brojeve razdvojene zarezom (,): <input id="poljeZaUnos" onkeypress="enterGo(event)"><button onclick="clickGo()">GO!</button> <button onclick="clickRandom()">Insert Random</button>
</div>
<div id="testVrednosti" class="testVrednosti">
<h4>Neke generisane vrednosti (poslednje 3):</h4>
<ul id="listaVrednosti" class="listaVrednosti">
<!-- <li id="test1...test5"></li> -->
</ul>
<div class="fadeoutVrednosti"></div>
</div>
<div id="resenja" class="resenja">
<p>Uneti podatak je: <span id="unetiPodatak"></span></p>
<p>Rezultat je: <span id="resultat"></span>, vreme za obradu : <span id="vreme"></span></p>
<div class="fadeoutResenja"></div>
</div>
<div id="liste" class="liste">
<p>Obradjene liste:</p>
<div class="fadeoutResenja"></div>
</div>
</div>
</body>
<script>
var brTestova = 0;
function solution(str){
document.getElementById("unetiPodatak").innerHTML = str.toString();
var vowelsCount = 0;
return (str.length === 0)?0:vowelsCount = str.match(/[a|e|i|o|u]/ig).length;
};
function dblLinearSlow(n) {
if (n >= 1000) return -1;
var u = [1], t = [], i = 0, counter = 0, walker = 0;
for(i = 1; i < n; i += 1){
u.push(2*u[i-1]+1);
u.push(3*u[i-1]+1);
//Bubble sort samo drugu polovinu niza
for(counter = Math.floor(u.length/2); counter < u.length-1; counter += 1){
for(walker = counter+1; walker < u.length; walker +=1){
if(u[walker] === u[counter]) {
u.splice(walker, 1);
continue;
};
if(u[walker] < u[counter]){
t[0] = u[counter]; u[counter] = u[walker]; u[walker] = t[0];};};};
if (u.length >= n+1) {
if (u[n-1] === u[n] && u[n-2] === u[n]) break;};
};
var tmpEl = document.createElement("P");
var tekst = document.createTextNode("Slow:".concat(u.join(", ")));
tmpEl.appendChild(tekst);
document.getElementById("liste").appendChild(tmpEl);
if (document.getElementById("liste").style.visibility != "visible") document.getElementById("liste").style.visibility = "visible";
return u[n];
};
function dblLinearFast(n) {
var index = 0, u = [1], oldn = 0, oldern = 0;
for(index = 1; index < n; index += 1){
u.push(2*u[index-1]+1);
u.push(3*u[index-1]+1);
oldern = oldn;
oldn = u[n];
// console.log(index + " - " + u + " - " + u.length + "elemenata");
for (var i = u.length-3; i > 0; i -= 1){
if(u[i] === u[u.length-1]) {
u.splice(u.length-1, 1);
if(u[i] === u[u.length-1]) u.splice(u.length-1, 1);
break; };
if(u[i] === u[u.length-2]) {
u.splice(u.length-2, 1);
break; };
};
//Quick Sort start
var newU = (function quickSort(list, left, right){
var i = left, j = right;
// definisi nasumicno mesto preloma niza za sortiranje
var mestoPreloma = left+Math.floor(Math.random()*(right+1-left));
var x = list[mestoPreloma];
// pronadji elemente za zamenu tako da su manji na levoj a veci na desnoj strani
while (i <= j) { // kada i===j, treba uporediti da bi se utvrdilo da li prebaciti levo ili desno
while (list[i] < x) { i++; }; // dok je tacno ne radi nista element je u pravoj grupi
while (x < list[j]) { j--; }; // dok je tacno ne radi nista element je u pravoj grupi
if (i < j) { // ovde smo ako je list[i] ili list[j] ili oba van redosleda
var temp = list[i];
list[i] = list[j];
list[j] = temp;
i++;
j--; }
else if (i === j) { // u ovom slucaju ne treba zamena
i++;
j--; };
};
// Sada bi trebalo da je sve od left do j manje ili jednako prelomnoj vrednosti
// a sve od i do right da je vece ili jednako prelomnoj vrednosti.
if (left < j) {
quickSort(list, left, j); };
if (i < right) {
quickSort(list, i, right); };
return list;
})(u, Math.ceil(u.length/2), u.length-1);
if (newU.length >= n+1) {
if ((newU[n] === oldn) && (newU[n] === oldern)) break; };
};
var tmpEl = document.createElement("P");
var tekst = document.createTextNode("Fast:".concat(newU.join(", ")));
tmpEl.appendChild(tekst);
document.getElementById("liste").appendChild(tmpEl);
if (document.getElementById("liste").style.visibility != "visible") document.getElementById("liste").style.visibility = "visible";
return u[n];
};
function clickGo(){
var unetiString = function cleanup(str){
return (str.trim().charAt(str.trim().length-1) === ",")?cleanup(str.trim().slice(0, str.trim().length-1)): str.trim();
}(document.getElementById("poljeZaUnos").value);
document.getElementById("poljeZaUnos").value = unetiString;
var vremePocetka = Date.now();
document.getElementById("resultat").innerHTML = "Slow-".concat(dblLinearSlow(Number(unetiString)));
var vremeKraja = Date.now();
document.getElementById("vreme").innerHTML = "Slow-".concat(((vremeKraja - vremePocetka) / 1000 < 1/10)?(vremeKraja - vremePocetka)+"ms":((vremeKraja - vremePocetka) / 1000)+"s");
var vremePocetka = Date.now();
document.getElementById("resultat").innerHTML = document.getElementById("resultat").innerHTML.concat("|Fast-", dblLinearFast(Number(unetiString)));
var vremeKraja = Date.now();
document.getElementById("vreme").innerHTML = document.getElementById("vreme").innerHTML.concat("|Fast-", ((vremeKraja - vremePocetka) / 1000 < 1/10)?(vremeKraja - vremePocetka)+"ms":((vremeKraja - vremePocetka) / 1000)+"s");
document.getElementById("resenja").style.maxHeight = "220px";
document.getElementById("resenja").style.visibility = "visible";
document.getElementById("resenja").scrollIntoView;
};
function enterGo(event){ if (event.keyCode === 13) clickGo(); };
function clickRandom(){/*
brTestova++;
var gen = "", tmpArr = [];
for(var N = 2; N <= Math.floor(Math.random()*100000+1); N+=1 ){
tmpArr.push(Math.floor(Math.random()*2000+1)-1000);
};
gen = tmpArr.join(", ");
document.getElementById("poljeZaUnos").value = tmpArr;
if (brTestova >= 3){
for(t = 1; t < 2 ; t++){
document.getElementById("test"+t).innerHTML = document.getElementById("test"+(t+1)).innerHTML;
};
document.getElementById("test3").innerHTML = tmpArr.length + " - " + gen;
}
else {
var noviElement = document.createElement("LI");
noviElement.id = "test"+brTestova;
document.getElementById("listaVrednosti").appendChild(noviElement);
document.getElementById("test"+brTestova).innerHTML = tmpArr.length + "-" + gen;
document.getElementById("testVrednosti").style.visibility = "visible";
};
*/};
</script>
</html>