-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.pl
86 lines (66 loc) · 2.21 KB
/
12.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Encabezado
% C# solucion / Python
%------------------------------------------------------------------------------
% Módulo: 50_programas
% Propósito: Elaborar 50 programas en Prolog
%
% Autor: Diaz Morales Katherine Giselle
% Fecha: 21 de octubre de 2024
%
% Descripción:
% Elaborar 50 programas en Prolog, usar templete.
--------------------------------------------------------------------------------------
- - - - - PROLOG - - - - -
12. Decodificar una lista codificada mediante Run-Length.
% Decodifica una lista codificada mediante Run-Length.
% Expande cada par (N, X) en una lista con N elementos X.
decode([], []).
decode([[N,X]|T], R) :- decode(T, R1), expand(X, N, E), append(E, R1, R).
% Expande un elemento X en una lista de longitud N.
expand(_, 0, []).
expand(X, N, [X|T]) :- N > 0, N1 is N - 1, expand(X, N1, T).
- - - - - C# - - - - -
using System;
using System.Collections.Generic;
class Program
{
// Función que decodifica una lista codificada mediante Run-Length
static List<T> Decode<T>(List<(int, T)> encodedList)
{
List<T> result = new List<T>();
foreach (var pair in encodedList)
{
var expanded = Expand(pair.Item2, pair.Item1);
result.AddRange(expanded);
}
return result;
}
// Función que expande un elemento X en una lista con N elementos X
static List<T> Expand<T>(T element, int count)
{
List<T> expandedList = new List<T>();
for (int i = 0; i < count; i++)
{
expandedList.Add(element);
}
return expandedList;
}
static void Main()
{
// Lista codificada usando Run-Length
List<(int, char)> encodedInput = new List<(int, char)>
{
(3, 'a'), (2, 'b'), (1, 'c'), (4, 'd')
};
// Decodificar la lista
List<char> decodedOutput = Decode(encodedInput);
// Imprimir resultado
Console.WriteLine("Encoded Input: ");
foreach (var pair in encodedInput)
{
Console.WriteLine($"({pair.Item1}, {pair.Item2})");
}
Console.WriteLine("\nDecoded Output: " + string.Join(", ", decodedOutput));
Console.ReadLine();
}
}