update readme.py

This commit is contained in:
Rohit Tanwar
2018-01-21 12:13:24 +05:30
parent 862327ceed
commit 3266885fc0
6 changed files with 145 additions and 104 deletions

208
README.md
View File

@ -8,6 +8,8 @@
**Note**:- This is in no way affiliated with the original [30-seconds-of-code](https://github.com/Chalarangelo/30-seconds-of-code/).
# list
### chunk
Chunks an array into smaller lists of a specified size.
@ -50,6 +52,8 @@ compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
```
### count_occurences
:information_source: Already implemented via `list.count()`.
Counts the occurrences of a value in an list.
Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list.
@ -67,28 +71,6 @@ def count_occurences(arr, val):
```python
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
```
### count_vowels
Retuns `number` of vowels in provided `string`.
Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string.
```python
import re
def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
```
``` python
count_vowels('foobar') # 3
count_vowels('gym') # 0
```
### deep_flatten
Deep flattens a list.
@ -137,6 +119,111 @@ def difference(a, b):
``` python
difference([1, 2, 3], [1, 2, 4]) # [3]
```
### shuffle
:information_source: The same algorithm is already implemented via `random.shuffle`.
Randomizes the order of the values of an list, returning a new list.
Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list.
```python
from copy import deepcopy
from random import randint
def shuffle(arr):
temp_arr = deepcopy(arr)
m = len(temp_arr)
while (m):
m -= 1
i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
return temp_arr
```
``` python
foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]
```
### spread
Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list.
```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]
```
### zip
:information_source: Already implemented via `itertools.zip_longest()`
Creates a list of elements, grouped based on the position in the original lists.
Use `max` combined with `list comprehension` to get the length of the longest list in the arguments. Loops for `max_length` times grouping elements. If lengths of `lists` vary `fill_value` is used. By default `fill_value` is `None`.
```python
def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args])
result = []
for i in range(max_length):
result.append([
args[k][i] if i < len(args[k]) else None for k in range(len(args))
])
return result
```
``` python
zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]
zip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]]
zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]
```
# string
### count_vowels
Retuns `number` of vowels in provided `string`.
Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string.
```python
import re
def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
```
``` python
count_vowels('foobar') # 3
count_vowels('gym') # 0
```
# math
### gcd
Calculates the greatest common divisor between two or more numbers/lists.
@ -266,83 +353,6 @@ def min_n(arr, n=1):
min_n([1, 2, 3]) # [1]
min_n([1, 2, 3], 2) # [1,2]
```
### shuffle
Randomizes the order of the values of an list, returning a new list.
Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list.
```python
from copy import deepcopy
from random import randint
def shuffle(arr):
temp_arr = deepcopy(arr)
m = len(temp_arr)
while (m):
m -= 1
i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
return temp_arr
```
``` python
foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]
```
### spread
Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list.
```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]
```
### zip
:information_source: Already implemented via `itertools.zip_longest()`
Creates a list of elements, grouped based on the position in the original lists.
Use `max` combined with `list comprehension` to get the length of the longest list in the arguments. Loops for `max_length` times grouping elements. If lengths of `lists` vary `fill_value` is used. By default `fill_value` is `None`.
```python
def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args])
result = []
for i in range(max_length):
result.append([
args[k][i] if i < len(args[k]) else None for k in range(len(args))
])
return result
```
``` python
zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]
zip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]]
zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]
```
## Credits

View File

@ -6,7 +6,6 @@ files = os.listdir('snippets')
codeRe = "```\s*python([\s\S]*?)```"
for file in files:
someFile = open("snippets/" + file)
print(file)
fileData = someFile.read()
someFile.close()
originalCode = re.search(codeRe,fileData).group(0)

View File

@ -1,10 +1,25 @@
import os
def tagger():
tag_data = open('tag_database').read()
tag_dict = {}
tag_list = tag_data.split('\n')
for tag in tag_list:
category = tag.split(':')[1]
snippet = tag.split(':')[0]
if category in tag_dict:
tag_dict[category].append(snippet)
else:
tag_dict[category] = [snippet]
return tag_dict
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)
tag_dict = tagger()
for category in tag_dict:
toAppend = toAppend + '# ' + category +'\n\n'
for snippet in tag_dict[category]:
someFile = open("snippets/" + snippet + '.md')
fileData = someFile.read()
toAppend += fileData + '\n'
open("README.md",'w').write(start+toAppend+'\n'+end)

View File

@ -1,5 +1,7 @@
### count_occurences
:information_source: Already implemented via `list.count()`.
Counts the occurrences of a value in an list.
Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list.

View File

@ -1,5 +1,7 @@
### shuffle
:information_source: The same algorithm is already implemented via `random.shuffle`.
Randomizes the order of the values of an list, returning a new list.
Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list.

13
tag_database Normal file
View File

@ -0,0 +1,13 @@
chunk:list
compact:list
count_occurences:list
count_vowels:string
deep_flatten:list
difference:list
gcd:math
lcm:math
max_n:math
min_n:math
shuffle:list
spread:list
zip:list