lundi 27 avril 2015

List forward or backward

I was trying to solve some exercises I get at school and I can't get a solution.

So the main quest is to "move" in a list. Random list was generated, negatives and positives numbers, then programm was intended to choose a random value from that list and move forward if that num were positive or backward if it were negative. For example: Choosen number was "-5" and had as index "6", programm need to move 5 times backward and have as index "1". Cycle will reapeat and saving indexes into a list. That cycle, as the main programm, will stop if programm has found already "choosen" index.

P.S. If that randomly choosen were -5 and index were 3 it needs to go to the end of list and start moving there.

There starts mine "code":

#coding: UTF-8
import random
List = []                                               #List with random numbers                                                                     
NewIndex = []                                           #list with indexes that programm will save while doing cycle.                                                          
ListLong = int(raw_input("listlong "))                  #len list    
counter = ListLong
Tester1 = True
NewVal = 0                                                                  
while counter != 0:                                     #this and next lines adds random nums without 0
    valoradd = random.randrange(-10,10)
    if valoradd != 0:
        List.append(valoradd)
        counter -= 1
    elif valoradd == 0:
        valoradd = random.randrange(-10,10)
RandomVal = random.choice(List)                         #choices random num                               
ValueIndex = List.index(RandomVal)                      #Index of that num                        
AnotherIndex = ValueIndex                               
while Tester1 == True:                                  #here begins the thing i cant solve.
    if RandomVal < 0:                                   #that condition sees if num is "<0"                      
        while AnotherIndex != 0:                        
            AnotherIndex -= 1                                                   
            NewVal += 1                                                       
        NewVal = RandomVal - NewVal                                             
        Move = ListLong - NewVal
        if Move not in NewIndex:                        #if number not in list of NewIndexes it adds a new one there.                
            NewIndex.append(Move)                                      
        else:                                           #if number is already in list. Programm stops.                    
            Tester1 = False                                                     
    elif RandomVal > 0:                                 #these condition sees if num is ">0"                 
        Move = ValueIndex + RandomVal
        if Move not in NewIndex:                        #if number not in list of NewIndexes it adds a new one there.
            NewIndex.append(Move)
        else:                                           #if number is already in list. Programm stops.   
            Tester1 = False
print "Found repeated value"




Aucun commentaire:

Enregistrer un commentaire