473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Tomatoes

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:
Expand|Select|Wrap|Line Numbers
  1. # The Tomato Program
  2.  
  3.  
  4.  
  5.  
  6. #get input from user
  7. N = input("Enter number of Tomatoes: ");
  8. R = input("Enter number of Ripe Tomatoes: ");
  9. D = input("Enter number of Days: ");
  10.  
  11.  
  12.  
  13. #assign position to an array
  14. a = 0;
  15. position = []
  16. print ("Enter position of the Ripe Tomatoes in ascending order: ");
  17. for a in range (0,R):
  18.      pos = raw_input();
  19.  
  20.      position.append(int(pos))
  21.  
  22.  
  23. #create Tomatoes  and Ripetomatoes array
  24. e = 0;
  25. tomatoes = []
  26.  
  27. for e in range(0,N):
  28.     tomatoes.append(0);
  29.  
  30.  
  31.  
  32. #assign ripe Tomatoes to their positions in the apple array
  33. e = 0;
  34. i = 0;
  35. Ripetomatoes = []
  36. for e in range(0,R):
  37.      i = position[e] -1 ;
  38.      tomatoes[i-1] = 1;
  39.      Ripetomatoes.append(0);
  40.  
  41. print "Tomatoes array: ",tomatoes;
  42.  
  43. # For loop to test which tomotoes will be ripe
  44. e = 0
  45. a = 0
  46. i = 0;
  47. for a in range (0,D):
  48.     for i in range (0,N):
  49.         # Test to see if the current item is ripe
  50.         if tomatoes[i] == 1:
  51.             #test for first tomatoe to prevent out of bounds
  52.             if i == 0:
  53.                 Ripetomatoes[i] = 1;
  54.                 break;
  55.             #test for last tomatoe to prevent out of bounds
  56.             if i == N:
  57.                 Ripetomatoes[i-2] = 1;
  58.                 break;
  59.             Ripetomatoes [i] = 1;
  60.             Ripetomatoes[i-2] = 1
  61.     for e in range(0,N):
  62.         if Ripetomatoes[e] == 1:
  63.             tomatoes[e] = 1
  64.     i = 0;


someone please help :(
Apr 28 '10 #1
1 1450
Glenton
391 Expert 256MB
Hi

If you're allowed to use numpy, the calculation bit could be more efficient, but I've stuck with the native lists, because it's maybe easier to understand what's going on, and up to 200 tomatoes is not going to strain the processor!

With something like this, the user input is often more fiddly and tricky than the actual calculation which is only a few lines. Eg do you want to make sure they enter stuff correctly. There's a redundancy in the number of ripe tomatoes being entered. But anyway, here's how I did it:

Expand|Select|Wrap|Line Numbers
  1. #get input from user
  2. while True:
  3.     try:
  4.         N = input("Enter number of Tomatoes: ")
  5.     except:
  6.         print "Enter an integer from 1 to 200"
  7.         continue
  8.     if N>200:
  9.         print "N must be less than or equal to 200"
  10.         continue
  11.     if N<1:
  12.         print "N must be great than or equal to 1"
  13.         continue
  14.     if type(N)!=int:
  15.         print "Must be an integer"
  16.         continue
  17.     break
  18. print N
  19. R = input("Enter number of Ripe Tomatoes: ")
  20. D = input("Enter number of Days: ")
  21.  
  22.  
  23. print ("Enter position of the Ripe Tomatoes in ascending order: ")
  24. pos=raw_input()
  25.  
  26. #Create tomatoes (list of length N) with 0=unripe, and 1=ripe
  27. #Initialise with all unripe
  28. toms=[0 for i in range(N)]
  29.  
  30. #split the raw input into a list of strings
  31. pos=pos.strip().split(" ")
  32.  
  33.  
  34. #convert strings to integers
  35. pos=[int(p) for p in pos]
  36.  
  37. #set those positions to 1 (ripe).  Note list index runs from 0,...,N-1
  38. for p in pos:
  39.     toms[p-1]=1
  40.  
  41. #Check that the right number of ripe toms has been entered
  42. if R!=sum(toms):
  43.     print "Warning!"
  44.     print "The number of ripe tomatoes in the list is %d"%(sum(toms))
  45.     print "but you said there were %d" %R
  46.     print "continuing anyway"
  47.  
  48.  
  49. #OKAY!  Now we've got to the point where we can do the calculations!
  50. #Basically we want to loop through the number of days, and in each
  51. #day we want to check whether any of the neighbours are ripe.
  52. for day in range(D):
  53.     #make a copy of toms to work on
  54.     temp=toms[:]  #note the [:] is necessary
  55.  
  56.     #Run through N-1 possibilities:
  57.     for i in range(N-1):
  58.         #Check whether neighbours on the right are ripe
  59.         if toms[i+1]==1:
  60.             temp[i]=1
  61.         #Check whether neighbours on the left are ripe
  62.         if toms[i]==1:
  63.             temp[i+1]=1
  64.     #update toms
  65.     toms=temp[:]
  66.  
  67. #Print output
  68. print toms, sum(toms)
  69.  
Please post back with any questions, and to let us know whether this was useful.
Apr 29 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: pauld | last post by:
Ive tried almost every combination of while,foreach, key, value , array,0 I can some up with Ive got an associative array that Ive been using as a counter eg if (blah blah blah ) {$array++}...
36
by: Ron Johnson | last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49 -- ----------------------------------------------------------------- Ron Johnson, Jr....
6
by: PA | last post by:
My dream XML will look like as follows: <?xml version="1.0"?> <bk:bookList xmlns:bk="urn:BookSampleSchema.xdr"> <book> <title>Doe's Book</title> <author>J Doe</author> <year>2002</year>...
26
by: gswork | last post by:
i hadn't designed a web page from the ground up for about 9 years, then i was asked to do one. I'd dabbled with html and vaigly kept up with some of the developments but other than that i've been...
3
by: vijaykumarkakatkar | last post by:
Hii all can u help me in resolving my prob...with doubles= quotes.. in my case :: I hav a sentence called " 6-medium tomatoes, chopped into 1/4" chunks".....but its giving me error....ie the...
4
by: pmactdot | last post by:
Hi, I'm looking for some assistance on a case study...I have two arrays <head>: one for daily special dish name, second for the daily dish description, then I have two document.write: a daily...
1
by: ioioioio | last post by:
Hi Hope you are well. I didnt know where else to post this on this website, so thought it would probably be most appropriate here. With CHM files (microsoft help html executable files),...
1
by: ioioioio | last post by:
Hi Hope you are well. I didnt know where else to post this on this website, so thought it would probably be most appropriate here. With CHM files (microsoft help html executable files),...
1
by: shabda raaj | last post by:
I want to strip punctuation from text. So I am trying, ' !! ! ... ?' Which gave me all the chars which I want to replace. So Next I tried by negating the regex,
52
by: marc | last post by:
Hello, Is it possible to right pad with "0" (or other character != blank) ? for example : 1 , length 10 ="1000000000" I've tried with sprintf but I can only left pad with "0" or right pad with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.