-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eb7168
commit 386c222
Showing
2 changed files
with
21 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,18 @@ | ||
class Node { | ||
init (value) { | ||
this.value = value; | ||
this.next = nil; | ||
} | ||
} | ||
|
||
|
||
class List { | ||
init() { | ||
this.head = nil; | ||
this.tail = nil; | ||
} | ||
|
||
add(value) { | ||
var node = Node(value); | ||
if (!this.head) { | ||
this.head = node; | ||
this.tail = node; | ||
} else { | ||
this.tail.next = node; | ||
this.tail = node; | ||
} | ||
} | ||
var cache = hash(); | ||
|
||
remove(value) { | ||
var node = this.head; | ||
var prev = nil; | ||
while (node) { | ||
if (node.value == value) { | ||
if (!prev) { | ||
this.head = node.next; | ||
} else { | ||
prev.next = node.next; | ||
} | ||
return; | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
} | ||
|
||
write() { | ||
var node = this.head; | ||
while (node) { | ||
print node.value; | ||
node = node.next; | ||
} | ||
} | ||
fun fib(n) { | ||
if (n < 2) { | ||
return n; | ||
} | ||
if (cache.get(n)) { | ||
return cache.get(n); | ||
} | ||
var res = fib(n - 1) + fib(n - 2); | ||
cache.set(n, res); | ||
return res; | ||
} | ||
|
||
|
||
var list = List(); | ||
for (var i = 0; i < 10; i = i + 1) { | ||
list.add(i); | ||
} | ||
for (var i = 0; i < 10; i = i + 1) { | ||
if (i % 2 == 0) { | ||
list.remove(i); | ||
} | ||
} | ||
list.write(); | ||
var before = clock(); | ||
print fib(30); | ||
var after = clock(); | ||
print after - before; |