{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Mid Sem : MTH 103 Introduction to Computing" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "(For those who could not attend the midsem due to medical reasons)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note : Please read the following before continuing\n", "\n", "1. Answer all the questions\n", "1. Each question carries 20 points\n", "1. The solution to each question is some python code. Write the code for each question in a separate file. *The name of the file should be of the form **qn1.py** for question 1 and so on.*\n", "1. You have total of **two** hours to answer all the questions.\n", "\n", "***Best of luck***" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Question 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do the following :\n", "1. Create a list, `l1`, containing the squares of the numbers 1, 2, ..., 10, i.e. containing the numbers 1, 4, 9, ..., 100.\n", "1. Create another list `l2` which contain the *successive differences* of the numbers in `l1`. That is it should contain 4-1, 9-4, 16-9 etc.\n", "1. Create a third list, `l3`, which contain the successive differnces of the numbers in `l2`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#### Solution to question 1\n", "\n", "def succ_diff(l) :\n", " nl = len(l)\n", " if nl == 0 or nl == 1 :\n", " print \"There are too few elements.\"\n", " rval = []\n", " else :\n", " rval = []\n", " for i in range(1, nl) :\n", " rval.append(l[i] - l[i-1])\n", " return rval\n", "\n", "l1 = [k*k for k in range(1, 11)]\n", "l2 = succ_diff(l1)\n", "l3 = succ_diff(l2)\n", "\n", "print l1\n", "print l2\n", "print l3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n", "[3, 5, 7, 9, 11, 13, 15, 17, 19]\n", "[2, 2, 2, 2, 2, 2, 2, 2]\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 2\n", "\n", "Write a function `l(x)` which returns\n", "\\begin{equation}\n", "x - \\frac{x^2}{2} + \\frac{x^3}{3} - \\frac{x^4}{4} + \\dotsb\n", "\\end{equation}\n", "upto 100 terms.\n", "\n", "Using this function compute `l(0.6487212)`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#### Solution for question 2\n", "def l(x) :\n", " xp = 1.0\n", " s = -1\n", " lx = 0.0\n", " for i in range(1, 101) :\n", " xp *= x\n", " s *= -1\n", " t = s * xp / float(i)\n", " lx += t\n", " return lx\n", "\n", "print l(0.6487212)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.499999957118\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 3\n", "\n", "Two primes $p < q$ are said to be *twin primes* if $q - p = 2$. For example, $3, 5$, $41, 43$ are examples of twin primes. Write a function `tp(n)` which returns the first pair of twin primes occuring after `n`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "def is_prime(n) :\n", " isprime = True\n", " check_till = int(math.sqrt(n))\n", " for i in range(2, check_till + 1) :\n", " if n % i == 0 :\n", " isprime = False\n", " return isprime\n", "\n", "print \"\"\"\n", "Testing is_prime\n", "---------------\n", "\"\"\"\n", "def test_is_prime(n) :\n", " print \"is_prime(%d) = %s,\\t\" % (n, is_prime(n)),\n", " \n", "test_is_prime(31)\n", "test_is_prime(100)\n", "test_is_prime(9999991)\n", "print\n", "\n", "def tp(n) :\n", " tryp = n\n", " foundtp = False\n", " while not foundtp :\n", " tryp += 1\n", " if is_prime(tryp) :\n", " tryq = tryp + 2\n", " if is_prime(tryq) :\n", " foundtp = True\n", " return (tryp, tryq)\n", "\n", "print \"\"\"\n", "______________________________\n", "Testing tp\n", "______________________________\n", "\"\"\"\n", "\n", "print \"tp(20) =\", tp(20)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Testing is_prime\n", "---------------\n", "\n", "is_prime(31) = True,\tis_prime(100) = False,\tis_prime(9999991) = True,\t\n", "\n", "______________________________\n", "Testing tp\n", "______________________________\n", "\n", "tp(20) = (29, 31)\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 4\n", "\n", "Write a function `filter(l, f)` which takes a list `l` and a function `f` as argument. `f` is a function such that for elements of the list `l`, it returns either `True` or `False`. `filter` should return a new list which contains the elements `e` of `l` which satisfy `f(e)` is `True`.\n", "\n", "###### Example :\n", "`filter([2,5,1,55,1,10,33,4,2,4], lambda t : t >=10)` should return `[55, 10, 33]`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#### Solution to question 4\n", "\n", "def filter(l, f) :\n", " rlist = []\n", " for e in l :\n", " if f(e) :\n", " rlist.append(e)\n", " return rlist\n", "\n", "print \"Testing filter.\\n\\n\"\n", "l = [2, 5, 1, 55, 1, 10, 33, 4, 2, 4]\n", "\n", "print \"filter(%s, lambda t : t >= 10) returns %s.\" % (l, filter(l, lambda t : t >= 10))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Testing filter.\n", "\n", "\n", "filter([2, 5, 1, 55, 1, 10, 33, 4, 2, 4], lambda t : t >= 10) returns [55, 10, 33].\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }