Tomatoes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donatello
    New Member
    • Apr 2010
    • 1

    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:
    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 :(
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    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:

    Code:
    #get input from user
    while True:
        try:
            N = input("Enter number of Tomatoes: ")
        except:
            print "Enter an integer from 1 to 200"
            continue
        if N>200:
            print "N must be less than or equal to 200"
            continue
        if N<1:
            print "N must be great than or equal to 1"
            continue
        if type(N)!=int:
            print "Must be an integer"
            continue
        break
    print N
    R = input("Enter number of Ripe Tomatoes: ")
    D = input("Enter number of Days: ")
    
    
    print ("Enter position of the Ripe Tomatoes in ascending order: ")
    pos=raw_input()
    
    #Create tomatoes (list of length N) with 0=unripe, and 1=ripe
    #Initialise with all unripe
    toms=[0 for i in range(N)]
    
    #split the raw input into a list of strings
    pos=pos.strip().split(" ")
    
    
    #convert strings to integers
    pos=[int(p) for p in pos]
    
    #set those positions to 1 (ripe).  Note list index runs from 0,...,N-1
    for p in pos:
        toms[p-1]=1
    
    #Check that the right number of ripe toms has been entered
    if R!=sum(toms):
        print "Warning!"
        print "The number of ripe tomatoes in the list is %d"%(sum(toms))
        print "but you said there were %d" %R
        print "continuing anyway"
    
    
    #OKAY!  Now we've got to the point where we can do the calculations!
    #Basically we want to loop through the number of days, and in each
    #day we want to check whether any of the neighbours are ripe.
    for day in range(D):
        #make a copy of toms to work on
        temp=toms[:]  #note the [:] is necessary
        
        #Run through N-1 possibilities:
        for i in range(N-1):
            #Check whether neighbours on the right are ripe
            if toms[i+1]==1:
                temp[i]=1
            #Check whether neighbours on the left are ripe
            if toms[i]==1:
                temp[i+1]=1
        #update toms
        toms=temp[:]
    
    #Print output
    print toms, sum(toms)
    Please post back with any questions, and to let us know whether this was useful.

    Comment

    Working...