From a09142e1eb9430193bc1c0ee77ef126bc400d5bb Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 9 Jan 2018 10:09:42 +0530 Subject: [PATCH] add chunk --- CONTRIBUTING.md | 8 ++++---- README.md | 25 +++++++++++++++++++++++-- snippet_template.md | 15 +++++++++++++++ snippets/chunk.md | 20 ++++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 snippet_template.md create mode 100644 snippets/chunk.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 474670f0c..d57bda60f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,9 +4,9 @@ Here's what you can do to help: -- [Open issues](https://github.com/kriadmin/30-seconds-of-code-python/issues/new) for things you want to see added or modified. -- Be part of the discussion by helping out with [existing issues](https://github.com/kriadmin/30-seconds-of-code-python/issues) or talking on our [gitter channel](https://gitter.im/30-seconds-of-code-python/Lobby). -- Submit [pull requests](https://github.com/kriadmin/30-seconds-of-code-python/pulls) with snippets you have created (see below for guidelines). +- [Open issues](https://github.com/kriadmin/30-seconds-of-python-code/issues/new) for things you want to see added or modified. +- Be part of the discussion by helping out with [existing issues](https://github.com/kriadmin/30-seconds-of-python-code/issues) or talking on our [gitter channel](https://gitter.im/30-seconds-of-python-code/Lobby). +- Submit [pull requests](https://github.com/kriadmin/30-seconds-of-python-code/pulls) with snippets you have created (see below for guidelines). - Fix typos in existing snippets, improve snippet descriptions and explanations or provide better examples. ### Snippet submission and Pull request guidelines @@ -24,7 +24,7 @@ Here's what you can do to help: - **Snippets _CAN NOT_ use any external modules**. Only the modules and function inbuilt in `python 3.6` shall be used. - **Snippet code** must be enclosed inside ` ```python ` and ` ``` `. - Remember to start your snippet's code on a new line below the opening backticks. - - Please follow [This Style Guide](https://www.python.org/dev/peps/pep-0008/) for your code. + - Please follow [This Style Guide](https://www.python.org/dev/peps/pep-0008/) for your code.(Though it doen't matter much as your code is automatically prettified) - Try to keep your snippets' code short and to the point. Use modern techniques and features. Make sure to test your code before submitting. - All snippets must be followed by one (more if necessary) test case after the code, in a new block enclosed inside ` ```python ` and ` ``` `. The syntax for this is `myFunction('testInput') # 'testOutput'`. Use multiline examples only if necessary. - Try to make your function name unique, so that it does not conflict with existing snippets. diff --git a/README.md b/README.md index 66bd6a028..58a537647 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,31 @@ ![Logo](/icon.png) -# 30-seconds-of-code-python +# 30-seconds-of-python-code 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/) +**Note**:- This is in no way affiliated with the original [30-seconds-of-code](https://github.com/Chalarangelo/30-seconds-of-code/). + +### chunk + +Chunks an array into smaller arrays of a specified size. + +Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `arr` + +```python +from math import ceil + + +def chunk(arr, size): + return list( + map(lambda x: arr[x * size:x * size + size], + list(range(0, ceil(len(arr) / size))))) + +``` + +``` python + chunk([1,2,3,4,5],2) # [[1,2],[3,4],5] +``` ### gcd Calculates the greatest common divisor between two or more numbers/arrays. diff --git a/snippet_template.md b/snippet_template.md new file mode 100644 index 000000000..2cc4e2a68 --- /dev/null +++ b/snippet_template.md @@ -0,0 +1,15 @@ +### functionName + +Describe briefly what the function does + +Explain your method and the functions used. + +``` python +def functionName(args): + # code + return 0 +``` + +``` python +functionName(val) # result +``` \ No newline at end of file diff --git a/snippets/chunk.md b/snippets/chunk.md new file mode 100644 index 000000000..de3624fa5 --- /dev/null +++ b/snippets/chunk.md @@ -0,0 +1,20 @@ +### chunk + +Chunks an array into smaller arrays of a specified size. + +Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `arr` + +```python +from math import ceil + + +def chunk(arr, size): + return list( + map(lambda x: arr[x * size:x * size + size], + list(range(0, ceil(len(arr) / size))))) + +``` + +``` python +chunk([1,2,3,4,5],2) # [[1,2],[3,4],5] +``` \ No newline at end of file