Created
March 17, 2015 21:27
-
-
Save jtgi/693ce8147ec945a2eec6 to your computer and use it in GitHub Desktop.
vim challenge
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
import java.util.*; | |
/* | |
* NORMAL MODE | |
* challenge 0: motions | |
* hjkl - move in all directions | |
* www - move to next word | |
* bbb - move to prev word, | |
* eee - move to next word, | |
* $ - move to end of line | |
* _ - move to beginning of line | |
* gg - move to top of file, | |
* G - move to end of file | |
* f<char> - move to next <char> in line | |
* t<char> - move to char before <char> | |
* F<char> - move to prev <char> in line | |
* T<char> - move to char ahead of <char> previously <- confusing | |
* x - delete character under cursor | |
* r<char> - replace character under cursor with <char> stay in normal mode | |
* :<line_num> - jump to line num | |
* } - jump to next paragraph | |
* { - jump to previous paragraph | |
* | |
* challenge 1: make it right - motion with www, delete with x, replace with r | |
* The ccow iumpedd ovverr thhe mooon. | |
* | |
* INSERT MODE | |
* <esc> - move out of insert mode | |
* i - move into insert mode left of cursor | |
* a - move into insert mode right of cursor | |
* A - move into insert mode at end of line | |
* | |
* challenge 2: Insert | |
* ---> There is text misng this . | |
* ---> There is some text missing from this line. | |
* | |
* challenge 3: Append | |
* ---> There is some text missing from th | |
* There is some text missing from this line. | |
* ---> There is also some text miss | |
* There is also some text missing here. | |
* | |
* DELETE | |
* challenge 4: Delete - wwww, dw | |
* ---> There are a some words fun that don't belong paper in this sentence. | |
* | |
* challenge 5: Delete rest of this line - d$ | |
* ---> There are some fun words. I like turtles. | |
* | |
* What is this magic: OPERATORS AND MOTIONS! | |
* Okay we have some primitives `d`, and a bunch of movements | |
* We can combine delete `d` with any motion. | |
* d$ - delete from current cursor position until end of line | |
* dw - delete up until next word | |
* db - delete backward until beginning of prev word | |
* | |
* MULTIPLIERS | |
* What if we want to move 5 words forward? | |
* 5w <- move 5 | |
* 5j <- 5 lines down | |
* | |
* What if you want to delete 5 words? prefix your command with a number | |
* 5dw <- delete the next 5 words | |
* ---> This is just a line with words you can move around in. | |
* | |
* MORE IN NORMAL MODE: OPERATING ON LINES | |
* So often we need to operate on lines, vim is excellent at this | |
* in insert mode. Double up on any command to apply it to a line | |
* | |
* dd <- delete the whole line | |
* | |
* -----> DELETE ME | |
* | |
* of course multipliers work | |
* 2dd <- delete 2 lines | |
* | |
* -----> DELETE ME | |
* -----> ME TOO | |
* | |
* `p` | `P` - vim does cool stuff when you delete. It puts it in your paste buffer. | |
* p - pastes last deleted or yanked thing below cursor | |
* P - pastes last deleted or yanked thing above cursor | |
* | |
* Put these in order using `p` | |
* -----> 1st | |
* -----> 3nd | |
* -----> 2nd | |
* | |
* Put these in order using `P` | |
* -----> 1st | |
* -----> 3nd | |
* -----> 2nd | |
* | |
* COPYING AKA YANK | |
* yy - copys line | |
* p - pastes below | |
* P - pastes above | |
* | |
* Challenge: yank and paste this 10 times. | |
* -----> I will not use the arrow keys | |
* | |
* Challenge: yank and paste this 100 times. | |
* you can use multipliers | |
* -----> I will not use the arrow keys | |
* | |
* Favourites: | |
* ciw - delete word and move into insert mode | |
* diw - delete whol word, stay in normal mode | |
* ci( - delete up until surrounding parens | |
* ci{ - delete up until braces | |
* | |
* SEARCHING | |
* /<term> | |
* n -> move to next | |
* N -> move to prev (like less) | |
*/ | |
public class Bst { | |
public Bst left; | |
public Bst right; | |
public int value; | |
/* try and search up until `,` */ | |
/* try ciw inside of the params */ | |
public Bst(int value, Bst left, Bst right) { | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
/* | |
* Given a list of numbers create a balanced bst | |
* First sort the list, then choose the median | |
* and set its left and right child as the | |
* median of the left and right half recursively. | |
* | |
* Running time: O(nlgn) from comparison-sort | |
*/ | |
//TODO: f( lciw int <esc> | |
public static Bst createBalancedBst(String[] arr) { //<- fix this int[] ci( challenge | |
Arrays.sort(arr); | |
return createBalancedBstHelper(arr, 0, arr.length - 1); | |
} | |
private static Bst createBalancedBstHelper(int[] arr, int lo, int hi) { | |
if(hi < lo) return null; | |
int mid = (lo + hi) / 2; | |
//TODO: fix indentation 2<< . to repeat | |
Bst left = createBalancedBstHelper(arr, lo, mid-1); | |
Bst right = createBalancedBstHelper(arr, mid+1, hi); | |
return new Bst(arr[mid], left, right); | |
} | |
public boolean isBalanced() { | |
//TODO add semi colons: A ; | |
int leftHeight = (left == null) ? 0 : left.height() | |
int rightHeight = (right == null) ? 0 : right.height() | |
return Math.abs(left.height() - right.height()) <= 1; | |
} | |
public int height() { | |
int leftHeight = (left == null) ? 0 : 1 + left.height(); | |
//TODO Fix the ordering: dd p | |
return Math.max(leftHeight, rightHeight); | |
int rightHeight = (right == null) ? 0 : 1 + right.height(); | |
} | |
//rename to `root` `head` | |
//TODO: :170, dt) b ciw head <esc> j 2f( l ciw head <esc> f( l ciw head <esc> | |
public static boolean isBalanced(Bst root) { | |
return Math.abs(height(root.left) - height(root.right)) <= 1; | |
} | |
public static int height(Bst root) { | |
if(root == null) return 0; | |
int leftHeight = 1 + height(root.left); | |
int rightHeight = 1 + height(root.right); | |
return Math.max(leftHeight, rightHeight); | |
} | |
public String toString() { | |
StringBuffer buff = new StringBuffer(); | |
if(left != null) { | |
buff.append(left); | |
} | |
buff.append(value); | |
if(right != null) { | |
buff.append(right); | |
} | |
return buff.toString(); | |
} | |
public static void main(String[] args) { | |
Bst balanced = new Bst(10, | |
new Bst(5, null, null), new Bst(15, null, null)); | |
Bst unbalanced = new Bst(10, | |
new Bst(5, null, null), new Bst(15, null, | |
new Bst(20, null, | |
new Bst(25, null, null)))); | |
System.out.println(String.format("balanced bst: %b", isBalanced(balanced))); | |
System.out.println(String.format("unbalanced bst: %b", isBalanced(unbalanced))); | |
System.out.println(String.format("balanced OOP bst: %b", balanced.isBalanced())); | |
System.out.println(String.format("unbalanced OOP bst: %b", unbalanced.isBalanced())); | |
Bst balancedBst = Bst.createBalancedBst(new int[] { 5, 4, 2, 3, 8 }); | |
System.out.println(balancedBst.toString()); | |
System.out.println(String.format("Balanced bst is balanced? %b", balancedBst.isBalanced())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment