From 6d89b8741f1c7470b000e463259761fbd2517312 Mon Sep 17 00:00:00 2001 From: jbachanek Date: Tue, 24 May 2016 00:14:31 +0200 Subject: [PATCH 1/6] Wykonane zadania 1-5 --- solution.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.py | 12 +++++++++++ 2 files changed, 69 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..7d51092 --- /dev/null +++ b/solution.py @@ -0,0 +1,57 @@ +def Newton(n, m): + if m==0 or m==n: + return 1 + elif n < 0 or m < 0: + return "Blad danych" + elif n < m: + return "Blad danych" + else: + return Newton(n-1, m-1) + Newton(n-1, m) + +def Pascal(n): + if n==0: + return n + elif n<0: + return "Blad danych" + else: + return [[Newton(j, k) for k in range(0,j+1)] for j in range(0,n)] + +def LotOfHash(n): + for line in Pascal(n): + triangle_line = [] + for number in line: + if number%2==1: + triangle_line.append("#") + else: + triangle_line.append(" ") + print "".join(triangle_line) + +def PowerModulo(a,b,n): + return (a**b)%n + +def Intersect(a,b): + import math + d = math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2) + if (d > a[2] + b[2]): #brak rozwiazan - okregi rozlaczne zewnetrznie + return [] + elif (d < (math.fabs(a[2]-b[2]))): #brak rozwiazan - okregi rozlaczne wewnetrznie + return [] + elif (d == 0) and a[2]==b[2]: #okregi sie pokrywaja + return [] + elif (d < (a[2] + b[2])): + l = float(a[2] ** 2 - b[2] ** 2 + d ** 2) / (2 * d) + h = math.sqrt(a[2] ** 2 - l ** 2) + + x1 = l/d * (b[0]-a[0]) + h/d*(b[1]-a[1]) + a[0] + y1 = l/d * (b[1]-a[1]) - h/d*(b[0]-a[0]) + a[1] + + x2 = l/d * (b[0]-a[0]) - h/d*(b[1]-a[1]) + a[0] + y2 = l/d * (b[1]-a[1]) + h/d*(b[0]-a[0]) + a[1] + + return [(x1, y1), (x2, y2)] + elif (d == (a[2] + b[2])): + l = float(a[2] ** 2 - b[2] ** 2 + d ** 2) / (2 * d) + + x1 = l/d * (b[0] - a[0]) + y1 = l/d * (b[1] - a[1]) + return [(x1, y1)] \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..76b4802 --- /dev/null +++ b/test.py @@ -0,0 +1,12 @@ +import solution as sol + +# print sol.Newton(40,5) +# print sol.Pascal(5)[3] +# sol.LotOfHash(10) +# print sol.PowerModulo(2,200,100) + +print sol.Intersect( (0,0,5), (6,0,10) ) +print sol.Intersect( (0,0,5), (0,0,10) ) +print sol.Intersect( (0,0,5), (30,0,10) ) +print sol.Intersect( (0,0,5), (0,0,10) ) +print sol.Intersect( (0,0,5), (0,10,5) ) \ No newline at end of file From 0b37d8edabcd24038586fd4774904a3e61d3fcc0 Mon Sep 17 00:00:00 2001 From: jbachanek Date: Tue, 24 May 2016 00:43:40 +0200 Subject: [PATCH 2/6] Poprawa zad. 4 --- solution.py | 6 +++++- test.py | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/solution.py b/solution.py index 7d51092..3cb2f2b 100644 --- a/solution.py +++ b/solution.py @@ -27,7 +27,11 @@ def LotOfHash(n): print "".join(triangle_line) def PowerModulo(a,b,n): - return (a**b)%n + + out = a + for k in range(0, b-1): + out=(out*a)%n + return out def Intersect(a,b): import math diff --git a/test.py b/test.py index 76b4802..5e9b5d5 100644 --- a/test.py +++ b/test.py @@ -1,12 +1,12 @@ import solution as sol # print sol.Newton(40,5) -# print sol.Pascal(5)[3] +# print sol.Pascal(5) # sol.LotOfHash(10) -# print sol.PowerModulo(2,200,100) +# print sol.PowerModulo(233,4,100) -print sol.Intersect( (0,0,5), (6,0,10) ) -print sol.Intersect( (0,0,5), (0,0,10) ) -print sol.Intersect( (0,0,5), (30,0,10) ) -print sol.Intersect( (0,0,5), (0,0,10) ) -print sol.Intersect( (0,0,5), (0,10,5) ) \ No newline at end of file +# print sol.Intersect( (0,0,5), (6,0,10) ) +# print sol.Intersect( (0,0,5), (0,0,10) ) +# print sol.Intersect( (0,0,5), (30,0,10) ) +# print sol.Intersect( (0,0,5), (0,0,10) ) +# print sol.Intersect( (0,0,5), (0,10,5) ) \ No newline at end of file From f2e274c497a4b9537c1f2ee4ce796a875b88bba5 Mon Sep 17 00:00:00 2001 From: jbachanek Date: Tue, 24 May 2016 00:55:25 +0200 Subject: [PATCH 3/6] Poprawa zad. 4v2 --- solution.py | 12 ++++++++---- test.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/solution.py b/solution.py index 3cb2f2b..acf787e 100644 --- a/solution.py +++ b/solution.py @@ -28,10 +28,14 @@ def LotOfHash(n): def PowerModulo(a,b,n): - out = a - for k in range(0, b-1): - out=(out*a)%n - return out + out = 1 + if(b%2==0): + for k in range(0, b/2): + out = out * a**2 + else: + for k in range(0, b): + out=out*a + return out%n def Intersect(a,b): import math diff --git a/test.py b/test.py index 5e9b5d5..ff499fa 100644 --- a/test.py +++ b/test.py @@ -3,7 +3,7 @@ # print sol.Newton(40,5) # print sol.Pascal(5) # sol.LotOfHash(10) -# print sol.PowerModulo(233,4,100) +# print sol.PowerModulo(81,2,10000) # print sol.Intersect( (0,0,5), (6,0,10) ) # print sol.Intersect( (0,0,5), (0,0,10) ) From 28cf33c1cc6b65e86a68b244caa5433b1687e68b Mon Sep 17 00:00:00 2001 From: jbachanek Date: Tue, 24 May 2016 01:11:36 +0200 Subject: [PATCH 4/6] Poprawa zad. 4v3 --- solution.py | 21 +++++++++++++-------- test.py | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/solution.py b/solution.py index acf787e..7821d94 100644 --- a/solution.py +++ b/solution.py @@ -28,14 +28,19 @@ def LotOfHash(n): def PowerModulo(a,b,n): - out = 1 - if(b%2==0): - for k in range(0, b/2): - out = out * a**2 - else: - for k in range(0, b): - out=out*a - return out%n + b_bin = bin(b) #lista bitow + b_bin_len = len(b_bin) + + wynik = 1 + mod = a%n + for i in range(b_bin_len-1, -1, -1): + if b_bin[i]=='1': #jezeli mamy w zapisie binarnym jedynki - liczymy potegi + wynik=wynik*mod%n + wynik = wynik**2 #do kwadratu + wynik = wynik%n + return wynik + +PowerModulo(1,3,1) def Intersect(a,b): import math diff --git a/test.py b/test.py index ff499fa..08a1c3f 100644 --- a/test.py +++ b/test.py @@ -3,7 +3,7 @@ # print sol.Newton(40,5) # print sol.Pascal(5) # sol.LotOfHash(10) -# print sol.PowerModulo(81,2,10000) +# print sol.PowerModulo(2406,3619,3143) # print sol.Intersect( (0,0,5), (6,0,10) ) # print sol.Intersect( (0,0,5), (0,0,10) ) From 0bbad0a151aeb7cd6b6b601ae3eb9be68999035d Mon Sep 17 00:00:00 2001 From: jbachanek Date: Tue, 24 May 2016 01:49:47 +0200 Subject: [PATCH 5/6] Poprawa zad. 4v4 --- solution.py | 23 +++++++++++------------ test.py | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/solution.py b/solution.py index 7821d94..89b61f4 100644 --- a/solution.py +++ b/solution.py @@ -26,21 +26,20 @@ def LotOfHash(n): triangle_line.append(" ") print "".join(triangle_line) -def PowerModulo(a,b,n): +def PowerModulo(a,k,n): + b_bin = bin(k)[2:] # lista bitow - od drugiego (wycinamy 0b) + b_len = len(b_bin) # dlugos listy bitow + wynik = 1 # result + mod = a % n - b_bin = bin(b) #lista bitow - b_bin_len = len(b_bin) + for i in range(b_len - 1, -1, -1): # idziemy od konca listy + if b_bin[i] == '1': + wynik = wynik * mod % n - wynik = 1 - mod = a%n - for i in range(b_bin_len-1, -1, -1): - if b_bin[i]=='1': #jezeli mamy w zapisie binarnym jedynki - liczymy potegi - wynik=wynik*mod%n - wynik = wynik**2 #do kwadratu - wynik = wynik%n - return wynik + mod = mod**2 + mod = mod%n -PowerModulo(1,3,1) + return wynik def Intersect(a,b): import math diff --git a/test.py b/test.py index 08a1c3f..1411f2c 100644 --- a/test.py +++ b/test.py @@ -3,7 +3,7 @@ # print sol.Newton(40,5) # print sol.Pascal(5) # sol.LotOfHash(10) -# print sol.PowerModulo(2406,3619,3143) +print sol.PowerModulo(2,20,5353152) # print sol.Intersect( (0,0,5), (6,0,10) ) # print sol.Intersect( (0,0,5), (0,0,10) ) From 6bcd19ce7cad79710b3dc83c782d479915ac3b87 Mon Sep 17 00:00:00 2001 From: jbachanek Date: Tue, 24 May 2016 02:44:02 +0200 Subject: [PATCH 6/6] Poprawa zad. 1 --- solution.py | 11 ++++++++++- test.py | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/solution.py b/solution.py index 89b61f4..dea88f8 100644 --- a/solution.py +++ b/solution.py @@ -1,4 +1,6 @@ def Newton(n, m): + if m>n/2: + m=n-m if m==0 or m==n: return 1 elif n < 0 or m < 0: @@ -6,7 +8,14 @@ def Newton(n, m): elif n < m: return "Blad danych" else: - return Newton(n-1, m-1) + Newton(n-1, m) + tab=[[0 for k in range(m+1)] for k in range(n+1)] + for i in range(n+1): + for j in range(min(i,k)+1): + if j==0 or j==i: + tab[i][j] = 1 + else: + tab[i][j]=tab[i-1][j-1]+tab[i-1][j] + return tab[n][k] def Pascal(n): if n==0: diff --git a/test.py b/test.py index 1411f2c..410c5d2 100644 --- a/test.py +++ b/test.py @@ -1,9 +1,9 @@ import solution as sol -# print sol.Newton(40,5) +# print sol.Newton(222,131) # print sol.Pascal(5) -# sol.LotOfHash(10) -print sol.PowerModulo(2,20,5353152) +# sol.LotOfHash(100) +# print sol.PowerModulo(2,20,5353152) # print sol.Intersect( (0,0,5), (6,0,10) ) # print sol.Intersect( (0,0,5), (0,0,10) )