Advertisement
jabela

Team A - Python to Pseudocode

Mar 17th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #Team A Python To Pseudocode (Rahul Team)
  2. def Load(myfile):
  3. List = []
  4. w = open(myfile,'r')
  5. for line in w:
  6. w = str(line)
  7. List.append(w)
  8. return(List)
  9. OVERALL = Load('PYTHONTEST.py')
  10. FUnctionList = []
  11.  
  12. def makeIfCaps():
  13. global OVERALL
  14. List = OVERALL
  15. NewList = []
  16. for i in List:
  17. w = i.replace(' if ',' IF ')
  18. NewList.append(w)
  19. OVERALL = NewList
  20.  
  21. def makePercent():
  22. global OVERALL
  23. List = OVERALL
  24. NewList = []
  25. for i in List:
  26. w = i.replace('%','MOD')
  27. NewList.append(w)
  28. OVERALL = NewList
  29.  
  30. def makeDivision():
  31. global OVERALL
  32. List = OVERALL
  33. NewList = []
  34. for i in List:
  35. w = i.replace('//','DIV')
  36. NewList.append(w)
  37. OVERALL = NewList
  38.  
  39. def makeElseCaps():
  40. global OVERALL
  41. List = OVERALL
  42. NewList = []
  43. for i in List:
  44. w = i.replace('else','ELSE')
  45. NewList.append(w)
  46. OVERALL = NewList
  47.  
  48. def arrows():
  49. global OVERALL
  50. List = OVERALL
  51. NewList = []
  52. for i in List:
  53. w = i.replace(' = ',' ← ')
  54. NewList.append(w)
  55. OVERALL = NewList
  56.  
  57. def brek():
  58. global OVERALL
  59. List = OVERALL
  60. NewList = []
  61. for i in List:
  62. w = i.replace('break','BREAK')
  63. NewList.append(w)
  64. OVERALL = NewList
  65.  
  66. def comment():
  67. global OVERALL
  68. List = OVERALL
  69. NewList = []
  70. for i in List:
  71. w = i.replace('#','//')
  72. NewList.append(w)
  73. OVERALL = NewList
  74.  
  75. def makeWhileCaps():
  76. global OVERALL
  77. List = OVERALL
  78. NewList = []
  79. for i in List:
  80. w = i.replace('while','WHILE')
  81. NewList.append(w)
  82. OVERALL = NewList
  83.  
  84. def makeForCaps():
  85. global OVERALL
  86. List = OVERALL
  87. NewList = []
  88. for i in List:
  89. w = i.replace('for','FOR')
  90. NewList.append(w)
  91. OVERALL = NewList
  92.  
  93. def changeequal():
  94. global OVERALL
  95. List = OVERALL
  96. NewList = []
  97. for i in List:
  98. w = i.replace(' == ',' = ')
  99. NewList.append(w)
  100. OVERALL = NewList
  101.  
  102. def PRINT():
  103. global OVERALL
  104. List = OVERALL
  105. NewList = []
  106. for i in List:
  107. w = i.replace('print','OUTPUT')
  108. NewList.append(w)
  109. OVERALL = NewList
  110.  
  111. def expandfor():
  112. global OVERALL
  113. List = OVERALL
  114. NewList = []
  115. for i in List:
  116. if 'FOR' in i:
  117. LIST = i.split(' ')
  118. LIST2 = LIST[3]
  119. LIST2 = LIST2[6::]
  120. LIST4 = LIST2.split(',')
  121.  
  122. NAamo = ''
  123. for i in LIST4[1]:
  124. if i in ['0','1','2','3','4','5','6','7','8','9']:
  125. NAamo += i
  126. NewLine = LIST[0] + ' ' + LIST[1] + ' = ' + LIST2[0] + ' TO ' + NAamo + ':'
  127. NewList.append(NewLine)
  128. NewList.append(' ')
  129. else:
  130. NewList.append(i)
  131. NewList2 = []
  132. NewList3 = []
  133. NAME = ''
  134. CHECK = 0
  135. for i in List:
  136. count = 0
  137. if 'FOR' in i:
  138. CHECK = 1
  139. NewList3.append(1)
  140. List = i.split(' ')
  141. NAME = List[1]
  142. else:
  143. NewList3.append(0)
  144. for j in i:
  145. if j != ' ':
  146. break
  147. count += 1
  148. NewList2.append(count)
  149. currentindent = - 1
  150. position = 0
  151. if CHECK == 1:
  152. for j in range(len(NewList3)):
  153. i = NewList3[j]
  154. if i == 1:
  155. currentindent = NewList2[j]
  156. if NewList2[j] == currentindent:
  157. position = j
  158.  
  159. NewList.insert(position+5,'NEXT ' + NAME)
  160.  
  161.  
  162. OVERALL = NewList
  163.  
  164.  
  165. def Function():
  166. global FUnctionList
  167. global OVERALL
  168. List = OVERALL
  169. NewList = []
  170. for i in List:
  171. if 'def ' in i:
  172. w = i.split(' ')
  173. line = 'PROCEDURE ' + w[1]
  174. x = w[1]
  175.  
  176. FUnctionList.append(x.split('(')[0])
  177. else:
  178. line = i
  179. NewList.append(line)
  180. OVERALL = NewList
  181.  
  182. def AddCALL():
  183. global FUnctionList
  184. global OVERALL
  185. List = OVERALL
  186. NewList = []
  187. for i in List:
  188. if (i.split('(')[0]) in FUnctionList:
  189. newline = 'CALL ' + i
  190. else:
  191. newline = i
  192. NewList.append(newline)
  193. OVERALL = NewList
  194.  
  195. def Final():
  196. makeIfCaps()
  197. makeElseCaps()
  198. makeForCaps()
  199. makeWhileCaps()
  200. arrows()
  201. changeequal()
  202. makeDivision()
  203. makePercent()
  204. PRINT()
  205. expandfor()
  206. comment()
  207. Function()
  208. AddCALL()
  209. brek()
  210. #print(OVERALL)
  211. for i in OVERALL:
  212. print(i)
  213.  
  214. Final()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement