Finding minimum value from list - c#

How would i build an algorithm here in the most efficient way possible to find minimum value from list? I know the list hasnt done in the best way but, any ideas how to do ?
I have tried few ways but dont seem to get it work efficiently..
Thanks.
class MainClass
{
public class List
{
public int maxSize = 50;
public int MaxSize
{
get
{
return maxSize;
}
set
{
maxSize = value;
}
}
public int firstEmpty = 0;
public int FirstEmpty
{
get
{
return firstEmpty;
}
set
{
firstEmpty = value;
}
}
public int[] data;
public List()
{
data = new int[maxSize];
}
public int returnValueAtIndex(int i)
{
return data[i];
}
public void setValueAtIndex(int v, int i)
{
data[i] = v;
}
}
public static int FIRST(List L)
{
if (END(L) > 0)
return 0;
else
return -1;
}
public static int END(List L)
{
return L.FirstEmpty;
}
public static int NEXT(int p, List L)
{
if (p >= 0 && p < L.MaxSize && p < END(L))
return p+1;
else
return - 1;
}
public static int PREVIOUS(int p, List L)
{
if (p >= 0 && p < L.MaxSize && p <= END(L))
return p-1;
else
return -1;
}
public static int LOCATE (int x, List L)
{
int i = 0;
while (i<END(L) && RETRIEVE(i, L) != x)
{
i++;
}
if (i != END(L))
return i;
else
return -1;
}
public static int RETRIEVE(int p, List L)
{
if (p >= 0 && p < END(L))
return L.returnValueAtIndex(p);
else
return -1;
}
public static void INSERT(int x, int p, List L)
{
if (p >= 0 && p < L.MaxSize && p <= END(L))
{
if (p == END(L))
{
L.setValueAtIndex(x, p);
}
else
{
for (int i = END(L); i > p; i--)
{
L.setValueAtIndex(L.returnValueAtIndex(i - 1), i);
L.setValueAtIndex(x, p);
}
}
L.FirstEmpty = END(L) + 1;
}
else
Console.WriteLine("Alkiota ei voitu lisätä");
}
public void DELETE(int p, List L)
{
if (p >= 0 && p < END(L))
{
for (int i = p; i < p - 1; i++)
{
L.setValueAtIndex(L.returnValueAtIndex(i + 1), i);
}
L.FirstEmpty = END(L) - 1;
}
}
public void MAKENULL(List L)
{
L.FirstEmpty = 0;
}
public static void PRINT(List L)
{
Console.WriteLine("Listan sisältö:");
for (int i = 0; i < END(L); i++)
{
Console.Write(L.returnValueAtIndex(i) + " ");
}
Console.WriteLine();
}
public static void Main(string[] args)
{
List testilista = new List();
INSERT(2, END(testilista), testilista);
INSERT(7, END(testilista), testilista);
INSERT(9, END(testilista), testilista);
INSERT(12, END(testilista), testilista);
INSERT(9, END(testilista), testilista);
INSERT(38, END(testilista), testilista);
Console.WriteLine("testilista");
PRINT(testilista);
Console.ReadLine();
}
}
}

The easiest way to do that in C# is with LinQ:
var minValue = data.Min();
if you want the highest value:
var maxValue = data.Max();

Note: Answer is not specific to C#
Given an unordered list of numbers, the fastest way to find the smallest number in the list is to look at every element in the list.
var unorderedList = [5,4,3,2,6,7,-23,8,-64,2,0,6];
function findSmallest(anArray){
var lowest = anArray[0];
for(var i = 1; i < anArray.length; i++){
var num = anArray[i];
if(num < lowest){
lowest = num;
}
}
return lowest;
}
var smallest = findSmallest(unorderedList);
console.log(smallest); //prints -64
You can run the code here
hit the run button

I don't think this is the best option. For me there is two ways.
Sort your list by this code.
int valueMin = L.returnValueAtIndex(0);
for (int i = 0; i < END(L); i++)
{
//if the value of i is smaller than the value
if (valueMin < L.returnValueAtIndex(i))
{
//i become the min Value
valueMin = L.returnValueAtIndex(i);
}
}
Console.WriteLine(valueMin);
Console.Read();
Or in C# you can use Array.Sort
Array.Sort(L);
Console.WriteLine(L.returnValueAtIndex(0));
Console.Read();
I hope this will help you !

Related

C# Binary Tree, Max and Min Value

I wrote some code and have some problems, but the most important question is: why is the array not working with AddNode? I know I am close to solving this problem, but I hope for a little hint.
Next question: the code for Min and Max value is good. How can I move it to Class Node?
And the last question: how to make Class Depth for tree?
class Node
{
public Node LeftNode { get; set; }
public Node MiddleNode { get; set; }
public Node RightNode { get; set; }
public int Value { get; set; }
public void AddNode(int value)
{
if (value < this.Value)
{
if (LeftNode != null)
{
LeftNode.AddNode(value);
return;
}
LeftNode = new Node(value);
return;
}
if (value > Value)
{
if (RightNode != null)
{
RightNode.AddNode(value);
return;
}
RightNode = new Node(value);
return;
}
if (MiddleNode != null)
{
MiddleNode.AddNode(value);
return;
}
MiddleNode = new Node(value);
}
public Node(int value)
{
this.Value = value;
}
public override string ToString()
{
return $"Value: {Value}";
}
public string SortedString(Array arr)
{
Array.Sort(arr);
foreach (int val in arr)
{
Console.WriteLine(val);
}
return "";
}
public int ValueCount(int value)
{
if (value < Value)
{
if (LeftNode == null)
{
return -1;
}
return LeftNode.ValueCount(value);
}
if (value > Value)
{
if (RightNode == null)
{
return -1;
}
return RightNode.ValueCount(value);
}
if (MiddleNode != null)
{
return 1 + MiddleNode.ValueCount(value);
}
return 1;
}
internal int Next(int min, int max)
{
Random rnd = new Random();
return rnd.Next(min, max);
}
}
class Program
{
static void Main(string[] args)
{
var arr = new int[1000];
var rnd = new Node(1);
for (int i = 0; i < arr.Length; i++)
{
rnd.AddNode(arr[i]);
//Console.WriteLine(arr[i]);
}
for(int i = 0; i < arr.Length; i++)
{
arr[i] = rnd.Next(1, 100);
Console.WriteLine(arr[i]);
}
min = arr[0];
max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (min > arr[i])
min = arr[i];
if (max < arr[i])
max = arr[i];
}
Console.WriteLine("największa liczba" + " " + max);
Console.WriteLine("najmniejsza liczba" + " " + min);
Array.Sort(arr);
foreach (int value in arr)
{
Console.WriteLine(value);
}
Console.WriteLine(rnd.ValueCount(6));
Console.WriteLine(rnd.ToString());
}
}
To be honest - your code is a bit confusing.
first of all - why do you have a pointer (ref) to a middle node ? A binary tree defines each node to have at most 2 children (left \ right).
If you wish to handle duplicate values you can either choose a strategy that defines one of the children to also includes equals, or better use ref count (e.g.: each node has both a value and a counter for this value).
re Min \ Max - if your tree is a BST (which it seems to be the case), you can easily implement this through simple tree traversing either to the left (for Min value) or to the right (for max value).

getting a performance hit for nested for loop in C#

I have an array of string, a total of(100k). I need to check if the same string has occurred previously, if it occurs all I have to do is return that string. I have written the code using nested for loops, but unfortunately I am getting bad performance. It takes 1.9 mins to process the function correctly for (string[100K]) whereas I am expecting it to finish it within a couple of seconds.
My concern is not the logic. My concern is the LOOP. How to increase the efficiency of the loop.
public string StartMatchingProcess(string[]inputArray)
{
string[] stringArray = inputArray;
string result = string.Empty;
for (long i = 0; i < stringArray.Length; i++)
{
for (long j = 0; j <= i; j++)
{
if(i == j) break;
if (IsPrefix(stringArray[i], stringArray[j]))
{
return stringArray[i];
}
}
}
Console.WriteLine("GOOD SET");
return result;
}
public bool IsPrefix(string string1,string string2)
{
if (AreString1ValuesValid(string1, string2))
{
if (string1 == string2.Substring(0, string1.Length))
{
Console.WriteLine("BAD SET");
Console.WriteLine(string1);
return true;
}
}
else if (AreString2ValuesValid(string1, string2))
{
if (string2 == string1.Substring(0, string2.Length))
{
Console.WriteLine("BAD SET");
Console.WriteLine(string1);
return true;
}
}
return false;
}
public bool AreString1ValuesValid(string string1, string string2)
=> string1.Length <= string2.Length;
public bool AreString2ValuesValid(string string1, string string2)
=> string2.Length <= string1.Length;
Sort the initial array, and you can check neighbors only:
public string StartMatchingProcess(string[] inputArray) {
if (null == inputArray)
throw new ArgumentNullException(nameof(inputArray));
string[] sorted = inputArray.OrderBy(item => item).ToArray();
for (int i = 1; i < sorted.Length; ++i) {
string prior = sorted[i - 1];
string current = sorted[i];
if (current.StartsWith(prior))
return prior;
}
return "";
}
So, you'll have O(n * log(n)) time complexity vs. O(n**2) (initial solution)
It's really bad idea to use nested loops for this task because you have O(n*n) complexity for the answer and need to make 10.000.000.000 calls of Substring() for 100k array.
There is a specific structures for strings. For example, you can use Trie:
public string StartMatchingProcess(string[] inputArray)
{
var trie = new Trie();
foreach(var w in inputArray)
trie.AddWord(w);
foreach(var w in inputArray)
if(trie.HasPrefix(w) || trie.HasWord(w)
return w;
return string.Empty;
}
If you are just trying to determine if your array has duplicate string values, consider LINQ to get the count of the occurences.
string[] arrayTest = new string[] { "hello", "hello", "world"};
string myValue = "hello";
var stringCount = arrayTest.Where(n => n == myValue).Count();
if (stringCount > 1) return myValue;
In the above, we check to see if "hello" is in the array more than once, and if it is, we return it.
Here is a complete solution using linq.
public class Node
{
public char letter { get; }
public int Index { get; set; }
public List<Node> ChildList { get; set; } = new List<Node>();
public Node(char item, int index)
{
Index = index;
letter = item;
}
}
public class NoPrefixSet
{
public Dictionary<char, Node> ParentNode { get; set; } = new Dictionary<char, Node>();
public string GenerateNodes(string[] inputArray)
{
for (int i = 0; i < inputArray.Length; i++)
{
if (IsWordPrefix(inputArray[i]))
{
Console.WriteLine("BAD SET");
Console.WriteLine(inputArray[i]);
return inputArray[i];
}
}
Console.WriteLine("Good Set");
return "Good Set";
}
private void InsertNodeInParent(char item)
=> ParentNode.Add(item, new Node(item, 0));
private bool IsWordPrefix(string word)
{
//Check parent
Node parentNode = null;
bool hasNotInserted = false;
int similarCounter = 0;
if (!ParentNode.Any(a => a.Key == word[0]))
{
InsertNodeInParent(word[0]);
}
parentNode = ParentNode.Where(a => a.Key == word[0]).FirstOrDefault().Value;
for (int letterIndex = 0; letterIndex < word.Length; letterIndex++)
{
if (!parentNode.ChildList.Any(a => a.letter == word[letterIndex]))
{
parentNode.ChildList.Add(new Node(word[letterIndex], letterIndex));
}
else
{
if (!parentNode.ChildList.Where(a => a.letter == word[letterIndex]).First().ChildList.Any() || word.Length == letterIndex+1)
{
if (similarCounter == letterIndex)
return hasNotInserted = true;
}
similarCounter++;
}
parentNode = parentNode.ChildList.Where(a => a.letter == word[letterIndex] && a.Index == letterIndex).First();
}
return hasNotInserted;
}
public void ReadInput()
{
long data = Convert.ToInt64(Console.ReadLine());
string[] stringArray = new string[data];
for (long i = 0; i < data; i++)
{
stringArray[i] = Console.ReadLine();
}
GenerateNodes(stringArray);
}
}

how to to multiply and divide in a static stack?

This is the static array I have been given in making a RPN calculator. From this code the RPN calculator adds and subtracts. Now I need to extend my code to multiply and divide but I cant I don't know how.
public class IntStack
{
private const int maxsize = 10;
private int top = 0;
private int[] array = new int[maxsize];
public void Push(int value)
{
array[top++] = value;
}
public int Pop()
{
return array[--top];
}
public int Peek()
{
return array[top - 1];
}
public bool IsEmpty()
{
return top == 0;
}
public bool IsFull()
{
return top == maxsize;
}
public string Print()
{
StringBuilder output = new StringBuilder();
for (int i = top - 1; i >= 0; i--)
output.Append(array[i] + Environment.NewLine);
return output.ToString();
}
}
Here are some methods you can add to your IntStack class that will perform the multiply and division operations. I've added minimal error checking.
public void Multiply()
{
if (array.Length < 2)
return;
var factor1 = Pop();
var factor2 = Pop();
Push(factor1 * factor2);
}
public void Divide()
{
if (array.Length < 2)
return;
var numerator = Pop();
var divisor = Pop();
if (divisor == 0) { // Return stack back to original state.
Push(divisor);
Push(numerator);
return;
}
Push(numerator / divisor);
}

permutation logic not working on List<int>

public class Solution
{
IList<IList<int>> ll=new List<IList<int>>();
public IList<IList<int>> PermuteUnique(int[] nums)
{
PermutationUntil(nums, nums.Length, 0);
return ll;
}
public void PermutationUntil(int[] l, int n, int index)
{
if(index >= n)
{
ll.Add(l.ToList());
return;
}
for (int i = index; i < n; i++)
{
if(containsDuplicate(l, index, i))
{
Swap(l,index,i);
PermutationUntil(l, n, index + 1);
Swap(l, index, i);
}
}
}
private void Swap(int[] l, int x, int y)
{
int tem = l[x];
l[x] = l[y];
l[y] = tem;
}
private bool containsDuplicate(int[] arr, int start, int end)
{
for (int i = start; i < end; i++)
{
if (arr[i] == arr[end])
{
return false;
}
}
return true;
}
}
PermuteUnique function return all unique permutations, but if i replace array int[] nums with a List<int> in PermuteUnique then below code does not produce expected result.
My guess is something to do with indexer but not sure. Could someone please explain why?

C# Linked Lists (adding an element to a defined position)

Our teacher asked us to implement the class LinkedList. I was able to implement everything he asked for. But he gave us a bonus question that I was not able to solving.
He asked us to implement this fonction:
public void add(float x, int pos)
{
// Add x at the position pos, pos = 0 refer to the first element.
}
He also demanded that his code verifies the following UnitTest:
public class UnitTest1
{
private MyList l;
public UnitTest1()
{
l = new MyList();
for (int i = 0; i < 10; ++i)
{
l.add(i * i);
}
for (int i = 0; i < 10; ++i)
{
l.add(i * i);
}
}
[TestMethod]
public void TestAdd()
{
Assert.AreEqual(l.count(), 20);
}
[TestMethod]
public void TestGet()
{
for (int i = 0; i < 10; ++i)
{
Assert.AreEqual(l.get(i), (9 - i) * (9 - i));
}
for (int i = 10; i < 20; ++i)
{
Assert.AreEqual(l.get(i), (19 - i) * (19 - i));
}
}
[TestMethod]
public void TestFind()
{
int k;
for (int i = 0; i < 100; ++i)
{
for (k = 0; k < 10; ++k)
{
if (k * k == i)
{
Assert.AreEqual(l.find(i), true);
break;
}
}
if (k == 10)
{
Assert.AreEqual(l.find(i), false);
}
}
}
[TestMethod]
public void TestStats()
{
Assert.AreEqual(l.max(), 81);
float s = 0;
for (int i = 0; i < 10; ++i)
{
s += i * i + i * i;
}
Assert.AreEqual(l.sum(), s);
Assert.AreEqual(l.average(), s / 20);
}
[TestMethod]
public void TestCountValue()
{
MyList l1 = new MyList();
for (int i = 0; i < 10; ++i)
{
l1.add(i);
l1.add(i * i);
}
Assert.AreEqual(l1.count(-1), 0);
Assert.AreEqual(l1.count(0), 2);
Assert.AreEqual(l1.count(1), 2);
Assert.AreEqual(l1.count(2), 1);
Assert.AreEqual(l1.count(3), 1);
Assert.AreEqual(l1.count(4), 2);
Assert.AreEqual(l1.count(5), 1);
Assert.AreEqual(l1.count(6), 1);
Assert.AreEqual(l1.count(7), 1);
Assert.AreEqual(l1.count(8), 1);
Assert.AreEqual(l1.count(9), 2);
Assert.AreEqual(l1.count(10), 0);
Assert.AreEqual(l1.count(16), 1);
}
[TestMethod]
public void TestRemoveFirst()
{
MyList l1 = new MyList();
for (int i = 0; i < 10; ++i)
{
l1.add(i);
l1.add(i * i);
}
Assert.AreEqual(l1.count(81), 1);
l1.removeFirst();
Assert.AreEqual(l1.count(), 19);
Assert.AreEqual(l1.count(81), 0);
Assert.AreEqual(l1.count(-1), 0);
Assert.AreEqual(l1.count(0), 2);
Assert.AreEqual(l1.count(1), 2);
Assert.AreEqual(l1.count(2), 1);
Assert.AreEqual(l1.count(3), 1);
Assert.AreEqual(l1.count(4), 2);
Assert.AreEqual(l1.count(5), 1);
Assert.AreEqual(l1.count(6), 1);
Assert.AreEqual(l1.count(7), 1);
Assert.AreEqual(l1.count(8), 1);
Assert.AreEqual(l1.count(9), 2);
Assert.AreEqual(l1.count(10), 0);
Assert.AreEqual(l1.count(16), 1);
}
[TestMethod]
public void TestInsert()
{
MyList l1 = new MyList();
for (int i = 9; i >= 0; --i)
{
l1.add(i);
}
for (int i = 0; i <= 10; ++i)
{
l1.add(i, 2 * i);
}
for (int i = 0; i < 10; ++i)
{
Assert.AreEqual(l1.get(2 * i), i, "i=" + i);
Assert.AreEqual(l1.get(2 * i + 1), i);
}
Assert.AreEqual(l1.get(20), 10);
}
}
That's what I was capable of:
public class MyList
{
class Element
{
public float value;
public Element next;
}
Element first;
public MyList()
{
first = null;
}
public void add(float x)
{
Element e = new Element();
e.value = x;
e.next = first;
first = e;
}
public float get(int i)
{
if (first == null)
{
throw new Exception("Empty list... no elements inside");
}
Element tmp = first;
for (int j = 0; j < i; ++j)
{
tmp = tmp.next;
if (tmp == null)
{
throw new Exception("...");
}
}
return tmp.value;
}
public void print()
{
Element e = first;
while (e != null)
{
Console.WriteLine(e.value);
e = e.next;
}
}
public bool find(float x)
{
Element e = first;
while (e != null)
{
if (e.value == x)
{
return true;
}
e = e.next;
}
return false;
}
public float max()
{
float G = 0;
for (Element e = first; e != null; e = e.next)
{
if (e.value > G)
{
G = e.value;
}
}
return G;
}
public int count()
{
Element e = first;
int c = 0;
while (e != null)
{
c++;
e = e.next;
}
return c;
}
public int count(float x)
{
int c = 0;
for (Element e = first; e != null; e = e.next)
{
if (e.value == x)
{
c++;
}
}
return c;
}
public float sum()
{
float S = 0;
for (Element e = first; e != null; e = e.next)
{
S += e.value;
}
return S;
}
public float average()
{
return sum() / count();
}
public void removeFirst()
{
Element e = first;
first = e.next;
}
public void add(float x, int pos)
{
//I have absolutely no idea how to implement this fonction.
}
}
It will be similar to the code in the get() method.
Break your problem down into smaller problems
Does pos = 0?
if so, create a new root and point it to the old root
if not, loop (pos) times and create a new element. Then set the next property of the new element to the next property of the current element. Set the current element next property to your new element
.
public void add(float x, int pos)
{
if(pos == 0)
{
// create a new root and point the new root to the existing root
}
else
{
Element tmp = first;
for(int i = 0; i < pos; i++)
{
tmp = tmp.next;
}
// create new element
// set new element next property to tmp.next
// set tmp.next to new element
}
}

Categories

Resources