Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fractions import Fraction as f
- import re
- def scale(i,num):
- w = i.split('\n')
- sc = sum(f(s) for s in num.split())
- def calc(object):
- fraction = object.group(1)
- sum = int(fraction) * sc
- sum = correct(str(sum))
- return ''.join(str(sum))
- def calcfract(object):
- numb = int(object.group(1))
- brot = object.group(2)
- su = numb + sum(f(n) for n in brot.split())
- su *= sc
- su = correct(str(su))
- return str(su)
- def correct(brot):
- if '/' in str(brot):
- a, b = str(brot).split('/')
- a = int(a)
- b = int(b)
- counter = 0
- while a > b:
- counter += 1
- a -= b
- brot = str(counter) + ' ' + str(a) + '/' + str(b)
- return brot
- for idx, i in enumerate(w):
- r = i.split(' ')
- for index, k in enumerate(r):
- if index < (len(r) - 1):
- ne = r[index + 1]
- if re.search('\d', k):
- if re.search('\d',ne) and re.search('/',ne) and index < (len(r) - 1):
- r[index] += ' ' + str(ne)
- if index + 2 < len(r) -2:
- ne = r[index + 2]
- if re.search('\d',ne) and re.search('/',ne):
- r[index] += ' ' + str(ne)
- del r[index + 2]
- del r[index + 1]
- k = r[index]
- if re.search('^\d+\w+/\d+ \d+/\d+\w+', k):
- spl = k.split('/',maxsplit=1)[0]
- oz = k.split('/', maxsplit=1)[1]
- spl = re.sub('(\d+)', calc, spl)
- oz = re.sub('(\d+) (\d+/\d+)',calcfract,oz)
- k = spl + '/' + oz
- elif re.search('^\d+ \d+/\d+\w+/\d+ \d+/\d+\w+', k):
- first = k.split('/')
- second = first[2] + '/' + first[3]
- first = first[0] + '/' + first[1]
- print(first)
- print(second)
- first = re.sub('^(\d+) (\d+/\d+)', calcfract, first)
- second = re.sub('^(\d+) (\d+/\d+)', calcfract, second)
- print(first)
- print(second)
- k = first + '/' + second
- elif re.search('^\d+ \d+/\d+\w+/', k):
- first = k.split('/')
- second = first[2]
- first = first[0] + '/' + first[1]
- second = re.sub('(\d+)', calc, second)
- first = re.sub('(\d+) (\d+/\d+)', calcfract, first)
- k = first + '/' + second
- elif re.search('^(\d+/\d+)', k):
- k = str(f(k) * sc)
- k = correct(k)
- elif re.search('^(\d+ \d+/\d+)', k):
- k = re.sub('(\d+) (\d+/\d+)', calcfract, k)
- else:
- k = re.sub('(\d+)', calc, k)
- r[index] = k
- r = ' '.join(str(e) for e in r)
- w[idx] = r
- s = '\n'.join(str(e) for e in w)
- return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement