Added Fibonacci.
This commit is contained in:
@@ -14,4 +14,15 @@
|
|||||||
<natures>
|
<natures>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1731947415548</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
12
fibonacci/.classpath
Normal file
12
fibonacci/.classpath
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="lib" path="/home/jacob/School/CS3/libs/javalib.jar"/>
|
||||||
|
<classpathentry kind="lib" path="/home/jacob/School/CS3/libs/tester.jar"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
28
fibonacci/.project
Normal file
28
fibonacci/.project
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>fibonacci</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1731947415557</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
2
fibonacci/.settings/org.eclipse.core.resources.prefs
Normal file
2
fibonacci/.settings/org.eclipse.core.resources.prefs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding/<project>=UTF-8
|
11
fibonacci/.settings/org.eclipse.jdt.core.prefs
Normal file
11
fibonacci/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=21
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=21
|
174
fibonacci/src/fibonacci/Main.java
Normal file
174
fibonacci/src/fibonacci/Main.java
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package fibonacci;
|
||||||
|
|
||||||
|
import tester.Tester;
|
||||||
|
|
||||||
|
class Examples {
|
||||||
|
|
||||||
|
ISequence<Integer> f;
|
||||||
|
ISequence<String> a;
|
||||||
|
ISeqGen<String> aGen = new AGen();
|
||||||
|
|
||||||
|
void init() {
|
||||||
|
this.f = new Fibonacci();
|
||||||
|
this.a = new GenSeq<String>("", aGen);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testFibonacci(Tester t) {
|
||||||
|
init();
|
||||||
|
t.checkExpect(f.get(), 0);
|
||||||
|
t.checkExpect(f.get(), 1);
|
||||||
|
t.checkExpect(f.get(), 1);
|
||||||
|
t.checkExpect(f.get(), 2);
|
||||||
|
t.checkExpect(f.get(), 3);
|
||||||
|
t.checkExpect(f.get(), 5);
|
||||||
|
t.checkExpect(f.get(), 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testASeq(Tester t) {
|
||||||
|
init();
|
||||||
|
t.checkExpect(a.get(), "");
|
||||||
|
t.checkExpect(a.get(), "a");
|
||||||
|
t.checkExpect(a.get(), "aa");
|
||||||
|
t.checkExpect(a.get(), "aaa");
|
||||||
|
t.checkExpect(a.get(), "aaaa");
|
||||||
|
t.checkExpect(a.get(), "aaaaa");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A generic list.
|
||||||
|
interface ILo<A> {}
|
||||||
|
|
||||||
|
class Cons<A> implements ILo<A> {
|
||||||
|
|
||||||
|
A first;
|
||||||
|
ILo<A> rest;
|
||||||
|
|
||||||
|
Cons(A first, ILo<A> rest) {
|
||||||
|
this.first = first;
|
||||||
|
this.rest = rest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Mt<A> implements ILo<A> {}
|
||||||
|
|
||||||
|
interface ISequence<X> {
|
||||||
|
// Returns the current element in the sequence.
|
||||||
|
// EFFECT: Updates the state to the next element in the sequence.
|
||||||
|
X get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sequence generator method.
|
||||||
|
interface ISeqGen<X> {
|
||||||
|
X gen(X state); // Generates the next value in the sequence given the current state.
|
||||||
|
}
|
||||||
|
|
||||||
|
// The Fibonacci sequence.
|
||||||
|
class Fibonacci implements ISequence<Integer> {
|
||||||
|
|
||||||
|
int idx; // Number of times `get()` has been called.
|
||||||
|
int pre; // The previous (last) number.
|
||||||
|
int pen; // The penultimate (second to last) number.
|
||||||
|
|
||||||
|
Fibonacci() {
|
||||||
|
this.pen = 0;
|
||||||
|
this.pre = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the next in the sequence.
|
||||||
|
public Integer get() {
|
||||||
|
int ret; // What to return.
|
||||||
|
/*
|
||||||
|
|
||||||
|
idx pen pre ret
|
||||||
|
0 0 1 0
|
||||||
|
1 0 1 1
|
||||||
|
2 1 1 2
|
||||||
|
3 1 2 3
|
||||||
|
4 2 3 5
|
||||||
|
5 3 5 8
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// F(0) = 0; F(1) = 1
|
||||||
|
if (this.idx <= 1) ret = this.idx;
|
||||||
|
else { // F(n) = F(n-1) + F(n-2)
|
||||||
|
ret = this.pre + this.pen;
|
||||||
|
|
||||||
|
this.pen = this.pre;
|
||||||
|
this.pre = ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.idx = this.idx + 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A generic sequence.
|
||||||
|
class GenSeq<X> implements ISequence<X> {
|
||||||
|
|
||||||
|
X state; // The current state of the sequence.
|
||||||
|
ISeqGen<X> seqGen; // The method by which new values are generated.
|
||||||
|
|
||||||
|
// Create new generic sequence given an initial state.
|
||||||
|
GenSeq(X init, ISeqGen<X> seqGen) {
|
||||||
|
this.state = init;
|
||||||
|
this.seqGen = seqGen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public X get() {
|
||||||
|
X ret = this.state;
|
||||||
|
this.state = this.seqGen.gen(this.state);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A string of n "a"s.
|
||||||
|
class AGen implements ISeqGen<String> {
|
||||||
|
|
||||||
|
public String gen(String state) {
|
||||||
|
return state.concat("a");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SimpleMarkov implements ISeqGen<String> {
|
||||||
|
|
||||||
|
public String gen(String state) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The probability of a word.
|
||||||
|
class ProbPair {
|
||||||
|
|
||||||
|
String word; // The word itself.
|
||||||
|
int count; // The number of times the word has appeared after this word.
|
||||||
|
|
||||||
|
ProbPair(String word, int count) {
|
||||||
|
this.word = word;
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The probable completions for a word.
|
||||||
|
class Completion {
|
||||||
|
|
||||||
|
String word; // The word the completions are for.
|
||||||
|
ILo<ProbPair> completions; //The probable completions.
|
||||||
|
|
||||||
|
Completion(String word, ILo<ProbPair> completions) {
|
||||||
|
this.word = word;
|
||||||
|
this.completions = completions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The list of all known completions.
|
||||||
|
class CompletionTable {
|
||||||
|
|
||||||
|
ILo<Completion> table;
|
||||||
|
|
||||||
|
CompletionTable(ILo<Completion> table) {
|
||||||
|
this.table = table;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ProcessInput {}
|
@@ -14,4 +14,15 @@
|
|||||||
<natures>
|
<natures>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1731947415563</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
Reference in New Issue
Block a user