Travis build: 910

This commit is contained in:
Rohit Tanwar
2018-02-22 15:54:31 +00:00
parent 8e86b83e9a
commit 601344b68a
5 changed files with 480 additions and 434 deletions

View File

@ -1,44 +1,44 @@
#!/bin/bash #!/bin/bash
setup_git() { setup_git() {
git config --global user.email "mst10041967@gmail.com" git config --global user.email "mst10041967@gmail.com"
git config --global user.name "Rohit Tanwar" git config --global user.name "Rohit Tanwar"
} }
commit_website_files() { commit_website_files() {
if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then
if [ $TRAVIS_BRANCH == "master" ]; then if [ $TRAVIS_BRANCH == "master" ]; then
git checkout master git checkout master
echo "Committing to master branch..." echo "Committing to master branch..."
git add -A git add -A
echo "All files added" echo "All files added"
git status git status
if [ $TRAVIS_EVENT_TYPE == "cron" ]; then if [ $TRAVIS_EVENT_TYPE == "cron" ]; then
git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [cron]" git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [cron]"
elif [ $TRAVIS_EVENT_TYPE == "api" ]; then elif [ $TRAVIS_EVENT_TYPE == "api" ]; then
git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [custom]" git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [custom]"
else else
git commit --message "Travis build: $TRAVIS_BUILD_NUMBER" git commit --message "Travis build: $TRAVIS_BUILD_NUMBER"
fi fi
echo "Files commited" echo "Files commited"
git status git status
fi fi
fi fi
} }
upload_files() { upload_files() {
if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then
if [ $TRAVIS_BRANCH == "master" ]; then if [ $TRAVIS_BRANCH == "master" ]; then
echo "Pushing to master branch..." echo "Pushing to master branch..."
git push --force "https://${GH_TOKEN}@github.com/kriadmin/30-seconds-of-python-code.git" master > /dev/null 2>&1 git push --force "https://${GH_TOKEN}@github.com/kriadmin/30-seconds-of-python-code.git" master > /dev/null 2>&1
echo "Pushing done" echo "Pushing done"
echo "Pushing to website" echo "Pushing to website"
git subtree push --prefix website "https://${GH_TOKEN}@github.com/kriadmin/30-seconds-of-python-code.git" website git subtree push --prefix website "https://${GH_TOKEN}@github.com/kriadmin/30-seconds-of-python-code.git" website
echo "Pushed to master branch" echo "Pushed to master branch"
git status git status
fi fi
fi fi
} }
setup_git setup_git
commit_website_files commit_website_files
upload_files upload_files

View File

@ -1,6 +1,6 @@
![Logo](/icon.png) ![Logo](/icon.png)
# 30-seconds-of-python-code # 30-seconds-of-python-code [![Tweet](http://jpillora.com/github-twitter-button/img/tweet.png)](http://www.twitter.com/share?text=%2330secondsofcode+30-seconds-of-python-code+-+Python+Implementation+of+30+seconds+of+code%0Ahttps://github.com/kriadmin/30-seconds-of-python-code&url=a")
[![License](https://img.shields.io/aur/license/yaourt.svg)](https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-python-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/kriadmin/30-seconds-of-python-code.svg?branch=master)](https://travis-ci.org/kriadmin/30-seconds-of-python-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/kriadmin/30-seconds-of-python-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) [![License](https://img.shields.io/aur/license/yaourt.svg)](https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-python-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/kriadmin/30-seconds-of-python-code.svg?branch=master)](https://travis-ci.org/kriadmin/30-seconds-of-python-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/kriadmin/30-seconds-of-python-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code?ref=badge_shield) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code?ref=badge_shield)
@ -52,6 +52,7 @@
### chunk ### chunk
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Chunks an array into smaller lists of a specified size. Chunks an array into smaller lists of a specified size.
@ -77,6 +78,7 @@ chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
### compact ### compact
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Removes falsey values from a list. Removes falsey values from a list.
@ -97,6 +99,7 @@ compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
### count_by ### count_by
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
:information_source: Already implemented via `collections.Counter` :information_source: Already implemented via `collections.Counter`
@ -108,7 +111,7 @@ Use `map()` to map the values of the list using the given function. Iterate over
def count_by(arr, fn=lambda x: x): def count_by(arr, fn=lambda x: x):
key = {} key = {}
for el in map(fn, arr): for el in map(fn, arr):
key[el] = 0 if not el in key else key[el] key[el] = 0 if el not in key else key[el]
key[el] += 1 key[el] += 1
return key return key
``` ```
@ -125,6 +128,7 @@ count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
### count_occurences ### count_occurences
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
:information_source: Already implemented via `list.count()`. :information_source: Already implemented via `list.count()`.
@ -152,6 +156,7 @@ count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
### deep_flatten ### deep_flatten
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Meet Zaveri](https://www.github.com/meetzaveri) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Meet Zaveri](https://www.github.com/meetzaveri)
Deep flattens a list. Deep flattens a list.
@ -185,6 +190,7 @@ deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
### difference ### difference
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Returns the difference between two arrays. Returns the difference between two arrays.
@ -206,6 +212,7 @@ difference([1, 2, 3], [1, 2, 4]) # [3]
### difference_by ### difference_by
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Returns the difference between two list, after applying the provided function to each list element of both. Returns the difference between two list, after applying the provided function to each list element of both.
@ -229,6 +236,7 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x
### insertion_sort ### insertion_sort
<span style="color:grey">Author:-</span> [Meet Zaveri](https://www.github.com/meetzaveri) <span style="color:grey">Author:-</span> [Meet Zaveri](https://www.github.com/meetzaveri)
<span style="color:grey">Contributors:-</span>[Meet Zaveri](https://www.github.com/meetzaveri), [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Meet Zaveri](https://www.github.com/meetzaveri), [Rohit Tanwar](https://www.github.com/kriadmin)
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! 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!
@ -256,6 +264,7 @@ print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]
### shuffle ### shuffle
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
:information_source: The same algorithm is already implemented via `random.shuffle`. :information_source: The same algorithm is already implemented via `random.shuffle`.
@ -289,6 +298,7 @@ shuffle(foo) # [2,3,1] , foo = [1,2,3]
### spread ### spread
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Implements javascript's `[].concat(...arr)`. Flattens the list(non-deep) and returns an list. Implements javascript's `[].concat(...arr)`. Flattens the list(non-deep) and returns an list.
@ -313,6 +323,7 @@ spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
### zip ### zip
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
:information_source: Already implemented via `itertools.zip_longest()` :information_source: Already implemented via `itertools.zip_longest()`
@ -345,13 +356,14 @@ zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2,
### average ### average
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Hui Binyuan](https://www.github.com/huybery)
:information_source: Already implemented via `statistics.mean`. `statistics.mean` takes an array as an argument whereas this function takes variadic arguments. :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. Returns the average of two or more numbers.
Takes the sum of all the `args` and divides it by `len(args)`. The secind argument `0.0` in sum is to handle floating point division in `python2`. Takes the sum of all the `args` and divides it by `len(args)`. The second argument `0.0` in sum is to handle floating point division in `python2`.
```py ```py
def average(*args): def average(*args):
return sum(args, 0.0) / len(args) return sum(args, 0.0) / len(args)
@ -368,6 +380,7 @@ average(1, 2, 3) # 2.0
### factorial ### factorial
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Calculates the factorial of a number. Calculates the factorial of a number.
@ -391,6 +404,7 @@ factorial(6) # 720
### gcd ### gcd
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [cclauss][@cclauss] <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [cclauss][@cclauss]
:information_source: `math.gcd` works with only two numbers :information_source: `math.gcd` works with only two numbers
@ -434,6 +448,7 @@ gcd(8,36) # 4
### lcm ### lcm
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Returns the least common multiple of two or more numbers. Returns the least common multiple of two or more numbers.
@ -479,6 +494,7 @@ lcm([1, 3, 4], 5) # 60
### max_n ### max_n
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
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). 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).
@ -506,6 +522,7 @@ max_n([1, 2, 3], 2) # [3,2]
### min_n ### min_n
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
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). 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).
@ -534,6 +551,7 @@ min_n([1, 2, 3], 2) # [1,2]
### byte_size ### byte_size
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Returns the length of a string in bytes. Returns the length of a string in bytes.
@ -555,6 +573,7 @@ byte_size('Hello World') # 11
### capitalize ### capitalize
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Capitalizes the first letter of a string. Capitalizes the first letter of a string.
@ -576,6 +595,7 @@ capitalize('fooBar', True) # 'Foobar'
### capitalize_every_word ### capitalize_every_word
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Capitalizes the first letter of every word in a string. Capitalizes the first letter of every word in a string.
@ -596,6 +616,7 @@ capitalize_every_word('hello world!') # 'Hello World!'
### count_vowels ### count_vowels
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Retuns `number` of vowels in provided `string`. Retuns `number` of vowels in provided `string`.
@ -620,6 +641,7 @@ count_vowels('gym') # 0
### decapitalize ### decapitalize
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Decapitalizes the first letter of a string. Decapitalizes the first letter of a string.
@ -641,6 +663,7 @@ decapitalize('FooBar', True) # 'fOOBAR'
### is_lower_case ### is_lower_case
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Checks if a string is lower case. Checks if a string is lower case.
@ -663,6 +686,7 @@ is_lower_case('Ab4') # False
### is_upper_case ### is_upper_case
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Checks if a string is upper case. Checks if a string is upper case.
@ -685,6 +709,7 @@ is_upper_case('aB4') # False
### palindrome ### palindrome
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin) <span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
Returns `True` if the given string is a palindrome, `False` otherwise. Returns `True` if the given string is a palindrome, `False` otherwise.

View File

@ -1,6 +1,6 @@
def count_by(arr, fn=lambda x: x): def count_by(arr, fn=lambda x: x):
key = {} key = {}
for el in map(fn, arr): for el in map(fn, arr):
key[el] = 0 if not el in key else key[el] key[el] = 0 if el not in key else key[el]
key[el] += 1 key[el] += 1
return key return key

View File

@ -1,8 +1,8 @@
count_vowels count_vowels
byte_size byte_size
capitalize capitalize
capitalize_every_word capitalize_every_word
decapitalize decapitalize
palindrome palindrome
is_upper_case is_upper_case
is_lower_case is_lower_case

View File

@ -1,379 +1,400 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %}<div class="row" style="height:calc(100vh - 5.875rem);overflow:hidden"><input id="doc-drawer-checkbox" class="drawer" value="on" type="checkbox"><nav class="col-md-4 col-lg-3" style="border-top:0"><div class="group"><input class="search" id="searchInput" onkeyup="search(this)" type="text"><label id="search-label">Search for snippet...</label></div><label for="doc-drawer-checkbox" class="button drawer-close"></label><h3 style = "">Math</h3><a class="sublink-1" tags="math" href="#average" style="">average</a><a class="sublink-1" tags="math" href="#factorial" style="">factorial</a><a class="sublink-1" tags="math" href="#gcd" style="">gcd</a><a class="sublink-1" tags="math" href="#lcm" style="">lcm</a><a class="sublink-1" tags="math" href="#max_n" style="">max_n</a><a class="sublink-1" tags="math" href="#min_n" style="">min_n</a><h3 style = "">List</h3><a class="sublink-1" tags="list" href="#chunk" style="">chunk</a><a class="sublink-1" tags="list" href="#compact" style="">compact</a><a class="sublink-1" tags="list" href="#count_occurences" style="">count_occurences</a><a class="sublink-1" tags="list" href="#deep_flatten" style="">deep_flatten</a><a class="sublink-1" tags="list" href="#difference" style="">difference</a><a class="sublink-1" tags="list" href="#shuffle" style="">shuffle</a><a class="sublink-1" tags="list" href="#spread" style="">spread</a><a class="sublink-1" tags="list" href="#zip" style="">zip</a><a class="sublink-1" tags="list" href="#count_by" style="">count_by</a><a class="sublink-1" tags="list" href="#difference_by" style="">difference_by</a><h3 style = "">String</h3><a class="sublink-1" tags="string" href="#count_vowels" style="">count_vowels</a><a class="sublink-1" tags="string" href="#byte_size" style="">byte_size</a><a class="sublink-1" tags="string" href="#capitalize" style="">capitalize</a><a class="sublink-1" tags="string" href="#capitalize_every_word" style="">capitalize_every_word</a><a class="sublink-1" tags="string" href="#decapitalize" style="">decapitalize</a><a class="sublink-1" tags="string" href="#palindrome" style="">palindrome</a><a class="sublink-1" tags="string" href="#is_upper_case" style="">is_upper_case</a><a class="sublink-1" tags="string" href="#is_lower_case" style="">is_lower_case</a></nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"></a><h2 style="text-align:center">Math</h2><div class="card fluid"><h3 id="average" class="section double-padded">average</h3><!--<form action="" method="post"><button type="submit" value="average" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> {% block content %}<div class="row" style="height:calc(100vh - 5.875rem);overflow:hidden"><input id="doc-drawer-checkbox" class="drawer" value="on" type="checkbox"><nav class="col-md-4 col-lg-3" style="border-top:0"><div class="group"><input class="search" id="searchInput" onkeyup="search(this)" type="text"><label id="search-label">Search for snippet...</label></div><label for="doc-drawer-checkbox" class="button drawer-close"></label><h3 style = "">Math</h3><a class="sublink-1" tags="math" href="#average" style="">average</a><a class="sublink-1" tags="math" href="#factorial" style="">factorial</a><a class="sublink-1" tags="math" href="#gcd" style="">gcd</a><a class="sublink-1" tags="math" href="#lcm" style="">lcm</a><a class="sublink-1" tags="math" href="#max_n" style="">max_n</a><a class="sublink-1" tags="math" href="#min_n" style="">min_n</a><h3 style = "">List</h3><a class="sublink-1" tags="list" href="#chunk" style="">chunk</a><a class="sublink-1" tags="list" href="#compact" style="">compact</a><a class="sublink-1" tags="list" href="#count_occurences" style="">count_occurences</a><a class="sublink-1" tags="list" href="#deep_flatten" style="">deep_flatten</a><a class="sublink-1" tags="list" href="#difference" style="">difference</a><a class="sublink-1" tags="list" href="#shuffle" style="">shuffle</a><a class="sublink-1" tags="list" href="#spread" style="">spread</a><a class="sublink-1" tags="list" href="#zip" style="">zip</a><a class="sublink-1" tags="list" href="#count_by" style="">count_by</a><a class="sublink-1" tags="list" href="#difference_by" style="">difference_by</a><a class="sublink-1" tags="list" href="#insertion_sort" style="">insertion_sort</a><h3 style = "">String</h3><a class="sublink-1" tags="string" href="#count_vowels" style="">count_vowels</a><a class="sublink-1" tags="string" href="#byte_size" style="">byte_size</a><a class="sublink-1" tags="string" href="#capitalize" style="">capitalize</a><a class="sublink-1" tags="string" href="#capitalize_every_word" style="">capitalize_every_word</a><a class="sublink-1" tags="string" href="#decapitalize" style="">decapitalize</a><a class="sublink-1" tags="string" href="#palindrome" style="">palindrome</a><a class="sublink-1" tags="string" href="#is_upper_case" style="">is_upper_case</a><a class="sublink-1" tags="string" href="#is_lower_case" style="">is_lower_case</a></nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"></a><h2 style="text-align:center">Math</h2><div class="card fluid"><h3 id="average" class="section double-padded">average</h3><!--<form action="" method="post"><button type="submit" value="average" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<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><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>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 secind argument <code>0.0</code> in sum is to handle floating point division in <code>python2</code>.</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>python2</code>.</p>
<pre class="language-python">def average(*args): <pre class="language-python">def average(*args):
return sum(args, 0.0) / len(args)</pre> return sum(args, 0.0) / len(args)</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">average(*[1, 2, 3]) # 2.0 <pre class="language-python">average(*[1, 2, 3]) # 2.0
average(1, 2, 3) # 2.0</pre> average(1, 2, 3) # 2.0</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="factorial" class="section double-padded">factorial</h3><!--<form action="" method="post"><button type="submit" value="factorial" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="factorial" class="section double-padded">factorial</h3><!--<form action="" method="post"><button type="submit" value="factorial" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Calculates the factorial of a number.</p> <p>Calculates the factorial of a number.</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> <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>
<pre class="language-python">def factorial(num): <pre class="language-python">def factorial(num):
if not ((num &gt;= 0) &amp; (num % 1 == 0)): if not ((num &gt;= 0) &amp; (num % 1 == 0)):
raise Exception( raise Exception(
f"Number( {num} ) can't be floating point or negative ") f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1)</pre> return 1 if num == 0 else num * factorial(num - 1)</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">factorial(6) # 720</pre> <pre class="language-python">factorial(6) # 720</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="gcd" class="section double-padded">gcd</h3><!--<form action="" method="post"><button type="submit" value="gcd" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="gcd" class="section double-padded">gcd</h3><!--<form action="" method="post"><button type="submit" value="gcd" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> <code>math.gcd</code> works with only two numbers</p> <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>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>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>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>
<pre class="language-python">from functools import reduce <pre class="language-python">from functools import reduce
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 gcd(*args): def gcd(*args):
numbers = [] numbers = []
numbers.extend(spread(list(args))) numbers.extend(spread(list(args)))
def _gcd(x, y): def _gcd(x, y):
return x if not y else gcd(y, x % y) return x if not y else gcd(y, x % y)
return reduce((lambda x, y: _gcd(x, y)), numbers)</pre> return reduce((lambda x, y: _gcd(x, y)), numbers)</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">gcd(8,36) # 4</pre> <pre class="language-python">gcd(8,36) # 4</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="lcm" class="section double-padded">lcm</h3><!--<form action="" method="post"><button type="submit" value="lcm" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="lcm" class="section double-padded">lcm</h3><!--<form action="" method="post"><button type="submit" value="lcm" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Returns the least common multiple of two or more numbers.</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>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>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>
<pre class="language-python">from functools import reduce <pre class="language-python">from functools import reduce
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 lcm(*args): def lcm(*args):
numbers = [] numbers = []
numbers.extend(spread(list(args))) numbers.extend(spread(list(args)))
def _gcd(x, y): def _gcd(x, y):
return x if not y else gcd(y, x % y) return x if not y else _gcd(y, x % y)
def _lcm(x, y): def _lcm(x, y):
return x * y / _gcd(x, y) return x * y / _gcd(x, y)
return reduce((lambda x, y: _lcm(x, y)), numbers)</pre> return reduce((lambda x, y: _lcm(x, y)), numbers)</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">lcm(12, 7) # 84 <pre class="language-python">lcm(12, 7) # 84
lcm([1, 3, 4], 5) # 60</pre> lcm([1, 3, 4], 5) # 60</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="max_n" class="section double-padded">max_n</h3><!--<form action="" method="post"><button type="submit" value="max_n" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="max_n" class="section double-padded">max_n</h3><!--<form action="" method="post"><button type="submit" value="max_n" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<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>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 array</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 array</p>
<pre class="language-python">from copy import deepcopy <pre class="language-python">from copy import deepcopy
def max_n(arr, n=1): def max_n(arr, n=1):
numbers = deepcopy(arr) numbers = deepcopy(arr)
numbers.sort() numbers.sort()
numbers.reverse() numbers.reverse()
return numbers[:n]</pre> return numbers[:n]</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">max_n([1, 2, 3]) # [3] <pre class="language-python">max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3,2]</pre> max_n([1, 2, 3], 2) # [3,2]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="min_n" class="section double-padded">min_n</h3><!--<form action="" method="post"><button type="submit" value="min_n" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="min_n" class="section double-padded">min_n</h3><!--<form action="" method="post"><button type="submit" value="min_n" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<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>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 array</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 array</p>
<pre class="language-python">from copy import deepcopy <pre class="language-python">from copy import deepcopy
def min_n(arr, n=1): def min_n(arr, n=1):
numbers = deepcopy(arr) numbers = deepcopy(arr)
numbers.sort() numbers.sort()
return numbers[:n]</pre> return numbers[:n]</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">min_n([1, 2, 3]) # [1] <pre class="language-python">min_n([1, 2, 3]) # [1]
min_n([1, 2, 3], 2) # [1,2]</pre> min_n([1, 2, 3], 2) # [1,2]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><h2 style="text-align:center">List</h2><div class="card fluid"><h3 id="chunk" class="section double-padded">chunk</h3><!--<form action="" method="post"><button type="submit" value="chunk" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><h2 style="text-align:center">List</h2><div class="card fluid"><h3 id="chunk" class="section double-padded">chunk</h3><!--<form action="" method="post"><button type="submit" value="chunk" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Chunks an array into smaller lists of a specified size.</p> <p>Chunks an array into smaller lists of a specified size.</p>
<p>Uses <code>range</code> to create a list of desired size. Then use <code>map</code> on this list and fill it with splices of <code>arr</code>.</p> <p>Uses <code>range</code> to create a list of desired size. Then use <code>map</code> on this list and fill it with splices of <code>arr</code>.</p>
<pre class="language-python">from math import ceil <pre class="language-python">from math import ceil
def chunk(arr, size): def chunk(arr, size):
return list( return list(
map(lambda x: arr[x * size:x * size + size], map(lambda x: arr[x * size:x * size + size],
list(range(0, ceil(len(arr) / size)))))</pre> list(range(0, ceil(len(arr) / size)))))</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]</pre> <pre class="language-python">chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="compact" class="section double-padded">compact</h3><!--<form action="" method="post"><button type="submit" value="compact" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="compact" class="section double-padded">compact</h3><!--<form action="" method="post"><button type="submit" value="compact" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Removes falsey values from a list.</p> <p>Removes falsey values from a list.</p>
<p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p> <p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p>
<pre class="language-python">def compact(arr): <pre class="language-python">def compact(arr):
return list(filter(lambda x: bool(x), arr))</pre> return list(filter(lambda x: bool(x), arr))</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]</pre> <pre class="language-python">compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="count_occurences" class="section double-padded">count_occurences</h3><!--<form action="" method="post"><button type="submit" value="count_occurences" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="count_occurences" class="section double-padded">count_occurences</h3><!--<form action="" method="post"><button type="submit" value="count_occurences" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> Already implemented via <code>list.count()</code>.</p> <p><emoji></emoji> Already implemented via <code>list.count()</code>.</p>
<p>Counts the occurrences of a value in an list.</p> <p>Counts the occurrences of a value in an list.</p>
<p>Uses the <code>reduce</code> functin from built-in module <code>functools</code> to increment a counter each time you encounter the specific value inside the list.</p> <p>Uses the <code>reduce</code> functin from built-in module <code>functools</code> to increment a counter each time you encounter the specific value inside the list.</p>
<pre class="language-python">def count_occurences(arr, val): <pre class="language-python">from functools import reduce
return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr)</pre> def count_occurences(arr, val):
<label class="collapse">Show examples</label> return reduce(
<pre class="language-python">count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3</pre> (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> arr)</pre>
<label class="collapse">Show examples</label>
</div><div class="card fluid"><h3 id="deep_flatten" class="section double-padded">deep_flatten</h3><!--<form action="" method="post"><button type="submit" value="deep_flatten" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <pre class="language-python">count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3</pre>
<p>Deep flattens a list.</p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<p>Use recursion. Use <code>list.extend()</code> with an empty array (<code>result</code>) and the spread function to flatten a list. Recursively flatten each element that is a list.</p>
</div><div class="card fluid"><h3 id="deep_flatten" class="section double-padded">deep_flatten</h3><!--<form action="" method="post"><button type="submit" value="deep_flatten" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<pre class="language-python">def spread(arg): <p>Deep flattens a list.</p>
ret = [] <p>Use recursion. Use <code>list.extend()</code> with an empty array (<code>result</code>) and the spread function to flatten a list. Recursively flatten each element that is a list.</p>
for i in arg:
if isinstance(i, list): <pre class="language-python">def spread(arg):
ret.extend(i) ret = []
else: for i in arg:
ret.append(i) if isinstance(i, list):
return ret ret.extend(i)
else:
ret.append(i)
def deep_flatten(arr): return ret
result = []
result.extend(
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) def deep_flatten(arr):
return result</pre> result = []
<label class="collapse">Show examples</label> result.extend(
<pre class="language-python">deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]</pre> spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr))))
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> return result</pre>
<label class="collapse">Show examples</label>
</div><div class="card fluid"><h3 id="difference" class="section double-padded">difference</h3><!--<form action="" method="post"><button type="submit" value="difference" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <pre class="language-python">deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]</pre>
<p>Returns the difference between two arrays.</p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<p>Create a <code>set</code> from <code>b</code>, then use list comprehension to only keep values not contained in <code>b</code></p>
</div><div class="card fluid"><h3 id="difference" class="section double-padded">difference</h3><!--<form action="" method="post"><button type="submit" value="difference" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<pre class="language-python">def difference(a, b): <p>Returns the difference between two arrays.</p>
b = set(b) <p>Create a <code>set</code> from <code>b</code>, then use list comprehension to only keep values not contained in <code>b</code></p>
return [item for item in a if item not in b]</pre>
<label class="collapse">Show examples</label> <pre class="language-python">def difference(a, b):
<pre class="language-python">difference([1, 2, 3], [1, 2, 4]) # [3]</pre> b = set(b)
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> return [item for item in a if item not in b]</pre>
<label class="collapse">Show examples</label>
</div><div class="card fluid"><h3 id="shuffle" class="section double-padded">shuffle</h3><!--<form action="" method="post"><button type="submit" value="shuffle" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <pre class="language-python">difference([1, 2, 3], [1, 2, 4]) # [3]</pre>
<p><emoji></emoji> The same algorithm is already implemented via <code>random.shuffle</code>.</p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<p>Randomizes the order of the values of an list, returning a new list.</p>
<p>Uses the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates algorithm</a> to reorder the elements of the list.</p> </div><div class="card fluid"><h3 id="shuffle" class="section double-padded">shuffle</h3><!--<form action="" method="post"><button type="submit" value="shuffle" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> The same algorithm is already implemented via <code>random.shuffle</code>.</p>
<pre class="language-python">from copy import deepcopy <p>Randomizes the order of the values of an list, returning a new list.</p>
from random import randint <p>Uses the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates algorithm</a> to reorder the elements of the list.</p>
<pre class="language-python">from copy import deepcopy
def shuffle(arr): from random import randint
temp_arr = deepcopy(arr)
m = len(temp_arr)
while (m): def shuffle(arr):
m -= 1 temp_arr = deepcopy(arr)
i = randint(0, m) m = len(temp_arr)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] while (m):
return temp_arr</pre> m -= 1
<label class="collapse">Show examples</label> i = randint(0, m)
<pre class="language-python">foo = [1,2,3] temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
shuffle(foo) # [2,3,1] , foo = [1,2,3]</pre> return temp_arr</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <label class="collapse">Show examples</label>
<pre class="language-python">foo = [1,2,3]
</div><div class="card fluid"><h3 id="spread" class="section double-padded">spread</h3><!--<form action="" method="post"><button type="submit" value="spread" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> shuffle(foo) # [2,3,1] , foo = [1,2,3]</pre>
<p>Implements javascript's <code>[].concat(...arr)</code>. Flattens the list(non-deep) and returns an list.</p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<pre class="language-python">def spread(arg): </div><div class="card fluid"><h3 id="spread" class="section double-padded">spread</h3><!--<form action="" method="post"><button type="submit" value="spread" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
ret = [] <p>Implements javascript's <code>[].concat(...arr)</code>. Flattens the list(non-deep) and returns an list.</p>
for i in arg:
if isinstance(i, list): <pre class="language-python">def spread(arg):
ret.extend(i) ret = []
else: for i in arg:
ret.append(i) if isinstance(i, list):
return ret</pre> ret.extend(i)
<label class="collapse">Show examples</label> else:
<pre class="language-python">spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]</pre> ret.append(i)
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> return ret</pre>
<label class="collapse">Show examples</label>
</div><div class="card fluid"><h3 id="zip" class="section double-padded">zip</h3><!--<form action="" method="post"><button type="submit" value="zip" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <pre class="language-python">spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]</pre>
<p><emoji></emoji> Already implemented via <code>itertools.zip_longest()</code></p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<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> </div><div class="card fluid"><h3 id="zip" class="section double-padded">zip</h3><!--<form action="" method="post"><button type="submit" value="zip" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> Already implemented via <code>itertools.zip_longest()</code></p>
<pre class="language-python">def zip(*args, fillvalue=None): <p>Creates a list of elements, grouped based on the position in the original lists.</p>
max_length = max([len(arr) for arr in args]) <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>
result = []
for i in range(max_length): <pre class="language-python">def zip(*args, fillvalue=None):
result.append([ max_length = max([len(arr) for arr in args])
args[k][i] if i &lt; len(args[k]) else None for k in range(len(args)) result = []
]) for i in range(max_length):
return result</pre> result.append([
<label class="collapse">Show examples</label> args[k][i] if i &lt; len(args[k]) else None for k in range(len(args))
<pre class="language-python">zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]] ])
zip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]] return result</pre>
zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]</pre> <label class="collapse">Show examples</label>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <pre class="language-python">zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]
zip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]]
</div><div class="card fluid"><h3 id="count_by" class="section double-padded">count_by</h3><!--<form action="" method="post"><button type="submit" value="count_by" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]</pre>
<p><emoji></emoji> Already implemented via <code>collections.Counter</code></p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<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> </div><div class="card fluid"><h3 id="count_by" class="section double-padded">count_by</h3><!--<form action="" method="post"><button type="submit" value="count_by" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p><emoji></emoji> Already implemented via <code>collections.Counter</code></p>
<pre class="language-python">def count_by(arr, fn=lambda x: x): <p>Groups the elements of a list based on the given function and returns the count of elements in each group.</p>
key = {} <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>
for el in map(fn, arr):
key[el] = 0 if not el in key else key[el] <pre class="language-python">def count_by(arr, fn=lambda x: x):
key[el] += 1 key = {}
return key</pre> for el in map(fn, arr):
<label class="collapse">Show examples</label> key[el] = 0 if el not in key else key[el]
<pre class="language-python">from math import floor key[el] += 1
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2} return key</pre>
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}</pre> <label class="collapse">Show examples</label>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <pre class="language-python">from math import floor
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}
</div><div class="card fluid"><h3 id="difference_by" class="section double-padded">difference_by</h3><!--<form action="" method="post"><button type="submit" value="difference_by" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}</pre>
<p>Returns the difference between two list, after applying the provided function to each list element of both.</p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<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>
</div><div class="card fluid"><h3 id="difference_by" class="section double-padded">difference_by</h3><!--<form action="" method="post"><button type="submit" value="difference_by" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<pre class="language-python">def difference_by(a, b, fn): <p>Returns the difference between two list, after applying the provided function to each list element of both.</p>
b = set(map(fn, b)) <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>
return [item for item in a if fn(item) not in b]</pre>
<label class="collapse">Show examples</label> <pre class="language-python">def difference_by(a, b, fn):
<pre class="language-python">from math import floor b = set(map(fn, b))
difference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2] return [item for item in a if fn(item) not in b]</pre>
difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]</pre> <label class="collapse">Show examples</label>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <pre class="language-python">from math import floor
difference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2]
</div><h2 style="text-align:center">String</h2><div class="card fluid"><h3 id="count_vowels" class="section double-padded">count_vowels</h3><!--<form action="" method="post"><button type="submit" value="count_vowels" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]</pre>
<p>Retuns <code>number</code> of vowels in provided <code>string</code>.</p> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<p>Use a regular expression to count the number of vowels <code>(A, E, I, O, U)</code> in a string.</p>
</div><div class="card fluid"><h3 id="insertion_sort" class="section double-padded">insertion_sort</h3><!--<form action="" method="post"><button type="submit" value="insertion_sort" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<pre class="language-python">import re <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>
<pre class="language-python">def insertion_sort(arr):
def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))</pre> for i in range(1, len(arr)):
<label class="collapse">Show examples</label> key = arr[i]
<pre class="language-python">count_vowels('foobar') # 3 j = i - 1
count_vowels('gym') # 0</pre> while j &gt;= 0 and key &lt; arr[j]:
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> arr[j + 1] = arr[j]
j -= 1
</div><div class="card fluid"><h3 id="byte_size" class="section double-padded">byte_size</h3><!--<form action="" method="post"><button type="submit" value="byte_size" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> arr[j + 1] = key</pre>
<p>Returns the length of a string in bytes.</p> <label class="collapse">Show examples</label>
<p><code>utf-8</code> encodes a given string and find its length.</p> <pre class="language-python">arr = [7,4,9,2,6,3]
insertionsort(arr)
<pre class="language-python">def byte_size(string): print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]</pre>
return(len(string.encode('utf-8')))</pre> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<label class="collapse">Show examples</label>
<pre class="language-python">byte_size('😀') # 4 </div><h2 style="text-align:center">String</h2><div class="card fluid"><h3 id="count_vowels" class="section double-padded">count_vowels</h3><!--<form action="" method="post"><button type="submit" value="count_vowels" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
byte_size('Hello World') # 11</pre> <p>Retuns <code>number</code> of vowels in provided <code>string</code>.</p>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <p>Use a regular expression to count the number of vowels <code>(A, E, I, O, U)</code> in a string.</p>
</div><div class="card fluid"><h3 id="capitalize" class="section double-padded">capitalize</h3><!--<form action="" method="post"><button type="submit" value="capitalize" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <pre class="language-python">import re
<p>Capitalizes the first letter of a string.</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>
def count_vowels(str):
<pre class="language-python">def capitalize(string, lower_rest=False): return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))</pre>
return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])</pre> <label class="collapse">Show examples</label>
<label class="collapse">Show examples</label> <pre class="language-python">count_vowels('foobar') # 3
<pre class="language-python">capitalize('fooBar') # 'FooBar' count_vowels('gym') # 0</pre>
capitalize('fooBar', True) # 'Foobar'</pre> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="byte_size" class="section double-padded">byte_size</h3><!--<form action="" method="post"><button type="submit" value="byte_size" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
</div><div class="card fluid"><h3 id="capitalize_every_word" class="section double-padded">capitalize_every_word</h3><!--<form action="" method="post"><button type="submit" value="capitalize_every_word" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <p>Returns the length of a string in bytes.</p>
<p>Capitalizes the first letter of every word in a string.</p> <p><code>utf-8</code> encodes a given string and find its length.</p>
<p>Uses <code>str.title</code> to capitalize first letter of evry word in the string.</p>
<pre class="language-python">def byte_size(string):
<pre class="language-python">def capitalize_every_word(string): return(len(string.encode('utf-8')))</pre>
return string.title()</pre> <label class="collapse">Show examples</label>
<label class="collapse">Show examples</label> <pre class="language-python">byte_size('😀') # 4
<pre class="language-python">capitalize_every_word('hello world!') # 'Hello World!'</pre> byte_size('Hello World') # 11</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="decapitalize" class="section double-padded">decapitalize</h3><!--<form action="" method="post"><button type="submit" value="decapitalize" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="capitalize" class="section double-padded">capitalize</h3><!--<form action="" method="post"><button type="submit" value="capitalize" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Decapitalizes the first letter of a string.</p> <p>Capitalizes 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>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>
<pre class="language-python">def decapitalize(string, upper_rest=False): <pre class="language-python">def capitalize(string, lower_rest=False):
return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])</pre> return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">decapitalize('FooBar') # 'fooBar' <pre class="language-python">capitalize('fooBar') # 'FooBar'
decapitalize('FooBar', True) # 'fOOBAR'</pre> capitalize('fooBar', True) # 'Foobar'</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="palindrome" class="section double-padded">palindrome</h3><!--<form action="" method="post"><button type="submit" value="palindrome" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> </div><div class="card fluid"><h3 id="capitalize_every_word" class="section double-padded">capitalize_every_word</h3><!--<form action="" method="post"><button type="submit" value="capitalize_every_word" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Returns <code>True</code> if the given string is a palindrome, <code>False</code> otherwise.</p> <p>Capitalizes the first letter of every word in a string.</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>Uses <code>str.title</code> to capitalize first letter of evry word in the string.</p>
<pre class="language-python">def palindrome(string): <pre class="language-python">def capitalize_every_word(string):
from re import sub return string.title()</pre>
s = sub('[\W_]', '', string.lower()) <label class="collapse">Show examples</label>
return s == s[::-1]</pre> <pre class="language-python">capitalize_every_word('hello world!') # 'Hello World!'</pre>
<label class="collapse">Show examples</label> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
<pre class="language-python">palindrome('taco cat') # True</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> </div><div class="card fluid"><h3 id="decapitalize" class="section double-padded">decapitalize</h3><!--<form action="" method="post"><button type="submit" value="decapitalize" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Decapitalizes the first letter of a string.</p>
</div><div class="card fluid"><h3 id="is_upper_case" class="section double-padded">is_upper_case</h3><!--<form action="" method="post"><button type="submit" value="is_upper_case" name="submit">Vote</button></form><p></p>--><div class="section double-padded"> <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>Checks if a string is upper case.</p>
<p>Convert the given string to upper case, using <code>str.upper()</code> method and compare it to the original.</p> <pre class="language-python">def decapitalize(string, upper_rest=False):
return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])</pre>
<pre class="language-python">def is_upper_case(str): <label class="collapse">Show examples</label>
return str == str.upper()</pre> <pre class="language-python">decapitalize('FooBar') # 'fooBar'
<label class="collapse">Show examples</label> decapitalize('FooBar', True) # 'fOOBAR'</pre>
<pre class="language-python">is_upper_case('ABC') # True <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
is_upper_case('a3@$') # True
is_upper_case('aB4') # False</pre> </div><div class="card fluid"><h3 id="palindrome" class="section double-padded">palindrome</h3><!--<form action="" method="post"><button type="submit" value="palindrome" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <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>
</div><div class="card fluid"><h3 id="is_lower_case" class="section double-padded">is_lower_case</h3><!--<form action="" method="post"><button type="submit" value="is_lower_case" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Checks if a string is lower case.</p> <pre class="language-python">def palindrome(string):
<p>Convert the given string to lower case, using <code>str.lower()</code> method and compare it to the original.</p> from re import sub
s = sub('[\W_]', '', string.lower())
<pre class="language-python">def is_lower_case(str): return s == s[::-1]</pre>
return str == str.lower()</pre> <label class="collapse">Show examples</label>
<label class="collapse">Show examples</label> <pre class="language-python">palindrome('taco cat') # True</pre>
<pre class="language-python">is_lower_case('abc') # True <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
is_lower_case('a3@$') # True
is_lower_case('Ab4') # False</pre> </div><div class="card fluid"><h3 id="is_upper_case" class="section double-padded">is_upper_case</h3><!--<form action="" method="post"><button type="submit" value="is_upper_case" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <p>Checks if a string is upper case.</p>
<p>Convert the given string to upper case, using <code>str.upper()</code> method and compare it to the original.</p>
</div><button class="scroll-to-top"></button>
<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> <pre class="language-python">def is_upper_case(str):
return str == str.upper()</pre>
<label class="collapse">Show examples</label>
<pre class="language-python">is_upper_case('ABC') # True
is_upper_case('a3@$') # True
is_upper_case('aB4') # False</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="is_lower_case" class="section double-padded">is_lower_case</h3><!--<form action="" method="post"><button type="submit" value="is_lower_case" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Checks if a string is lower case.</p>
<p>Convert the given string to lower case, using <code>str.lower()</code> method and compare it to the original.</p>
<pre class="language-python">def is_lower_case(str):
return str == str.lower()</pre>
<label class="collapse">Show examples</label>
<pre class="language-python">is_lower_case('abc') # True
is_lower_case('a3@$') # True
is_lower_case('Ab4') # False</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><button class="scroll-to-top"></button>
<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>
</main></div>{% endblock %} </main></div>{% endblock %}