Skip to content

Instantly share code, notes, and snippets.

@harpiechoise
Last active July 22, 2024 02:00
Show Gist options
  • Save harpiechoise/a3ca3831180b289d9ee8cdd74474ad7f to your computer and use it in GitHub Desktop.
Save harpiechoise/a3ca3831180b289d9ee8cdd74474ad7f to your computer and use it in GitHub Desktop.
Ejercicios Basicos de Python Resueltos
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def mayor(a,b):\n",
" if(a > b):\n",
" return a\n",
" if(b > a):\n",
" return b"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12\n"
]
}
],
"source": [
"print(mayor(12,6))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def mayor3(a,b,c):\n",
" if((a > b) and (a > c)):\n",
" return a\n",
" if((b > a) and (b > c)):\n",
" return b\n",
" if((c > a) and (c > b)):\n",
" return c"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mayor3(16,12,3)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def isnum(a):\n",
" try:\n",
" int(a)\n",
" except:\n",
" return False;\n",
" return True;"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def length(a):\n",
" i = 0\n",
" if not (isnum(a)):\n",
" for i in range(len(a)):\n",
" i = i + 1\n",
" elif(isnum(a)):\n",
" return(a)\n",
" return(i)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"3\n"
]
}
],
"source": [
"print(length(\"hola\"))\n",
"print(length([\"1\",\"2\",\"3\"]))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def isChar(s):\n",
" if(len(s) > 1):\n",
" return False\n",
" if(len(s) == 1):\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def isvoc(s):\n",
" vocales = \"aeiouáéíóú\"\n",
" if not(isnum(s)) and (isChar(s)):\n",
" for i in range(len(vocales)):\n",
" for j in range(len(s)):\n",
" if (vocales[i] == s):\n",
" return True\n",
" else:\n",
" return False\n",
" elif(isnum(s)):\n",
" print(\"El valor es un numero\")\n",
" elif not(isChar(s)):\n",
" print(\"El valor no es solo un caracter\")"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"El valor no es solo un caracter\n",
"El valor es un numero\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isvoc(\"as\")\n",
"isvoc(1)\n",
"isvoc(\"a\")"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def suma(a):\n",
" total = 0\n",
" for i in range(len(a)):\n",
" total += a[i] \n",
" return total\n",
"def mult(a):\n",
" total = 1\n",
" for i in range(len(a)):\n",
" total *= a[i]\n",
" return total"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"24\n"
]
}
],
"source": [
"print(suma([1,2,3,4]))\n",
"print(mult([1,2,3,4]))"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def inversa(a):\n",
" trans = \"\"\n",
" if(isnum(a)):\n",
" print(\"La entrada es un numero\")\n",
" return \"Error\"\n",
" elif not(isChar(a) and isnum(a)):\n",
" for i in range(len(a)):\n",
" trans = trans + a[(len(a)-1)-i]\n",
" return trans\n",
" \n",
" else:\n",
" print(\"La entrada no deve ser un numero o una sola letra\")"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"estoy probando\n",
"La entrada es un numero\n",
"Error\n"
]
}
],
"source": [
"print(inversa(\"odnaborp yotse\"))\n",
"print(inversa(1))"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def isPalindromo(a):\n",
" inv = inversa(a)\n",
" if(inv == a):\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"print(isPalindromo(\"radar\"))\n",
"print(isPalindromo(\"hola\"))"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def sPos(a,b):\n",
" for i in range(len(a)):\n",
" for j in range(len(b)):\n",
" if(a[i] == b[j]):\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"print(sPos([1,2,3,4,5],[6,7,8,9,0]))\n",
"print(sPos([1,2,3,4],[1,6,7,8,8]))"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def Cgen(a,b):\n",
" c = b\n",
" for i in range(a):\n",
" b = b + c\n",
" return b"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Cgen(100,\"x\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1- Definir una función max() que tome como argumento dos números y devuelva el mayor de ellos. (Es cierto que python tiene una función max() incorporada, pero hacerla nosotros mismos es un muy buen ejercicio.
Solucion:
def mayor(a,b):
if(a > b):
return a
if(b > a):
return b
2- Definir una función max_de_tres(), que tome tres números como argumentos y devuelva el mayor de ellos.
Solucion:
def mayor3(a,b,c):
if((a > b) and (a > c)):
return a
if((b > a) and (b > c)):
return b
if((c > a) and (c > b)):
return c
3- Definir una función que calcule la longitud de una lista o una cadena dada. (Es cierto que python tiene la función len() incorporada, pero escribirla por nosotros mismos resulta un muy buen ejercicio.
Solucion:
Funcion isNum():
def isnum(a):
try:
int(a)
except:
return False;
return True;
Funcion Length():
def length(a):
i = 0
if not (isnum(a)):
for i in range(len(a)):
i = i + 1
elif(isnum(a)):
return(a)
return(i)
4- Escribir una función que tome un carácter y devuelva True si es una vocal, de lo contrario devuelve False.
Solucion:
Funcion isChar:
def isChar(s):
if(len(s) > 1):
return False
if(len(s) == 1):
return True
Funcion isVoc:
def isvoc(s):
vocales = "aeiouáéíóú"
if not(isnum(s)) and (isChar(s)):
for i in range(len(vocales)):
for j in range(len(s)):
if (vocales[i] == s):
return True
else:
return False
elif(isnum(s)):
print("El valor es un numero")
elif not(isChar(s)):
print("El valor no es solo un caracter")
5- Escribir una funcion sum() y una función multip() que sumen y multipliquen respectivamente todos los números de una lista. Por ejemplo: sum([1,2,3,4]) debería devolver 10 y multip([1,2,3,4]) debería devolver 24.
Solucion:
def suma(a):
total = 0
for i in range(len(a)):
total += a[i]
return total
def mult(a):
total = 1
for i in range(len(a)):
total *= a[i]
return total
6- Definir una función inversa() que calcule la inversión de una cadena. Por ejemplo la cadena "estoy probando" debería devolver la cadena "odnaborp yotse"
Solucion:
def inversa(a):
trans = ""
if(isnum(a)):
print("La entrada es un numero")
return "Error"
elif not(isChar(a) and isnum(a)):
for i in range(len(a)):
trans = trans + a[(len(a)-1)-i]
return trans
else:
print("La entrada no deve ser un numero o una sola letra")
7 - Definir una función es_palindromo() que reconoce palíndromos (es decir, palabras que tienen el mismo aspecto escritas invertidas), ejemplo: es_palindromo ("radar") tendría que devolver True.
Solucion:
def isPalindromo(a):
inv = inversa(a)
if(inv == a):
return True
else:
return False
8- Definir una función superposicion() que tome dos listas y devuelva True si tienen al menos 1 miembro en común o devuelva False de lo contrario. Escribir la función usando el bucle for anidado.
Solucion:
def sPos(a,b):
for i in range(len(a)):
for j in range(len(b)):
if(a[i] == b[j]):
return True
else:
return False
9- Definir una función generar_n_caracteres() que tome un entero n y devuelva el caracter multiplicado por n. Por ejemplo: generar_n_caracteres(5, "x") debería devolver "xxxxx".
Solucion:
def Cgen(a,b):
c = b
for i in range(a):
b = b + c
return b
@Andxzon
Copy link

Andxzon commented Mar 11, 2023

Me sirve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment