Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 6b75769

Browse files
committed
merged master
2 parents a95a285 + 255addf commit 6b75769

30 files changed

+604
-1
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
8+
[*]
9+
10+
indent_style = space
11+
indent_size = 2
12+
end_of_line = lf
13+
charset = utf-8
14+
trim_trailing_whitespace = true
15+
insert_final_newline = true
16+
17+
[*.md]
18+
trim_trailing_whitespace = false
19+
20+
[Makefile]
21+
indent_style = tab
22+
indent_size = 4

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
22
.idea
33
/out
4-
/core
4+
/core
5+
node_modules

.jshintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
3+
# Pretty dodgy JS to keep it so small
4+
shortest-fizz-buzz

.jshintrc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"node": true,
3+
"browser": true,
4+
"jquery": true,
5+
"devel": true,
6+
"nonstandard": true,
7+
"worker": true,
8+
"esnext": true,
9+
"bitwise": false,
10+
"boss": true,
11+
"camelcase": true,
12+
"curly": true,
13+
"eqeqeq": true,
14+
"eqnull": true,
15+
"expr": true,
16+
"forin": true,
17+
"immed": true,
18+
"latedef": true,
19+
"newcap": true,
20+
"noarg": true,
21+
"quotmark": "single",
22+
"regexp": true,
23+
"strict": false,
24+
"undef": true,
25+
"unused": true,
26+
"trailing": true,
27+
"smarttabs": true,
28+
"immed": true,
29+
"globals": {
30+
"it": true,
31+
"describe": true
32+
}
33+
}

byte-string/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Byte String
2+
3+
Convert a number to a string that represents a rounded size in bytes.
4+
5+
## Example
6+
7+
```
8+
f(156833213) // => "149.57 MB"
9+
f(8101) // => "7.91 KB"
10+
f(12331, 3) // => "12.042 KB"
11+
```
12+
13+
## Source
14+
15+
By [Riga](https://github.com/riga).

byte-string/byte-string.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function (bytes, precision) {
2+
var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
3+
factor = Math.pow(10, precision > 0 ? precision : 2);
4+
// Using a for loop since it's perfect for this kind of problem
5+
for (var i = bytes, k = 0; i >= 1024 && k < suffixes.length; i /= 1024, k++) {}
6+
// Return the number rounded to precision
7+
return (Math.round(i * factor) / factor) + ' ' + suffixes[k];
8+
};

debounce/Readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Debounce
2+
3+
Write a function that accepts a function and timeout, `x`, in number of milliseconds. It will return a new function that can only be executed on per timeout period - and if the function is invoked during the timeout period, the timeout period restarts. This is useful for functions that can be need to be blocked on subsequent attempts over short period of times. Once such is example, is clicks on a button.
4+
5+
Once written, add a third parameter that will allow the function to be executed immediately if set to true. Otherwise the function will run at the end of the timeout period.

debounce/debounce.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = function (fn, delay, execAsap) {
2+
var timeout; // Keep a reference to the timeout outside the function
3+
4+
return function () {
5+
// Keep the functions execution context and arguments in tact
6+
var that = this,
7+
args = arguments;
8+
9+
// If we already have a function ready to execute, clear it
10+
// Else if we are allowed to execute immediately, call the function
11+
if (timeout) {
12+
clearTimeout(timeout);
13+
} else if (execAsap) {
14+
fn.apply(that, args);
15+
}
16+
17+
timeout = setTimeout(function () {
18+
execAsap || fn.apply(that, args);
19+
timeout = null;
20+
}, delay || 100);
21+
};
22+
};

factorial/factorial.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Recursive function example
2+
exports.recursive = function factorial (number) {
3+
if (number < 2) { return 1; }
4+
5+
return number * factorial(number - 1);
6+
};
7+
8+
// Iterative solution
9+
exports.iterative = function (number) {
10+
var result = 1;
11+
12+
for (var i = 1; i <= number; i++) {
13+
result *= i;
14+
}
15+
16+
return result;
17+
};
18+
19+
// Iterative using a reverse loop
20+
exports.iterativeReverse = function (number) {
21+
var result = 1;
22+
23+
while (number) {
24+
result *= number;
25+
number -= 1;
26+
}
27+
28+
return result;
29+
};

factorial/factorial.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
// Iterative solution
4+
function factorial($number){
5+
$result = 1;
6+
for($i=1; $i <= $number ; $i++){
7+
$result *=$i;
8+
}
9+
return $result;
10+
}
11+
12+
// Iterative using a reverse loop
13+
function factorialReverse($number){
14+
$result = 1;
15+
while($number !=1){
16+
$result *= $number;
17+
$number--;
18+
}
19+
return $result;
20+
}
21+
22+
// Recursive solution
23+
function factorialRecursive($number){
24+
if($number ==1){
25+
return 1;
26+
}
27+
else{
28+
return $number *factorialRecursive($number-1);
29+
}
30+
}
31+
32+
?>

factorial/readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Factorial
2+
==========
3+
4+
Factorial of any number n is defined as the multiplication of numbers from one to the given number.
5+
6+
``
7+
n! = 1 x 2 x 3 x 4 x ........ x n
8+
``

hotel-room/hotel-room.cs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* this is program written in C# by Alvareto */
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace hotelRoom
10+
{
11+
class Program
12+
{
13+
static int[] getDivisors (int number)
14+
{
15+
List<int> divisors = new List<int>();
16+
for (int i = number; --i > 0; )
17+
if (number % i == 0)
18+
divisors.Add(i);
19+
return divisors.ToArray();
20+
}
21+
22+
//get the smallest integer m greater than n,
23+
//both n and m have same number of '1' in their binary string
24+
static int getNextN(int n){
25+
int temp1 = n & (-n);//point out the first '1' in n's binary string form right to left
26+
int temp2 = n + temp1;
27+
int ret = temp2 | ((n ^ temp2) / temp1) >> 2;
28+
return ret;
29+
}
30+
static List<List<int>> getSubsets(int[] divisors)
31+
{
32+
List<List<int>> subsets = new List<List<int>>();
33+
int num = divisors.Length;
34+
for (int i = 1; i <= num; i++) { //get divisors's i element subsets
35+
int beginIdx = (1 << i) - 1;
36+
int endIdx = (1 << num) - (1 << (num - i));
37+
for (int j = beginIdx; j <= endIdx; ) { //the index of '1' in j's binary string from rigth to left indicates divisors[index] is in the subset
38+
//TODO:output subset
39+
List<int> subset = new List<int>();
40+
int index = 0;
41+
int k = j;
42+
while (k>0) {
43+
if (k % 2 == 1) subset.Add(divisors[index]);
44+
k /= 2;
45+
index++;
46+
}
47+
subsets.Add(subset);
48+
j = getNextN(j);
49+
}
50+
}
51+
return subsets;
52+
}
53+
54+
55+
static bool isRoom(List<List<int>> subsets, int room)
56+
{
57+
foreach (List<int> subset in subsets)
58+
{
59+
if (subset.Sum() == room)
60+
return false;
61+
}
62+
return true;
63+
}
64+
65+
static void findRoom(int totalRooms)
66+
{
67+
for (int i = 0; ++i < 101; )
68+
{
69+
int[] divisors = getDivisors(i);
70+
int sum = divisors.Sum();
71+
72+
if (sum <= i)
73+
continue;
74+
75+
if (isRoom(getSubsets(divisors), i))
76+
{
77+
Console.WriteLine(i);
78+
break;
79+
}
80+
}
81+
}
82+
83+
static void Main(string[] args)
84+
{
85+
int input = Convert.ToInt32(Console.ReadLine());
86+
findRoom(input);
87+
}
88+
}
89+
}

integer-length/integer-length.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
function IntegerLength($num){
4+
return strlen((string) $num);
5+
}
6+
7+
?>

longest-words/LongestWords.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
char[] delimiter = new char[] { ' ' };
3+
public List<string> FindLongestWords(string sentence)
4+
{
5+
List<string> longestWords = new List<string>();
6+
int currentLongestLength = 0;
7+
string[] words = sentence.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
8+
if (words != null && words.Length > 0)
9+
{
10+
foreach (string word in words)
11+
{
12+
// Duplicate check.
13+
if (!longestWords.Contains(word.ToLower()))
14+
{
15+
// If word is longer than the current longest. We clear our word list and add only this one.
16+
if (word.Length > currentLongestLength)
17+
{
18+
longestWords.Clear();
19+
longestWords.Add(word.ToLower());
20+
currentLongestLength = word.Length;
21+
}
22+
// If word's length equals currentLongest, we just add it to the list.
23+
else if (word.Length == currentLongestLength)
24+
{
25+
longestWords.Add(word);
26+
}
27+
}
28+
}
29+
}
30+
return longestWords;
31+
}

merge-sort/Readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Merge Sort
2+
3+
Implement the [merge sort algorithm](http://en.wikipedia.org/wiki/Merge_sort).

merge-sort/merge-sort.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Sort the array by breaking it down into smaller chunks
2+
module.exports = function mergeSort (array, compare) {
3+
// If's not an array or an array of just one element, it's already sorted
4+
if (!Array.isArray(array) || array.length < 2) { return array; }
5+
6+
var length = array.length,
7+
middle = Math.floor(length * 0.5),
8+
left = array.slice(0, middle),
9+
right = array.slice(middle, length);
10+
11+
// Create a compare func if not passed in
12+
if (typeof compare !== 'function') {
13+
compare = function (a, b) {
14+
return a > b ? 1 : -1;
15+
};
16+
}
17+
18+
var merge = function (left, right) {
19+
var result = [];
20+
while (left.length || right.length) {
21+
if (left.length && right.length) {
22+
if (compare(left[0], right[0]) < 1) {
23+
result.push(left.shift());
24+
} else {
25+
result.push(right.shift());
26+
}
27+
} else if (left.length) {
28+
result.push(left.shift());
29+
} else {
30+
result.push(right.shift());
31+
}
32+
}
33+
return result;
34+
};
35+
36+
return merge(mergeSort(left), mergeSort(right));
37+
};

once/Readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Once
2+
3+
Write a function that accepts a function as it's only argument and returns a new function that can only ever be executed once.
4+
5+
Once completed, add a second arguments that allows the function to be executed `x` number of times before it stops working.

once/once.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = function (fn, times) {
2+
// Set times to one if nothing is passed through and keep track of the
3+
// latest return value to return when we run out of execution times
4+
var memo;
5+
times = times || 1;
6+
// Return another function that will be executed
7+
return function () {
8+
if (!times) { return memo; }
9+
// Set memo to the result of the function and decrement the number of executions
10+
memo = fn.apply(this, arguments);
11+
times -= 1;
12+
// If there are no more execution times, set the function to `null` so
13+
// it can be garbage collected and return the memo
14+
times || (fn = null);
15+
return memo;
16+
};
17+
};

0 commit comments

Comments
 (0)