Added Fibonacci.

This commit is contained in:
Jacob Signorovitch
2024-11-18 20:05:45 -05:00
parent 17a6d8b635
commit 0b9d52848a
7 changed files with 249 additions and 0 deletions

View 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 {}