Post on 01-May-2018
transcript
22.11.2015 Laborator 2
1
~ Python Editorul IDLE şi mediul Eclipse
Probleme complexe
1. EasyEclipse-for-Python-1.3.1:
Instalări şi configurări:
http://www.cs.ubbcluj.ro/~camelia/fp/lab/Instalari%20si%20configurari.pdf
Python download: http://www.python.org/getit/
Eclipse download: http://www.eclipse.org/downloads/
Eclipse installation: http://wiki.eclipse.org/Eclipse/Installation#Install_a_JVM
http://www.oracle.com/technetwork/java/javase/downl oads/jdk8-downloads-2133151.html
Eclipse Download and Installation (for Python) Instructions: https://www.ics.uci.edu/~pattis/common/handouts/pythoneclipsejava/eclipsepython.html
1. Installing (for Python) ...
2. Install New Software ...
3. Select the interpreter [14] : Click Window and then click Preferences click
22.11.2015 Laborator 2
2
1. Click Window (to the left of Help on the far right of the line below this window's blue title Java - Eclipse) and then
click Preferences in its pull-down menu, as shown below:
22.11.2015 Laborator 2
3
2. Drill down to select the interpreter:
o Disclose PyDev by clicking the + in front of PyDev or double-clicking PyDev (after the +); the + changes to a -.
o Disclose Interpreters by clicking the + in front of Interpreters or double-clicking Interpreters (after the +);
the + changes to a -.
o Select Python - Interpreter.
The Preferences pop-up window should appear as follows:
22.11.2015 Laborator 2
4
3. Click New... at the top-right of the top window
A Select Interpreter pop-up window appears.
Type Python34 in the Interpreter Name text box.
Type (or cut/paste) C:\Python34\python.exe in the Interpreter Executable text box (or browse to this file).
The Select Interpreter pop-up window should appear as:
22.11.2015 Laborator 2
5
4. Click OK.
A Selection needed pop-up window will appear:
Ensure that a check appears in all four checkboxes.
22.11.2015 Laborator 2
6
5. Click OK.
The Preferences pop-up window will be changed to
22.11.2015 Laborator 2
7
6. Click OK.
A Progress Information pop-up window will appear:
During installation, it will show the various operations it is performing and a progress bar for each one. Eventually the
installation will terminate.
22.11.2015 Laborator 2
8
If the following pop-up window appears
Ensure the "Private networks, such as my home or work network" checkbox is checked, and click Allow Access.
22.11.2015 Laborator 2
9
7. Click Window (to the left of Help on the far right of the line below this window's blue title Java - Eclipse SDK), hover
over Open Perspective in the pull-down menu and then clickOther... in the next pull-down menu.
22.11.2015 Laborator 2
10
The Open Perspective pop-up window will appear.
22.11.2015 Laborator 2
11
8. Double-click PyDev (third from the bottom)
The blue label on the Eclipse window will change to PyDev - Eclipse, the Package Explorer in the upper-left tab will
change to PyDev Package Explorer, and a PyDev icon will appear to the right of the Java icon.
22.11.2015 Laborator 2
12
9. Right-click the Java icon to the left of the PyDev icon and select close
22.11.2015 Laborator 2
13
The Eclipse workbench will now have only the PyDev icon here and will look like:
10. Locate the workspace folder created in step 3 (see C:\Users\yourname\workspace) and download the <=""
b="">courselib.zip file into this workspace and unzip it there, producing the courselibfolder; it should should contain a
bunch of Python module files (ending in .py). From time to time you will be asked to put other course-specific Python
module files into this courselibfolder; these modules will be usable in all Python projects in Eclipse.
22.11.2015 Laborator 2
14
11. Inform Eclipse to use the courselib folder in all Python projects.
o Select Window | Preferences.
o Disclose PyDev by clicking the + in front of PyDev or double-clicking PyDev (after the +); the + changes to a -.
o Disclose Interpreters by clicking the + in front of Interpreters or double-clicking Interpreters (after the +);
the + changes to a -.
o Select Python - Interpreter.
22.11.2015 Laborator 2
15
o Click the Libraries tab (leftmost, above the bottom pane)
o Click the New Folder button (topmost, right of the bottom pane)
o Browse to the courselib folder (added above) in the Browse for Folder pop-up window
(C:\Users\yourname\workspace\courselib).
o Click the courselib folder selected, and then click OK
o Click Apply (you will see some progress bars)
The <b<preferences< b="">pop-up window should now appear as
Notice a fifth entry (ending in courselib appears at the bottom of the bottom pane named System PYTHONPATH
</b<preferences<>
o Cick OK
12. Terminate (click X on) the Eclipse window.
22.11.2015 Laborator 2
16
Eclipse.Exe
1. Project ...
2. I-ul_Proj:
3. I-ul_Modul:
22.11.2015 Laborator 2
17
4. I-ul_Program:
# Reads two integers and prints the sum of them
a = input("Enter the first number: ")
b = input("Enter the second number: ")
c = int(a) + int(b)
print("The sum of ", a, " + ", b, " is ", c)
22.11.2015 Laborator 2
18
I-ul_Modul: from M_f_g import *
def main(): # Reads two integers and prints the sum of them a = int(input("Enter the first number: ")) b = int(input("Enter the second number: ")) c = f(a) + g(b) print("The sum of f(", a, ") + g(", b, ") is ", c)
main()
print("*Stop*")
M_f_g:
def f(x): return x+x
def g(x): return x*x
22.11.2015 Laborator 2
19
2. Pydev ~ Run As (Python Run):
22.11.2015 Laborator 2
20
Exemplul 1. Să se determine toate numerele naturale deosebite dintr-un interval [a,b] dat.
Se spune că n este deosebit dacă există un număr natural m astfel încât n=m+Suma_Cifrelor(m).
☺ Exemplu: Numărul 1235 este deosebit pentru că 1235=1225+10 (1+2+2+5).
22.11.2015 Laborator 2
21
Ex1.Py :
# Sa se det. nr. n din [a,b]: exista m n=m+Sc(m):
def Per(n): # Perechea gasita sau n+1
for m in range(0,n+1):
if n==m+Sc(m): return m
return n+1
def Exist(n,m):
return m<=n
def Sc(m):
if (m<10): return m
else: return Sc(m/10)+m%10
a=input(" a:"); b=input(" b:")
for n in range(a,b+1):
m=Per(n)
if Exist(n,m): print n,"=",m,"+",Sc(m)
Console:
a:1233
b:1236
1233 = 1224 + 9
1235 = 1225 + 10
1236 = 1230 + 6
22.11.2015 Laborator 2
22
Ex1.Py Test gresit:
# Sa se det. nr. n din [a,b]: exista m n=m+Sc(m):
def Per(n): # Perechea gasita sau n+1
for m in range(0,n+1):
if n==m+Sc(m): return m
return n+1
def Exist(n,m):
return m>n # Gresit ! ~ Corect: return m<=n
def Sc(m):
if m<10: return m
else: return Sc(m/10)+m%10
def Main():
a=input(" a:"); b=input(" b:")
for n in range(a,b+1):
m=Per(n)
if Exist(n,m): print n,"=",m,"+",Sc(m)
# Def Teste
def Test_Per():
assert Per(1235)==1225
def Test_Exist():
assert Exist(1235,1225)==True #!!!
def Test_Sc():
assert Sc(1225)==10
# Run Teste
Test_Per()
Test_Exist()
Test_Sc()
# *Start*
Main()
# *Stop*
Console:
Traceback (most recent call last):
File "D:\pythonwsp\Lab2\src\Ex1 Test.py", line 31, in <module>
Test_Exist()
File "D:\pythonwsp\Lab2\src\Ex1 Test.py", line 25, in Test_Exist
assert Exist(1235,1225)==True
AssertionError
Ex1.Py Test corect:
…
def Exist(n,m):
return m<=n # Corectat!
…
Console:
a:1233
b:1236
1233 = 1224 + 9
1235 = 1225 + 10
1236 = 1230 + 6
22.11.2015 Laborator 2
23
Ex2. Afişaţi numărul de cifre comune a două numere, precum şi cifrele. ☺ Ex.: Numerele 21348 şi 14513 au 3 cifre comune şi anume 1, 3, 4.
# Afisati numarul de cifre comune a doua numere, si cifrele.
def Test_Mc():
assert Mc(1225)==set([1,2,5])
assert (Mc(0123)==set([0,1,2,3]))==False
assert Mc(0123)==set([8,3])
# Multimea cifrelor lui x
# Date: x
# Prec: x natural < ...
# Rez.: M = {c1,c2, ..., cn}
# Post: c1c2...cn=x (baza 10)
def Mc(x):
M=set()
while x>0: M.add(x%10); x/=10
return M
def Main():
a=input(" a:"); Ma=Mc(a); print Ma
b=input(" b:"); Mb=Mc(b); print Mb
Cc=Ma&Mb; print(" Nr. Cc=",len(Cc)," Cc=",Cc)
# Run Test
Test_Mc()
# *Start*
Main()
# *Stop*
Console:
a:21348
set([8, 1, 2, 3, 4])
b:14513
set([1, 3, 4, 5])
(' Nr. Cc=', 3, ' Cc=', set([1, 3, 4]))
Pentru baza 8:
def Test_Mc():
assert Mc(1225)==set([1,2,5])
assert Mc(0123)==set([8,3])
Console:
a:012
set([0, 1]) #(012)8=1010 b:0123
set([8, 3]) #(0123)8=8310
(' Nr. Cc=', 0, ' Cc=', set([]))