Python Forum

Full Version: Python University Laboratory - Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, guys, how are you? My name is Cami and and I'm new to the coding community. I'm taking a course at the university and I have some problems with the last lab that I need to hand in as a weekly activity. I'd like to ask for help from some of you who are more experienced in the field, so that maybe you can recognize my mistake, and with it, I can improve my computing. Below is the problem statement to be solved:

"Your boss has asked you to analyze the monthly sales of the company in which you are working. During your studies, you discovered that this type of data, which has values recorded over time, is considered a time series and that there is a way of representing this information as a recurrence graph.

The recurrence graph transforms time series into binary images (with each point in the image having a value of 0 or 1). To do this, for each value in the time series, the difference from all the values in the series is checked, and if the (absolute) difference is less than or equal to a certain threshold, the recurrence value between the two points is indicated as 0, otherwise as 1.

The following example shows a time series with 4 values:

5 10 3 8

Consider a threshold equal to 3. For the first value in the series (5), the (absolute) differences between the evaluated value (5) and all the elements in the series are as follows:

0 5 2 3

Therefore, the recurrence for this first evaluated value (5) will be equal to:

0 1 0 0

As the (absolute) difference between the evaluated value (5) for the first value in the series (5), the third value in the series (3) and the fourth value in the series (8) is less than or equal to the threshold (3), these points will receive a value of 0. For the second value in the series (10), the (absolute) difference is greater than the threshold (3), so this point will receive a value of 1.

The same process can be done for all the other values in the series, considering them as evaluated values. In the end, we will have the following recurrence graph, with the first line corresponding to the evaluation for value 5, the second line corresponding to the evaluation for value 10, and so on:

0 1 0 0
1 0 1 0
0 1 0 1
0 0 1 0

Given your knowledge of programming, you have decided to create a program that reads the company's sales and a threshold value and transforms this information into a recurrence graph. Your program should read an N value, representing the size of the time series, followed by N values, corresponding to the monthly sales figures. Then, your code will receive a value L, representing the value of the threshold analyzed. As output, your code should print the recurrence graph. Note that there should be a space between each value (0 or 1), but no space after the last value."

With this statement, I made the following code (in Python language):
# Reading Data
N = int(input()) 
sale = list(map(int, input().split())) 
L = int(input())

# Recurrence Matrix
graphic = [[0 for _ in range(N)]for _ in range(N)]

# Recurrence Calculation
for i in range(N):
    for k in range(N):
        difference = abs(sale[i] - sale[k])
        if difference <= L :
           graphic[i][k] = 0
        else:
            graphic[i][k] = 1

# Printing recurrence data
for line in graphic:
    print(''.join(map(str, linha)))
Given this code, we have the entrance:
4
5 10 3 8
3

And we should have the exit:
0 1 0 0
1 0 1 0
0 1 0 1
0 0 1 0

But I'm not receiving it 'cause of this error (pointed by BlackBox Chat): IndexError: list index out of range. Can someone please help me solve this situation?
Please, post full traceback you get, not just the last line, in error tags

Also, what is linha (on line#20)?
Try this. I double checked, it works.

# Reading Data
N = int(input())

sale = list(map(int, input().split()))
L = int(input())

# Recrurrence Matrix
graphic = [[0 for _ in range(N)] for _ in range(N)]

# Recurrence calculation
for i in range(N):
    for k in range(N):
        difference = abs(sale[i] - sale[k])
        if difference <= L:
            graphic[i][k] = 0
        else:
            graphic[i][k] = 1
            
# Printing  recurrence data
for line in graphic:
    print(''.join(map(str, line)))
The problem statement you've shared about analyzing monthly sales data as a time series and transforming it into a recurrence graph sounds interesting! We'd be happy to help you out. Just let us know what specific issues you're facing or any questions you have. Feel free to share your code or approach, and we'll do our best to assist you.

Combining coding and cinematography is a unique mix, and I'd love to hear more about how these two interests intersect in your journey.