2017-05-03 3 views
1

J'ai cherché dans la pile, mais je n'arrive toujours pas à faire fonctionner ça. J'apprends à construire des algorithmes et j'ai besoin de coder un algithme (en utilisant le tri d'insertion) pour trier le tableau dans l'ordre croissant et afficher la valeur qu'il contient. Actuellement, je reçois Aucun.Insertion Trier: ne m'en donne pas lorsque j'imprime

def sortedRainfall (rainfall): 
     month = ['January','Febuary','March','April','May','June','July','August'\ 
        ,'September','October','November','December'] 
     for index in range(1,len(rainfall)): 
      value = rainfall[index] 
      i = index - 1 
      while (i >= 0): 
       if value < rainfall[i]: 
        rainfall[i+1] = rainfall[i] #shift number in slot i right to slot i + 1 
        rainfall[i] = value #shif value left into slot i 
        i = i - 1 
       else: 
        break 

     return 

code complet est ci-dessous:

#Main module -------------------------------------------------------------------------------------------------------------- 
def main(): 
    rainfall = rainInput() #Gets rainfall per month from user. 
    totalRain = totalRainfall (rainfall) #Totals rainfall from user inputted data. 
    average_Rainfall = averageRainfall (totalRain) #Averages rainfall for the year. 
    highestMonth, highestMonthly = highestMonthNumber (rainfall) #Finds highest month of rainfall and displays to the user. 
    lowestMonth, lowestMonthly = lowestMonthNumber (rainfall) #Finds lowest month of rainfall and displays to the user. --- 
    rainfallSorted = sortedRainfall(rainfall) 
    print #this is for spacing output 
    print ('The total rainfall for the year was: ' +str(totalRain) + ' inche(s)') 
    print #this is for spacing output 
    print ('The average rainfall for the year was: ' +str(average_Rainfall) +\ 
      ' inche(s)') 
    print #this is for spacing in output 
    print ('The highest amount of rain was', highestMonthly, 'in' , highestMonth) 
    print #this is for spacing in output 
    print ('The lowest amount of rain was', lowestMonthly, 'in' , lowestMonth) 
    print(rainfallSorted) 


#Gets rainfall per month from user. --------------------------------------------------------------------------------------- 
def rainInput(): 
    rainfall = ['January','Febuary','March','April','May','June','July','August'\ 
    ,'September','October','November','December'] 
    month = 0 

    while month < len(rainfall): 
     rainfall[month] = int(input ('Please enter the amount for month ' + str\ 
     (month + 1) + ': ')) 
     month += 1 
     rainfall.append 

    return rainfall 

#Totals rainfall from user inputted data. --------------------------------------------------------------------------------- 
def totalRainfall (rainfall): 
    return sum([int(x) for x in rainfall]) 

#Averages rainfall for the year. ------------------------------------------------------------------------------------------ 
def averageRainfall (totalRain): 
    average_Rainfall = totalRain/12 

    return average_Rainfall 

#Finds highest month of rainfall and displays to the user. ---------------------------------------------------------------- 
def highestMonthNumber (rainfall): 
    month = ['January','Febuary','March','April','May','June','July','August'\ 
       ,'September','October','November','December'] 
    highestMonthly = 0 
    highestMonth = 0 
    for m, n in enumerate(rainfall): 
     if n > highestMonthly: 
      highestMonthly = n 
      highestMonth = m 

    return month[highestMonth], highestMonthly 

#Finds lowest month of rainfall and displays to the user. ----------------------------------------------------------------- 
def lowestMonthNumber (rainfall): 
    month = ['January','Febuary','March','April','May','June','July','August'\ 
       ,'September','October','November','December'] 
    lowestMonthly = 1000000000 
    lowestMonth = 0 
    for m, n in enumerate(rainfall): 
     if n < lowestMonthly: 
      lowestMonthly = n 
      lowestMonth = m 

    return month[lowestMonth], lowestMonthly 

def sortedRainfall (rainfall): 
    month = ['January','Febuary','March','April','May','June','July','August'\ 
       ,'September','October','November','December'] 
    for index in range(1,len(rainfall)): 
     value = rainfall[index] 
     i = index - 1 
     while (i >= 0): 
      if value < rainfall[i]: 
       rainfall[i+1] = rainfall[i] #shift number in slot i right to slot i + 1 
       rainfall[i] = value #shif value left into slot i 
       i = i - 1 
      else: 
       break 

    return 

main() 
+1

puisque vous n'êtes pas quoi que ce soit dans la fonction retournerez sortedRainfall .. donc Aucun – Aditya

+0

Êtes-vous le droit d'utiliser la 'sorted' fonction? – Neil

+0

Non. Je voudrais pouvoir:/ –

Répondre

1

Essayez ceci:

def sortedRainfall (rainfall): 

for i in range(1, len(rainfall)): 
    if rainfall[i] < rainfall[i-1]: 
     for j in range(i): 
      if rainfall[i] < rainfall[j]: 
       rainfall[i], rainfall[j] = rainfall[j], rainfall[i] 
return rainfall 
2
def sortNames(listOfNames): 
    sortedNames = [] 
    for index in range(1,len(listOfNames)): 
     value = listOfNames[index] 
     i = index - 1 
     while (i >= 0): 
      if value < listOfNames[i]: 
       listOfNames[i+1] = listOfNames[i] #shift number in slot i right to slot i + 1 
       listOfNames[i] = value #shif value left into slot i 
       i = i - 1 
      else: 
       break 
    return rainfall 

juste dû retourner quelque chose.

+0

(Psst, vous devriez marquer comme solution, si vous avez trouvé la solution). – Neil