Update some snippets

This commit is contained in:
Angelos Chalaris
2019-08-20 10:38:09 +03:00
parent 1c93753ade
commit f10d3f018b
6 changed files with 37 additions and 47 deletions

View File

@ -1,16 +1,15 @@
--- ---
title: count_occurences title: count_occurences
tags: list tags: list,beginner
--- ---
:information_source: Already implemented via `list.count()`.
Counts the occurrences of a value in a 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. Increment a counter for every item in the list that has the given value and is of the same type.
```py ```py
def count_occurrences(lst, val): def count_occurrences(lst, val):
return len([x for x in lst if x == val and type(x) == type(val)]) return len([x for x in lst if x == val and type(x) == type(val)])
``` ```
```py ```py

View File

@ -1,17 +0,0 @@
---
title: count_vowels
tags: string
---
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(re.findall(r'[aeiou]', str, re.IGNORECASE))
count_vowels('foobar') # 3
count_vowels('gym')# 0
```

View File

@ -1,14 +1,16 @@
--- ---
title: decapitalize title: decapitalize
tags: string tags: string,intermediate
--- ---
Decapitalizes the first letter of a string. Decapitalizes the first letter of a string.
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. Decapitalize the first letter of the string and then add 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.
```py ```py
def decapitalize(string, upper_rest=False): def decapitalize(string, upper_rest=False):
return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:]) return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])
``` ```
```py ```py

View File

@ -1,27 +1,30 @@
--- ---
title: deep_flatten title: deep_flatten
tags: list tags: list,recursion,intermediate
--- ---
Deep flattens a list. Deep flattens a list.
Use recursion. Use `list.extend()` with an empty list (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list. Use recursion.
Define a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it.
Use `list.extend()` with an empty list and the `spread` function to flatten a list.
Recursively flatten each element that is a list.
```py ```py
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
if isinstance(i, list): if isinstance(i, list):
ret.extend(i) ret.extend(i)
else: else:
ret.append(i) ret.append(i)
return ret return ret
def deep_flatten(lst): def deep_flatten(lst):
result = [] result = []
result.extend( result.extend(
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst)))) spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
return result return result
``` ```
```py ```py

View File

@ -1,16 +1,18 @@
--- ---
title: difference title: difference
tags: list tags: list,beginner
--- ---
Returns the difference between two iterables. Returns the difference between two iterables.
Use list comprehension to only keep values not contained in `b`. Create a `set` fromn `b`, then use list comprehension on `a` to only keep values not contained in the previously created set, `_b`.
```py ```py
def difference(a, b): def difference(a, b):
_b = set(b) _b = set(b)
return [item for item in a if item not in _b] return [item for item in a if item not in _b]
``` ```
```py ```py
difference([1, 2, 3], [1, 2, 4]) # [3] difference([1, 2, 3], [1, 2, 4]) # [3]
``` ```

View File

@ -1,15 +1,16 @@
--- ---
title: difference_by title: difference_by
tags: list tags: list,function,intermediate
--- ---
Returns the difference between two list, after applying the provided function to each list element of both.
Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with fn on a to only keep values not contained in the previously created `set`. Returns the difference between two lists, after applying the provided function to each list element of both.
Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values not contained in the previously created set, `_b`.
```py ```py
def difference_by(a, b, fn): def difference_by(a, b, fn):
b = set(map(fn, b)) _b = set(map(fn, b))
return [item for item in a if fn(item) not in b] return [item for item in a if fn(item) not in _b]
``` ```
```py ```py