Update snippet structure to new template

This commit is contained in:
Angelos Chalaris
2019-08-19 14:32:26 +03:00
parent 811f3672ef
commit 59fd9ad9a8
38 changed files with 150 additions and 138 deletions

View File

@ -1,32 +0,0 @@
average:[Rohit Tanwar](@kriadmin),[Hui Binyuan](@huybery)
chunk:[Rohit Tanwar](@kriadmin)
compact:[Rohit Tanwar](@kriadmin)
count_occurences:[Rohit Tanwar](@kriadmin), [Hui Binyuan](@huybery)
count_vowels:[Rohit Tanwar](@kriadmin)
deep_flatten:[Rohit Tanwar](@kriadmin),[Meet Zaveri](@meetzaveri)
difference:[Rohit Tanwar](@kriadmin)
factorial:[Rohit Tanwar](@kriadmin)
gcd:[Rohit Tanwar](@kriadmin),[cclauss](@cclauss),[Sandhya Saravanan](@sandy9999)
lcm:[Rohit Tanwar](@kriadmin)
max_n:[Rohit Tanwar](@kriadmin)
min_n:[Rohit Tanwar](@kriadmin)
shuffle:[Rohit Tanwar](@kriadmin)
spread:[Rohit Tanwar](@kriadmin)
zip:[Rohit Tanwar](@kriadmin),[Leonardo Galdino](@LeonardoGaldino)
byte_size:[Rohit Tanwar](@kriadmin)
capitalize:[Rohit Tanwar](@kriadmin)
capitalize_every_word:[Rohit Tanwar](@kriadmin)
decapitalize:[Rohit Tanwar](@kriadmin)
palindrome:[Rohit Tanwar](@kriadmin)
is_upper_case:[Rohit Tanwar](@kriadmin)
is_lower_case:[Rohit Tanwar](@kriadmin)
count_by:[Rohit Tanwar](@kriadmin)
insertion_sort:[Meet Zaveri](@meetzaveri),[Rohit Tanwar](@kriadmin)
difference_by:[Rohit Tanwar](@kriadmin)
bubble_sort: [Shobhit Sachan](@sachans)
has_duplicates: [Rob-Rychs](@Rob-Rychs)
keys_only: [Rob-Rychs](@Rob-Rychs),[Matteo Veraldi](@mattveraldi)
values_only: [Rob-Rychs](@Rob-Rychs)
all_unique: [Rob-Rychs](@Rob-Rychs)
fibonacci_until_num: [Nian Lee](@scraggard)
fermat_test: [Alexander Pozdniakov](@0awawa0)

View File

@ -1,8 +1,11 @@
### function_name
---
title: function_name
tags:
---
Describe briefly what the function does
Explain briefly what the snippet does.
Explain your method and the functions used.
Explain briefly how the snippet works.
``` python
def function_name(args):
@ -11,5 +14,5 @@ def function_name(args):
```
``` python
function_name(val) # result
functionName(val) # result
```

View File

@ -1,5 +1,7 @@
### all_unique
---
title: all_unique
tags: list
---
Checks a flat list for all unique values. Returns True if list values are all unique and False if list values aren't all unique.
This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list.

View File

@ -1,5 +1,7 @@
### average
---
title: average
tags: math
---
:information_source: Already implemented via `statistics.mean`. `statistics.mean` takes an array as an argument whereas this function takes variadic arguments.
Returns the average of two or more numbers.

View File

@ -1,4 +1,8 @@
### bubble_sort
---
title: bubble_sort
tags: list
---
Bubble_sort uses the technique of comparing and swapping
```python

View File

@ -1,5 +1,7 @@
### byte_size
---
title: byte_size
tags: string
---
Returns the length of a string in bytes.
`utf-8` encodes a given string, then `len` finds the length of the encoded string.

View File

@ -1,5 +1,7 @@
### capitalize
---
title: capitalize
tags: string
---
Capitalizes the first letter of a string.
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.

View File

@ -1,5 +1,7 @@
### capitalize_every_word
---
title: capitalize_every_word
tags: string
---
Capitalizes the first letter of every word in a string.
Uses `str.title` to capitalize first letter of every word in the string.

View File

@ -1,5 +1,7 @@
### chunk
---
title: chunk
tags: list
---
Chunks an list into smaller lists of a specified size.
Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `lst`.

View File

@ -1,5 +1,7 @@
### compact
---
title: compact
tags: list
---
Removes falsey values from a list.
Use `filter()` to filter out falsey values (False, None, 0, and "").

View File

@ -1,5 +1,7 @@
### count_by
---
title: count_by
tags: list
---
:information_source: Already implemented via `collections.Counter`
Groups the elements of a list based on the given function and returns the count of elements in each group.

View File

@ -1,5 +1,7 @@
### count_occurences
---
title: count_occurences
tags: list
---
:information_source: Already implemented via `list.count()`.
Counts the occurrences of a value in a list.

View File

@ -1,5 +1,7 @@
### count_vowels
---
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.

View File

@ -1,5 +1,7 @@
### decapitalize
---
title: decapitalize
tags: 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.

View File

@ -1,5 +1,7 @@
### deep_flatten
---
title: deep_flatten
tags: 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.

View File

@ -1,5 +1,7 @@
### difference
---
title: difference
tags: list
---
Returns the difference between two iterables.
Use list comprehension to only keep values not contained in `b`.

View File

@ -1,5 +1,7 @@
### difference_by
---
title: difference_by
tags: list
---
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`.

View File

@ -1,5 +1,7 @@
### factorial
---
title: factorial
tags: math
---
Calculates the factorial of a number.
Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, return the product of `num` and the factorial of `num - 1`. Throws an exception if `num` is a negative or a floating point number.

View File

@ -1,5 +1,7 @@
### fermat_test
---
title: fermat_test
tags: math
---
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.

View File

@ -1,5 +1,7 @@
### fibonacci
---
title: fibonacci
tags: math
---
Generates an array, containing the Fibonacci sequence, up until the nth term.
Starting with 0 and 1, adds the sum of the last two numbers of the list to the end of the list using ```list.append()``` until the length of the list reaches n. If the given nth value is 0 or less, the method will just return a list containing 0.

View File

@ -1,5 +1,7 @@
### fibonacci_until_num
---
title: fibonacci_until_num
tags: math
---
Returns the n-th term in a Fibonnaci sequence that starts with 1
A term in a Fibonnaci sequence is the sum of the two previous terms.

View File

@ -1,5 +1,7 @@
### gcd
---
title: gcd
tags: math
---
Calculates the greatest common divisor of a list of numbers.
Uses the reduce function from the inbuilt module `functools`. Also uses the `math.gcd` function over a list.

View File

@ -1,5 +1,7 @@
### has_duplicates
---
title: has_duplicates
tags: list
---
Checks a flat list for duplicate values. Returns True if duplicate values exist and False if values are all unique.
This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list.

View File

@ -1,5 +1,7 @@
### insertion_sort
---
title: insertion_sort
tags: list
---
On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting!
```python

View File

@ -1,5 +1,7 @@
### is_anagram
---
title: is_anagram
tags: string
---
Determine if 2 strings are anagrams.
Returns true if 2 strings are anagrams of each other, false otherwise.

View File

@ -1,5 +1,7 @@
### is_lower_case
---
title: is_lower_case
tags: string
---
Checks if a string is lower case.
Convert the given string to lower case, using `str.lower()` method and compare it to the original.

View File

@ -1,5 +1,7 @@
### is_upper_case
---
title: is_upper_case
tags: string
---
Checks if a string is upper case.
Convert the given string to upper case, using `str.upper()` method and compare it to the original.

View File

@ -1,5 +1,7 @@
### keys_only
---
title: keys_only
tags: object
---
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
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.

View File

@ -1,5 +1,7 @@
### lcm
---
title: lcm
tags: math
---
Returns the least common multiple of two or more numbers.
Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple. The GCD formula uses recursion.

View File

@ -1,5 +1,7 @@
### max_n
---
title: max_n
tags: math
---
Returns the `n` maximum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in descending order).
Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order and then use `list.reverse()` reverse it to make it descending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element list

View File

@ -1,5 +1,7 @@
### min_n
---
title: min_n
tags: math
---
Returns the `n` minimum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in ascending order).
Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element list

View File

@ -1,5 +1,7 @@
### palindrome
---
title: palindrome
tags: string
---
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.

View File

@ -1,5 +1,7 @@
### shuffle
---
title: shuffle
tags: list
---
:information_source: The same algorithm is already implemented via `random.shuffle`.
Randomizes the order of the values of an list, returning a new list.

View File

@ -1,5 +1,7 @@
### spread
---
title: spread
tags: list
---
Implements javascript's `[].concat(...arr)`. Flattens the list(non-deep) and returns an list.
```python

View File

@ -1,5 +1,7 @@
### unique_elements
---
title: unique_elements
tags: list
---
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`

View File

@ -1,5 +1,7 @@
### values_only
---
title: values_only
tags: object
---
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.

View File

@ -1,5 +1,7 @@
### zip
---
title: zip
tags: list
---
:information_source: Already implemented via `itertools.zip_longest()`
Creates a list of elements, grouped based on the position in the original lists.

View File

@ -1,31 +0,0 @@
average:math
chunk:list
compact:list
count_occurences:list
count_vowels:string
deep_flatten:list
difference:list
factorial:math
gcd:math
lcm:math
max_n:math
min_n:math
shuffle:list
spread:list
zip:list
byte_size:string
capitalize:string
capitalize_every_word:string
decapitalize:string
palindrome:string
is_upper_case:string
is_lower_case:string
count_by:list
difference_by:list
insertion_sort:list
bubble_sort:list
has_duplicates:list
keys_only:object
values_only:object
all_unique:object
fibonacci_until_num:math