Hi guys... I'm taking part in the South-African Computer Olympiad and I just started learning Python (about a week now)...
This is one of the questions in one of the previous papers:
Description
An interesting fact is that if one places some red
tomatoes amongst unripe tomatoes, adjacent unripe
tomatoes will start reddening.
Task
Suppose you have a row of N tomatoes, of which R
are red. After each day, the unripe neighbours of
each red tomato become red. Write a program that
determines the number of red tomatoes after D days.
Contraints
1 <= N <= 200; 1 <=R <= 20; 1 <= D <= 30
Example
First the program reads the number of tomatoes
N=10, the number that are ripe R=3, the number of
days D=2 and then the positions (in the range 1...10
in ascending order) of the tomatoes that are red to
begin with.
Input
Enter number of tomatoes N: 10
Enter number of Ripe Tomatoes R: 3
Enter number of days D: 2
Enter position of Tomatoes in
ascending order: 1 8 9
Output
8
Test your program with
a. N=30 R=5 D=4
4 13 23 24 30
b. N=200 R=10 D=20
3 71 72 79 140 142
145 172
Kind of tricky isn't it?
I got this far and its giving me out of bounds errors:
someone please help :(
This is one of the questions in one of the previous papers:
Description
An interesting fact is that if one places some red
tomatoes amongst unripe tomatoes, adjacent unripe
tomatoes will start reddening.
Task
Suppose you have a row of N tomatoes, of which R
are red. After each day, the unripe neighbours of
each red tomato become red. Write a program that
determines the number of red tomatoes after D days.
Contraints
1 <= N <= 200; 1 <=R <= 20; 1 <= D <= 30
Example
First the program reads the number of tomatoes
N=10, the number that are ripe R=3, the number of
days D=2 and then the positions (in the range 1...10
in ascending order) of the tomatoes that are red to
begin with.
Input
Enter number of tomatoes N: 10
Enter number of Ripe Tomatoes R: 3
Enter number of days D: 2
Enter position of Tomatoes in
ascending order: 1 8 9
Output
8
Test your program with
a. N=30 R=5 D=4
4 13 23 24 30
b. N=200 R=10 D=20
3 71 72 79 140 142
145 172
Kind of tricky isn't it?
I got this far and its giving me out of bounds errors:
Code:
# The Tomato Program
#get input from user
N = input("Enter number of Tomatoes: ");
R = input("Enter number of Ripe Tomatoes: ");
D = input("Enter number of Days: ");
#assign position to an array
a = 0;
position = []
print ("Enter position of the Ripe Tomatoes in ascending order: ");
for a in range (0,R):
pos = raw_input();
position.append(int(pos))
#create Tomatoes and Ripetomatoes array
e = 0;
tomatoes = []
for e in range(0,N):
tomatoes.append(0);
#assign ripe Tomatoes to their positions in the apple array
e = 0;
i = 0;
Ripetomatoes = []
for e in range(0,R):
i = position[e] -1 ;
tomatoes[i-1] = 1;
Ripetomatoes.append(0);
print "Tomatoes array: ",tomatoes;
# For loop to test which tomotoes will be ripe
e = 0
a = 0
i = 0;
for a in range (0,D):
for i in range (0,N):
# Test to see if the current item is ripe
if tomatoes[i] == 1:
#test for first tomatoe to prevent out of bounds
if i == 0:
Ripetomatoes[i] = 1;
break;
#test for last tomatoe to prevent out of bounds
if i == N:
Ripetomatoes[i-2] = 1;
break;
Ripetomatoes [i] = 1;
Ripetomatoes[i-2] = 1
for e in range(0,N):
if Ripetomatoes[e] == 1:
tomatoes[e] = 1
i = 0;
someone please help :(
Comment