Python Forum
Python University Laboratory - Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python University Laboratory - Help
#1
Lightbulb 
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?
buran write Sep-25-2023, 07:01 AM:
I fixed the sample input, second one is 4 numbers separated by space
Gribouillis write Sep-24-2023, 06:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Please, post full traceback you get, not just the last line, in error tags

Also, what is linha (on line#20)?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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)))
Reply
#4
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.
Larz60+ write Nov-03-2023, 08:25 PM:
removed link
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] University Assignment Help needed Nomathemba 4 3,771 Apr-20-2024, 12:24 PM
Last Post: jas31
  Help with university work - reading and writing files MrKnd94 3 1,248 Nov-01-2022, 08:29 PM
Last Post: deanhystad
  University Assignment Help needed Diovanno 3 4,425 Apr-10-2018, 02:46 PM
Last Post: Diovanno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020