Update remaining snippets
This commit is contained in:
@ -1,17 +1,21 @@
|
||||
---
|
||||
title: palindrome
|
||||
tags: string
|
||||
tags: string,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
Use `str.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string.
|
||||
Then, compare the new string with its reverse.
|
||||
|
||||
```py
|
||||
from re import sub
|
||||
|
||||
def palindrome(string):
|
||||
from re import sub
|
||||
s = sub('[\W_]', '', string.lower())
|
||||
return s == s[::-1]
|
||||
```
|
||||
|
||||
```py
|
||||
palindrome('taco cat') # True
|
||||
```
|
||||
@ -1,8 +1,7 @@
|
||||
---
|
||||
title: shuffle
|
||||
tags: list
|
||||
tags: list,random,intermediate
|
||||
---
|
||||
:information_source: The same algorithm is already implemented via `random.shuffle`.
|
||||
|
||||
Randomizes the order of the values of an list, returning a new list.
|
||||
|
||||
@ -12,7 +11,6 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y
|
||||
from copy import deepcopy
|
||||
from random import randint
|
||||
|
||||
|
||||
def shuffle(lst):
|
||||
temp_lst = deepcopy(lst)
|
||||
m = len(temp_lst)
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
---
|
||||
title: spread
|
||||
tags: list
|
||||
tags: list,utility,intermediate
|
||||
---
|
||||
Implements javascript's `[].concat(...arr)`. Flattens the list(non-deep) and returns an list.
|
||||
|
||||
Flattens a list, by spreading its elements into a new list.
|
||||
|
||||
Loop over elements, use `list.extend()` if the element is a list, `list.append()` otherwise.
|
||||
|
||||
```py
|
||||
def spread(arg):
|
||||
@ -15,7 +18,6 @@ def spread(arg):
|
||||
return ret
|
||||
```
|
||||
|
||||
|
||||
```py
|
||||
spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
|
||||
```
|
||||
@ -1,10 +1,11 @@
|
||||
---
|
||||
title: unique_elements
|
||||
tags: list
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Returns the unique elements in a given list.
|
||||
|
||||
The given `list` is first converted to a `set` using the `set()` function. By definition, a `set` cannot have duplicate elements. So, the duplicate elements are automatically removed. Before returning, we convert it back to a `list`
|
||||
Create a `set` from the list to discard duplicated values, then return a `list` from it.
|
||||
|
||||
```py
|
||||
def unique_elements(li):
|
||||
|
||||
@ -1,17 +1,16 @@
|
||||
---
|
||||
title: values_only
|
||||
tags: object
|
||||
tags: object,list,beginner
|
||||
---
|
||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the values.
|
||||
|
||||
Uses the .items() function with a for loop on the dictionary to track both the key and value of the iteration and returns a new list by appending the values to it. Best used on 1 level-deep key:value pair dictionaries and not nested data-structures.
|
||||
Returns a flat list of all the values in a flat dictionary.
|
||||
|
||||
Use `dict.values()` to return the values in the given dictionary.
|
||||
Return a `list()` of the previous result.
|
||||
|
||||
```py
|
||||
def values_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(v)
|
||||
return lst
|
||||
return list(flat_dict.values())
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
---
|
||||
title: zip
|
||||
tags: list
|
||||
tags: list,math,intermediate
|
||||
---
|
||||
: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`.
|
||||
|
||||
Use `max` combined with `list comprehension` to get the length of the longest list in the arguments.
|
||||
Loop for `max_length` times grouping elements.
|
||||
If lengths of `lists` vary, use `fill_value` (defaults to `None`).
|
||||
|
||||
```py
|
||||
def zip(*args, fillvalue=None):
|
||||
|
||||
Reference in New Issue
Block a user