Merge branch 'master' into master
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
|
||||
Returns the length of a string in bytes.
|
||||
|
||||
`utf-8` encodes a given string and find its length.
|
||||
`utf-8` encodes a given string, then `len` finds the length of the encoded string.
|
||||
|
||||
```python
|
||||
def byte_size(string):
|
||||
@ -12,4 +12,4 @@ def byte_size(string):
|
||||
```python
|
||||
byte_size('😀') # 4
|
||||
byte_size('Hello World') # 11
|
||||
```
|
||||
```
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Capitalizes the first letter of a string.
|
||||
|
||||
Capitalizes the fist letter of the sring and then adds it with rest of the string. Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.
|
||||
Capitalizes the first letter of the string and then adds it with rest of the string. Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.
|
||||
|
||||
```python
|
||||
def capitalize(string, lower_rest=False):
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Capitalizes the first letter of every word in a string.
|
||||
|
||||
Uses `str.title` to capitalize first letter of evry word in the string.
|
||||
Uses `str.title` to capitalize first letter of every word in the string.
|
||||
|
||||
```python
|
||||
def capitalize_every_word(string):
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
:information_source: Already implemented via `list.count()`.
|
||||
|
||||
Counts the occurrences of a value in an list.
|
||||
Counts the occurrences of a value in a list.
|
||||
|
||||
Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.
|
||||
|
||||
|
||||
@ -7,12 +7,9 @@ Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a st
|
||||
```python
|
||||
import re
|
||||
|
||||
|
||||
def count_vowels(str):
|
||||
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
|
||||
```
|
||||
return len(re.findall(r'[aeiou]', str, re.IGNORECASE))
|
||||
|
||||
``` python
|
||||
count_vowels('foobar') # 3
|
||||
count_vowels('gym') # 0
|
||||
count_vowels('gym')# 0
|
||||
```
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Decapitalizes the first letter of a string.
|
||||
|
||||
Decapitalizes the fist letter of the sring and then adds it with rest of the string. Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to uppercase.
|
||||
Decapitalizes the first letter of the string and then adds it with rest of the string. Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to uppercase.
|
||||
|
||||
```python
|
||||
def decapitalize(string, upper_rest=False):
|
||||
|
||||
33
snippets/fermat_test.md
Normal file
33
snippets/fermat_test.md
Normal file
@ -0,0 +1,33 @@
|
||||
### fermat_test
|
||||
|
||||
Checks if the number is prime or not. Returns True if passed number is prime, and False if not.
|
||||
|
||||
The function uses Fermat's theorem.
|
||||
First, it picks the number `A` in range `1`..`(n-1)`, then it checks if `A` to the power of `n-1` modulo `n` equals `1`.
|
||||
If not, the number is not prime, else it's pseudoprime with probability 1/2. Applying this test `k `times we have probability `1/(2^k)`.
|
||||
For example, if the number passes the test `10` times, we have probability `0.00098`.
|
||||
|
||||
``` python
|
||||
from random import randint
|
||||
|
||||
|
||||
def fermat_test(n, k=100):
|
||||
if n <= 1:
|
||||
return False
|
||||
for i in range(k):
|
||||
a = randint(1, n - 1)
|
||||
if pow(a, n - 1, n) != 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
```
|
||||
|
||||
``` python
|
||||
fermat_test(0) # False
|
||||
fermat_test(1) # False
|
||||
fermat_test(561) # False
|
||||
fermat_test(41041) # False
|
||||
fermat_test(17) # True
|
||||
fermat_test(162259276829213363391578010288127) # True
|
||||
fermat_test(-1) # False
|
||||
```
|
||||
20
snippets/is_anagram.md
Normal file
20
snippets/is_anagram.md
Normal file
@ -0,0 +1,20 @@
|
||||
### is_anagram
|
||||
|
||||
Determine if 2 strings are anagrams.
|
||||
|
||||
Returns true if 2 strings are anagrams of each other, false otherwise.
|
||||
Capital letters and whitespaces are ignored.
|
||||
|
||||
``` python
|
||||
def is_anagram(str1, str2):
|
||||
str1, str2 = str1.replace(" ", ""), str2.replace(" ", "")
|
||||
|
||||
if len(str1) != len(str2):
|
||||
return False
|
||||
else:
|
||||
return sorted(str1.lower()) == sorted(str2.lower())
|
||||
```
|
||||
|
||||
``` python
|
||||
is_anagram("anagram", "Nag a ram") # True
|
||||
```
|
||||
@ -11,6 +11,6 @@ def is_upper_case(string):
|
||||
|
||||
```python
|
||||
is_upper_case('ABC') # True
|
||||
is_upper_case('a3@$') # True
|
||||
is_upper_case('a3@$') # False
|
||||
is_upper_case('aB4') # False
|
||||
```
|
||||
```
|
||||
|
||||
@ -2,14 +2,11 @@
|
||||
|
||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
|
||||
|
||||
Uses the .items() function with a for loop on the dictionary to track both the key and value and returns a new list by appending the keys to it. Best used on 1 level-deep key:value pair dictionaries (a flat dictionary) and not nested data-structures which are also commonly used with dictionaries. (a flat dictionary resembles a json and a flat list an array for javascript people).
|
||||
Uses the .keys() method of "dict" objects. dict.keys() returns a view object that displays a list of all the keys. Then, list(dict.keys()) returns a list that stores all the keys of a dict.
|
||||
|
||||
``` python
|
||||
def keys_only(flat_dict):
|
||||
lst = []
|
||||
for k, v in flat_dict.items():
|
||||
lst.append(k)
|
||||
return lst
|
||||
return list(flat_dict.keys())
|
||||
```
|
||||
|
||||
``` python
|
||||
|
||||
@ -12,7 +12,7 @@ def zip(*args, fillvalue=None):
|
||||
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))
|
||||
args[k][i] if i < len(args[k]) else fillvalue for k in range(len(args))
|
||||
])
|
||||
return result
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user