LAB 02.01 - Python#
!wget --no-cache -O init.py -q https://raw.githubusercontent.com/rramosp/ai4eng.v1/main/content/init.py
import init; init.init(force_download=False); init.get_weblink()
from local.lib.rlxmoocapi import submit, session
session.LoginSequence(endpoint=init.endpoint, course_id=init.course_id, lab_id="L02.01", varname="student");
import numpy as np
Task 1: Palindrome#
Determine whether an integer number True
if so, otherwise return False
.
Execution example
>>> palindromo(n=3456543)
True
>>> palindromo(n=543)
False
hint: transform it into string, reverse it and check equality
challenge: use one single line of code
def palindromo(n): # assume n is an integer
# your code here
r = ...
return r # r must be boolean True/False
check your code manually
print (palindromo(12321))
print (palindromo(12322))
submit your code
student.submit_task(globals(), task_id="task_01");
Task 2: Money Change#
En este ejercicio, su programa recibirá un monto entre 1 y 99 centavos de Dolar, Escriba un metodo que retorne en una lista, cuantas monedas son necesarias para devolverle a alguien dicho valor, si únicamente se cuenta con monedas de 1, 10 y 25 centavos. La lista debe entregarse de la siguiente manera:
In this task your function will receive an amount between 1 and 99 cents and will have to return a list with three numbers [
If the amount given is less than 1 or more than 99 you must return None
.
Execution example
>>> change_money(n=55)
[5,0,2]
>>> change_money(n=200)
None
>>> change_money(n=47)
[2,2,1]
hint: use \\
for integer division
def change_money(n):
r=[0,0,0] #recall: r[0]== n_1, r[1]== n_10 y r[2]== n_25
#if n>99 or n<1: use return None
## tu codigo aquí
submit your code
student.submit_task(globals(), task_id="task_02");
Task 3: Fibonacci#
Complete the function below so that given an integer
Where $
Execution example#
>>> fibonacci(10)
55
>>> fibonacci(36)
14930352
def fibonacci(n):
f_1=1
f_2=1
suma=0
# tu codigo aqui
return suma
check manually your code
fibonacci(10), fibonacci(36)
submit your code
student.submit_task(globals(), task_id="task_03");
Task 4: Perfect number#
complete the following function so that it accepts an integer True
if False
otherwise.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance
See https://en.wikipedia.org/wiki/Perfect_number
Execution example
>>> perfecto(6)
True
>>> perfecto(51)
False
>>> perfecto(496)
True
hint: make a loop of all numbers up to %
to identify which are the divisors, then sum them up.
n = 12
n%2, n%3, n%4, n%5, n%6, n%7, n%8
def perfecto(n):
#tu codigo aqui, pista:
# halle todos los divisores de n y sumelos en una variable auxiliar,
#compare este resultado con el valor original
check your answer
perfecto(6), perfecto(51), perfecto(496)
submit your code
student.submit_task(globals(), task_id="task_04");