Travis build: 955

This commit is contained in:
Rohit Tanwar
2018-04-18 12:20:52 +00:00
parent 602c95ce9c
commit 1f4f026086
4 changed files with 195 additions and 16 deletions

130
README.md
View File

@ -1,14 +1,20 @@
![Logo](/icon.png)
# 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")
# 30-seconds-of-python-code [![Tweet](https://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)
[![first-timers-only](http://img.shields.io/badge/first--timers--only-friendly-blue.svg?style=flat-square)](http://www.firsttimersonly.com/) [![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)
[![first-timers-only](https://img.shields.io/badge/first--timers--only-friendly-blue.svg?style=flat-square)](http://www.firsttimersonly.com/) [![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)
>Python implementation of 30-seconds-of-code.
## Welcome to 30-seconds-of-🐍-code!
>A Python implementation of 30-seconds-of-code.
**Note**:- This is in no way affiliated with the original [30-seconds-of-code](https://github.com/Chalarangelo/30-seconds-of-code/).
If you've come here from javascript land then you should be aware that this project uses `python 3`, therefore not all snippets will work as expected in every python interpreter or on system. You'll need to check your python version with the command `python -v`. If you need help installing the latest stable release of python 3 on your system checkout docs.python.org if you run into trouble make sure you research stackoverflow. Eventually it might be worth looking into how to set up a virtual environment for python projects with virtualenv or even a tool like anaconda.
This project contains plenty of useful snippets which can help beginners and newcomers quickly ramp-up on grasping python 3's syntax.
### Table of contents
### :books: List
@ -20,6 +26,7 @@
<li><a href = "#deep_flatten"><code>deep_flatten</code></a></li>
<li><a href = "#difference"><code>difference</code></a></li>
<li><a href = "#difference_by"><code>difference_by</code></a></li>
<li><a href = "#has_duplicates"><code>has_duplicates</code></a></li>
<li><a href = "#insertion_sort"><code>insertion_sort</code></a></li>
<li><a href = "#shuffle"><code>shuffle</code></a></li>
<li><a href = "#spread"><code>spread</code></a></li>
@ -36,6 +43,13 @@
<li><a href = "#min_n"><code>min_n</code></a></li>
</ul></details>
### card_file_box Object
<details><summary>View contents</summary> <ul><li><a href = "#all_unique"><code>all_unique</code></a></li>
<li><a href = "#keys_only"><code>keys_only</code></a></li>
<li><a href = "#values_only"><code>values_only</code></a></li>
</ul></details>
### :scroll: String
<details><summary>View contents</summary> <ul><li><a href = "#byte_size"><code>byte_size</code></a></li>
@ -256,6 +270,30 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
### has_duplicates
<span style="color:grey">Author:-</span> [Rob-Rychs](@Rob-Rychs)
<span style="color:grey">Contributors:-</span> [Rob-Rychs](@Rob-Rychs)
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.
```py
def has_duplicates(lst):
return len(lst) != len(set(lst))
```
<details><summary>View Examples</summary>
```py
x = [1,2,3,4,5,5]
y = [1,2,3,4,5]
has_duplicates(x) # True
has_duplicates(y) # False
```
</details>
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
### insertion_sort
<span style="color:grey">Author:-</span> [Meet Zaveri](https://www.github.com/meetzaveri)
@ -385,7 +423,7 @@ zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2,
Returns the average of two or more numbers.
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`.
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 `python3`.
```py
def average(*args):
return sum(args, 0.0) / len(args)
@ -563,6 +601,90 @@ min_n([1, 2, 3], 2) # [1,2]
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
## card_file_box Object
### all_unique
<span style="color:grey">Author:-</span> [Rob-Rychs](@Rob-Rychs)
<span style="color:grey">Contributors:-</span> [Rob-Rychs](@Rob-Rychs)
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.
```py
def all_unique(lst):
return len(lst) == len(set(lst))
```
<details><summary>View Examples</summary>
```py
x = [1,2,3,4,5,6]
y = [1,2,2,3,4,5]
all_unique(x) # True
all_unique(y) # False
```
</details>
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
### keys_only
<span style="color:grey">Author:-</span> [Rob-Rychs](@Rob-Rychs)
<span style="color:grey">Contributors:-</span> [Rob-Rychs](@Rob-Rychs)
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
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).
```py
def keys_only(flat_dict):
lst = []
for k, v in flat_dict.items():
lst.append(k)
return lst
```
<details><summary>View Examples</summary>
```py
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
keys_only(ages) # ['Peter', 'Isabel', 'Anna']
```
</details>
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
### values_only
<span style="color:grey">Author:-</span> [Rob-Rychs](@Rob-Rychs)
<span style="color:grey">Contributors:-</span> [Rob-Rychs](@Rob-Rychs)
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.
```py
def values_only(dict):
lst = []
for k, v in dict.items():
lst.append(v)
return lst
```
<details><summary>View Examples</summary>
```py
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
values_only(ages) # [10, 11, 9]
```
</details>
<br><a href = "#table-of-contents">:arrow_up: Back to top</a>
## :scroll: String
### byte_size

View File

@ -1,5 +1,5 @@
def keys_only(dict):
def keys_only(flat_dict):
lst = []
for k, v in dict.items():
for k, v in flat_dict.items():
lst.append(k)
return lst

View File

@ -1,8 +1,3 @@
count_vowels
byte_size
capitalize
capitalize_every_word
decapitalize
palindrome
is_upper_case
is_lower_case
keys_only
values_only
all_unique

View File

@ -1,9 +1,9 @@
{% 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><a class="sublink-1" tags="list" href="#insertion_sort" style="">insertion_sort</a><a class="sublink-1" tags="list" href="#bubble_sort" style="">bubble_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">
{% 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><a class="sublink-1" tags="list" href="#bubble_sort" style="">bubble_sort</a><a class="sublink-1" tags="list" href="#has_duplicates" style="">has_duplicates</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><h3 style = "">Object</h3><a class="sublink-1" tags="object" href="#keys_only" style="">keys_only</a><a class="sublink-1" tags="object" href="#values_only" style="">values_only</a><a class="sublink-1" tags="object" href="#all_unique" style="">all_unique</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>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>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>python3</code>.</p>
<pre class="language-python">def average(*args):
return sum(args, 0.0) / len(args)</pre>
@ -306,6 +306,19 @@ bubble_sort(lst)
print("sorted %s" %lst) # [17,20,26,31,44,54,55,77,91]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="has_duplicates" class="section double-padded">has_duplicates</h3><!--<form action="" method="post"><button type="submit" value="has_duplicates" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<p>Checks a flat list for duplicate values. Returns True if duplicate values exist and False if values are 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>
<pre class="language-python">def has_duplicates(lst):
return len(lst) != len(set(lst))</pre>
<label class="collapse">Show examples</label>
<pre class="language-python">x = [1,2,3,4,5,5]
y = [1,2,3,4,5]
has_duplicates(x) # True
has_duplicates(y) # False</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</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">
<p>Retuns <code>number</code> of vowels in provided <code>string</code>.</p>
<p>Use a regular expression to count the number of vowels <code>(A, E, I, O, U)</code> in a string.</p>
@ -399,6 +412,55 @@ is_lower_case('a3@$') # True
is_lower_case('Ab4') # False</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><h2 style="text-align:center">Object</h2><div class="card fluid"><h3 id="keys_only" class="section double-padded">keys_only</h3><!--<form action="" method="post"><button type="submit" value="keys_only" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<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>
<pre class="language-python">def keys_only(flat_dict):
lst = []
for k, v in flat_dict.items():
lst.append(k)
return lst</pre>
<label class="collapse">Show examples</label>
<pre class="language-python">ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
keys_only(ages) # ['Peter', 'Isabel', 'Anna']</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="values_only" class="section double-padded">values_only</h3><!--<form action="" method="post"><button type="submit" value="values_only" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<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>
<pre class="language-python">def values_only(dict):
lst = []
for k, v in dict.items():
lst.append(v)
return lst</pre>
<label class="collapse">Show examples</label>
<pre class="language-python">ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
values_only(ages) # [10, 11, 9]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><div class="card fluid"><h3 id="all_unique" class="section double-padded">all_unique</h3><!--<form action="" method="post"><button type="submit" value="all_unique" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
<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>
<pre class="language-python">def all_unique(lst):
return len(lst) == len(set(lst))</pre>
<label class="collapse">Show examples</label>
<pre class="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>
<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 %}