{"id":41,"date":"2019-05-12T08:45:56","date_gmt":"2019-05-12T08:45:56","guid":{"rendered":"http:\/\/blog.metu.edu.tr\/e221325\/?page_id=41"},"modified":"2019-05-12T08:53:23","modified_gmt":"2019-05-12T08:53:23","slug":"my-python-works","status":"publish","type":"page","link":"https:\/\/blog.metu.edu.tr\/e221325\/my-python-works\/","title":{"rendered":"my python works"},"content":{"rendered":"<p>1)MULTIPLYING TWO ARBITRARY BIG INTEGERS BY PYTHON<br \/>\n$$&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;$$<br \/>\ndef phi(i,array):<br \/>\narray[i+1]=int(array[i+1])+int(int(array[i])\/10)<br \/>\narray[i]=int(array[i])%10<br \/>\nreturn array<\/p>\n<p>#In this method I heavily use polynomial product and correcting the order. Any two integer can be multiplied in this program.<br \/>\n#Only one function was enough to proceed, phi(i,array). This function goes to the input index of given array and changes its value to the number remainder when divided by 10 and changes next index to + the previous number\/10, i.e., we make first school division.<\/p>\n<p>n1=input(&#8220;Enter First Integer : &#8220;)<br \/>\nn2=input(&#8220;Enter Second Integer: &#8220;)<br \/>\na=[]<br \/>\nb=[]<br \/>\nfor x in range(len(n1)):<br \/>\na.append(n1[x])<br \/>\nfor y in range (len(n2)):<br \/>\nb.append(n2[y])<\/p>\n<p>a=a[::-1]<br \/>\nb=b[::-1]<\/p>\n<p>s=[]<br \/>\nfor x in range(len(a)+len(b)-1):<br \/>\ns.append(0)<\/p>\n<p>for k in range (len(a)):<br \/>\nfor l in range (len(b)):<br \/>\ns[k+l]=s[k+l]+int(a[k])*int(b[l])<\/p>\n<p>for x in range (len(a)+len(b)-2):<br \/>\nphi(x,s)<\/p>\n<p>s=s[::-1]<\/p>\n<p>print(&#8220;Multiplication of &#8220;,n1,&#8221; and &#8220;,n2 ,&#8221;is :&#8221;,end=&#8221;&#8221;)<br \/>\nf=1<br \/>\nfor i in s:<br \/>\nprint(i, end=&#8221;&#8221;)<br \/>\n$$&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;$$<\/p>\n<p>2)SORTING CODE BY PYTHON<br \/>\n$$&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;$$<\/p>\n<p>def phi1(array):<br \/>\ns=array[0]<br \/>\nfor x in range (len(array)):<br \/>\nif(array[x]&lt;s):<br \/>\ns=array[x]<br \/>\nreturn s<\/p>\n<p>def phi2(arr1,arr2,s):<br \/>\nfix=len(arr1)<br \/>\nx=0<br \/>\nwhile(x&lt;fix):<br \/>\nif(s==arr1[x]):<br \/>\narr2.append(s)<br \/>\ndel arr1[x]<br \/>\nfix=fix-1<br \/>\nx=x+1<br \/>\nreturn arr2,arr1<\/p>\n<p>def filetoarray(filee):<br \/>\nU=open(filee,&#8221;r&#8221;)<br \/>\ny=[]<br \/>\nfor line in U:<br \/>\na=line.split()<br \/>\ny.append(float(a[0]))<br \/>\nU.close()<br \/>\nreturn y<\/p>\n<p>#MAIN<br \/>\nfilee=input(&#8220;Write the data name and press &#8216;ENTER&#8217;: &#8220;)<\/p>\n<p>y=filetoarray(filee)<\/p>\n<p>print(y)<br \/>\nprint(&#8220;Testing with sorted function:\\n&#8221;,sorted(y))<\/p>\n<p>s=[]<br \/>\nfix2=len(y)<br \/>\nwhile(len(s)&lt;fix2):<br \/>\nphi2(y,s,phi1(y))<br \/>\nprint(&#8220;My result:\\n&#8221;,s)<\/p>\n<p>file = open(&#8220;sorted-result.txt&#8221;,&#8221;w&#8221;)<br \/>\nfor x in range (len(s)):<br \/>\nfile.write(str(s[x])+&#8221;\\n&#8221;)<\/p>\n<p>file.close()<br \/>\n$$&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;$$<\/p>\n<p>3)SIMPSON INTEGRATION BY PYTHON<br \/>\n$$&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;$$<\/p>\n<p>import math<\/p>\n<p>def filetoarray():<br \/>\n U=open(&#8220;S11.dat&#8221;,&#8221;r&#8221;)<br \/>\n y=[]<br \/>\n for line in U:<br \/>\n   a=line.split()<br \/>\n   y.append(float(a[0]))<br \/>\n U.close()<br \/>\n return y<\/p>\n<p>def valuesofF(array,f):<br \/>\n  F=[]<br \/>\n  for i in range(len(array)):<br \/>\n     F.append(f(array[i]))<br \/>\n  return F<\/p>\n<p>def simpsons(array,k):<br \/>\n  F=k<br \/>\n  U=F[0]+F[len(array)-1]<br \/>\n  for x in range(1,len(array)-1):<br \/>\n     if(x%2):<br \/>\n       U=U+4*F[x]<br \/>\n       continue<br \/>\n     U=U+2*F[x]<br \/>\n  U=U\/3*(array[len(array)-1]-array[0])\/(len(F)-1)<br \/>\n  return U   <\/p>\n<p>def functionstutoirals():<br \/>\n for x in range(len(&#8220;FUNCTION TUTOIRAL:&#8221;)):<br \/>\n   print(&#8220;-&#8220;, end=&#8221;&#8221;)<br \/>\n print(&#8220;\\nFUNCTION TUTOIRAL:&#8221;)<br \/>\n for x in range(len(&#8220;FUNCTION TUTOIRAL:&#8221;)):<br \/>\n   print(&#8220;-&#8220;, end=&#8221;&#8221;)<br \/>\n print(&#8220;\\nmath.sin(x)\\nReturn the sine of x (measured in radians)\\n&#8221;)<br \/>\n print(&#8220;math.sqrt(x)\\nReturn the square root of x\\n&#8221;)<br \/>\n print(&#8220;math.erf(x)\\nError function at x.\\n&#8221;)<br \/>\n print(&#8220;math.exp(x)\\nReturn e raised to the power of x.\\n&#8221;)<br \/>\n print(&#8220;math.log(x[, base])\\nReturn the logarithm of x to the given base.If the base not specified, returns the natural logarithm (base e)of x. \\n&#8221;)<br \/>\n print(&#8220;math.gamma(x)\\nGamma function at x.\\n&#8221;)<br \/>\n for x in range(len(&#8220;DATA:&#8221;)):<br \/>\n   print(&#8220;-&#8220;, end=&#8221;&#8221;)<br \/>\n print(&#8220;\\nDATA:&#8221;)<br \/>\n for x in range(len(&#8220;DATA:&#8221;)):<br \/>\n   print(&#8220;-&#8220;, end=&#8221;&#8221;)<br \/>\n print(&#8220;\\nmath.e = 2.718281828459045\\nmath.inf = inf\\nmath.nan = nan\\nmath.pi = 3.141592653589793&#8230;\\nmath.tau = 6.283185307179586\\n&#8221;)<\/p>\n<p>functionstutoirals()<br \/>\ny=filetoarray()<br \/>\nFF=eval(input(&#8220;Enter the function according to tutorial above: &#8220;))<br \/>\nj=valuesofF(y,FF)<br \/>\nprint(&#8220;\\n&#8221;,simpsons(y,j))<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1)MULTIPLYING TWO ARBITRARY BIG INTEGERS BY PYTHON $$&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;$$ def phi(i,array): array[i+1]=int(array[i+1])+int(int(array[i])\/10) array[i]=int(array[i])%10 return array #In this method I heavily use polynomial product and correcting the order. Any two integer can be multiplied in this program. #Only one function was enough to proceed, phi(i,array). This function goes to the input index of given array and changes <a href='https:\/\/blog.metu.edu.tr\/e221325\/my-python-works\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":5643,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-41","page","type-page","status-publish","hentry","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/pages\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/users\/5643"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/pages\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.metu.edu.tr\/e221325\/wp-json\/wp\/v2\/media?parent=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}