Skip to content

Instantly share code, notes, and snippets.

View jordi-petit's full-sized avatar

Jordi Petit jordi-petit

View GitHub Profile
#include <iostream>
#include <algorithm>
using namespace std;
void ordernacio_per_insercio(vector<double>& v) {
int n = v.size();
for (int i = 1; i < n; ++i) {
for (int j = i; j > 0 and v[j - 1] > v[j]; --j) {
swap(v[j - 1], v[j]);
@jordi-petit
jordi-petit / p1.cc
Last active April 26, 2018 07:29
AP1 2017-11-07 Ordenació per selecció
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cassert>
using namespace std;
double now() {
@jordi-petit
jordi-petit / p1.hs
Created November 6, 2017 13:25
LP 2017-11-06 Tipus
-- Joc de pedra paper o tisora
data Jugada = Pedra | Paper | Tisora
guanya :: Jugada -> Jugada -> Bool
Pedra `guanya` Tisora = True
Tisora `guanya` Paper = True
Paper `guanya` Pedra = True
_ `guanya` _ = False
@jordi-petit
jordi-petit / p1.cc
Last active April 26, 2018 07:29
AP1 2071-10-27
// Cerca lineal en un vector
#include <iostream>
#include <vector>
#include <cstdlib> // permet usar el rand() i el srand()
#include <ctime> // permet usar el time() i el clock()
using namespace std;
@jordi-petit
jordi-petit / p0.cc
Last active April 26, 2018 07:28
AP1 2017-10-25
// Dir si un text és palíndrom
#include <iostream>
#include <string>
using namespace std;
bool palindrom(const string& s) { // string s també estaria bé
int esq = 0;
@jordi-petit
jordi-petit / p0.md
Last active April 26, 2018 07:28
AP1 2017-10-24

Vectors

Un vector agrupa diferents valors d'un mateix tipus sota un uníc nom.

(FIGURA)

Constructors

  • vector v;
@jordi-petit
jordi-petit / p1.hs
Last active October 23, 2017 12:31
LP 2017-10-23
-- Diccionari de Strings a Ints amb funcions d'ordre superior
-- i valors per defecte.
type Dict = (String -> Int)
-- Versió 1
create :: Int -> Dict
create def = \key -> def
@jordi-petit
jordi-petit / p0.md
Last active January 24, 2025 13:48
Useful list functions in Haskell

Useful list functions in Haskell

head, last

  • Header:

    head, last :: [a] -> a
@jordi-petit
jordi-petit / p0.md
Last active March 20, 2018 09:14
Useful high order functions in Haskell

Useful high order functions in Haskell

flip

  • Header:

    flip :: (a -> b -> c) -> (b -> a -> c)
@jordi-petit
jordi-petit / p0.md
Last active February 28, 2018 15:35
LP 2017-10-16

Tipus

Int

Enters 32/64 bits amb complement a 2.

Exemples: 0, 3, 28, (- 55).

Operacions:

  • +