Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
121 changes: 121 additions & 0 deletions becky/algorithms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
### Code Wars
### Get the mean of an array

It's the academic year's end, fateful moment of your school report. The averages must be calculated. All the students come to you and entreat you to calculate their average for them. Easy ! You just need to write a script.

Return the average of the given array rounded down to its nearest integer.

The array will never be empty.


```
function getAverage(marks){
let sum = 0;
for (let i = 0; i< marks.length; i++) {
sum += marks[i];
}
return Math.floor(sum / marks.length);
}
```


### Code Wars
### Smallest unused ID

You've got much data to manage and of course you use zero-based and non-negative ID's to make each data item unique!

Therefore you need a method, which returns the smallest unused ID for your next new data item...

Note: The given array of used IDs may be unsorted. For test reasons there may be duplicate IDs, but you don't have to find or remove them!

```
function nextId(ids){
const arr = new Set(ids);
for (let i = 0; i <= ids.length; i++) {
if (!arr.has(i)) {
return i;
}
}
}
```


### Code Wars
### Keep Hydrated!

Nathan loves cycling.

Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling.

You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.

For example:

time = 3 ----> litres = 1

time = 6.7---> litres = 3

time = 11.8--> litres = 5

```
function litres(time) {
return Math.floor(time * 0.5);
}
```


### Code Wars
### Simple directions reversal

In this Kata, you will be given directions and your task will be to find your way back.

```
solve(["Begin on Road A","Right on Road B","Right on Road C","Left on Road D"]) = ['Begin on Road D', 'Right on Road C', 'Left on Road B', 'Left on Road A']
solve(['Begin on Lua Pkwy', 'Right on Sixth Alley', 'Right on 1st Cr']) = ['Begin on 1st Cr', 'Left on Sixth Alley', 'Left on Lua Pkwy']
```

### my solution
```
function solve(arr){
let res = [];
let a = [];
for (let i of arr)
a.push(i.split(' '));
res.push('Begin ' + a.slice(-1).pop().slice(1).join(' '));
for (let i = a.length-1; i > 0; i--) {
a[i][0] === 'Right' ? res.push('Left ' + a[i-1].slice(1).join(' ')) :
res.push('Right ' + a[i-1].slice(1).join(' '));
}
return res;
}
```

### Code Wars
### Multiplication table

Your task, is to create NxN multiplication table, of size provided in parameter.

for example, when given size is 3:
```
1 2 3
2 4 6
3 6 9
```
for given example, the return value should be: [[1,2,3],[2,4,6],[3,6,9]]

### my solution
```
multiplicationTable = function(size) {
var arr = []
for (var i = 1; i <= size; i++ ) {
var result = [];
for (var j = 1; j <= size; j++) {
result.push(j*i);
}
arr.push(result);
}
return arr;
}
```


117 changes: 117 additions & 0 deletions becky/leet-code-Binary-Tree-stuff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
### Leet Code
### 226. Invert Binary Tree

Invert a binary tree.

Example:

Input:
```
4
/ \
2 7
/ \ / \
1 3 6 9
```
Output:
```
4
/ \
7 2
/ \ / \
9 6 3 1
```
### my solution:
```
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
// check if root null
var temp;
if (root === null) {
return null;
}
// swap root.left with root.right
temp = root.left;
root.left = root.right;
root.right = temp;
// invoke recursion by calling invertTree function
// on root.left and root.right
invertTree(root.left);
invertTree(root.right);

return root
};
```

### 617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:
```
Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7

Output:
Merged tree:
3
/ \
4 5
/ \ \
5 4 7
```
Note: The merging process must start from the root nodes of both trees.

### my solution:
```
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} t1
* @param {TreeNode} t2
* @return {TreeNode}
*/
var mergeTrees = function(t1, t2) {
if (t1 === null && t2 === null) {
return null;
}
if (t1 === null) {
return t2;
}
if (t2 === null) {
return t1;
} else {
var t3Val = t1.val + t2.val;
}
var t3Left = mergeTrees(t1.left, t2.left);
var t3Right = mergeTrees(t1.right, t2.right);
return new TreeNode(t3Val, t3Left, t3Right);
};
```



87 changes: 87 additions & 0 deletions becky/leet-code-Min-stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
### 155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.

Example 1:
```
Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output
[null,null,null,null,-3,null,0,-2]

Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
```
Constraints:

Methods pop, top and getMin operations will always be called on non-empty stacks.

### my solution
```
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.stack = [];
// this.newMin = null;
};

/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.stack.push(x);
// if (x < this.stack.length -2) {
// this.newMin = x;
// }
};

/**
* @return {void}
*/
MinStack.prototype.pop = function() {
this.stack.pop();
};

/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stack[this.stack.length -1];
};

/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return Math.min(...this.stack);
// return this.newMin;
};

/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
```
### my comments:

__The commented out parts were attempt at running constant time. It worked but when pop() was run, it became inaccurate with out sorting within the pop() function which makes it O(n) instead of constant time. In order to do what the instructions say, there would have to be a trade off.__