Oktatás
* Programozás 1 + feladatsor + GitHub oldal
* Szkriptnyelvek + feladatsor + quick link
Teaching
* Programming 1 (BI) ◇ exercises ◇ quick link
teaching assets
Félévek
* 2024/25/2 * archívum
Linkek
* kalendárium - munkaszüneti napok '20 * tételsorok * jegyzetek * szakdolgozat / PhD * ösztöndíjak * certificates * C lang. * C++ * C# * Clojure * D lang. * Java * Nim * Scala
[ edit | logout ] [ sandbox | passwd ]
|
See also dictionaries.
If your goal is to remove duplicates, check out this approach.
In D, there's no separate set data structure. However, you can use a dictionary for this purpose. The keys form the set, and you can use a dummy value in the dictionary (for instance a boolean true value). Keys in a dictionary are unique, thus they can represent a set.
(1) create a set
|
def main():
numbers = [2, 2, 5, 1, 1, 5]
my_set = set() # empty set
for n in numbers:
my_set.add(n)
print(my_set) # {1, 2, 5}
|
import std.stdio;
void main()
{
bool[int] d;
auto numbers = [2, 2, 5, 1, 1, 5];
foreach (n; numbers) {
d[n] = true;
}
writeln(d.byKey); // [5, 2, 1]
}
|
Union
(2) union
|
def main():
a = set([1, 2, 4, 5])
b = set([4, 5, 9, 15])
c = a.union(b)
print(c) # {1, 2, 4, 5, 9, 15}
print(a) # {1, 2, 4, 5}
print(b) # {9, 4, 5, 15}
|
import std.stdio;
import std.range; // chain()
bool[T] make_set(T)(const T[] numbers) pure
{
bool[T] result;
foreach (n; numbers)
{
result[n] = true;
}
return result;
}
// union is a keyword in D
bool[T] set_union(T)(const bool[T] a, const bool[T] b) pure
{
bool[T] result;
foreach (n; chain(a.byKey, b.byKey))
{
result[n] = true;
}
return result;
}
void main()
{
bool[int] a = make_set([1, 2, 4, 5]);
bool[int] b = make_set([4, 5, 9, 15]);
bool[int] c = a.set_union(b);
writeln(c.byKey); // [15, 5, 4, 2, 1, 9]
writeln(a.byKey); // [5, 4, 2, 1]
writeln(b.byKey); // [5, 4, 15, 9]
}
|
Intersection
(3) intersection
|
def main():
a = set([1, 2, 4, 5])
b = set([4, 5, 9, 15])
c = a.intersection(b)
print(c) # {4, 5}
print(a) # {1, 2, 4, 5}
print(b) # {9, 4, 5, 15}
|
import std.stdio;
bool[T] make_set(T)(const T[] numbers) pure
{
bool[T] result;
foreach (n; numbers)
{
result[n] = true;
}
return result;
}
bool[T] intersection(T)(const bool[T] a, const bool[T] b) pure
{
bool[T] result;
foreach (k; a.byKey)
{
if (k in b)
{
result[k] = true;
}
}
return result;
}
void main()
{
bool[int] a = make_set([1, 2, 4, 5]);
bool[int] b = make_set([4, 5, 9, 15]);
bool[int] c = a.intersection(b);
writeln(c.byKey); // [5, 4]
writeln(a.byKey); // [5, 4, 2, 1]
writeln(b.byKey); // [5, 4, 15, 9]
}
|
Difference
(4) difference
|
def main():
a = set([1, 2, 4, 5])
b = set([4, 5, 9, 15])
c = a.difference(b)
print(c) # {1, 2}
print(a) # {1, 2, 4, 5}
print(b) # {9, 4, 5, 15}
|
import std.stdio;
bool[T] make_set(T)(const T[] numbers) pure
{
bool[T] result;
foreach (n; numbers)
{
result[n] = true;
}
return result;
}
bool[T] difference(T)(const bool[T] a, const bool[T] b) pure
{
bool[T] result;
foreach (k; a.byKey)
{
if (k !in b)
{
result[k] = true;
}
}
return result;
}
void main()
{
bool[int] a = make_set([1, 2, 4, 5]);
bool[int] b = make_set([4, 5, 9, 15]);
bool[int] c = a.difference(b);
writeln(c.byKey); // [2, 1]
writeln(a.byKey); // [5, 4, 2, 1]
writeln(b.byKey); // [5, 4, 15, 9]
}
|
|
Blogjaim, hobbi projektjeim
* The Ubuntu Incident * Python Adventures * @GitHub * heroku * extra * haladó Python * YouTube listák
Debrecen | la France
[ edit ]
|