diff --git a/.travis.yml b/.travis.yml index fb21b738c..721a02ffd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,5 @@ python: install: pip install requirements.txt script: - python3 scripts/pretty.py \ No newline at end of file + python3 scripts/pretty.py + python3 scripts/readme.py \ No newline at end of file diff --git a/README.md b/README.md index f5efb883f..66bd6a028 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,102 @@ Python implementation of 30-seconds-of-code. **Note**:- This is in no way affiliated with the original [30-seconds-of-code](https://github.com/Chalarangelo/30-seconds-of-code/) +### gcd + +Calculates the greatest common divisor between two or more numbers/arrays. + +The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. + +Uses the reduce function from the inbuild module `functools`. Also defines a method `spread` for javascript like spreading of arrays. + +```python +from functools import reduce + + +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + + +def gcd(*args): + numbers = [] + numbers.extend(spread(list(args))) + + def _gcd(x, y): + return x if not y else gcd(y, x % y) + + return reduce((lambda x, y: _gcd(x, y)), numbers) + +``` + + +``` python +gcd(8,36) # 4 +``` +### lcm + +Returns the least common multiple of two or more numbers. + +Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple. The GCD formula uses recursion. + +```python +from functools import reduce + + +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + + +def lcm(*args): + numbers = [] + numbers.extend(spread(list(args))) + + def _gcd(x, y): + return x if not y else gcd(y, x % y) + + def _lcm(x, y): + return x * y / _gcd(x, y) + + return reduce((lambda x, y: _lcm(x, y)), numbers) + +``` + + +``` python +lcm(12, 7) # 84 +lcm([1, 3, 4], 5) # 60 +``` +### spread + +Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an array. + +```python +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + +``` + + +```python +spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9] +``` ## Credits diff --git a/scripts/pretty.py b/scripts/pretty.py index 051ed4a4e..b74c57a18 100644 --- a/scripts/pretty.py +++ b/scripts/pretty.py @@ -1,6 +1,5 @@ from yapf.yapflib.yapf_api import FormatCode import re -import pprint import os files = os.listdir('snippets') codeRe = "```\s*python([\s\S]*?)```" diff --git a/scripts/readme.py b/scripts/readme.py new file mode 100644 index 000000000..4b89b98d5 --- /dev/null +++ b/scripts/readme.py @@ -0,0 +1,10 @@ +import os +files = os.listdir('snippets') +start = open("static-parts/readme-start.md").read() + '\n\n' +end = open("static-parts/readme-end.md").read() +toAppend = '' +for file in files: + someFile = open("snippets/" + file) + fileData = someFile.read() + toAppend += fileData + '\n' +open("README.md",'w').write(start+toAppend+'\n'+end) \ No newline at end of file diff --git a/static-parts/readme-end.md b/static-parts/readme-end.md new file mode 100644 index 000000000..c9d2d14b1 --- /dev/null +++ b/static-parts/readme-end.md @@ -0,0 +1,3 @@ +## Credits + +*Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).* diff --git a/static-parts/readme-start.md b/static-parts/readme-start.md new file mode 100644 index 000000000..f72548c49 --- /dev/null +++ b/static-parts/readme-start.md @@ -0,0 +1,6 @@ +![Logo](/icon.png) + +# 30-seconds-of-code-python +Python implementation of 30-seconds-of-code. + +**Note**:- This is in no way affiliated with the original [30-seconds-of-code](https://github.com/Chalarangelo/30-seconds-of-code/) \ No newline at end of file