LAB 02.01 - Python

!wget --no-cache -O init.py -q https://raw.githubusercontent.com/fagonzalezo/ai4eng-unal/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 \(n\) is a palindrome, this is, if it is the same number when read left to right and when read right to left. Return 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 [\(n_1\), \(n_{10}\), \(n_{25}\)], specifying how many coins of 1, 10 and 25 cents are required to obtain the given amonut. We only have coints of 1, 10 and 25 cents. There are no other kinds of coins.

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 \(n\) it computes the \(n^{th}\) term of the Fibonacci series:

\[ f_n = f_{n-1}+f_{n-2}\]

Where $\(f_0=0 \:\:\text{ y }\:\:f_1=1 \)\( so that: \)\(f_2=1+0=1\)\( \)\(f_3=1+1=2\)\( \)\(f_4=2+1=3\)$ and so on

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 \(n\) and returns True if \(n\) is a perfect number and 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 \(6=3+2+1\)

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 \(n\) and use the modulus operator % 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");