|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | +<metacharset="utf-8"> |
| 6 | +<title>pseudocode.js Samples with MathJax</title> |
| 7 | + |
| 8 | +<!-- Setup MathJax --> |
| 9 | +<script> |
| 10 | +MathJax={ |
| 11 | +tex:{ |
| 12 | +inlineMath:[['$','$'],['\\(','\\)']], |
| 13 | +displayMath:[['$$','$$'],['\\[','\\]']], |
| 14 | +processEscapes:true, |
| 15 | +processEnvironments:true, |
| 16 | +} |
| 17 | +} |
| 18 | +</script> |
| 19 | +<scriptsrc="https://cdn.jsdelivr.net/npm/mathjax@4.0.0-beta.3/tex-chtml.js" |
| 20 | +integrity="sha256-ljPODBK7Jf/VfUrVqec63xzZbysEmwB9Ab20TWRMQRU=" |
| 21 | +crossorigin="anonymous"> |
| 22 | +</script> |
| 23 | + |
| 24 | +<!-- Pseudocode --> |
| 25 | +<linkrel="stylesheet"href="pseudocode.css"type="text/css"> |
| 26 | +<scriptsrc="pseudocode.js"type="text/javascript"></script> |
| 27 | +</head> |
| 28 | + |
| 29 | +<body> |
| 30 | +<preid="test-basics"data-line-number=true> |
| 31 | + \begin{algorithm} |
| 32 | + \caption{Test text-style} |
| 33 | + \begin{algorithmic} |
| 34 | + \REQUIRE some preconditions |
| 35 | + \ENSURE some postconditions |
| 36 | + \INPUT some inputs |
| 37 | + \OUTPUT some outputs |
| 38 | + \PROCEDURE{Test-Declarations}{} |
| 39 | + \STATE font families: {\sffamily sffamily, \ttfamily ttfamily, \normalfont normalfont, \rmfamily rmfamily.} |
| 40 | + \STATE font weights: {normal weight, \bfseries bold, \mdseries |
| 41 | + medium, \lfseries lighter. } |
| 42 | + \STATE font shapes: {\itshape itshape \scshape Small-Caps \slshape slshape \upshape upshape.} |
| 43 | + \STATE font sizes: \tiny tiny \scriptsize scriptsize \footnotesize |
| 44 | + footnotesize \small small \normalsize normal \large large \Large Large |
| 45 | + \LARGE LARGE \huge huge \Huge Huge \normalsize |
| 46 | + \ENDPROCEDURE |
| 47 | + \PROCEDURE{Test-Commands}{} |
| 48 | + \STATE \textnormal{textnormal,} \textrm{textrm,} \textsf{textsf,} \texttt{texttt.} |
| 49 | + \STATE \textbf{textbf,} \textmd{textmd,} \textlf{textlf.} |
| 50 | + \STATE \textup{textup,} \textit{textit,} \textsc{textsc,} \textsl{textsl.} |
| 51 | + \STATE \uppercase{uppercase,} \lowercase{LOWERCASE.} |
| 52 | + \ENDPROCEDURE |
| 53 | + \PROCEDURE{Test-Colors}{} |
| 54 | + \STATE colors: $\color{red}{red}$, $\color{green}{green}$, $\color{blue}{blue}$ |
| 55 | + \STATE colors: $\color{yellow}{yellow}$, $\color{cyan}{cyan}$, $\color{magenta}{magenta}$ |
| 56 | + \ENDPROCEDURE |
| 57 | + \end{algorithmic} |
| 58 | + \end{algorithm} |
| 59 | + |
| 60 | + \begin{algorithm} |
| 61 | + \caption{Test atoms} |
| 62 | + \begin{algorithmic} |
| 63 | + \STATE \textbf{Specials:} \{ \} \$ \& \# \% \_ |
| 64 | + \STATE \textbf{Bools:} \AND \OR \NOT \TRUE \FALSE |
| 65 | + \STATE \textbf{Carriage return:} first line \\ second line |
| 66 | + \STATE \textbf{Text-symbols:} \textbackslash |
| 67 | + \STATE \textbf{Quote-symbols:} `single quotes', ``double quotes'' |
| 68 | + \STATE \textbf{Math:} $(\mathcal{C}_m)$, $i \gets i + 1$, $E=mc^2$, \( x^n + y^n = z^n \), $\$$, \(\$\) |
| 69 | + \END{ALGORITHMIC} |
| 70 | + \END{ALGORITHM} |
| 71 | +</pre> |
| 72 | +<preid="test-codes"> |
| 73 | + \begin{algorithm} |
| 74 | + \caption{Test control blocks} |
| 75 | + \begin{algorithmic} |
| 76 | + \PROCEDURE{Test-If}{} |
| 77 | + \IF{<cond>} |
| 78 | + \STATE <block> |
| 79 | + \ELIF{<cond>} |
| 80 | + \STATE <block> |
| 81 | + \ELSE |
| 82 | + \STATE <block> |
| 83 | + \ENDIF |
| 84 | + \ENDPROCEDURE |
| 85 | + \PROCEDURE{Test-For}{$n$} |
| 86 | + \STATE $i \gets 0$ |
| 87 | + \FOR{$i<n$} |
| 88 | +\PRINT$i$ |
| 89 | +\STATE$i\getsi+1$ |
| 90 | +\ENDFOR |
| 91 | +\ENDPROCEDURE |
| 92 | +\PROCEDURE{Test-For-To}{$n$} |
| 93 | +\STATE$i\gets0$ |
| 94 | +\FOR{$i$\TO$n$} |
| 95 | +\PRINT$i$ |
| 96 | +\ENDFOR |
| 97 | +\ENDPROCEDURE |
| 98 | +\PROCEDURE{Test-For-DownTo}{$n$} |
| 99 | +\FOR{$i\getsn$\DOWNTO$0$} |
| 100 | +\PRINT$i$ |
| 101 | +\ENDFOR |
| 102 | +\ENDPROCEDURE |
| 103 | +\PROCEDURE{Test-For-All}{$n$} |
| 104 | +\FORALL{$i\in\{0,1,\cdots,n\}$} |
| 105 | +\PRINT$i$ |
| 106 | +\ENDFOR |
| 107 | +\ENDPROCEDURE |
| 108 | +\PROCEDURE{Test-While}{$n$} |
| 109 | +\STATE$i\gets0$ |
| 110 | +\WHILE{$i<n$} |
| 111 | +\PRINT$i$ |
| 112 | +\STATE$i\getsi+1$ |
| 113 | +\ENDWHILE |
| 114 | +\ENDPROCEDURE |
| 115 | +\PROCEDURE{Test-Repeat}{$n$} |
| 116 | +\STATE$i\gets0$ |
| 117 | +\REPEAT |
| 118 | +\PRINT$i$ |
| 119 | +\STATE$i\getsi+1$ |
| 120 | +\UNTIL{$i>n$} |
| 121 | + \ENDPROCEDURE |
| 122 | + \PROCEDURE{Test-Break-Continue}{$n$} |
| 123 | + \FOR{$i = 0$ \TO $2n$} |
| 124 | + \IF{$i<n/2$} |
| 125 | + \CONTINUE |
| 126 | + \ELIF{$i> n$} |
| 127 | + \BREAK |
| 128 | + \ENDIF |
| 129 | + \PRINT $i$ |
| 130 | + \ENDFOR |
| 131 | + \ENDPROCEDURE |
| 132 | + \end{algorithmic} |
| 133 | + \end{algorithm} |
| 134 | + \begin{algorithm} |
| 135 | + \caption{Test statements and comments} |
| 136 | + \begin{algorithmic} |
| 137 | + \PROCEDURE{Test-Statements}{} |
| 138 | + \STATE This line is a normal statement |
| 139 | + \PRINT \texttt{`this is print statement'} |
| 140 | + \RETURN $retval$ |
| 141 | + \ENDPROCEDURE |
| 142 | + |
| 143 | + \PROCEDURE{Test-Comments}{} \COMMENT{comment for procedure} |
| 144 | + \STATE a statement \COMMENT{inline comment} |
| 145 | + \STATE \COMMENT{line comment} |
| 146 | + \IF{some condition}\COMMENT{comment for if} |
| 147 | + \RETURN \TRUE \COMMENT{another inline comment} |
| 148 | + \ELSE \COMMENT{comment for else} |
| 149 | + \RETURN \FALSE \COMMENT{yet another inline comment} |
| 150 | + \ENDIF |
| 151 | + \ENDPROCEDURE |
| 152 | + \end{algorithmic} |
| 153 | + \end{algorithm} |
| 154 | +</pre> |
| 155 | +<preid="test-examples"data-title-prefix="Procedure"> |
| 156 | + % This quicksort algorithm is extracted from Chapter 7, Introduction |
| 157 | + % to Algorithms (3rd edition) |
| 158 | + \begin{algorithm} |
| 159 | + \caption{Quicksort} |
| 160 | + \begin{algorithmic} |
| 161 | + \PROCEDURE{Quicksort}{$A, p, r$} |
| 162 | + \IF{$p<r$} |
| 163 | +\STATE$q =$\CALL{Partition}{$A,p,r$} |
| 164 | +\STATE\CALL{Quicksort}{$A,p,q-1$} |
| 165 | +\STATE\CALL{Quicksort}{$A,q+1,r$} |
| 166 | +\ENDIF |
| 167 | +\ENDPROCEDURE |
| 168 | +\PROCEDURE{Partition}{$A,p,r$} |
| 169 | +\STATE$x =A[r]$ |
| 170 | +\STATE$i =p-1$ |
| 171 | +\FOR{$j =p$\TO$r-1$} |
| 172 | +\IF{$A[j]<x$} |
| 173 | +\STATE$i =i+1$ |
| 174 | +\STATEexchange |
| 175 | +$A[i]$with$A[j]$ |
| 176 | +\ENDIF |
| 177 | +\STATEexchange$A[i]$with$A[r]$ |
| 178 | +\ENDFOR |
| 179 | +\ENDPROCEDURE |
| 180 | +\end{algorithmic} |
| 181 | +\end{algorithm} |
| 182 | +</pre> |
| 183 | +<preid="test-examples-custom-title"> |
| 184 | + % This quicksort algorithm is extracted from Chapter 7, Introduction |
| 185 | + % to Algorithms (3rd edition) |
| 186 | + \begin{algorithm} |
| 187 | + \caption{Quicksort} |
| 188 | + \begin{algorithmic} |
| 189 | + \PROCEDURE{Quicksort}{$A, p, r$} |
| 190 | + \IF{$p<r$} |
| 191 | +\STATE$q =$\CALL{Partition}{$A,p,r$} |
| 192 | +\STATE\CALL{Quicksort}{$A,p,q-1$} |
| 193 | +\STATE\CALL{Quicksort}{$A,q+1,r$} |
| 194 | +\ENDIF |
| 195 | +\ENDPROCEDURE |
| 196 | +\PROCEDURE{Partition}{$A,p,r$} |
| 197 | +\STATE$x =A[r]$ |
| 198 | +\STATE$i =p-1$ |
| 199 | +\FOR{$j =p$\TO$r-1$} |
| 200 | +\IF{$A[j]<x$} |
| 201 | +\STATE$i =i+1$ |
| 202 | +\STATEexchange |
| 203 | +$A[i]$with$A[j]$ |
| 204 | +\ENDIF |
| 205 | +\STATEexchange$A[i]$with$A[r]$ |
| 206 | +\ENDFOR |
| 207 | +\ENDPROCEDURE |
| 208 | +\end{algorithmic} |
| 209 | +\end{algorithm} |
| 210 | +</pre> |
| 211 | +<preclass="scopeline-pseudocode"data-line-number=true> |
| 212 | + \begin{algorithm} |
| 213 | + \caption{Classical Euclidean Algorithm} |
| 214 | + \begin{algorithmic} |
| 215 | + \PROCEDURE{Euclid}{$a,b$} |
| 216 | + \WHILE{$a \neq b$} |
| 217 | + \IF{$a> b$} |
| 218 | + \STATE $a \gets a - b$ |
| 219 | + \ELSE |
| 220 | + \STATE $b \gets b - a$ |
| 221 | + \ENDIF |
| 222 | + \ENDWHILE |
| 223 | + \RETURN $a$ |
| 224 | + \ENDPROCEDURE |
| 225 | + \end{algorithmic} |
| 226 | + \end{algorithm} |
| 227 | +</pre> |
| 228 | +<preclass="scopeline-pseudocode"data-no-end=true> |
| 229 | + \begin{algorithm} |
| 230 | + \caption{DBSCAN} |
| 231 | + \begin{algorithmic} |
| 232 | + \INPUT A dataset $D$, the $\varepsilon$ distance threshold, and the minimum number of points $minPts$ |
| 233 | + \OUTPUT A set of clusters $K$ |
| 234 | + \PROCEDURE{DBSCAN}{$D, \varepsilon, minPts$} |
| 235 | + \STATE $K \gets \emptyset$ |
| 236 | + \FORALL{$p \in D$} |
| 237 | + \IF{$p$ has not been visited} |
| 238 | + \STATE mark $p$ as visited |
| 239 | + \STATE $N_{\varepsilon}(p) \gets $ \textsc{RangeQuery}$(p, \varepsilon, D)$ |
| 240 | + \IF{$N_{\varepsilon}(p)<minPts$} |
| 241 | +\STATEmark$p$asnoise |
| 242 | +\ELSE |
| 243 | +\COMMENT{pisacoreobject} |
| 244 | +\STATE$C\gets$\textsc{ExpandCluster}$(p,N_{\varepsilon}(p))$ |
| 245 | +\STATE$K\getsK\cup\{C\}$ |
| 246 | +\ENDIF |
| 247 | +\ENDIF |
| 248 | +\ENDFOR |
| 249 | +\RETURN$K$ |
| 250 | +\ENDPROCEDURE |
| 251 | +\end{algorithmic} |
| 252 | +\end{algorithm} |
| 253 | +</pre> |
| 254 | +<script> |
| 255 | +pseudocode.renderElement(document.getElementById("test-basics"), |
| 256 | +{ |
| 257 | +lineNumber:false, |
| 258 | +noEnd:true |
| 259 | +}); |
| 260 | +pseudocode.renderElement(document.getElementById("test-codes")); |
| 261 | +pseudocode.renderElement(document.getElementById("test-examples"), |
| 262 | +{ |
| 263 | +lineNumber:true, |
| 264 | +noEnd:false |
| 265 | +}); |
| 266 | +pseudocode.renderElement(document.getElementById("test-examples-custom-title"), |
| 267 | +{ |
| 268 | +lineNumber:true, |
| 269 | +noEnd:false, |
| 270 | +titlePrefix:"My pretty Algorithm" |
| 271 | +}); |
| 272 | +pseudocode.renderClass("scopeline-pseudocode",{scopeLines:true}); |
| 273 | +</script> |
| 274 | +</body> |
| 275 | +</html> |