Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. from fractions import Fraction as f
  2. import re
  3.  
  4.  
  5. def scale(i,num):
  6. w = i.split('\n')
  7. sc = sum(f(s) for s in num.split())
  8.  
  9. def calc(object):
  10. fraction = object.group(1)
  11. sum = int(fraction) * sc
  12. sum = correct(str(sum))
  13. return ''.join(str(sum))
  14. def calcfract(object):
  15. numb = int(object.group(1))
  16. brot = object.group(2)
  17.  
  18. su = numb + sum(f(n) for n in brot.split())
  19. su *= sc
  20. su = correct(str(su))
  21. return str(su)
  22. def correct(brot):
  23. if '/' in str(brot):
  24. a, b = str(brot).split('/')
  25. a = int(a)
  26. b = int(b)
  27. counter = 0
  28. while a > b:
  29. counter += 1
  30. a -= b
  31. brot = str(counter) + ' ' + str(a) + '/' + str(b)
  32. return brot
  33.  
  34.  
  35. for idx, i in enumerate(w):
  36. r = i.split(' ')
  37. for index, k in enumerate(r):
  38. if index < (len(r) - 1):
  39. ne = r[index + 1]
  40. if re.search('\d', k):
  41. if re.search('\d',ne) and re.search('/',ne) and index < (len(r) - 1):
  42. r[index] += ' ' + str(ne)
  43. if index + 2 < len(r) -2:
  44. ne = r[index + 2]
  45. if re.search('\d',ne) and re.search('/',ne):
  46. r[index] += ' ' + str(ne)
  47. del r[index + 2]
  48. del r[index + 1]
  49. k = r[index]
  50. if re.search('^\d+\w+/\d+ \d+/\d+\w+', k):
  51. spl = k.split('/',maxsplit=1)[0]
  52. oz = k.split('/', maxsplit=1)[1]
  53. spl = re.sub('(\d+)', calc, spl)
  54. oz = re.sub('(\d+) (\d+/\d+)',calcfract,oz)
  55. k = spl + '/' + oz
  56. elif re.search('^\d+ \d+/\d+\w+/\d+ \d+/\d+\w+', k):
  57. first = k.split('/')
  58. second = first[2] + '/' + first[3]
  59. first = first[0] + '/' + first[1]
  60. print(first)
  61. print(second)
  62. first = re.sub('^(\d+) (\d+/\d+)', calcfract, first)
  63. second = re.sub('^(\d+) (\d+/\d+)', calcfract, second)
  64. print(first)
  65. print(second)
  66.  
  67. k = first + '/' + second
  68. elif re.search('^\d+ \d+/\d+\w+/', k):
  69. first = k.split('/')
  70. second = first[2]
  71. first = first[0] + '/' + first[1]
  72. second = re.sub('(\d+)', calc, second)
  73. first = re.sub('(\d+) (\d+/\d+)', calcfract, first)
  74. k = first + '/' + second
  75. elif re.search('^(\d+/\d+)', k):
  76. k = str(f(k) * sc)
  77. k = correct(k)
  78. elif re.search('^(\d+ \d+/\d+)', k):
  79. k = re.sub('(\d+) (\d+/\d+)', calcfract, k)
  80. else:
  81. k = re.sub('(\d+)', calc, k)
  82. r[index] = k
  83. r = ' '.join(str(e) for e in r)
  84. w[idx] = r
  85. s = '\n'.join(str(e) for e in w)
  86. return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement