Advertisement
jabela

Team B - Python To Pseudocode

Mar 17th, 2019
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.58 KB | None | 0 0
  1. # Team B Pseudocode Convertor - Nicole's Team
  2. # CURRENT STATUS: EQUALS SIGN TO ARROWS, CAPS AND OUTPUT WORKING, INDENTATION WORKING, IF LOOPS WORKING BUT LIMITED TO NON-NESTED LOOPS, FOR LOOPS ARE WORKING AND INCLUSIVE OF STEP BUT DOESN'T YET SUPPORT NESTED FOR LOOPS, ACCEPTS WHILE LOOPS WHERE THE CONDITION IS IN THE WHILE STATEMENT (NO BREAKS YET)
  3. # PLEASE TEST WITH SIMPLE ASSIGNMENT, SELECTION AND ITERATION IF YOU WANT TO SEE ACTUAL FUNCTIONING CODE THANKS
  4.  
  5. total = []
  6. inlooop = []
  7. file_name = input("Please input your file name\n   > ")
  8. print()
  9. python = open(file_name, "r")
  10.  
  11. reed = python.readlines()
  12. for i in reed:
  13.    total.append(i[:-1])
  14. # print(total)
  15.  
  16. # MELVYN
  17. def equal2arrowconverter():
  18.     for line in range(0,len(total)):
  19.         List = []
  20.         for i in total[line]:
  21.             if i == "=":
  22.                 if total[line][total[line].index(i)+1] != "=" and total[line][total[line].index(i)-1] != "=":
  23.                     List.append(" <- ")
  24.                 else:
  25.                     List.append(i)
  26.             else:
  27.                 List.append(i)
  28.         List = "".join(List)
  29.         total[line] = List
  30.     return
  31.  
  32. # NICOLE
  33. def indent():
  34.     for line in total:
  35.         count = 0
  36.         while True:
  37.             if line[4*count:4*count+4] == "    ":
  38.                 count += 1
  39.             else:
  40.                 break
  41.         total[total.index(line)] = [count,line[4*count:]]
  42.     return total
  43.  
  44. # BRANDON
  45. def codewordconverter():
  46.     pythonwd = ["print","while","for","if","else:","input","True","False"]
  47.     pseudowd = ["OUTPUT","WHILE","FOR","IF","ELSE","INPUT","TRUE","FALSE"]
  48.     for j in range(0,len(total)):
  49.         line = total[j][1].split()
  50.         for i in range(len(line)):
  51.             for x in range(0,len(pseudowd)):
  52.                 if line[i] == pythonwd[x]:
  53.                     line[i] = pseudowd[x]
  54.         line = " ".join(line)
  55.         if "print" in line:
  56.             line = line.replace("print","OUTPUT")
  57.             line = line.replace("("," ")
  58.             line = line.replace(")"," ")
  59.         total[j][1] = line
  60.  
  61. # JACOB
  62. indenter = []
  63. def indentation_identifier():
  64.     for line in total:
  65.         words = line[1].split()
  66.         if words == []:
  67.             indenter.append([line[0],""])
  68.         else:
  69.             indenter.append([line[0],words[0]])
  70.  
  71. # JACOB
  72. def for_i_in_range_x_to_y():
  73.     new = []
  74.     can = []
  75.     counting = 0
  76.     unlock = ["#", "#"]
  77.     for v in range(0,len(indenter)):  # line, v
  78.         if unlock[0] == 1:
  79.             if unlock[1] == indenter[v][0]:
  80.                 total.insert(v, [indenter[v][0], "END FOR"])
  81.                 unlock = ["#", "#"]
  82.         if indenter[v][1] == "FOR":  # v 0
  83.             new.append("FOR")
  84.             can.append(total[v][len(total[v])-1].split(","))
  85.             unlock = [1, indenter[v][0]]
  86.             for q in can:
  87.                 for w in q:
  88.                     for j in w:
  89.                         try:
  90.                             y = int(j)
  91.                             counting = counting + 1
  92.                             if counting == 1:
  93.                                 new.append(str(y))
  94.                                 new.append("->")
  95.                             if counting == 2:
  96.                                 new.append(str(y))
  97.                             if counting == 3:
  98.                                 new.append("STEP")
  99.                                 new.append(str(y))
  100.                         except ValueError:
  101.                             y = 0
  102.             total[v] = [indenter[v][0]," ".join(new)]
  103.  
  104. # NICOLE
  105. def whilestatement():
  106.     inloop = [0, 0]
  107.     for line in range(0,len(total)):
  108.         if inloop[0] > 0 and inloop[1] == total[line][0]:
  109.             total.insert(line, [inloop[1],"END WHILE"])
  110.             inloop = [inloop[0]-1, 0]
  111.             continue
  112.  
  113.         if "WHILE" in total[line][1]:
  114.             total[line][1] = total[line][1][:-1]
  115.             total[line][1] = total[line][1].replace("==","=")
  116.             total[line][1] = total[line][1].replace("!=","<>")
  117.             inloop = [inloop[0]+1, total[line][0]]
  118.             continue
  119.     if inloop[0] == 1:
  120.         total.append([inloop[1],"END WHILE"])
  121.     return total
  122.  
  123. # NICOLE
  124. def ifstatement():
  125.     inloop = [0, 0] # Number of loops that it's in, Number of indents of the latest loop
  126.     for line in range(0,len(total)):
  127.         if inloop[0] > 0:
  128.             if total[line][0] > inloop[1]:
  129.                 if total[line][1] != "THEN":
  130.                     total[line][0] = total[line][0]+inloop[0]
  131.                 if "IF" not in total[line][1]:
  132.                     continue
  133.             else:
  134.                 if "ELSE" not in total[line][1]:
  135.                     total.insert(line, [inloop[1],"ENDIF"])
  136.                     inloop = [inloop[0]-1, 0]
  137.                     continue
  138.  
  139.         if "IF" in total[line][1]:
  140.             total[line][1] = total[line][1][:-1]
  141.             total[line][1] = total[line][1].replace("==","=")
  142.             total[line][1] = total[line][1].replace("!=","<>")
  143.             inloop = [inloop[0]+1, total[line][0]]
  144.             total.insert(line+1, [int(total[line][0])+1,"THEN"])
  145.             continue
  146.  
  147.         if "ELSE" in total[line][1]:
  148.             total[line][0] = total[line][0] + inloop[0]
  149.  
  150.     if inloop[0] == 1:
  151.         total.append([inloop[1],"ENDIF"])
  152.     return total
  153.  
  154. # NICOLE
  155. def output():
  156.     print("START PROGRAM")
  157.     for line in total:
  158.         out = " "*4*line[0] + line[1]
  159.         print(out)
  160.  
  161. equal2arrowconverter()
  162. indent()
  163. codewordconverter()
  164. whilestatement()
  165. ifstatement()
  166. indentation_identifier()
  167. for_i_in_range_x_to_y()
  168. output()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement