0

I'm having a little problem from my python code. This program is about process scheduling, which will get a user input of the number of processes, arrival time and burst time, but the requirement is to have a dynamic list (for the number of processes), and **get the user input of arrival and burst time like presented below.

I edited my previous post and removed all unnecessary codes to make the code more precised just for the problem.

Case 1: If I run the program with 4 processes:

Enter No. Of Processes: 4
Arrival Time:
P1: 0
P2: 2
P3: 4
P4: 5
Burst Time:
P1: 7
P2: 4
P3: 1
P4: 4

[1, 2, 3, 4, 0, 2, 4, 5, 7, 4, 1, 4] < ---- Output

From the output above I wanted to transform it like

[[1, 0, 7, 0], [2, 2, 4, 0], [3, 4, 1, 0], [4, 5, 4, 0]]

What I wanted is that the process data would be arranged in this way while still getting the inputs in for loop:

[process_id, arrival_time, burst_time, 0] -- I need to add a value of '0' for every end of a list

def initializeData(process_qty):
initialize_data = []
temporary = []

for i in range(process_qty):
    process_id = i + 1
    temporary.extend([process_id])
print("Arrival Time: ")

for i in range(process_qty):
    arrival_time = int(input(" P" + str(i + 1) + ": "))
    temporary.append(arrival_time)

print("Burst Time: ")
for i in range(process_qty):
    burst_time = int(input(" P" + str(i + 1) + ": "))
    temporary.append(burst_time)

initialize_data.extend(temporary)
print(initialize_data)

# I have good amounts functions and codes here

if __name__ == "__main__":
    process_qty = int(input("Enter No. of Processes: "))
    initializeData(process_qty)

Any suggestions guys? I am just learning python and happy about it!

2 Answers2

1

I believe you're asking for a simpler / cleaner / pythonic code. Here's how I would read the inputs

n = int(input('Enter no of processes: ')) # n contains the number of processes
arrivals = list() # this list will contain the arrival times of the processes
bursts = list() # this will contain the burst times of the processes
for i in range(n):
  arrivals.append(int(input(f'Enter arrival time for P{i+1}: ')))
for i in range(n):
  bursts.append(int(input(f'Enter burst time for P{i+1}: ')))

I obviously can't write the entire program for you, but this should help you get started in the right direction.

Also, functional and object oriented programs are great, but don't over do it. I see that you've written too many functions. For a simple program like this it's not advised to write separate functions (at least not that many)

Eeshaan
  • 1,557
  • 1
  • 10
  • 22
1

You should append the arrival time and burst time in two different lists as it will be easy to process them,

no_p=int(input("No of processes:"))
ar=list()
br=list()
final_list=list()

for i in range(no_p):
    at=int(input("arrival time:"))
    ar.append(at)

for j in range(no_p):
    bt=int(input("burst time:"))
    br.append(bt)

for k in range(no_p):
    a=[k+1,ar[k],br[k],0]
    final_list.append(a)
print(final_list)

output:

No of processes:4
arrival time:1
arrival time:2
arrival time:3
arrival time:4
burst time:1
burst time:2
burst time:3
burst time:4
[[1, 1, 1, 0], [2, 2, 2, 0], [3, 3, 3, 0], [4, 4, 4, 0]]

A more compact solution can also be applied without using 3 for loops.

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
  • Thanks a lot! I have been spending a lot of time with this maybe (simple) problem but kind of complicated to me. You have accomplished the output that I wanted. Btw, is ar = list () a list initial declaration? If yes, are there any differences with ar = [ ] declaration? – SilverBullet16 Jun 09 '20 at 05:32
  • you should always use [] to declare a list and list() to convert other structures such as tuple to list. I have accidentally used list(). Have a look at the answers of this question https://stackoverflow.com/questions/33716401/whats-the-difference-between-list-and – Prathamesh Jun 09 '20 at 05:36