Python scheduling algorithm code explanation, python Algorithm

Source: Internet
Author: User

Python scheduling algorithm code explanation, python Algorithm

Scheduling Algorithm

The operating system manages the limited resources of the system. When multiple processes (or requests from multiple processes) need to use these resources, because of the limited resources, the process (request) must be selected to occupy resources according to certain principles. This is scheduling. The purpose is to control the number of resource users and select resource users to use resources or occupy resources.

In the operating system, scheduling refers to resource allocation. Therefore, scheduling algorithms refer to the resource allocation algorithms defined in the system's resource allocation policies. Different scheduling algorithms are usually used for different systems and system objectives. For example, in a batch processing system, short job priority scheduling algorithms should be used to take care of a large number of jobs; for another example, in a time-based system, rotation should be used to schedule the system to ensure a reasonable response time. Some of the existing scheduling algorithms are applicable to Job Scheduling and some are applicable to process scheduling. However, some scheduling algorithms can be used for Job Scheduling or process scheduling.

Goal description:

Convert an infix expression to a suffix expression (Reverse Polish Notation: RPN inverse Polish expression)
The Regular Expression of the data involved in the operation is in decimal format [0-9] {1 ,}.

Operator priority: (from high to low) ---------------------- () Brackets/* % division multiplication +-addition and subtraction ------------------------

Solution:

Step 1: use the regular lexical analyzer flex to generate a lexical analyzer to process input infix expressions.
Receives input from stdin, detects illegal characters, and outputs the processed infix expression to stdout.

%option noyywrap%{#include<stdio.h>#include<stdlib.h>%}%%[0-9]+ { printf("%s ",yytext); }[()*/%+-] { printf("%s ",yytext); }[[:space:]] {}. { printf("\nError\n");exit(1); }%%int main(){ yylex(); printf("\n"); return 0;}

Step 2: Use Python for conversion.

Receives an infix expression in a certain format from stdin, detects whether an error occurs during lexical analyzer processing, and then processes the data using the scheduling field algorithm to obtain the rpn list.

import sysline=sys.stdin.readline()line2=sys.stdin.readline()if len(line2)>0: sys.stderr.write("Syntax Error after : ") sys.stderr.write(line) sys.stderr.write("\n") exit(1)lis=line.split(' ')lis.pop()lis_old=lis[:]lis.reverse()oplis=[]rpnlis=[]str=''arith_op="+-*/%" # '(' ')' [0-9]+prior={ '/':1,'*':1,'%':1, '+':2,'-':2 }while len(lis)>0:  str=lis.pop()  if str=='(':    oplis.append('(')  elif str.isdigit():    rpnlis.append(str)  elif len(str)==1 and arith_op.find(str[0])!=-1:    if len(oplis)==0 or oplis[len(oplis)-1]=='(':      oplis.append(str)    else:      while len(oplis)>0 and oplis[len(oplis)-1]!='(' \               and prior[oplis[len(oplis)-1]]<=prior[str]:        rpnlis.append(oplis.pop())      oplis.append(str)  elif str==')':    while len(oplis)>0 and oplis[len(oplis)-1]!='(':      rpnlis.append(oplis.pop())    if len(oplis)>0:         oplis.pop()        else:         sys.stderr.write("Syntax Error while translating : Expected '('")         sys.stderr.write("\n")         exit(2)    else:     sys.stderr.write("Syntax Error : unkown notation -->")     sys.stderr.write(str)     sys.stderr.write("\n")     exit(3)while len(oplis)>0 :  if oplis[len(oplis)-1]!='(':     rpnlis.append(oplis.pop())    else:     sys.stderr.write("Syntax Error while translating : Unexpected '('")     sys.stderr.write("\n")     exit(1)print lis_oldfor i in lis_old:  sys.stdout.write(i)print ''print rpnlisfor i in rpnlis:  print i,print ''exit(0)

Experiment results:

Limitations of the current program:
No syntax check is performed.
Function and variable identification are not supported.

Appendix:

The algorithm uses three spaces. The input is replaced by a symbol. If the input is a number, it is directly entered in the output queue, that is, B), d), f), h ). If the input is an operator, it is pushed into the operator stack, that is, c), e) in the figure. However, if the priority of the input operator is lower than or equal to the operator priority at the top of the operator stack, then, the elements in the stack enter the output queue (Cyclic determination), and the input operator is pushed into the operator stack, that is, g in the figure ). Finally, elements in the operator Stack are in the output queue, and the algorithm ends.

The information in the appendix is taken from Wikipedia • scheduling field algorithm entry.

Summary

The above is all the details about how to implement the scheduling algorithm code in Python. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.