{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programming with Python\n", "#### Stefan Güttel, [guettel.com](http://guettel.com)\n", "\n", "## Test 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** Don't forget to write the programs that allow the examiners to test the functions that you write." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 1.** Write a function `is_sorted_sum(L)` that returns `True` if the list `L` of integers is sorted ascendingly by the sum of its elements' digits.\n", "\n", "For example, the list \$13, 11111, -17, 9, 1719\$ is sorted as requested because the respective sums of digits are as follows:\n", "\n", "Numbers | 13 | 11111 | -17 | 9 | 1719\n", "-------------------|---:|------:|----:|--:|----:\n", "Sums of the digits | 4 | 5 | 8 | 9 | 18\n", "\n", "Your function should **not** use the in-build `sort` or `sorted` commands: checking whether or not a list is sorted does not require any sorting." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 2.** Write a function `even_slice(L, a, b)` that returns a new list containing all the even numbers among the elments of `L` between indices `a` and `b` (**including both of them**) in their original order. You may assume that `0 <= a <= b < len(L)`.\n", "\n", "For example,\n", "```python\n", "even_slice([11, 12, 2, 17, 20, 22, 2, 19, 7, 42, 18, 20], 2, 10)\n", "```\n", "should return the list of all the even numbers in the list `[2, 17, 20, 22, 2, 19, 7, 42, 18]`. In other words, it should return\n", "```python\n", "[2, 20, 22, 2, 42, 18]\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }