Random Homework

Module 4: IDEs & Tools

import numpy as np
import random
ls_exercises = [
    "22.6",
    "22.7",
    "22.8",
    "22.9",
    "22.10"
]
ls_students = [
    "MATISSE VALENTINA RITA STEPHAN AAFTINK-DE LUCA",
    #"MATHIAS ANDRES ALVAREZ MORALES",
    "ADAM HENRYK BAJER",
    "PAUL ANDRE PLERRE CHABERT",
    "FERNANDO CORDERO MARTÍN-CARO",
    "LUIS FERNANDO DIEZ JORRETO",
    "QIN YU EGUÍA FERNÁNDEZ",
    "MIGUEL FERNÁNDEZ ORTIZ",
    #"PABLO GARMA TARDÍO",
    #"IGNACIO GIMENO ALEMANY",
    #"ZOYA INAARA HAIDER",
    "EVA JU?NA",
    #"PANAGIOTIS KAMARINOS",
    "NOA KESERI",
    "DONOVAN LOWE",
    #"SEBASTIAN ALEXANDER LUBCZONOK ALEXANDER",
    #"MARIAM MOHAMED ESMAT EZZAT",
    #"OMAR MOHAMED KAMAL",
    "DARIUS-LUCA PETRUTI",
    "MARÍA RIVERA LÓPEZ",
    "CARLOS SALDAÑA TEJERA",
    "ANA SANTOS ESCORIAL",
    "TAMARA SHOPOVA",
    "PABLO TORRES COLOMINA",
    "ZIPENG YANG"
]

print(len(ls_students))
# Pick a random student
student = random.choice(ls_students)
# Remove the student from list
ls_students.remove(student)
# Print the student
print(student)

# Pick a random exercise
exercise = random.choice(ls_exercises)
# Remove the exercise from list
ls_exercises.remove(exercise)
# Print the exercise
print(exercise)
score = []

print(np.mean(score))

22.6. Closest Value

Write a Python function named closest_indices(). It accepts two input parameters: an array of any shape, and a number. The function returns a tuple containing the indices of the closest value to that number. If more than one element is as close, the function returns one of those indices.

a = np.array(
    [[10, 30, 50],
     [20, 40, 60]])

print(closest_indices(a, 33))  # (0, 1)
print(closest_indices(a, 100))  # (1, 2).

22.7. Matrix Rotation

Write a Python function rotate_matrix() that accepts an n \(\times n\) matrix and rotates it 90 degrees clockwise.

def rotate_matrix(matrix):
  rotated = matrix.T
  flipped = np.flip(rotated, axis=0)
  return flipped
def rotate_matrix(matrix):
  rotated = matrix.T[::-1]
  return rotated
def rotate_matrix(matrix):
  rotated = np.rot90(matrix, k=-1)
  return rotated
a = np.array(
    [[1, 2, 3, 0],
     [4, 5, 6, 0],
     [7, 8, 9, 0],
     [0, 0, 0, 0]]
)

print(rotate_matrix(a))

22.8. Palindrome Partitioning

Write a Python function partition_palindromes() that splits a given string into as few substrings as possible such that each substring is a palindrome. Return the list of palindromic substrings.

print(partition_palindromes("racecarannakayak"))
# Expected result: ["racecar", "anna", "kayak"]

22.9. Fibonacci Matrix Power

Create a Python function fibonacci_matrix() that uses matrix exponentiation to compute the n-th Fibonacci number:

x = np.array([[1, 1], [1, 0]])
n = 1000

print(np.linalg.matrix_power(x, n)[0, 1])
print(fibonacci_matix(200))
# Expected: 280571172992510140037611932413038677189525

22.10. Array Subset Sum

Write a Python function subset_sum() that takes an array of integers and a target sum as input. The function should determine if any subset of the array sums to the target. It should return True if such a subset exists and False otherwise. If True. the function prints the combination.

x = np.array([1, 5, 10])
mask = x <= 9
x2 = x[mask]

print(x2)
print(subset_sum([3, 3, 3], 9))  # True
print(backtrack(1, 1, [1, 2, 3]))