Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Resumen Programacion Avanzada, Esquemas y mapas conceptuales de Programación Informática

´nhtrbefjsdkanmS´PEFRETPHRU8IQEW4oñielsot gjfbjdhstj 34wep

Tipo: Esquemas y mapas conceptuales

2022/2023

Subido el 01/06/2023

amina-aasifar
amina-aasifar 🇪🇸

1 documento

1 / 11

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
Programació
LLISTES
insert(i,x) permet afegir un element x en una determinada posició i.
Per eliminar un element al final de la llista ho farem amb la mètode pop(). Retorna
l’element eliminat i muta la llista.
Per eliminar un element d’una determinada posició de la llista ho farem amb la funció
del.
Per eliminar un element amb un determinat valor farem amb la mètode
remove(element). busca l'element i l'elimina. si l'element apareix diverses vegades,
elimina la primera aparició. – si l'element no és a la llista, dóna un error
ITERADORS I GENERADORS
for i, j in enumerate(x): print(i, j, x[i])
exemple : x = ['a', 'b', 'c'] resultat: 0 a an\ 1 b b\n 2 c c
for i, j in zip(x,y): print(i,j)
tenint x = ['a', 'b', 'c', 'd', 'e'] i y = ['a', 'e', 'i', 'o', 'u']
yield
exemple: def factorial(x):
a = 1 b = 1
while b < x:
a = a * b
b += 1
yield a
PARADIGMA FUNCIONAL
map (funcio_a_aplicar, llista_elements)
list(map(lambda x: x**2,l))
si diferents funcions ja definides prèviament
pf3
pf4
pf5
pf8
pf9
pfa

Vista previa parcial del texto

¡Descarga Resumen Programacion Avanzada y más Esquemas y mapas conceptuales en PDF de Programación Informática solo en Docsity!

Programació

LLISTES

  • insert(i,x) permet afegir un element x en una determinada posició i.
  • Per eliminar un element al final de la llista ho farem amb la mètode pop(). Retorna l’element eliminat i muta la llista.
  • Per eliminar un element d’una determinada posició de la llista ho farem amb la funció del.
  • Per eliminar un element amb un determinat valor farem amb la mètode remove(element). – busca l'element i l'elimina. – si l'element apareix diverses vegades, elimina la primera aparició. – si l'element no és a la llista, dóna un error ITERADORS I GENERADORS ➔ for i, j in enumerate(x): print(i, j, x[i]) exemple : x = ['a', 'b', 'c'] resultat: 0 a an\ 1 b b\n 2 c c ➔ for i, j in zip(x,y): print(i,j) tenint x = ['a', 'b', 'c', 'd', 'e'] i y = ['a', 'e', 'i', 'o', 'u'] ➔ yield exemple: def factorial(x): a = 1 b = 1 while b < x: a = a * b b += 1 yield a PARADIGMA FUNCIONAL map (funcio_a_aplicar, llista_elements) list(map(lambda x: x**2,l)) si diferents funcions ja definides prèviament

funcions=[mult,suma,parell] for i in range(10): print(list(map(lambda x: x(i), funcions))) filter (funcio_que_retorna_boolea, llista_elements) list(filter(lambda x: x%2 == 0, l)) map + filter + lambda exemple: list(map(lambda x: x*.5, filter(lambda x: x>=0,l))) list comprehension = [expressió for variable in llista if condició] l2 = [x5 for x in [x+2 for x in l if x>10] if x%2==1] #primer fa l’if x>10 i després x%2== list comprehension if-else ["mango" if i%3==0 else "orange" for i in range(10)] CONJUNTS ➢ Pertinença (in) ➢ Diferència (-) Elements es troben en un conjunt i no en l’altre. ➢ Or (|) Elements presents en un conjunt o l’altre. La unió en sentit matemàtic. ➢ And (&) Elements presents en un conjunt i l’altre. La intersecció en sentit matemàtic. ➢ Xor (^) Elements presents en un conjunt o l’altre, però no en els dos. ➢ conjunt.add(element) ➢ conjunt1.update(conjunt2) ➢ conjunt.remove(element)

  • np.array([np.arange(0,10,2), np.arange(1,10,2), np.arange(10,20,2)]) [[ 0 2 4 6 8] [ 1 3 5 7 9] [10 12 14 16 18]] Recórrer els elements matriu
  • a = np.array([np.arange(0,10,2), np.arange(1,10,2), np.arange(10,20,2)]) for x in a.flat: print(x)
  • for index, element in np.ndenumerate(a): --- com enumerate de les llistes afegir elements
  • np.insert(array, index, valor o llista)
  • np.append(array, valor o llista) eliminar elements
  • np.delete(array, index) operacions dins la matriu
  • matriu.sum(1) suma tots els elements de cada fila
  • matriu.sum(0) suma tots els elements de cada columna operacions amb matrius
  • a + b, es sumen les posicions de les matrius amb mateixa mida
  • np.dot(a,b) multiplicació de matriu
  • matriu.sum(), matriu.min(), matriu.max(), matriu.mean()
  • np.exp(matriu)

distància euclidània

  • u = np.array([1,2,3,4]) v = np.array([4,5,6,7]) distancia = np.sqrt(((u-v)**2).sum()) buscar element
  • bow = np.array([['a', 0], ['b', 1], ['c', 2]]) on = (np.where(bow == 'a')) pos_valor = (on[0], on[1]+1) print(bow[on]) >> ['a'] print(bow[pos_valor]) >> ['0'] format matriu
  • array.shape retorna dimensions
  • array.size retorna el nombre d’elements intercanviar files
  • a3=np.array([[1,2,3],[4,5,6]]) a3[[0,1]] = a3[[1,0]] condicions
  • matriu[condició] retorna matriu amb les posicions que compleixen la condició
  • matriu < o > o =, retorna booleans
  • matriu[condició] = valor, assigna el valor a les posicions que compleixin la condició
    • => or lògic o condicions = ((condició1) + (condició2) o condició = ((matriu < numero) + (numero < matriu)) matriu[condició]

Docstrings  help(Classe) Unittest  import unittest  class testNomClasse(unittest.TestCase): self.assertXXXX(“resultat esperat”, str(valor calculat)) GRASP PATTERS ➢ GRASP: General Responsibility Assignment Software Patterns ➢ GRASP patterns: how to assign responsibilities to classes. What class should do what ➢ Two types of responsibilities: doing or knowing o Doing responsibilities of an object include: ▪ Creating an object or doing a calculation ▪ Initiating action in other objects ▪ Controlling and coordinating activities in other objects o Knowing responsibilities of an object include:

▪ Knowing about private encapsulated data ▪ Knowing about related objects ▪ Knowing about things it can derive or calculate ➢ There are 9 GRASP patterns: ▪ Expert : Assign responsibility to the information expert, the class that has the information necessary to fulfil the responsibility. ▪ Creator: Assign class B the responsibility to create instances of class A if: ▪ Class B aggregates or contains objects of class A ▪ Class B closely uses objects of class A ▪ Class B has the initializing data to be passed to the A constructor. B is the expert with respect to creating A If more than one option applies, usually prefer a class B which aggregates or contains class A. Ex. Classe Tauler té info de Casella i Jugador ▪ Low Coupling : minimitzar l’impacte de canvis futurs i les dependències entre classes. measures how strongly a class is connected, depends, relies on or has knowledge of objects of other classes. Assign responsibilities so that coupling remains low. Try to avoid one class to have to know about many others.

  • Classes with strong coupling Suffer from changes in related classes, Are harder to understand and maintain and Are more difficult to reuse ▪ Controller: Assign the responsibility to an object representing one of these choices: ▪ Represents the overall “system” – a root object ▪ Represents a use case scenario within which the system operation occurs. A controller is the first object beyond the User Interface (UI) layer that is responsible for receiving or handling a system operation message. It delegates the work. ▪ Polymorphism: When related alternatives or behaviors vary by type (class), assign responsibility for the behavior using polymorphic operations to the types for which the behavior varies  Corollary: Do not test for the type of an object and use conditional logic to perform varying alternatives based on type --> utilitzar herència per exemple en el cas de la classe Casella

➢ Bloc condicional en un diagrama de seqüència: [condició] acció sobre la línia PRINCIPIS DE DISSENY ORIENTAT A OBJECTES  Technique or advice to be applied when designing or writing code to make software more maintainable, flexible, or extensible under the inevitable changes.  6 design principles: ▪ Information hiding: Minimize the accessibility of classes and members. Classes should not expose their internal implementation details (we set private attributes). A class should provide all and only the information (by setter i getters) clients need to effectively use it. ▪ Don’t talk to estrangers: ▪ An object A can request a service (call a method) of an object instance B, but object A should not "reach through" object B to access yet another object C to request its services. ▪ It is another name for low coupling A no pot accedir a C, però sí per B ▪ DRY: Don’t Repeat Yourself: Avoid duplicate code by abstracting out things that are common and placing those things in a single location. And put each piece of information and behavior in a unique, sensible place.

SRP (Single Responsibility Principle): Every object in your system should have a single responsibility, and all the object's services should be focused on carrying out that single responsibility. One class should have only one reason to change. --> HIGH COHESIONLSP (Liskov Substitution Principle): Objects of a superclass shall be replaceable with objects of its subclasses without breaking the application. Where an object of the base class is expected, it can be substituted by an object of the derived class. subclasses should behave in the same way as the objects of their superclass un mètode present a les subclasses tmb ha de ser-hi a la superclasse amb els mateixos paràmetres i així no haver de distingir entre els tipus de classes derivades ▪ Favor composition over inheritance: No fent herència d’una classe es fa que es pugui canviar més fàcilment el tipus, ja que una classe no es pot canviar. Amb la composició es blinda la informació interna, només dins la classe es pot veure els seus dos tipus i la informació de cada una. Ex: joc de dames, fent herència hi ha dos tipus de fitxes i fent composició hi ha una classe moviment i dins d’ella MovimentNormal i MovimentDama.