From 2380c2c8aa14a53ab05b1a42b3ededff831d6900 Mon Sep 17 00:00:00 2001 From: MaciekKrulak Date: Thu, 12 May 2016 11:43:01 +0200 Subject: [PATCH 1/5] Two first functions --- solution.py | 42 ++++++++++++++++++++++++++++++++++++++++++ test.py | 0 2 files changed, 42 insertions(+) create mode 100644 solution.py create mode 100644 test.py diff --git a/solution.py b/solution.py new file mode 100644 index 0000000..0c4e9e0 --- /dev/null +++ b/solution.py @@ -0,0 +1,42 @@ +import timeit + +def Newton(n,m): + res=1 + res2=1 + big=max(n-m,m) + small=min(n-m,m) + for i in range(big+1,n+1): + res *= i + for j in range(1,small+1): + res2 *= j + return res/res2 + +start = timeit.default_timer() +print Newton(3,1) +t2 = float(timeit.default_timer()-start) +print "Czas2 :", str(t2) + +def Pascal(n): + tab=[] + tab.append([1]) + for i in range(2,n+1): + add=[1]*i + if i%2==1: + add[i/2]=tab[-1][i/2-1]*2 + for j in range(1,i/2): + add[j]=tab[-1][j-1]+tab[-1][j] + if i%2==0: + p=add[:i/2] + add[i/2:]=p[::-1] + else: + p=add[:i/2] + add[i/2+1:]=p[::-1] + tab.append(add) + return tab + +print Pascal(200) + + + + + diff --git a/test.py b/test.py new file mode 100644 index 0000000..e69de29 From cdc4e7968629399f526261aa492be81ef65806f9 Mon Sep 17 00:00:00 2001 From: MaciekKrulak Date: Thu, 12 May 2016 13:18:00 +0200 Subject: [PATCH 2/5] Added LotOfHash --- solution.py | 33 +++++++++++++++++++++++++-------- test.py | 11 +++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/solution.py b/solution.py index 0c4e9e0..9ff07e4 100644 --- a/solution.py +++ b/solution.py @@ -1,4 +1,4 @@ -import timeit + def Newton(n,m): res=1 @@ -11,10 +11,6 @@ def Newton(n,m): res2 *= j return res/res2 -start = timeit.default_timer() -print Newton(3,1) -t2 = float(timeit.default_timer()-start) -print "Czas2 :", str(t2) def Pascal(n): tab=[] @@ -25,16 +21,37 @@ def Pascal(n): add[i/2]=tab[-1][i/2-1]*2 for j in range(1,i/2): add[j]=tab[-1][j-1]+tab[-1][j] + p = add[:i / 2] if i%2==0: - p=add[:i/2] add[i/2:]=p[::-1] else: - p=add[:i/2] add[i/2+1:]=p[::-1] tab.append(add) return tab -print Pascal(200) +def LotOfHash(n): + a=[] + a.append('#') + a.append('##') + print a[0] + print a[1] + for i in range(2,n): + s='#' + for j in range(i/2): + if (a[-1][j]=='#' and a[-1][j+1]==' ') or (a[-1][j]==' ' and a[-1][j+1]=='#') : + s+='#' + else: + s+=' ' + if i%2==1: + s+=s[::-1] + else: + p=s[:-1] + s+=p[::-1] + a.append(s) + print s + + + diff --git a/test.py b/test.py index e69de29..eb10afc 100644 --- a/test.py +++ b/test.py @@ -0,0 +1,11 @@ +import timeit +from solution import Newton,Pascal,LotOfHash + +start = timeit.default_timer() +print Newton(3,1) +t2 = float(timeit.default_timer()-start) +print "Czas2 :", str(t2) + +print Pascal(10) + +LotOfHash(50) \ No newline at end of file From 0171d174ad389b772b937c88c4b3c94b2d8f5654 Mon Sep 17 00:00:00 2001 From: MaciekKrulak Date: Mon, 16 May 2016 23:54:17 +0200 Subject: [PATCH 3/5] Added Intersect function --- solution.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/solution.py b/solution.py index 9ff07e4..b32d379 100644 --- a/solution.py +++ b/solution.py @@ -1,3 +1,5 @@ +from scipy.spatial import distance +import matplotlib.pyplot as plt def Newton(n,m): @@ -50,6 +52,77 @@ def LotOfHash(n): a.append(s) print s +def PowerModulo(a,b,n): + if a%n==0: + return 0 + else: + #tab = [] + rest=1 + for i in range(b): + rest *= a + rest=rest%n + #tab.append(rest) + #if tab[-1]==tab[0] and len(tab)>1: + #return tab[b%(len(tab))] + return rest + + +def Intersect(a,b): + def SquareFunction(a, b, c): + delta = b ** 2 - 4 * a * c + result = [] + if delta > 0: + result.append((-b + delta ** 0.5) / (2 * a)) + result.append((-b - delta ** 0.5) / (2 * a)) + elif delta == 0: + result.append((-b / (2 * a))) + return result + + x1=a[0] + x2=b[0] + y1=a[1] + y2=b[1] + r1=a[2] + r2=b[2] + c1=x2-x1 + c2=y2-y1 + c6=(r1**2-r2**2-x1**2+x2**2-y1**2+y2**2)/2 + if not c2==0: + c7=-c1/c2 + c8=c6/c2-y1 + dist = distance.euclidean((x1, y1), (x2, y2)) + s=[] + e = [x2 - x1, y2 - y1] / dist + if dist == (r1 + r2) or dist == abs(r1-r2): + s.append((e[0] * r1 + x1, e[1] * r1 + y1)) + if dist<(r1+r2): + if not c2==0: + solution=SquareFunction(c7+1,2*(c7*c8-x1),x1**2+c8**2-r1**2) + s.append((solution[0],c7*solution[0]+c8+y1)) + s.append((solution[1],c7*solution[1]+c8+y1)) + else: + solution=c6/c1 + ycoord=SquareFunction(1,-2*y1,y1**2-r1**2+(solution-x1)**2) + s.append((solution,ycoord[0])) + s.append((solution, ycoord[1])) +###### drawing +# print s +# fig = plt.figure(figsize=(8, 8)) +# ax = plt.subplot(aspect='equal') +# ax.set_xlim((-6, 6)) +# ax.set_ylim((-6, 6)) +# for point in s: +# ax.plot((point[0]),(point[1]) , 'o', color='y') +# circle1 = plt.Circle((x1, y1), r1, fill=False) +# circle2 = plt.Circle((x2, y2), r2, color='red', fill=False) +# fig.gca().add_artist(circle1) +# fig.gca().add_artist(circle2) +# plt.show() +############## + return s + + + From d0a86b7782e588633269cd3bbf8c5c8e6dec34a4 Mon Sep 17 00:00:00 2001 From: MaciekKrulak Date: Tue, 17 May 2016 12:49:20 +0200 Subject: [PATCH 4/5] All the functions implemented --- solution.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/solution.py b/solution.py index b32d379..fe6d5d3 100644 --- a/solution.py +++ b/solution.py @@ -56,16 +56,22 @@ def PowerModulo(a,b,n): if a%n==0: return 0 else: - #tab = [] - rest=1 - for i in range(b): - rest *= a - rest=rest%n - #tab.append(rest) - #if tab[-1]==tab[0] and len(tab)>1: - #return tab[b%(len(tab))] - return rest - + if b<5: + rest=1 + for i in range(b): + rest *= a + rest=rest%n + return rest + else: + number=2 + sol=a**2%n + while number<=b: + if number*2 Date: Tue, 17 May 2016 13:11:55 +0200 Subject: [PATCH 5/5] References to matplotlib and scipy removed --- solution.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/solution.py b/solution.py index fe6d5d3..3f877ce 100644 --- a/solution.py +++ b/solution.py @@ -1,5 +1,5 @@ -from scipy.spatial import distance -import matplotlib.pyplot as plt +###from scipy.spatial import distance +###import matplotlib.pyplot as plt def Newton(n,m): @@ -96,12 +96,12 @@ def SquareFunction(a, b, c): if not c2==0: c7=-c1/c2 c8=c6/c2-y1 - dist = distance.euclidean((x1, y1), (x2, y2)) + dist=((x1-x2)**2+(y1-y2)**2)**0.5 s=[] - e = [x2 - x1, y2 - y1] / dist + e = [(x2 - x1)/dist, (y2 - y1)/dist] if dist == (r1 + r2) or dist == abs(r1-r2): s.append((e[0] * r1 + x1, e[1] * r1 + y1)) - if dist<(r1+r2): + elif dist<(r1+r2): if not c2==0: solution=SquareFunction(c7+1,2*(c7*c8-x1),x1**2+c8**2-r1**2) s.append((solution[0],c7*solution[0]+c8+y1)) @@ -134,5 +134,3 @@ def SquareFunction(a, b, c): - -