diff --git a/README.md b/README.md index 1c3997aaf..b8d13024c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +![Logo](/logo.png) # 30-seconds-of-code-python Python implementation of 30-seconds-of-code. diff --git a/icon.png b/icon.png new file mode 100644 index 000000000..631b8b740 Binary files /dev/null and b/icon.png differ diff --git a/scripts/pretty.py b/scripts/pretty.py new file mode 100644 index 000000000..0c6eca544 --- /dev/null +++ b/scripts/pretty.py @@ -0,0 +1,16 @@ +from yapf.yapflib.yapf_api import FormatCode +import re +import pprint +import os +files = os.listdir('../snippets') +codeRe = "```\s*python([\s\S]*?)```" +for file in files: + someFile = open("../snippets/" + file) + fileData = someFile.read() + someFile.close() + originalCode = re.search(codeRe,fileData).group(0) + formatedCode = FormatCode(re.split(codeRe,fileData)[1]) + fileToSave = fileData.replace(originalCode,('```python \n'+formatedCode[0]+'\n```\n')) + someFile = open("../snippets/"+file,'w') + someFile.write(fileToSave) + someFile.close() \ No newline at end of file diff --git a/snippets/gcd.md b/snippets/gcd.md index c37d1d73a..bbdd0f462 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -6,20 +6,34 @@ The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In thi Uses the reduce function from the inbuild module `functools`. Also defines a method `spread` for javascript like spreading of arrays. -``` python +```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 + 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 helperGcd(x,y): - return x if not y else gcd(y,x%y) - return reduce((lambda x,y : helperGcd(x,y)),numbers) + + 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 ``` \ No newline at end of file diff --git a/snippets/lcm.md b/snippets/lcm.md new file mode 100644 index 000000000..b269a7427 --- /dev/null +++ b/snippets/lcm.md @@ -0,0 +1,40 @@ +### 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 +``` \ No newline at end of file diff --git a/snippetsgcd.md b/snippetsgcd.md new file mode 100644 index 000000000..8cebe9a7a --- /dev/null +++ b/snippetsgcd.md @@ -0,0 +1 @@ +fileToSave \ No newline at end of file diff --git a/stopwatch.png b/stopwatch.png new file mode 100644 index 000000000..b84672d14 Binary files /dev/null and b/stopwatch.png differ