tag:blogger.com,1999:blog-36589231794281036582024-03-04T23:31:55.840-08:00Experiments with PythonProblems, programs and experiments in Python Unknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-3658923179428103658.post-77888226100695092922020-07-17T02:32:00.000-07:002020-07-17T02:32:14.392-07:00Guess the nunber<link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/github.min.css" rel="stylesheet"></link>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script> <b id="docs-internal-guid-6b7e88ed-7fff-3d29-f648-d3eef3826229" style="font-weight: normal;"><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">In this program, the computer draws a number from 1 to 100, and our task is to guess this number. <br /></span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"></span><b id="docs-internal-guid-e5573c1b-7fff-8d1f-3f1a-a5011fd94915" style="font-weight: normal;"><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">We're trying to guess the number. If we give a smaller than the one invented by the computer,</span></b><b id="docs-internal-guid-e5573c1b-7fff-8d1f-3f1a-a5011fd94915" style="font-weight: normal;"><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"> we get <br /></span></b></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><b id="docs-internal-guid-e5573c1b-7fff-8d1f-3f1a-a5011fd94915" style="font-weight: normal;"><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">the information: "too small", if it is larger - "too much". </span></b><b id="docs-internal-guid-e5573c1b-7fff-8d1f-3f1a-a5011fd94915" style="font-weight: normal;"><span style="background-color: transparent; color: black; font-family: "arial"; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">If we hit, we get the information: "You guessed it". </span></b></p></b><br class="Apple-interchange-newline" /><pre><code class="python">#Guss the number
import random
N = 100
counter = 0
def draw_lots(n):
r = int(n*random.random()) + 1
return r
a = 1
b = N
x = draw_lots(N) # computer draws a random numbenr
print("C: I thought a number. Guess it!\n")
guess = 0
while (guess != x):
guess = int(input("C: put a number from " + str(a) + " to " + str(b) +": " ))
counter += 1
if(guess < x):
print("C: too small")
a = guess
elif(guess > x):
print ("C: two big")
b = guess
else:
print ("C: you guessed my number. It was " + str(x))
print("\nC: you needed " + str(counter) + " moves.")
</code></pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-3658923179428103658.post-64380184379794141102017-05-10T09:11:00.000-07:002017-05-10T09:12:35.243-07:00The summary of the letters problemThe last approach of letters problem I put <a href="https://anaconda.org/itstuff/letters_in_english/notebook">here</a>, on the Anaconda Cloud.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-3658923179428103658.post-28367712780500764932017-05-10T05:11:00.001-07:002017-05-15T04:32:10.609-07:00Telegraph vs Guardian<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<br />
And now we change the <b><i>letters_2.py</i></b><br />
program to make it more flexible.
First of all, we define a function <b><i>get_text</i></b> that takes some text (a string)
as an argument and returns three lists:
<br />
<ul>
<li>a list of small letters (for x axis),</li>
<li>list with the amount of given letters found in the text,</li>
<li>list of their rates.</li>
</ul>
<br />
Then we read the home pages of chosen web sites (to strings) and pass them as arguments respectively to the
function <b><i>get_text.</i></b>. The returned lists we pass to bokeh figure function and line metkod
to generate html page with the plot. From this page we can save the plot to the png image.
<br />
<div style="background-color: #f8f5f5; margin-left: 20px; margin-right: 20px; padding-left: 10px;">
<pre class="prettyprint">#letters_3.py
# -*- coding: utf-8 -*-
def get_text(s):
chars = []
for i in range(255):
chars.append(0)
for letter in s:
indeks=ord(letter)-1
chars[indeks]+=1
d = len(chars)
A = []
B = []
C = []
for i in range(d):
if chars[i]>0 and (i+1)>=97 and (i+1)<=122:
A.append(chr(i+1))
B.append(chars[i])
C.append(0)
sum_b = sum(B)
for i in range(len(A)):
C[i] = round(100.0*B[i]/sum_b,1)
return A, B, C
url1="http://www.telegraph.co.uk/"
url2="http://www.guardian.co.uk/"
import urllib
sock = urllib.urlopen(url1)
htmlSource1 = sock.read()
sock.close()
sock = urllib.urlopen(url2)
htmlSource2 = sock.read()
sock.close()
X, Y1, Y = get_text(htmlSource1)
X, Z1, Z = get_text(htmlSource2)
print 'number of letters'
print 'Telegraph:', sum(Y1)
print 'Guardian:', sum(Z1)
from bokeh.plotting import figure, output_file, show
p = figure(x_range = X, title = 'Telegraph letters vs Guardian letters')
output_file("letters3.html")
p.line(X, Y, legend="telegraph", line_width=3)
p.line(X, Z, legend="guardian", color = 'red', line_width=3)
show(p)
</pre>
</div>
The chart tells everything<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIoccEJIt_VE1q4UjEmyg70jUldQQKaxQKMqrvN5vhyphenhyphenmvfNOINT2pkhlb797hg433fwei15G_La_sJhZlqg2s5ezuA59PMoo1Dl3xeEEEzS5Zdtttdqm98Q7NLi9vwoi3uX9jbSaq0DL3g/s1600/bokeh_plot%25281%2529.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="480" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIoccEJIt_VE1q4UjEmyg70jUldQQKaxQKMqrvN5vhyphenhyphenmvfNOINT2pkhlb797hg433fwei15G_La_sJhZlqg2s5ezuA59PMoo1Dl3xeEEEzS5Zdtttdqm98Q7NLi9vwoi3uX9jbSaq0DL3g/s400/bokeh_plot%25281%2529.png" width="480" /></a></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-3658923179428103658.post-83784060830894802072017-05-10T01:10:00.000-07:002017-05-15T04:34:16.950-07:00Counting letters on a web page<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<p> Now we can change the previous program (letters_1.py). We'll take the text from a web page.</p>
<div style="background-color: #f8f5f5; margin-left: 20px; margin-right: 20px; padding-left: 10px;">
<pre class="prettyprint">
#letters_2.py
# -*- coding: utf-8 -*-
url="http://www.telegraph.co.uk/"
import urllib
sock = urllib.urlopen(url)
htmlSource = sock.read()
s = htmlSource
chars = []
for i in range(255):
chars.append(0)
for letter in s:
indeks=ord(letter)-1
chars[indeks]+=1
d = len(chars)
X = []
Y = []
for i in range(d):
if chars[i]>0 and (i+1)>=97 and (i+1)<=122:
X.append(chr(i+1))
Y.append(chars[i])
sum_y = sum(Y)
print 'All small letters on (the home page)', url, ' ', sum_y
print '\nThe frequency of letters in %:\n '
for i in range(len(X)):
Y[i] = round(100.0*Y[i]/sum_y,1)
print '%5s %10.1f' %(X[i], Y[i])
</pre>
</div>
<p>And the results are:</p>
<div style="background-color: #f8f5f5; margin-left: 20px; margin-right: 20px; padding-left: 10px;">
<pre class="prettyprint">
All small letters on (the home page) http://www.telegraph.co.uk/ 355470
The frequency of letters in %:
a 9.1
b 1.2
c 3.8
d 3.8
e 9.7
f 2.1
g 3.1
h 2.7
i 7.3
j 1.4
k 0.7
l 4.3
m 4.3
n 6.1
o 5.1
p 3.6
q 1.0
r 5.8
s 6.8
t 9.0
u 2.0
v 2.6
w 1.4
x 0.8
y 1.5
z 0.9
</pre>
</div>
<p>We can compare these results with the ones from letters.py. If we add some piece of code we can produce a bar chart that visualizes
the frequency of letters. We'll use bokeh charts and data frame from pandas package. So we append such a code:</p>
<div style="background-color: #f8f5f5; margin-left: 20px; margin-right: 20px; padding-left: 10px;">
<pre class="prettyprint">
import pandas as pd
df = pd.DataFrame(
{'letters': X,
'freq': Y
})
from bokeh.charts import Bar, output_file, show
p = Bar(df, 'letters', values='freq',
title="The frequency of letters in English texts",
bar_width=0.4, ylabel = "%",
color = "green", legend = False)
output_file("letters.html")
show(p)
</pre>
</div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDRV8r98lzR4HsPWuxMtB_-4nrX4-_xHA-XVM8xCOEtulQLd-FxSf5PDkTbVOiREsPxzx0zdHP47hoRYCMXg6GfpwEFM9skgypHyJA3opvx39cJij2I_xSGkaeoTPde0F0MgXSzVv5auK8/s1600/letters.png.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDRV8r98lzR4HsPWuxMtB_-4nrX4-_xHA-XVM8xCOEtulQLd-FxSf5PDkTbVOiREsPxzx0zdHP47hoRYCMXg6GfpwEFM9skgypHyJA3opvx39cJij2I_xSGkaeoTPde0F0MgXSzVv5auK8/s320/letters.png.png" width="480" height="480" /></a></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-3658923179428103658.post-67732952172028388772017-05-09T13:50:00.000-07:002017-05-10T03:37:05.064-07:00Counting letters in English text.<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<br />
Let's write a program that determines the frequency of occurring of different letters in English. <br />
We start with that short text (from a story about Sherlock Holmes - 'A scandal in Bohemia').<br />
<br />
I will comment this code tomorrow.<br />
<div style="background-color: #f8f5f5; margin-left: 20px; margin-right: 20px; padding-left: 10px;">
<pre class="prettyprint">#letters_1.py
# -*- coding: utf-8 -*-
s = '''
To Sherlock Holmes she is always the woman. I have seldom heard him mention her under
any other name. In his eyes she eclipses and predominates the whole of her sex.
It was not that he felt any emotion akin to love for Irene Adler. All emotions,
and that one particularly, were abhorrent to his cold, precise but admirably balanced mind.
He was, I take it, the most perfect reasoning and observing machine that the world has seen,
but as a lover he would have placed himself in a false position. '''
chars = []
for i in range(255):
chars.append(0)
for letter in s:
indeks=ord(letter)-1
chars[indeks]+=1
d = len(chars)
X = []
Y = []
for i in range(d):
if chars[i]>0 and (i+1)>=97 and (i+1)<=122:
X.append(chr(i+1))
Y.append(chars[i])
sum_y = sum(Y)
print 'All small letters in the text: ', sum_y
print '\nThe frequency of letters in %:\n '
for i in range(len(X)):
Y[i] = round(100.0*Y[i]/sum_y,1)
print '%5s %10.1f' %(X[i], Y[i])
</pre>
</div>
And the program prints something like this.<br />
<div style="background-color: #f8f5f5; margin-left: 20px; margin-right: 20px; padding-left: 10px;">
<pre class="prettyprint">All small letters in the text: 382
The frequency of letters in %:
a 9.2
b 1.6
c 2.4
d 3.9
e 14.4
f 1.6
g 0.5
h 6.8
i 5.8
k 0.8
l 5.8
m 3.7
n 7.3
o 7.9
p 1.8
r 5.8
s 6.8
t 7.6
u 1.3
v 1.3
w 2.1
x 0.3
y 1.6
</pre>
</div>
As we can see the most common letter as the letter 'e'.<br />
<br />
Unknownnoreply@blogger.com0