Skip to content

Commit 1107b61

Browse files
Merge branch 'mouredev:main' into main
2 parents f7fd47f + 2e6b3aa commit 1107b61

File tree

4 files changed

+484
-0
lines changed

4 files changed

+484
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
3+
web oficial:
4+
https://harbour.github.io/
5+
6+
Distribución utilizada:
7+
https://www.hmgextended.com/
8+
9+
Comentario en varias lineas
10+
11+
*/
12+
13+
// Comentario en una linea
14+
15+
** Comentario en una linea
16+
17+
#define CONSTANTE "Valor no Cambia"
18+
19+
function main()
20+
21+
local Entero, Flotante, Logico
22+
local Cadena
23+
local Fecha
24+
25+
Cadena := "Hola harbour"
26+
Entero := 1
27+
Flotante := 10.25
28+
Fecha := ctod('04-26-2024')
29+
Logico := .T.
30+
Logico := .F.
31+
32+
/*
33+
Se puede crear variables con tipado dinámico sin especificar su alcance, pero no es
34+
recomendable, el compilador identifica el tipo de datos automáticamente .
35+
Es case insensitive
36+
ValorCualquiera := "Cadena" Valorcualquiera := 12 valorCualquiera := 12.45
37+
es la misma variable.
38+
*/
39+
ValorCualquiera := "Cadena"
40+
Valorcualquiera := 12
41+
valorCualquiera := 12.45
42+
43+
? 'Hola Harbour'
44+
45+
return NIL
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
open Printf
2+
3+
(*****************************************************************************)
4+
(* *)
5+
(* Set Data Structure *)
6+
(* *)
7+
(* Sets are a {e finite collection} of {b unique} data with roots in *)
8+
(* mathematics. The way sets are implemented allow for logarithmic-time *)
9+
(* access at the cost of slower write times than an array. Sets don't *)
10+
(* guarantee element order and they can't be repeated, they must be unique *)
11+
(* and depending on the language, of same type (mostly). *)
12+
(* OCaml doesn't let you have sets of different types and the standard *)
13+
(* library implements them as functors, meaning, you need to pass a module *)
14+
(* to the [Set.Make] functor in order to get back a new module that is *)
15+
(* bound to the type that the passed module represent. Among the most *)
16+
(* popular modules to pass are [String], [Float], [Int], and [Char]. *)
17+
(* *)
18+
(* Set operations are: *)
19+
(* - Union (A ∪ B): A set composed of all the elements from sets A and B. *)
20+
(* - Intersection (A ∩ B): Elements of A and B that appear on both sets. *)
21+
(* - Symmetric difference (A Δ B): All the elements of A and B that don't *)
22+
(* appear on both at the same time. Ex: {1,2,3} Δ {4,2,6} = {1,3,4,6}. *)
23+
(* - Difference (A - B): The elements of A without the ones that also *)
24+
(* appear on B; basically, subtration. Ex: {9,1,4,7} - {1,7,5,2} = {9,4}. *)
25+
(* *)
26+
(*****************************************************************************)
27+
28+
module IntSet = Set.Make (Int)
29+
30+
let show_int_set s =
31+
if IntSet.is_empty s
32+
then ""
33+
else
34+
IntSet.to_list s
35+
|> List.map string_of_int
36+
|> String.concat ", "
37+
|> sprintf "{ %s }"
38+
;;
39+
40+
let tap str set =
41+
printf str (show_int_set set);
42+
set
43+
;;
44+
45+
(** [symm_diff a b] is the symmetric difference between set [a] and set [b].
46+
A symmetric difference can be described as the difference between the
47+
union of [a] and [b], and their intersection. *)
48+
let symm_diff a b = IntSet.diff (IntSet.union a b) (IntSet.inter a b)
49+
50+
let _ =
51+
let b_set = IntSet.of_list [ 5; 10; 6; 0 ] in
52+
let a_set =
53+
IntSet.empty
54+
|> tap "Empty set: %s\n"
55+
|> IntSet.add 8
56+
|> tap "After adding 1 element: %s\n"
57+
|> IntSet.add_seq (List.to_seq [ 1; 5; 10 ])
58+
(* NOTE: Adding elements to a certain position is irrelevant in an OCaml set
59+
as they can't guarantee insertion order since the elements get sorted. *)
60+
|> tap "After adding multiple elements: %s\n"
61+
|> IntSet.remove 8
62+
|> tap "After removing the number 8: %s\n"
63+
in
64+
printf
65+
"Union: A (%s) ∪ B (%s) = %s\n"
66+
(show_int_set a_set)
67+
(show_int_set b_set)
68+
(IntSet.union a_set b_set |> show_int_set);
69+
printf
70+
"Intersection: A (%s) ∩ B (%s) = %s\n"
71+
(show_int_set a_set)
72+
(show_int_set b_set)
73+
(IntSet.inter a_set b_set |> show_int_set);
74+
printf
75+
"Difference: B (%s) - A (%s) = %s\n"
76+
(show_int_set b_set)
77+
(show_int_set a_set)
78+
(IntSet.diff b_set a_set |> show_int_set);
79+
printf
80+
"Symmetric Difference: B (%s) Δ A (%s) = %s\n"
81+
(show_int_set a_set)
82+
(show_int_set b_set)
83+
(symm_diff a_set b_set |> show_int_set);
84+
printf
85+
"Is 0 an element of set B = %s? %b\n"
86+
(show_int_set b_set)
87+
(IntSet.mem 0 b_set);
88+
printf "Cardinality (elt. count) of set A: %d\n" (IntSet.cardinal a_set)
89+
;;
90+
91+
(* Output of [dune exec reto18]
92+
93+
Empty set: ∅
94+
After adding 1 element: { 8 }
95+
After adding multiple elements: { 1, 5, 8, 10 }
96+
After removing the number 8: { 1, 5, 10 }
97+
Union: A ({ 1, 5, 10 }) ∪ B ({ 0, 5, 6, 10 }) = { 0, 1, 5, 6, 10 }
98+
Intersection: A ({ 1, 5, 10 }) ∩ B ({ 0, 5, 6, 10 }) = { 5, 10 }
99+
Difference: B ({ 0, 5, 6, 10 }) - A ({ 1, 5, 10 }) = { 0, 6 }
100+
Symmetric Difference: B ({ 1, 5, 10 }) Δ A ({ 0, 5, 6, 10 }) = { 0, 1, 6 }
101+
Is 0 an element of set B = { 0, 5, 6, 10 }? true
102+
Cardinality (elt. count) of set A: 3
103+
*)
104+
105+
(*****************************************************************************)
106+
(* *)
107+
(* Implementation and Ordering *)
108+
(* *)
109+
(* Sets can be implemented in 2 ways: with a tree-like data structure and *)
110+
(* using a hash table. A binary properly balanced binary tree can give us *)
111+
(* logarithmic access times and the traversal is done in orderly manner. In *)
112+
(* OCaml's standard lib, [Set] is implemented with a binary tree and that's *)
113+
(* why the module we pass to the functor must have a comparator function *)
114+
(* called [compare]. If we were to implement a set that remembers insertion *)
115+
(* order we could add both to a linked list and a b-tree or hash bucket. *)
116+
(* It's worth noting that if we wanted a data structure to add stuff to *)
117+
(* either end (front/rear) or at a specific position then maybe we're better *)
118+
(* off with an array or a linked list which can be converted to a set at a *)
119+
(* later time anyway (getting rid of duplicates). *)
120+
(* *)
121+
(*****************************************************************************)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 1. Crear una lista
2+
lista = ['elemento1', 'elemento2', 'elemento3']
3+
print(f'1.: {lista}')
4+
5+
# 2. Añadir un elemento al final de la lista
6+
lista.append('elemento4')
7+
print(f'2.: {lista}')
8+
9+
# 3. Añadir un elemento al principio de la lista
10+
lista.insert(0, 'elemento0')
11+
print(f'3.: {lista}')
12+
13+
# 4. Añadir varios elementos en bloque al final de la lista
14+
lista.extend(['elemento5', 'elemento6', 'elemento7'])
15+
print(f'4.: {lista}')
16+
17+
# 5. Añadir varios elementos en bloque en una posición concreta de la lista
18+
lista[2:2] = ['elemento1.5', 'elemento1.75']
19+
print(f'5.: {lista}')
20+
21+
# 6. Eliminar un elemento en una posición concreta de la lista
22+
del lista[1]
23+
print(f'6.: {lista}')
24+
25+
# 7. Actualizar el valor de un elemento en una posición concreta de la lista
26+
lista[0] = 'elemento0.5'
27+
print(f'7.: {lista}')
28+
29+
# 8. Comprobar si un elemento está en la lista
30+
if 'elemento0.5' in lista:
31+
print('elemento0.5 está en la lista')
32+
print(f'8.: {lista}')
33+
34+
# 9. Eliminar todo el contenido de la lista
35+
lista.clear()
36+
print(f'9.: {lista}')
37+
38+
print('DIFICULTAD EXTRA')
39+
40+
# 1. Crear dos conjuntos de datos
41+
conjunto1 = set(['a', 'b', 'c', 'd'])
42+
conjunto2 = set(['c', 'd', 'e', 'f'])
43+
44+
# 2. Realizar la unión de los dos conjuntos
45+
union = conjunto1.union(conjunto2)
46+
print(f'Unión: {union}')
47+
48+
# 3. Realizar la intersección de los dos conjuntos
49+
interseccion = conjunto1.intersection(conjunto2)
50+
print(f'Intersección: {interseccion}')
51+
52+
# 4. Realizar la diferencia de los dos conjuntos
53+
diferencia = conjunto1.difference(conjunto2)
54+
print(f'Diferencia: {diferencia}')
55+
56+
# 5. Realizar la diferencia simétrica de los dos conjuntos
57+
diferencia_simetrica = conjunto1.symmetric_difference(conjunto2)
58+
print(f'Diferencia simétrica: {diferencia_simetrica}')

0 commit comments

Comments
 (0)