From 5c325d969ee6cf68c5d3ea9c306b2c9ba82624e3 Mon Sep 17 00:00:00 2001 From: mkrzesicki Date: Wed, 18 May 2016 03:34:53 +0200 Subject: [PATCH] python_task0 solution --- .idea/misc.xml | 14 +++++++ .idea/modules.xml | 8 ++++ .idea/python_task0.iml | 8 ++++ .idea/workspace.xml | 43 ++++++++++++++++++++ solution/solution.py | 84 ++++++++++++++++++++++++++++++++++++++ solution/test.py | 92 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 249 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/python_task0.iml create mode 100644 .idea/workspace.xml create mode 100644 solution/solution.py create mode 100644 solution/test.py diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..26dafb9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a166143 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/python_task0.iml b/.idea/python_task0.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/python_task0.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..6c565ad --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + 1463509940120 + + + + + + + + + + \ No newline at end of file diff --git a/solution/solution.py b/solution/solution.py new file mode 100644 index 0000000..d6d62c3 --- /dev/null +++ b/solution/solution.py @@ -0,0 +1,84 @@ +def Newton(n,m): + if n < m: + print "n nie moze byc mniejsze od m!" + + else: + + n_silnia = 1 + for i in range(1, n + 1): + n_silnia *= i + + m_silnia = 1 + for i in range(1, m + 1): + m_silnia *= i + + n_m_silnia = 1 + for i in range(1, n - m + 1): + n_m_silnia *= i + + + binomial_coeffcient = n_silnia/(m_silnia * n_m_silnia) + + return binomial_coeffcient + +def Pascal(n): + if n < 0: + print "n nie moze byc mniejsze od 0!" + + else: + trojkat_Pascala = [] + for i in range(n): + lista = [Newton(i, j) for j in range(i + 1)] + trojkat_Pascala.append(lista) + + return trojkat_Pascala + +def LotOfHash(n): + trojkat_Pascala = [] + for i in range(n): + lista = [] + for j in range(i + 1): + if Newton(i ,j) % 2 == 0: + lista.append(" ") + else: + lista.append("#") + trojkat_Pascala.append(lista) + print " ".join(lista) + +def PowerModulo(a,b,n): + if n == 0: + print "Nie mozna dzielic przez 0!" + + else: + wynik = a ** b % n + return wynik + +def Intersect(a,b): + if a[2] <= 0 or b[2] <= 0: + print "Promien musi byc wiekszy od 0!" + + else: + okrag1 = [] + okrag2 = [] + punkty_wspolne = [] + for x in range(a[0] - a[2], a[0] + a[2] + 1): + delta = 4 * (a[1] ** 2) - 4 * ((a[0] ** 2) + (a[1] ** 2) - (a[2] ** 2) + (x ** 2) - (2 * x * a[0])) + y1 = ((2 * a[1]) - int(delta ** 0.5)) / 2 + y2 = ((2 * a[1]) + int(delta ** 0.5)) / 2 + okrag1.append((x, y1)) + if (x, y2) not in okrag1: + okrag1.append((x, y2)) + + for x in range(b[0] - b[2], b[0] + b[2] + 1): + delta = 4 * (b[1] ** 2) - 4 * ((b[0] ** 2) + (b[1] ** 2) - (b[2] ** 2) + (x ** 2) - (2 * x * b[0])) + y1 = (2 * b[1] - int(delta ** 0.5)) / 2 + y2 = (2 * b[1] + int(delta ** 0.5)) / 2 + okrag2.append((x, y1)) + if (x, y2) not in okrag2: + okrag2.append((x, y2)) + + for i in okrag1: + if i in okrag2: + punkty_wspolne.append(i) + + return punkty_wspolne diff --git a/solution/test.py b/solution/test.py new file mode 100644 index 0000000..5d34ee9 --- /dev/null +++ b/solution/test.py @@ -0,0 +1,92 @@ +def Newton(n,m): + if n < m: + print "n nie moze byc mniejsze od m!" + + else: + + n_silnia = 1 + for i in range(1, n + 1): + n_silnia *= i + + m_silnia = 1 + for i in range(1, m + 1): + m_silnia *= i + + n_m_silnia = 1 + for i in range(1, n - m + 1): + n_m_silnia *= i + + + binomial_coeffcient = n_silnia/(m_silnia * n_m_silnia) + + return binomial_coeffcient + +def Pascal(n): + if n < 0: + print "n nie moze byc mniejsze od 0!" + + else: + trojkat_Pascala = [] + for i in range(n): + lista = [Newton(i, j) for j in range(i + 1)] + trojkat_Pascala.append(lista) + + return trojkat_Pascala + +def LotOfHash(n): + trojkat_Pascala = [] + for i in range(n): + lista = [] + for j in range(i + 1): + if Newton(i ,j) % 2 == 0: + lista.append(" ") + else: + lista.append("#") + trojkat_Pascala.append(lista) + print " ".join(lista) + +def PowerModulo(a,b,n): + if n == 0: + print "Nie mozna dzielic przez 0!" + + else: + wynik = a ** b % n + return wynik + +def Intersect(a,b): + if a[2] <= 0 or b[2] <= 0: + print "Promien musi byc wiekszy od 0!" + + else: + okrag1 = [] + okrag2 = [] + punkty_wspolne = [] + for x in range(a[0] - a[2], a[0] + a[2] + 1): + delta = 4 * (a[1] ** 2) - 4 * ((a[0] ** 2) + (a[1] ** 2) - (a[2] ** 2) + (x ** 2) - (2 * x * a[0])) + y1 = ((2 * a[1]) - int(delta ** 0.5)) / 2 + y2 = ((2 * a[1]) + int(delta ** 0.5)) / 2 + okrag1.append((x, y1)) + if (x, y2) not in okrag1: + okrag1.append((x, y2)) + + for x in range(b[0] - b[2], b[0] + b[2] + 1): + delta = 4 * (b[1] ** 2) - 4 * ((b[0] ** 2) + (b[1] ** 2) - (b[2] ** 2) + (x ** 2) - (2 * x * b[0])) + y1 = (2 * b[1] - int(delta ** 0.5)) / 2 + y2 = (2 * b[1] + int(delta ** 0.5)) / 2 + okrag2.append((x, y1)) + if (x, y2) not in okrag2: + okrag2.append((x, y2)) + + for i in okrag1: + if i in okrag2: + punkty_wspolne.append(i) + + return punkty_wspolne + + +print Newton(200,2) +print Pascal(10) +LotOfHash(10) +print PowerModulo(2,200,100) +print Intersect((0,0,5), (6,0,5)) +