Logo Studenta

Algoritmo a codificacion - PASCAL a PYTHON

¡Este material tiene más páginas!

Vista previa del material en texto

Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
 
E
S
T
R
U
C
T
U
R
A
S
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Algoritmo 
 
Programa 
Diagrama CHAPIN Lenguaje PASCAL Lenguaje PYTHON 
De Entrada: ASIGNACION INTERNA : = = 
 
variable: = dato; 
(Dato: variable, constante, expresión, función) 
 
variable = dato 
(Dato: variable, constante, expresión, 
función) 
De Entrada: LECTURA READ input 
 
 
1. READ (variable); 
 
2. READ (variable1, variable, …, variableN) 
 
1. variable=input () 
 
2. variable=input (“cadena de 
caracteres”) 
De Salida: EXHIBIR WRITE print 
 
1. WRITE (variable1, variable2, …, variableN) 
 
2. WRITE (‘cadena de caracteres’, variables) 
 
3. WRITE (‘cadena de caracteres’) 
 
 
1. print (variable1, variable2, 
variableN) 
 
2. print (“cadena de caracteres”, 
variables) 
 
3. print (“cadena de caracteres”) 
 
4. variable1, variable2, …, variableN 
 
variable  dato 
LEER (variable_1, variable_2, …, variable_n) 
 variable_1, variable_2, …, variable_n E 
EXHIBIR (variable_1,variable_2,…, variable_n) 
 variable_1, variable_2, …, variable_n S 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
Diagrama CHAPIN Lenguaje PASCAL Lenguaje PYTHON 
De Control: DECISIÓN SIMPLE IF THEN ELSE if: else: 
 
 
 Definir (variable) 
 
 Variable 
 
 Verdadero Falso 
 AcciónV1 AcciónF1 
 
 AcciónV2 AcciónF2 
 
 … … 
 
 
 AcciónVN AcciónVF 
1. IF expresión-lógica THEN 
 sentencia-verdadera; 
 
2. IF expresión_lógica THEN 
 sentenciaA 
 ELSE 
 sentenciaB; 
 
3. IF expresión_lógica THEN 
 BEGIN 
 sentencia1; 
 sentencia2; 
 . 
 sentenciaN 
 END; 
 
4. IF expresión_lógica THEN 
 BEGIN 
 sentencia1; 
 sentencia2; 
 . 
 sentenciaN 
 END 
 ELSE 
 BEGIN 
 sentencia1; 
 sentencia2; 
 … 
 sentenciaN 
 END; 
1. if expresión-lógica: 
 sentencia-verdadera 
 
2. if expresión-lógica: 
 sentencia-verdadera 
 else: 
 sentencia-falsa 
 
3. if expresión-lógica1: 
 sentencia1 
 sentencia2 
 … 
 sentenciaN 
 
 
 4. if expresión-lógica1: 
 sentencia1 
 sentencia2 
 … 
 sentenciaN 
 elif expresión_logica2: 
 sentencia1 
 sentencia2 
 … 
 sentenciaN 
 else: 
 sentencia-falsa 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
Diagrama CHAPIN Lenguaje PASCAL Lenguaje PYTHON 
De Iteración Condicional: PARA FOR TO DO for in: 
 
 PARA variable v_inicial A v_final HACER 
 
 
 Acción1 
 
 Acción2 
 
 …. 
 
 
 AcciónN 
 
 
 
1. FOR variable: =valor_inicial TO valor_final DO 
 sentencia; 
 
2. FOR variable: = valor_inicial TO valor_final DO 
 BEGIN 
 sentencia1; 
 sentencia2; 
 … 
 sentenciaN 
 END; 
 
 
1. for variable in dato_iterable: 
 sentencia 
 
2. for variable in dato_iterable: 
 sentencia1 
 sentencia2 
 … . 
 sentenciaN 
 
(dato_iterable: función range (), listas, 
diccionarios, tuplas, etc.) 
 
E
S
T
R
U
C
T
U
R
A
S
 
De Iteración Pre-Condicional: MIENTRAS WHILE DO while : 
 
 Definir (condición) 
 
 MIENTRAS condición DO 
 
 Acción1 
 
 Acción2 
 
 
 AcciónN 
 Definir (condición) 
 
1. WHILE condición DO 
 Sentencia; 
 
2. WHILE condición DO 
 BEGIN 
 sentencia1; 
 sentencia2; 
 … 
 sentenciaN 
 END; 
 
 
1. while condición: 
 sentencia 
 
2. while condición: 
 sentencia1 
 sentencia2 
 … 
 sentenciaN 
 
 
 
 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
 RESOLUCION DE PROBLEMAS PRACTICA NRO. 1 IF…THEN…ELSE 
EJERCICIO NRO 4 (Pág. 4) PASCAL PYTHON 
 
 
 
 
 
Mostrar (‘Ingrese 3 nros.’) 
Leer(A) 
Leer(B) 
Leer(C) 
 
 (A < B) and (B < C) 
 V F 
 
Mostrar Mostrar 
(‘ORDENADOS’) (‘DESORDENADOS’) 
 
program ej4pract1if; 
uses crt; 
var 
A,B,C:integer; 
 
Begin 
 Writeln (‘ingrese 3 nros.’); 
 Readln (A); 
 Readln (B); 
 Readln (C); 
 If (A<B) and (B<C) then 
 Writeln(‘ORDENADOS’) 
 Else 
 Writeln(‘DESORDENADOS’); 
Readkey () 
End. 
 
 
 
 
 
 
 
 
 
 
 
 
# programa ej4pract1if 
a = int(input("Ingresar un número: ")) 
b = int(input("Ingresar otro número: ")) 
c = int(input("Ingresar último número: ")) 
 
if(a<b<c): 
 print("ORDENADOS’”) 
else: 
 print("DESORDENADOS ") 
 
input(‘presione una tecla para continuar…’) 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
APLICACIÓN EN PROBLEMAS DE PRACTICA NRO. 1 
 
EJERCICIO NRO 9 (Pág. 4) 
 
IF…THEN…ELSE 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Mostrar (‘Tipo de Cliente (L: Librerias, P: Particulares :’) 
 
 Leer (TipoCliente) 
 
Hasta (TipoCliente= ’L’) o (TipoCliente = ’P’) 
Mostrar(‘Cantidad: ‘) 
Leer(Cantidad) 
Mostrar(‘’Precio: ‘) 
Leer(Precio) 
 
 TipoCliente = ’L’ 
 V F 
 
 Cantidad <= 24 Cantidad >= 6 
 V F V F 
 
 Mostrar(‘Importe: ‘, Mostrar(‘Importe: ‘, Cantidad <= 18 
Cantidad*precio*0.80) Cantidad*precio*0.75 ) V F Mostrar(‘Importe: ‘ , cantidad*precio) 
 
 
 Mostrar(‘Importe: ‘, Mostrar(‘Importe: ‘, 
 Cantidad*precio*0.95) cantidad*precio*0.9) 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
PASCAL PYTHON 
Program ej9pract1IF; 
uses crt; 
var 
 tipoCliente:char; 
 cantidad:integer; 
 precio,importe:real; 
begin 
 Write ('Tipo de Cliente (L: Librer o P: Partic'); 
 Repeat 
 Readln (tipoCliente) 
 Until (tipoCliente=’L’) or (tipoCliente=’P’); 
 Write('Cantidad de Libros: '); 
 Readln(cantidad); 
 Write('Precio: '); 
 Readln(precio); 
 If (tipoCliente='L') then 
 If (cantidad<=24) then 
 importe:=precio*cantidad*0.8 
 else 
 importe:=precio*cantidad*0.75 
 else 
 If (cantidad<6) then 
 importe:=precio*cantidad 
 else 
 If (cantidad <=18) then 
 importe:=precio*cantidad*0.95 
 else 
 importe:=precio*cantidad*0.90; 
 Writeln('El importe total es: ', importe:8:2); 
 Readkey() 
End. 
#programa ej9pract1IF 
 
tipoCliente = input("Tipo de Cliente (L: Librer o P: Partic): ") 
while (tipoCliente!=”L”) or (tipoCliente!=”P”): 
 tipoCliente = input("Tipo de Cliente (L: Librer o P: Partic”) 
cantidad = int(input("Cantidad de libros: ")) 
precio = float(input("Precio: ")) 
if (tipoCliente == "L"): 
 if(cantidad<=24): 
 importe=precio*cantidad*0.8 
 else: 
 importe=precio*cantidad*0.75 
else: 
 if(cantidad<6): 
 importe=precio*cantidad 
 elif(cantidad<=18): 
 importe=precio*cantidad*0.95 
 else: 
 importe=precio*cantidad*0.9 
 
print ("El importe total es: ", importe) 
input ("Presione una tecla para continuar...") 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
APLICACIÓN PRACTICA EN PROBLEMAS DE PRACTICA NRO 1 
EJERCICIO NRO 13 (Pág. 6) FOR …TO...DO 
 
Mostrar(‘Numero a obtener factorial’) 
Leer (N) 
 
FACT  1 
 PARA F  1 TO N HACER 
 
FACT  FACT*F 
 
 Mostrar (‘Factorial: ‘,FACT) 
 
 
 
PASCAL PYTHON 
 
Program ej13pract1for; 
Uses crt; 
Var F, FACT, N: integer; 
Begin 
 Write('Numero a obtener el factorial: '); 
 Readln(N); 
 FACT:=1; 
 
 FOR F:=1 TO N DO 
 FACT: =FACT*F; 
 
 Writeln ('Factorial de: ‘,N, ‘es ‘, FACT); 
 Readkey () 
End. 
 
 
# Programa ej13pract1FOR 
n = int (input("Numero a obtener el factorial: ")) 
f = 1 
 
for i in range (1, n+1): 
 f = f * i 
 
print(“Factorial de: “,n, “es “, f) 
input ("Presione una tecla para continuar...") 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
APLICACION PRACTICA EN PROBLEMAS DE PRACTICA NRO 1 
 EJERCICIO NRO 6 (Pág. 5) FOR …TO…DO 
 
 Mostrar (‘Ingrese cantidad de materias: ‘) 
 
 Leer (X) 
 
 TOTAL  0 
 
 PARA I  1 a X HACER 
 
 Mostrar(‘Ingrese Nota Materia: ‘, I) 
 
 Leer (NOTA) 
 
 TOTAL  TOTAL+NOTA 
 
 PROMEDIO  TOTAL/X 
 
 Mostrar (‘El promedio es: ‘, PROMEDIO) 
 
 
 
 
 
 
 
 
 
 
 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
 
PASCAL PYTHON 
 
Program ejpract1for; 
Uses crt; 
Var 
 I, X: integer; 
 TOTAL, NOTA, PROMEDIO: real; 
 
Begin 
 
 Write(‘Ingrese cantidad de Materias: ‘); 
 Readln(x); 
 TOTAL: =0; 
 
 FOR I:=1 TO X DO 
 Begin 
 Write(‘Ingrese nota obtenida en materia: ‘, I); 
 Readln (NOTA); 
 TOTAL:=TOTAL+NOTA 
 End; 
 
 PROMEDIO:=TOTAL/X; 
 Write(‘El promedio es: ‘, PROMEDIO); 
Readkey() 
End. 
 
 
 
 
 
 
 
 
# programa ejpract1for 
 
x = int(input(“Cantidad de materias: “)) 
total = 0 
 
for i in range(x): 
 print(“Materia: “, i) 
 nota = float(input(“Nota obtenida: “)) 
 total = total + nota 
 
promedio = total/x 
print (“El promedio es: “, promedio) 
input (‘presione una tecla paracontinuar…’) 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
 APLICACIÓN PRACTICA EN PROBLEMAS DE PRACTICA NRO 1 
EJERCICIO NRO 3 (Pag. 8) WHILE …DO 
 
 
 Mostrar(‘Ingrese un numero: ‘) 
 
 Leer (NUMERO) 
 
 S  0 
 
 MIENTRAS NUMERO <> 0 HACER 
 
S S+NUMERO 
 
Mostrar(‘Ingrese numero, 0 
Para finalizar `) 
 
Leer (NUMERO) 
 
 
Mostrar(‘La suma es: ‘, S) 
 
 
 
 
 
 
 
 
 
 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
PASCAL PYTHON 
 
Program ej3pract1wh; 
Uses crt; 
Var 
NUMERO, S:real; 
Begin 
 Write(‘Ingrese un numero: ‘); 
 Readln (NUMERO); 
 S:=0; 
 
 While NUMERO <> 0 DO 
 Begin 
 S:=S+NUMERO; 
 Write(‘Ingrese un numero (0 para finalizar’); 
 Readln (NUMERO) 
 End; 
 
 Write(‘La suma de los números: ‘, S); 
 Readkey () 
End. 
 
 
 
 
 
 
 
 
 
 
 
 
 
# programa ej3pract1wh; 
numero = int (input("Ingrese un número: ")) 
s = 0 
 
while numero != 0: 
 s = s + numero 
 numero = int(input("Ingrese más número (0 para finalizar): ")) 
 
print("La suma de los números es: ", s) 
input(“Presione una tecla para continuar…”) 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
APLICACIÓN PRACTICA EN PROBLEMAS DE PRACTICA NRO 1 
EJERCICIO NRO 9 (PAG. 8) WHILE …DO 
 
 
 
 Mostrar(‘Ingrese un carácter: ‘) 
 
 Leer (LETRA1) 
 
 MIENTRAS LETRA1 <> ‘*’ HACER 
 
 CONT  0 
 
 LETRA2  LETRA1 
 
 MIENTRAS LETRA2 =LETRA1 HACER 
 
 CONT  CONT+ 1 
 
 Mostrar(‘Ingrese sig. caracter: ‘) 
 
 Leer (LETRA1) 
 
 CONT = 1 
 V F 
 
 
 Mostrar(‘El carácter’, Mostrar(‘El carácter ‘, 
 LETRA2, ‘se LETRA2, ‘se repite ‘, 
 repite ‘,CONT,’vez’) CONT, ‘veces’) 
 
 
 Del Algoritmo a la Programación Algoritmos y Estructura de Datos 
Comparativa de estructuras en lenguajes Pascal y Python 
 
Prof. Claudia Perez 
PASCAL PYTHON 
 
 
Program ej8pract1wh; 
uses crt; 
var 
 LETRA1, LETRA2 : char; 
 Cont : integer; 
begin 
 Write ('Ingresar un caracter: '); 
 Readln (LETRA1); 
 
 While LETRA1 <> '*' do 
 Begin 
 CONT:=0; 
 LETRA2 := LETRA1; 
 
 While LETRA2 = LETRA1 DO 
 Begin 
 CONT := CONT+1; 
 Write ('Ingresar un caracter:'); 
 Readln (LETRA1) 
 End; 
 
 If CONT=1 then 
 Writeln('El caracter ', letra2,' se repitió ‘, cont,' vez') 
 else 
 Writeln('El caracter ', letra2, 'se repitió ‘, cont,' veces') 
 End; 
 Readkey () 
 End. 
 
 
# programa ej8pract1wh 
letra1 = input("Ingresar un caracter: ") 
 
while letra1 !="*": 
 cont = 0 
 letra2 = letra1 
 
 while letra2 == letra1: 
 cont += 1 
 letra1 = input("Ingresar un caracter: ") 
 
 if(cont==1): 
 print("El caracter “, letra2 , “ se repitió “,cont,” vez”) 
 else: 
 print("El caracter “, letra2, “ se repitió “, cont,” veces”) 
 
input(“Presione una tecla para continuar…”)

Continuar navegando