# Longer Exercises using Python

Posted on 22-05-19 in Exercises

## Q : School trip to the theme park!¶

Our program needs to estimate the cost per ticket sold to students. Each ticket will cost £25 however the theme park gives 1 free ticket for every 10 sold. Each coach can hold 50 people maximum and costs £300 per coach.

1. *N* Students have promised they wish to come.
Use this to calculate the cost per ticket based on *N* students going.
If the cost per ticket is greater than £50 cancel the trip.
2. Keep track of each student who has paid/not-paid.
Tell the teacher who has not yet paid and how much in profit or in debt the school is for the trip.



Ensure user inputs are validated at each step.

In [1]:
N = 50
cost_per_ticket = 50
# ...


## Q : Ask for Parcels¶

A post office would like a program to classify (and charge!) customers sending parcels.

1. Ask user for parcels.
For each parcel ask for the following Information (length, width, height, weight)
Continue to ask until the user wishes to stop.
Reject parcels with any dimension greater than a metre.
Reject weights > 100kg.
2. After collection of parcel information classify according to the following:

small : all dimensions < 10cm
medium : one dimension can be upto 50cm. All other dimensions below 20cm.
large : all dimensions < 1m

light : weight < 1kg
normal : weight inbetween
heavy : weight > 50kg

For example
length : 0.1, width : 0.4, height : 0.15, weight : 0.5
would be a medium-normal parcel

3. Parcels are charged at different rates.

1st class parcels have a base price of £1.00
2nd class parcels have a base price of £0.50

medium : additional 20 pence per kg
heavy : additional 50 pence per kg
In [2]:
# ...


## Q. List all factors of a given number¶

1. Input a positive integer and output the total number of factors along with what they are.

 For example:
value = 12
12 has 5 factors these are : 2, 3, 4, 6 and 12
2. Calculate the number of factors for all numbers between 2 and the user input integer N. Output the number with the highest number of factors

 For example:
N = 10
should output :

In the range 1 to 10:
6 has the most number of factors (3)
In [3]:
value = 15

# ...


## Q. Count frequency from a list¶

Create a list of N random numbers between 1 and 100. Count the frequency of each number 1 to 100.

    For example:
N = 1000

39 occurs 52 times
53 occurs 82 times
24 occurs 74 times
0 occurs 39 times
11 occurs 18 times
...
In [4]:
# ...


## Q. Convert number into arbitrary base¶

Take a number N and turn it into an arbitrary base (between 2 and 32 inclusive)

For example
N = 7 in base 2 is 111
N = 255 in base 16 is ff
N = 16 in base 8 is 20
In [5]:
# ...


## Q. Summation Upto Limit¶

Create an empty list
Add a random number between -50 and 50 to the list
Continue to do so until the list has a total over 200
Output the list and how many numbers were added
In [6]:
# ...


## Q. Implement the sieve of eratosthenes¶

The sieve of eratosthenes is a method to calculate primes[1]. Implement this in Python for numbers between 1 and N

In [7]:
# ...


## Q. Output Overlapping Terms¶

Two words are passed. Only output letters from the first which appear in the second, otherwise '_'

For example first = 'cheese' second = 'he'

outputs : '_hee_e'



extension Use this to produce a game of hangman

In [8]:
# ...


## Q. Output the times table¶

Create a times table from 1x1 to $N \times N$

  1   2   3   4   5   6   7   8   9  10  11  12
2   4   6   8  10  12  14  16  18  20  22  24
3   6   9  12  15  18  21  24  27  30  33  36
4   8  12  16  20  24  28  32  36  40  44  48
5  10  15  20  25  30  35  40  45  50  55  60
6  12  18  24  30  36  42  48  54  60  66  72
7  14  21  28  35  42  49  56  63  70  77  84
8  16  24  32  40  48  56  64  72  80  88  96
9  18  27  36  45  54  63  72  81  90  99 108
10  20  30  40  50  60  70  80  90 100 110 120
11  22  33  44  55  66  77  88  99 110 121 132
12  24  36  48  60  72  84  96 108 120 132 144 
In [9]:
# ...


## Q. Number of chocolate bars that can be bought¶

If chocolate bar costs X and money is Y (Y > x)
How many chocolate bars can we afford?
How much change do we get?
In [10]:
# ...


## Q. Produce Pascal's Triangle¶

Output Pascal's triangle[1] upto the Nth row

                                    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1        
In [11]:
# ...


## Q. Multiplication and Division of positive numbers¶

Implement multiplication and division using '+' and '-' not built-in x or /

In [12]:
# ...