Created
March 29, 2012 13:31
-
-
Save botic/2237460 to your computer and use it in GitHub Desktop.
FMI V1
This file contains hidden or 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
| \begin{lstlisting}[mathescape=true] | |
| $\Pi'$($\Pi$, I) = { | |
| // from ex. 1: $\Pi$ may not halt $\Rightarrow$ (c) | |
| var result = call $\Pi$(I); | |
| // == is an operator, not a function call | |
| return (result == I + I); | |
| } | |
| \end{lstlisting} |
This file contains hidden or 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
| /** | |
| * N-SORTED-ELEMENTS | |
| * INSTANCE: A non-empty list L = (e1 , . . . , en ) of non-negative integers. | |
| * QUESTION: Does the list L contain a sub-list of k consecutive sorted numbers | |
| * in ascending order (from left to right)? | |
| */ | |
| include("ringo/term"); | |
| include("ringo/shell"); | |
| var size = parseInt(readln("Enter size of list: "), 10); | |
| var k = parseInt(readln("Enter k: "), 10); | |
| var numRange = parseInt(readln("Enter numRange: "), 10); | |
| var list = []; | |
| for (var i = 0; i < size; i++) { | |
| list.push(Math.floor(Math.random() * numRange)); | |
| } | |
| writeln("k: " + k); | |
| writeln("List: " + list); | |
| // The FMI relevant code starts here | |
| var N_SORTED_ELEMENTS = function(list, k) { | |
| var ascCounter = 0; | |
| for (var i = 0; i < size - 1; i++) { | |
| writeln("Comparing: list[" + i + "] :: list[" + (i + 1) + "] --> " + list[i] + " :: " + list[i + 1]); | |
| if (list[i + 1] - list[i] === 1) { | |
| ascCounter ++; | |
| writeln(" << ASC >> ascCounter++ to " + ascCounter); | |
| // Check if the number of consecutive is fulfilled | |
| // Positive Instance! | |
| if (ascCounter >= (k - 1)) { | |
| return true; | |
| } | |
| } else { | |
| ascCounter = 0; | |
| } | |
| } | |
| // Negative Instance | |
| return false; | |
| }; | |
| writeln((N_SORTED_ELEMENTS(list, k) ? "YES" : "NO")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment