// Traditional JavaScript Primitives
$1 = [1,2,3,4]
$2 = 'String'
$3 = true
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
| List merge(List first, List second) { | |
| if(first.isEmpty) { | |
| return second; | |
| } | |
| if(second.isEmpty) { | |
| return first; | |
| } | |
| if(first.first <= second.first) { |
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
| // Must have be non-empty. | |
| peaks(list) { | |
| if(list.length == 1) { | |
| return list.first; | |
| } | |
| if(list.length == 2) { | |
| return list.reduce((prev, curr) => curr >= prev ? curr : prev); | |
| } | |
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
| search(List list, int value, int min, int max) { | |
| if(min > max) { | |
| return null; | |
| } | |
| final int mid = (max + min) ~/ 2; | |
| if(value < list[mid]) { | |
| return search(list, value, min, mid - 1); | |
| } |
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
| class Node { | |
| dynamic value; | |
| bool discovered = false; | |
| List<Node> links = []; | |
| int distance = 0; | |
| int weight; | |
| @override | |
| String toString() => | |
| ''' |
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
| String encode(String message) { | |
| final Map frequencies = count(message); | |
| final sorted = frequencies.values.toList() | |
| ..sort(); | |
| print(sorted); | |
| return ''; | |
| } | |
| Map<String, int> count(String message) { | |
| Map freq = {}; |
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
| class Activity { | |
| String name; | |
| int start; | |
| int end; | |
| @override | |
| String toString() => '$name: $start, $end'; | |
| } | |
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
| var results = { | |
| 0: 0, | |
| 1: 1 | |
| }; | |
| fib(n) { | |
| if(!results.containsKey(n)) { | |
| results[n] = fib(n - 1) + fib(n - 2); | |
| } | |
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
| // This method takes O(n) time but only O(1) space. | |
| fib(int n) { | |
| if(n == 0) { | |
| return 0; | |
| } | |
| else { | |
| var previousFib = 0; | |
| var currentFib = 1; | |
| while(--n != 0) { // loop is skipped if n = 1 |
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
| import 'dart:math'; | |
| // The cost function c(i, j) = ... | |
| final costs = [ | |
| [1,2,3,4,6], // 0 | |
| [5,6,2,3,1], // 1 | |
| [6,2,0,2,6], // 2 | |
| [5,0,2,8,6], // 3 | |
| [1,2,3,4,5], // 4 | |
| // 0 1 2 3 4 |
OlderNewer