LAB 02.01 - Python

!wget --no-cache -O init.py -q https://raw.githubusercontent.com/rramosp/20201.xai4eng/master/content/init.py
import init; init.init(force_download=False); init.get_weblink()
Copy to clipboard
from local.lib.rlxmoocapi import submit, session
student = session.Session(init.endpoint).login( course_id=init.course_id, 
                                                session_id="UDEA", 
                                                lab_id="L02.01" )
init.get_weblink()
Copy to clipboard
import numpy as np
Copy to clipboard

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
Copy to clipboard

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
Copy to clipboard

check your code manually

print (palindromo(12321))
print (palindromo(12322))
Copy to clipboard

submit your code

student.submit_task(globals(), task_id="task_01");
Copy to clipboard

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 [n1, n10, n25], 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

>>> cambio(n=55)
[5,0,2]

>>> cambio(n=200)
None

>>> cambio(n=47)
[2,2,1]
Copy to clipboard

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í
Copy to clipboard

submit your code

student.submit_task(globals(), task_id="task_02");
Copy to clipboard

Task 3: Fibonacci

Complete the function below so that given an integer n it computes the nth term of the Fibonacci series:

fn=fn1+fn2

Where $f0=0 y f1=1sothat:f2=1+0=1f3=1+1=2f4=2+1=3$ and so on

Execution example


>>> fibonacci(10)
55

>>> fibonacci(36)
14930352
Copy to clipboard
def fibonacci(n):
    f_1=1
    f_2=1
    suma=0
    
    # tu codigo aqui
    
    return suma
Copy to clipboard

check manually your code

fibonacci(10), fibonacci(36)
Copy to clipboard

submit your code

student.submit_task(globals(), task_id="task_03");
Copy to clipboard

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
Copy to clipboard

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
Copy to clipboard
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
Copy to clipboard

check your answer

perfecto(6), perfecto(51), perfecto(496)
Copy to clipboard

submit your code

student.submit_task(globals(), task_id="task_04");
Copy to clipboard