Dcexercises
Dcexercises
Student ID � 2203091121
Implementation:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3, 4, 1, 2, 3, 1, 5, 6, 5, 4, 3, 2, 1 };
return occurrences;
}
}
Exercise 03
Write a program that counts how many times each word from a given text file
words.txt appears in it. The result words should be ordered by their number of
occurrences in the text.
Example: "This is the TEXT. Text, text, text – THIS TEXT! Is this the text?"
Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string filePath = "words.txt"; // Specify the path to your text file
try
{
string text = File.ReadAllText(filePath);
var wordOccurrences = CountWordOccurrences(text);
Console.WriteLine("Result:");
foreach (var kvp in wordOccurrences.OrderByDescending(pair =>
pair.Value))
{
Console.WriteLine($"{kvp.Key} à {kvp.Value}");
}
}
catch (FileNotFoundException)
{
Console.WriteLine($"Error: File '{filePath}' not found.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
return wordOccurrences;
}
}
The example demonstrates the use of File.ReadAllText to read the entire contents of
a text file.
Exercise 05
Implement a hash-table, maintaining triples (key1, key2, value) and allowing quick
search by the pair of keys and adding of triples.
Solution approach
Solution
using System;
using System.Collections.Generic;
public TripleHashTable()
{
tripleDictionary = new Dictionary<(TKey1, TKey2), TValue>();
}
class Program
{
static void Main()
{
TripleHashTable<string, int, double> tripleTable = new
TripleHashTable<string, int, double>();
// Adding triples
tripleTable.Add("John", 1, 75.5);
tripleTable.Add("Alice", 2, 89.2);
tripleTable.Add("Bob", 1, 92.7);
Exercise 07
Solution
using System;
public CuckooHashTable()
{
keys1 = new TKey[TableSize];
keys2 = new TKey[TableSize];
values = new TValue[TableSize];
size = 0;
}
if (keys1[index1] == null)
{
keys1[index1] = key;
values[index1] = value;
size++;
return;
}
if (keys2[index2] == null)
{
keys2[index2] = key;
values[index2] = value;
size++;
return;
}
TKey displacedKey;
TValue displacedValue;
if (i % 2 == 0)
{
displacedKey = keys1[index1];
displacedValue = values[index1];
keys1[index1] = key;
values[index1] = value;
}
else
{
displacedKey = keys2[index2];
displacedValue = values[index2];
keys2[index2] = key;
values[index2] = value;
}
key = displacedKey;
value = displacedValue;
}
return default(TValue);
}
class Program
{
static void Main()
{
CuckooHashTable<string, int> cuckooHashTable = new CuckooHashTable<string,
int>();
cuckooHashTable.Add("One", 1);
cuckooHashTable.Add("Two", 2);
cuckooHashTable.Add("Three", 3);
cuckooHashTable.Add("Four", 4);
cuckooHashTable.Add("Five", 5);
cuckooHashTable.Add("Six", 6);
Implement the data structure "Set" in a class HashedSet<T>, using your class
HashTable<K, T> to hold the elements. Implement all standard set operations like
Add(T), Find(T), Remove(T), Count, Clear(), union and intersect.
.
Solution
using System;
using System.Collections.Generic;
using System.Linq;
public HashedSet()
{
hashTable = new HashTable<T, T>();
}
return unionSet;
}
return intersectSet;
}
class Program
{
static void Main()
{
HashedSet<int> set1 = new HashedSet<int>();
set1.Add(1);
set1.Add(2);
set1.Add(3);
// Union of sets
HashedSet<int> unionSet = set1.Union(set2);
Console.WriteLine("Union of sets: " + string.Join(", ", unionSet));
// Intersection of sets
HashedSet<int> intersectSet = set1.Intersect(set2);
Console.WriteLine("Intersection of sets: " + string.Join(", ",
intersectSet));
// Removing an element
set1.Remove(2);
// Count of elements
Console.WriteLine("Count of elements in set1: " + set1.Count);
Exercise 11
Solution
using System;
using System.Collections;
using System.Collections.Generic;
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public T Min()
{
if (elements.Count == 0)
throw new InvalidOperationException("The set is empty.");
return elements[0];
}
public T Max()
{
if (elements.Count == 0)
throw new InvalidOperationException("The set is empty.");
return index;
}
}
class Program
{
static void Main()
{
// Example usage of TreeMultiSet
TreeMultiSet<int> treeMultiSet = new TreeMultiSet<int>();
treeMultiSet.Add(3);
treeMultiSet.Add(1);
treeMultiSet.Add(2);
treeMultiSet.Add(3);
treeMultiSet.Add(1);
Exercise 13
* We are given a sequence P containing L integers L (1 < L < 50,000) and a number
N. We call a “lucky sub-sequence within P” every sub-sequence of integers from P
with a sum equal to N.
- [1, 2, 1, -1, 2]
- [3, -1, 1, 2]
- [2, 3, -1, 1]
- [1, 1, 2, 1]
- [1, -1, 2, 3]
- [1, -1, 2, 3]
- [-1, 1, 2, 3]
- [5, 1, -1]
- [2, 3]
- [2, 3]
- [2, 3]
- [5]
Solution Approach
This program defines a LuckySubsequences class with a Main method showcasing the
example sequence P and number N. The FindLuckySubsequences method generates all
sub-sequences of P and filters those with a sum equal to N. It then orders the
lucky sub-sequences by length and elements. The GetFirst10Elements method flattens
the list of sequences and takes the first 10 elements. The result is displayed in
the console.
Solution
using System;
using System.Collections.Generic;
using System.Linq;
class LuckySubsequences
{
static void Main()
{
// Example sequence P and number N
int[] P = { 1, 1, 2, 1, -1, 2, 3, -1, 1, 2, 3, 5, 1, -1, 2, 3 };
int N = 5;
if (sum == N)
{
luckySubsequences.Add(subsequence);
}
}
return luckySubsequences;
}
return result;
}
}
Testing & Output:
- Shortest path from vertex 0 to vertex 5:
0 1 3 5