<headerstyle="height:5.5rem"><h1class="logo"><imgsrc="static/favicon.png"style="height:4rem"alt="logo"><spanid="title"> 30 seconds of python code</span><small>Python Implementation of 30 seconds of code</small></h1><labelfor="doc-drawer-checkbox"class="button drawer-toggle"id="menu-toggle"></label></header>
<p><emoji>ℹ</emoji> Already implemented via <code>statistics.mean</code>. <code>statistics.mean</code> takes an array as an argument whereas this function takes variadic arguments.</p>
<p>Returns the average of two or more numbers.</p>
<p>Takes the sum of all the <code>args</code> and divides it by <code>len(args)</code>. The second argument <code>0.0</code> in sum is to handle floating point division in <code>python3</code>.</p>
<p>Use recursion. If <code>num</code> is less than or equal to <code>1</code>, return <code>1</code>. Otherwise, return the product of <code>num</code> and the factorial of <code>num - 1</code>. Throws an exception if <code>num</code> is a negative or a floating point number.</p>
<preclass="language-python">def factorial(num):
if not ((num >= 0) & (num % 1 == 0)):
raise Exception(
f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1)</pre>
<p><emoji>ℹ</emoji><code>math.gcd</code> works with only two numbers</p>
<p>Calculates the greatest common divisor between two or more numbers/lists.</p>
<p>The <code>helperGcdfunction</code> uses recursion. Base case is when <code>y</code> equals <code>0</code>. In this case, return <code>x</code>. Otherwise, return the GCD of <code>y</code> and the remainder of the division <code>x/y</code>.</p>
<p>Uses the reduce function from the inbuilt module <code>functools</code>. Also defines a method <code>spread</code> for javascript like spreading of lists.</p>
<p>Returns the least common multiple of two or more numbers.</p>
<p>Use the <code>greatest common divisor (GCD)</code> formula and the fact that <code>lcm(x,y) = x * y / gcd(x,y)</code> to determine the least common multiple. The GCD formula uses recursion.</p>
<p>Uses <code>reduce</code> function from the inbuilt module <code>functools</code>. Also defines a method <code>spread</code> for javascript like spreading of lists.</p>
<p>Returns the <code>n</code> maximum elements from the provided list. If <code>n</code> is greater than or equal to the provided list's length, then return the original list(sorted in descending order).</p>
<p>Use <code>list.sort()</code> combined with the <code>deepcopy</code> function from the inbuilt <code>copy</code> module to create a shallow clone of the list and sort it in ascending order and then use <code>list.reverse()</code> reverse it to make it descending order. Use <code>[:n]</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element list</p>
<p>Returns the <code>n</code> minimum elements from the provided list. If <code>n</code> is greater than or equal to the provided list's length, then return the original list(sorted in ascending order).</p>
<p>Use <code>list.sort()</code> combined with the <code>deepcopy</code> function from the inbuilt <code>copy</code> module to create a shallow clone of the list and sort it in ascending order. Use <code>[:n]</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element list</p>
<p>Use recursion. Use <code>list.extend()</code> with an empty list (<code>result</code>) and the spread function to flatten a list. Recursively flatten each element that is a list.</p>
<preclass="language-python">def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def deep_flatten(lst):
result = []
result.extend(
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
<p><emoji>ℹ</emoji> Already implemented via <code>itertools.zip_longest()</code></p>
<p>Creates a list of elements, grouped based on the position in the original lists.</p>
<p>Use <code>max</code> combined with <code>list comprehension</code> to get the length of the longest list in the arguments. Loops for <code>max_length</code> times grouping elements. If lengths of <code>lists</code> vary <code>fill_value</code> is used. By default <code>fill_value</code> is <code>None</code>.</p>
<p><emoji>ℹ</emoji> Already implemented via <code>collections.Counter</code></p>
<p>Groups the elements of a list based on the given function and returns the count of elements in each group.</p>
<p>Use <code>map()</code> 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.</p>
<p>Returns the difference between two list, after applying the provided function to each list element of both.</p>
<p>Create a <code>set</code> by applying <code>fn</code> to each element in <code>b</code>, then use list comprehension in combination with fn on a to only keep values not contained in the previously created <code>set</code>.</p>
<p>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!</p>
<p>Capitalizes the fist letter of the sring and then adds it with rest of the string. Omit the <code>lower_rest</code> parameter to keep the rest of the string intact, or set it to <code>true</code> to convert to lowercase.</p>
<p>Decapitalizes the first letter of a string.</p>
<p>Decapitalizes the fist letter of the sring and then adds it with rest of the string. Omit the <code>upper_rest</code> parameter to keep the rest of the string intact, or set it to <code>true</code> to convert to uppercase.</p>
<p>Returns <code>True</code> if the given string is a palindrome, <code>False</code> otherwise.</p>
<p>Convert string <code>str.lower()</code> and use <code>re.sub</code> to remove non-alphanumeric characters from it. Then compare the new string to the reversed.</p>
<p>Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.</p>
<p>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).</p>
<p>Function which accepts a dictionary of key value pairs and returns a new flat list of only the values.</p>
<p>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.</p>
<p>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.</p>
<p>This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list.</p>
<preclass="language-python">def all_unique(lst):
return len(lst) == len(set(lst))</pre>
<labelclass="collapse">Show examples</label>
<preclass="language-python">x = [1,2,3,4,5,6]
y = [1,2,2,3,4,5]
all_unique(x) # True
all_unique(y) # False</pre>
<buttonclass="primary clipboard-copy">📋 Copy to clipboard</button></div>
</div><buttonclass="scroll-to-top">↑</button>
<footer><pstyle="display:inline-block"><strong>30 seconds of python code</strong> is licensed under the <ahref="https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE">GPL-3.0</a> license.<br>Icons made by <ahref="https://www.flaticon.com/authors/smashicons">Smashicons</a> from <ahref="https://www.flaticon.com/">www.flaticon.com</a> is licensed by <ahref="http://creativecommons.org/licenses/by/3.0/">CC 3.0 BY</a>.<br>Ribbon made by <ahref="https://github.com/tholman/github-corners">Tim Holman</a> is licensed by <ahref="https://opensource.org/licenses/MIT">The MIT License</a><br>Built with the <ahref="https://minicss.org">mini.css framework</a>.</p></footer>
<footer><p style="display:inline-block"><strong>30 seconds of python code</strong> is licensed under the <a href="https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE">GPL-3.0</a> license.<br>Icons made by <a href="https://www.flaticon.com/authors/smashicons">Smashicons</a> from <a href="https://www.flaticon.com/">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/">CC 3.0 BY</a>.<br>Ribbon made by <a href="https://github.com/tholman/github-corners">Tim Holman</a> is licensed by <a href="https://opensource.org/licenses/MIT">The MIT License</a><br>Built with the <a href="https://minicss.org">mini.css framework</a>.</p></footer>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.