Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82d1c2a

Browse files
authored
Merge branch 'master' into master
2 parents 98d2cb7 + 7af7a1f commit 82d1c2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+421
-347
lines changed

‎.github/workflows/UpdateDirectory.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

‎.github/workflows/UpdateDirectory.mjs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
import { globby } from 'globby'
4+
5+
const URL_BASE = 'https://github.com/TheAlgorithms/Javascript/blob/master'
6+
7+
function pathPrefix (i) {
8+
if (i) {
9+
const res = ' '.repeat(i)
10+
return res + '*'
11+
} else {
12+
return '\n##'
13+
}
14+
}
15+
16+
function printPath (oldPath, newPath, output) {
17+
const oldParts = oldPath.split(path.sep)
18+
const newParts = newPath.split(path.sep)
19+
for (let i = 0; i < newParts.length; ++i) {
20+
const newPart = newParts[i]
21+
if (i + 1 > oldParts.length || oldParts[i] !== newPart) {
22+
if (newPart) {
23+
output.push(`${pathPrefix(i)} ${newPart.replace('_', ' ')}`)
24+
}
25+
}
26+
}
27+
return newPath
28+
}
29+
30+
function pathsToMarkdown (filePaths) {
31+
const output = []
32+
33+
let oldPath = ''
34+
filePaths.sort(function (a, b) {
35+
if (a.toLowerCase() < b.toLowerCase()) return -1
36+
if (a.toLowerCase() > b.toLowerCase()) return 1
37+
return 0
38+
})
39+
for (let filepath of filePaths) {
40+
const file = filepath.split(path.sep)
41+
let filename = ''
42+
if (file.length === 1) {
43+
filepath = ''
44+
filename = file[0]
45+
} else {
46+
const total = file.length
47+
filename = file[total - 1]
48+
filepath = file.splice(0, total - 1).join(path.sep)
49+
}
50+
if (filepath !== oldPath) {
51+
oldPath = printPath(oldPath, filepath, output)
52+
}
53+
let indent = 0
54+
for (let i = 0; i < filepath.length; ++i) {
55+
if (filepath[i] === path.sep) {
56+
++indent
57+
}
58+
}
59+
if (filepath) {
60+
++indent
61+
}
62+
63+
// prepare the markdown-esque prefix to the file's line
64+
const prefix = pathPrefix(indent)
65+
66+
// remove extension from filename
67+
const name = filename.split('.')[0]
68+
69+
// create URL to the actual file on github
70+
const url = encodeURI([URL_BASE, filepath, filename].join('/'))
71+
72+
output.push(`${prefix} [${name}](${url})`)
73+
}
74+
75+
return output.join('\n')
76+
}
77+
78+
// get paths of all .js files - excluding node_modules, the .github folder, tests and config stuff
79+
globby(['**/*.js', '!(node_modules|.github)/**/*', '!**/*.test.js', '!babel.config.js'])
80+
// create markdown content
81+
.then(pathsToMarkdown)
82+
// write markdown to file
83+
.then(markdown => fs.writeFileSync('DIRECTORY.md', markdown + '\n', { encoding: 'utf8' }))

‎.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Continuous Integration
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-node@v2
11+
with:
12+
node-version: '14'
13+
14+
- name: 📦 Install dependencies
15+
run: npm ci
16+
17+
- name: 🧪 Run tests
18+
run: |
19+
npm run doctest || true # TODO: Add all doctests
20+
npm test
21+
22+
- name: 💄 Code style
23+
run: npm run style
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if ! git diff --quiet DIRECTORY.md; then
2+
echo Changes found, attempting to commit and push...
3+
git add DIRECTORY.md
4+
git commit -am "Auto-update DIRECTORY.md" || true
5+
git push --force origin HEAD:$GITHUB_REF || true
6+
echo ... done.
7+
else
8+
echo No changes found, exiting.
9+
fi
10+

‎.github/workflows/nodejs.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
# This GitHub Action updates the DIRECTORY.md file (if needed) when doing a git push
2-
name: update_directory_md
2+
name: Update Directory
3+
34
on: [push]
5+
46
jobs:
5-
update_directory_md:
7+
updateDirectory:
68
runs-on: ubuntu-latest
79
steps:
8-
- uses: actions/checkout@master
9-
- uses: actions/setup-node@v1
10-
- run: |
11-
node .github/workflows/UpdateDirectory.js
12-
cat DIRECTORY.md
10+
- uses: actions/checkout@v2
11+
- uses: actions/setup-node@v2
12+
with:
13+
node-version: '14'
14+
15+
- name: 📦 Install dependencies
16+
run: npm ci
17+
18+
- name: 🗄️ Create Directory from JS files
19+
run: node .github/workflows/UpdateDirectory.mjs
20+
21+
- name: 🤓 Commit & push new Directory (if needed)
22+
run: |
1323
git config --global user.name github-actions
1424
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
1525
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
16-
git add DIRECTORY.md
17-
git commit -am "updating DIRECTORY.md" || true
18-
git push --force origin HEAD:$GITHUB_REF || true
26+
.github/workflows/commitAndPushDirectory.sh

‎Backtracking/SumOfSubset.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Given an ordered set W of non-negative integers and a value K,
66
* determine all possible subsets from the given set W whose sum
7-
* of its elemets equals to the given value K.
7+
* of its elements equals to the given value K.
88
*
99
* More info: https://www.geeksforgeeks.org/subset-sum-backtracking-4/
1010
*/
@@ -53,7 +53,7 @@ const sumOfSubset = (set, subset, setindex, sum, targetSum) => {
5353
targetSum
5454
)
5555

56-
// Concat the recursive result with current result arary
56+
// Concat the recursive result with current result array
5757
results = [...results, ...subsetResult]
5858
})
5959

‎Bit-Manipulation/BinaryCountSetBits.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
license: GPL-3.0 or later
44
55
This script will find number of 1's
6-
in binary representain of given number
6+
in binary representation of given number
77
88
*/
99

1010
function BinaryCountSetBits (a) {
1111
'use strict'
12-
// convert number into binary representation and return number of set bits in binary representaion
12+
// convert number into binary representation and return number of set bits in binary representation
1313
return a.toString(2).split('1').length - 1
1414
}
1515

‎Cache/Memoize.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Memoize
3+
*
4+
* From [Wikipedia](https://en.wikipedia.org/wiki/Memoization),
5+
* memoization is an optimization technique
6+
* used primarily to speed up computer programs,
7+
* by storing the results of expensive function calls
8+
* and returning the cached result when the same inputs occur again
9+
*
10+
* This function is a first class objects,
11+
* which lets us use it as [Higher-Order Function](https://eloquentjavascript.net/05_higher_order.html)
12+
* and return another function
13+
*
14+
* @param {Function} func Original function
15+
* @returns {Function} Memoized function
16+
*/
17+
export const memoize = (func) => {
18+
// Initialization of a slot to store the function result
19+
const cache = {}
20+
21+
return (...args) => {
22+
// Retrieving the first argument of the function
23+
const [arg] = args
24+
25+
/**
26+
* Checks if the argument is already present in the cache,
27+
* then return the associated value / result
28+
*/
29+
if (arg in cache) {
30+
return cache[arg]
31+
}
32+
33+
/**
34+
* If the argument is not yet present in the cache,
35+
* execute original function and save its value / result in cache,
36+
* finally return it
37+
*/
38+
const result = func(arg)
39+
cache[arg] = result
40+
return result
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /