Basic Python

Basic Python

  1. Simple arithmetics (5 min). Calculate:

    1. $2^5$
    2. 31/2
    3. 015 + 32
    4. 11 mod 2

    Are they results like you expected?

  2. Lists (30 min). Define following lists:

    1. Odd numbers from 1 to 11. Hint: Use range method.
    2. A list of arbitrary number of zeros
    3. Sequence of letters from "a" to "k". Hint: use chr method.
    4. Elements of power series ($A_i = \sum_{k=1}^{i} x^k$, x = 1.2) smaller than a 10.
    5. 10x10 identity matrix.
  3. Introspection (20 min). In Python interpreter define following variables:

    1. empty string
    2. empty list
    3. empty dictionary

    Using built-in dir and help method find out which methods these data structures offer. Print a table with non-special methods (without underscores) and first line of their docstring. Give a working example documenting at least one of the listed methods.

  4. Enigma (20 min). Recover the message hidden in the string using the following translation: a ➔ c, b ➔ d, ..., z ➔ b.

    g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr
    amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q
    ufw rfgq rcvr gq qm jmle. sqgle qrp.rpylqjyrc() gq pcamkkclbcb!
    
  5. Binary conversion (20 min). Write a program converting integers to their binary representation. You can store the result either in a list or in a string.

  6. Cashier (45 min). Implement a simple cashier machine with the following features :

    1. "codebar" associated with each product,
    2. reading of a price list from a file Hint: you will need to wait until the second part of the lecture or skip to the relevant section of the handouts,
    3. an interactive entry of a codebar or a product name. Hint: use raw_input
    4. warning if the product doesn't exist
    5. printing of subtotals after entering each product
    6. printing of the final bill
    7. your ideas
  7. Import. Write a program that counts lines and characters in a file. With your editor, code a Python module called mymode.py which provides three functions:

    • countlines(name) - reads an input file and count the number of lines in it Hint: file.readlines() does most of the work for you.
    • countChars(name) - reads an input file and counts the number of characters in it Hint: file.read() returns a single string.
    • test(name) - calls both counting functions with a given input filename.

    Next, test your module interactively, using import. What if your module is in a different directory than your current working directory? Add a "main" function to your module.

  8. Geometry. Create a Point class that consists of an ordered pair (x,y) representing a point's location on the X and Y axes. X and Y coordinates are passed to the constructor on instatiation and default to origin for any missing coordinate.

  9. Ecosystem (1h). Program an ecosystem in which each organism belongs to a hierarchical tree (for example, dog is a mamal, mamal is an animal, an animal is a living organism). Implement following functions and attributes:

    1. name of the species and its systematic classification,
    2. position in the environment,
    3. their specific means of locomotion,
    4. reproduction and death.

    In each iteration each organism should update its state (move, reproduce or die). You can also provide a simple ASCII output showing the position of the organisms.

    As a starting point you can take the program ecosystem.py

 
exercises.txt · Last modified: 2009/08/31 11:43 by bartosz
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki