count_by
This commit is contained in:
220
README.md
220
README.md
@ -23,25 +23,25 @@
|
||||
|
||||
<details><summary>View contents</summary> <ul><li><a href = "#chunk"><code>chunk</code></a></li>
|
||||
<li><a href = "#compact"><code>compact</code></a></li>
|
||||
<li><a href = "#count_by"><code>count_by</code></a></li>
|
||||
<li><a href = "#count_occurences"><code>count_occurences</code></a></li>
|
||||
<li><a href = "#deep_flatten"><code>deep_flatten</code></a></li>
|
||||
<li><a href = "#difference"><code>difference</code></a></li>
|
||||
<li><a href = "#shuffle"><code>shuffle</code></a></li>
|
||||
<li><a href = "#spread"><code>spread</code></a></li>
|
||||
<li><a href = "#zip"><code>zip</code></a></li>
|
||||
<li><a href = "#count_by"><code>count_by</code></a></li>
|
||||
</ul></details>
|
||||
|
||||
### :scroll: String
|
||||
|
||||
<details><summary>View contents</summary> <ul><li><a href = "#count_vowels"><code>count_vowels</code></a></li>
|
||||
<li><a href = "#byte_size"><code>byte_size</code></a></li>
|
||||
<details><summary>View contents</summary> <ul><li><a href = "#byte_size"><code>byte_size</code></a></li>
|
||||
<li><a href = "#capitalize"><code>capitalize</code></a></li>
|
||||
<li><a href = "#capitalize_every_word"><code>capitalize_every_word</code></a></li>
|
||||
<li><a href = "#count_vowels"><code>count_vowels</code></a></li>
|
||||
<li><a href = "#decapitalize"><code>decapitalize</code></a></li>
|
||||
<li><a href = "#palindrome"><code>palindrome</code></a></li>
|
||||
<li><a href = "#is_upper_case"><code>is_upper_case</code></a></li>
|
||||
<li><a href = "#is_lower_case"><code>is_lower_case</code></a></li>
|
||||
<li><a href = "#is_upper_case"><code>is_upper_case</code></a></li>
|
||||
<li><a href = "#palindrome"><code>palindrome</code></a></li>
|
||||
</ul></details>
|
||||
|
||||
<hr></hr>
|
||||
@ -307,6 +307,35 @@ compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### count_by
|
||||
|
||||
Groups the elements of a list based on the given function and returns the count of elements in each group.
|
||||
|
||||
Use `map()` to map the values of the list using the given function. Iterate over the map and increase the the elements count each time it occurs.
|
||||
|
||||
```py
|
||||
def count_by(arr, fn=lambda x: x):
|
||||
key = {}
|
||||
for el in map(fn, arr):
|
||||
key[el] = 0 if not el in key else key[el]
|
||||
key[el] += 1
|
||||
return key
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
from math import floor
|
||||
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}
|
||||
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### count_occurences
|
||||
|
||||
:information_source: Already implemented via `list.count()`.
|
||||
@ -490,64 +519,8 @@ zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2,
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### count_by
|
||||
|
||||
Groups the elements of a list based on the given function and returns the count of elements in each group.
|
||||
|
||||
Use `map()` to map the values of the list using the given function. Iterate over the map and increase the the elements count each time it occurs.
|
||||
|
||||
```py
|
||||
def count_by(arr, fn=lambda x: x):
|
||||
key = {}
|
||||
for el in map(fn, arr):
|
||||
key[el] = 0 if not el in key else key[el]
|
||||
key[el] += 1
|
||||
return key
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
from math import floor
|
||||
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}
|
||||
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
## :scroll: 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.
|
||||
|
||||
```py
|
||||
import re
|
||||
|
||||
|
||||
def count_vowels(str):
|
||||
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
count_vowels('foobar') # 3
|
||||
count_vowels('gym') # 0
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### byte_size
|
||||
|
||||
Returns the length of a string in bytes.
|
||||
@ -619,6 +592,33 @@ capitalize_every_word('hello world!') # 'Hello World!'
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### 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.
|
||||
|
||||
```py
|
||||
import re
|
||||
|
||||
|
||||
def count_vowels(str):
|
||||
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
count_vowels('foobar') # 3
|
||||
count_vowels('gym') # 0
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### decapitalize
|
||||
|
||||
Decapitalizes the first letter of a string.
|
||||
@ -643,55 +643,6 @@ decapitalize('FooBar', True) # 'fOOBAR'
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### palindrome
|
||||
|
||||
Returns `True` if the given string is a palindrome, `False` otherwise.
|
||||
|
||||
Convert string `str.lower()` and use `re.sub` to remove non-alphanumeric characters from it. Then compare the new string to the reversed.
|
||||
|
||||
```py
|
||||
def palindrome(string):
|
||||
from re import sub
|
||||
s = sub('[\W_]', '', string.lower())
|
||||
return s == s[::-1]
|
||||
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
palindrome('taco cat') # True
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### is_upper_case
|
||||
|
||||
Checks if a string is upper case.
|
||||
|
||||
Convert the given string to upper case, using `str.upper()` method and compare it to the original.
|
||||
|
||||
```py
|
||||
def is_upper_case(str):
|
||||
return str == str.upper()
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
is_upper_case('ABC') # True
|
||||
is_upper_case('a3@$') # True
|
||||
is_upper_case('aB4') # False
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### is_lower_case
|
||||
|
||||
Checks if a string is lower case.
|
||||
@ -717,6 +668,55 @@ is_lower_case('Ab4') # False
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### is_upper_case
|
||||
|
||||
Checks if a string is upper case.
|
||||
|
||||
Convert the given string to upper case, using `str.upper()` method and compare it to the original.
|
||||
|
||||
```py
|
||||
def is_upper_case(str):
|
||||
return str == str.upper()
|
||||
|
||||
```
|
||||
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
is_upper_case('ABC') # True
|
||||
is_upper_case('a3@$') # True
|
||||
is_upper_case('aB4') # False
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
### palindrome
|
||||
|
||||
Returns `True` if the given string is a palindrome, `False` otherwise.
|
||||
|
||||
Convert string `str.lower()` and use `re.sub` to remove non-alphanumeric characters from it. Then compare the new string to the reversed.
|
||||
|
||||
```py
|
||||
def palindrome(string):
|
||||
from re import sub
|
||||
s = sub('[\W_]', '', string.lower())
|
||||
return s == s[::-1]
|
||||
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
|
||||
palindrome('taco cat') # True
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
|
||||
|
||||
|
||||
## Credits
|
||||
|
||||
|
||||
@ -38,13 +38,13 @@ tag_dict = tagger()
|
||||
toAppend += '## Table of Contents \n'
|
||||
for category in tag_dict:
|
||||
toAppend = toAppend + '### ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n<details><summary>View contents</summary> <ul>'
|
||||
for snippet in tag_dict[category]:
|
||||
for snippet in sorted(tag_dict[category]):
|
||||
toAppend += f'<li><a href = "#{snippet}"><code>{snippet}</code></a></li>\n'
|
||||
toAppend += '</ul></details>\n\n'
|
||||
toAppend += '<hr></hr> \n\n'
|
||||
for category in tag_dict:
|
||||
toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n'
|
||||
for snippet in tag_dict[category]:
|
||||
for snippet in sorted(tag_dict[category]):
|
||||
someFile = open("snippets/" + snippet + '.md')
|
||||
fileData = someFile.read()
|
||||
codeParts = re.split(codeRe,fileData)
|
||||
|
||||
Reference in New Issue
Block a user