Skip to content

Instantly share code, notes, and snippets.

@gumeniukcom
Created November 9, 2011 20:26
Show Gist options
  • Save gumeniukcom/1352876 to your computer and use it in GitHub Desktop.
Save gumeniukcom/1352876 to your computer and use it in GitHub Desktop.
hw6_task2
import string
__author__ = '[email protected]'
# -*- coding: utf-8 -*-
class dlin(object):
#a = []
def __init__(self, str):
#self.a.append(str)
self.a = []
self.minus = 0
strLenght = len(str)
for i in reversed(xrange(0,strLenght,1)):
number = ord(str[i])-48
self.a.append(number)
#print ord(str[0])
def write(self):
out = ""
l = len(self.a)
if self.minus == 1:
out += "-"
for i in reversed(xrange(0,l,1)):
out += chr(self.a[i]+48)
return out
def balance(self):
l=len(self.a)
for i in reversed(xrange(1,l,1)):
if self.a[i] == 0:
self.a.pop()
else:
break
def __add__(self, other):
l1 = len(self.a)
l2 = len(other.a)
if l1>=l2 :
l3=l1
l4 = l2
q = self
w = other
if l2>l1 :
l3=l2
l4 = l1
q = other
w = self
#print l3
r = 0;
for i in xrange(0,l3,1):
if i<l4:
sum = (q.a[i]+w.a[i] +r) % 10
r = (q.a[i] + w.a[i]) / 10
q.a[i] = sum
else:
sum = (q.a[i] + r) % 10
r = (q.a[i] + r) /10
q.a[i] = sum
#print q.a[i]
if r>0:
q.a.append(r)
return q
def __ge__(self, other):
l1 = len(self.a)
l2 = len(other.a)
if l1>l2:
return True
if l1==l2:
for i in reversed(xrange(0,l1,1)):
#print "--"
#print self.a[i]
if self.a[i]<other.a[i]:
return False
elif self.a[i]>other.a[i]:
return True
if l1<l2:
return False
return True
def __sub__(self, other):
l1 = len(self.a)
l2 = len(other.a)
if self>=other:
l3=l1
l4 = l2
q = self
w = other
else:
l3=l2
l4 = l1
w = self
q = other
q.minus = 1
#print q.a[0]
#print w.a[0]
for i in xrange(0,l3,1):
if i < l4:
sum = q.a[i] - w.a[i]
else:
sum = q.a[i]
#print sum
#print q.a[i]
if sum < 0:
if (i+1)<l3:
sum += 10
q.a[i+1] -= 1
else:
q.minus = 1
q.a[i] = sum
q.balance()
return q
qwe = dlin('910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
#print qwe.write()
rty = dlin('100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
#print rty.write()
asd = qwe + rty
print asd.write()
asd = asd + asd
print asd.write()
z = dlin("121")
x = dlin("323")
c = z-x
print c.write()
xcv = dlin("0")
xcv.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment