Skip to content

exercises

Miguel Sozinho Ramalho edited this page May 2, 2018 · 7 revisions

Exercises

1. Initiation 🌱

Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included).

2. Find The D (for dictionary) :trollface:

Write a function that returns a dictionary (i, i*i) between 1 and n.

3. The Matrices Begin 🔢

Write a function which takes 2 digits, X,Y as input and generates a 2-dimensional array.

The element value in the i-th row and j-th column of the array should be i*j. Example for 3:

[0 0 0]
[0 1 2]
[0 2 4]

4. Follow the robots 🔍

A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:

UP 5
DOWN 3
LEFT 3
RIGHT 2

The numbers after the direction are steps. Write a program to read directions from a text file and compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.

Example: If the following tuples are given as input to the program:

UP 5
DOWN 3
LEFT 3
RIGHT 2

Then, the output of the program should be: 3

5. Literary Monster 📚

Write a program that, given a text file, prints the total number of words and the number of occurrences and percentage of each word in the file. (Try it out with lusidas.txt) The file with the contents:

The quick brown fox jumped over the lazy dog?!. The
quick jumped brown sheep lol xd

Should ouput:

16
The: 2 occurences - 0.125%
quick: 2 occurences - 0.125%
brown: 2 occurences - 0.125%
fox: 1 occurences - 0.0625%
jumped: 2 occurences - 0.125%
over: 1 occurences - 0.0625%
the: 1 occurences - 0.0625%
lazy: 1 occurences - 0.0625%
dog: 1 occurences - 0.0625%
sheep: 1 occurences - 0.0625%
lol: 1 occurences - 0.0625%
xd: 1 occurences - 0.0625%

6. Do it with Class 🎩

Write a class named Shape. Write the classes Circle and Rectangle, which extend Shape. Write the class Square, which extends Rectangle.

All these classes have a method that returns their area. For this to be possible, the classes' constructors must accept the necessary info (radius for Circle, side for square, ...)

7. Open exercise ⚠️

This exercise should be used to fill time if you finish early (and for some reason don't want to leave yet (not that I want you to leave, please stay as long as you like :P)). Write a text-based RPG game. This game can be as complex as you want it to. For inspiration please refer to rpg.py (you can actually play the game as well).

For a more complex example, refer to The Hitchhiker's Guide to the Galaxy Game

Solutions

Only when you feel helpless and like python is about to bring you to madness.

These are our solutions, there are many ways of achieving success in the exercises. That said:

  1. Solution to Initiation
  2. Solution to The D
  3. Solution to Matrices Begin
  4. Solution to Follow the robots
  5. Solution to Literary Monster
  6. Solution to Do it with Class
  7. Example Solution to Do it with Class not done by us