'My python def's doesnt work as expected when called, why?

I'm a bit stuck because I've already written those functions in Java and they work properly, and then, when "converted" to python, they didn't work as expected... There are my doubts and my function:

  1. My function and program works the following, I got a list (rawData), and then a second one (correctData), both with float values only, and everything should work in way to iterate "n" times with a formula (shown below), when the difference between rawData[n] and correctData[n] is greater than my tolerance, and then, finally change the rawData processed by the formula to a new list processed data -> procData

  2. The formula should iteract in this way: if any rawData[n] - correctData[n] > Tolerance, then catch the difference between then and apply rawData[n] = rawData[n] - difference, and for neighbors do the half inverse, like eg: if reducing rawData[1] by 1, then it increase rawData[0] and rawData[2] by 0.5

2.1 Here's a image explaining what it should do: https://i.imgur.com/P9dawZH.png

2.2 And here's what I'm getting after run: https://i.imgur.com/gjYOYfk.png

  1. Everything goes well except the data that should be adjusted, It someway adjust only the values after "n" indexes of the rawData, depending on the tolerance, this makes no sense...
import matplotlib.pyplot as plt

accuracyTolerance = 1 #In millimeters

def toCompare(rawData: list, correctData: list) -> int:
    n = 0
    for i in range(len(rawData)):
        if abs(rawData[i] - correctData[i]) >= accuracyTolerance:
            n = i
            i = len(rawData) + 1
    return n


def getDifference(rawData: list, correctData: list) -> list:
    procData = rawData
    n = toCompare(rawData, correctData)
    difference = rawData[n] - correctData[n]

    if difference > 0:
        procData[n] = rawData[n] - difference
        if n > 0:
            procData[n-1] = procData[n-1] + difference/2
        if n < len(procData)-1:
            procData[n+1] = procData[n+1] + difference/2

    if difference < 0:
        procData[n] = procData[n] + difference
        if n > 0:
            procData[n-1] = procData[n-1] - difference/2
        if n < len(procData)-1:
            procData[n+1] = procData[n+1] - difference/2

    return procData


def checkValues(rawData: list, correctData: list) -> bool:
    checkCounter = 0
    n = toCompare(rawData, correctData)
    for i in rawData:
        if abs(rawData[n]) - abs(correctData[n]) > accuracyTolerance:
            checkCounter += 1
            
    if checkCounter > 0:
        return False
    else:
        return True

list1 = [0.0, 11.0, 6.0, 9.0, 37.0, 39.0, 46.0, 70.0, 
         72.0, 84.0, 52.0, 62.0, 56.0, 57.0, 67.0, 53.0, 
         69.0, 59.0, 68.0, 60.0, 36.0, 63.0, 76.0, 67.0, 
         66.0, 55.0, 51.0, 36.0, 51.0, 18.0, 9.0, 3.0, 0.0, 0.0] #Raw data obtained in the field

list2 =[0.0, 0.0, 0.0, 0.0, 15.25, 30.50, 45.75, 61.0,
        61.0, 61.0, 61.0, 61.0, 61.0, 61.0, 61.0, 61.0,
        61.0, 61.0, 61.0, 61.0, 61.0, 61.0, 61.0, 61.0,
        61.0, 61.0, 48.80, 36.60, 24.40, 12.20, 0.0, 0.0, 0.0, 0.0] #Correct data that will be the guide to list1  

plt.plot(range(len(list1)), list1, '*--r', zorder = 2, label = 'The rawData from field.') #Plotting the raw Data
plt.plot(range(len(list2)), list2, '^--g', zorder = 3, linewidth = .5, label = 'The way data should be after') #Plotting literal correct data
iNumber = 0

while not checkValues(list1, list2):
    printArrows = getDifference(list1, list2)
    iNumber +=1
    
print(list1) #Check list correction
print(f'\nWith accuracy tolerance set as {accuracyTolerance}mm, the system had to iteract {iNumber} times to achieve this result.')

plt.plot(range(len(list1)), list1, 'o-b', zorder = 1, label ='Data after function processing') #Plotting data after correction
plt.legend(frameon=False, loc='upper right', fontsize=6)
plt.show()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source