C# Tutorial - Solving the turing.com coding challenge practise tests using C#

preview_player
Показать описание

Source Code:

Рекомендации по теме
Комментарии
Автор

You have done a great job here, my brother. One thing I got from your presentation is the principle of separation of concern and understanding the actual questions or requirements is essential. I never thought I could access the last two indexes; I only knew of one in a list. Thanks for the method, and I am grateful.

davidodediran
Автор

You have done a great job, Your video is helping me to prepare for a C# challenge, Thank you.

int sum = 0;

ArrayList arrList = new ArrayList();

for (int i=0; i< ops.Length;i++)
{
int Num = 0;
sum = 0;
if (int.TryParse(ops[i], out Num))
{
arrList.Add(Num);
}
else if(ops[i]=="+")
{
sum =(int) arrList[arrList.Count - 1];
sum += (int)arrList[arrList.Count - 2];
arrList.Add(sum);
}
else if (ops[i] == "D")
{
sum = 2*(int)arrList[arrList.Count - 1];
arrList.Add(sum);
}
else if (ops[i] == "C")
{
- 1);
}
}
sum = 0;
for (int i = 0; i < arrList.Count; i++)
{
sum += (int)arrList[i];
}

return sum;

kaleemabdul
Автор

Some tips: On the first problem It's possible to avoid the unnecessary nested loops: You can solve the problem with just one loop, which will give an O(n) algorithm. Also, try to use more meaningful names for variables. Don't forget to handle invalid inputs: It's not a good strategy to consider any input other than C, D, or + as an integer.

ricardosilva
Автор

I liked the solution very much
I've also tried one.

public int CalPointsFunc(string[] args)
{
int output;
List<int> record = new List<int>();
foreach(string arg in args)
{
if (Int32.TryParse(arg, out output))
{

}
else
{
if (arg == "C")
record.RemoveAt(record.Count - 1);
else if (arg == "D")
record.Add(record.Last() * 2);
else if (arg == "+")
- 2] + record.Last());
}
}
return record.Sum();
}

Thanks

lifeofintelligence
Автор

Wow! happy to discover your channel. I am preparing to take a Javascript live coding challenge, at least you have given me a hint of what to expect.

dr.kenedy-oltitia
Автор

Wow!!
Thànk you so much for this. Nice content 👌
The background music is perfect for learning.

tracybufazi
Автор

I really appreciate the background music. Helps my attention

raymondjavel
Автор

Can't wait for the full C# course. Im interested in learning that language

kennethihemadu
Автор

Why is the music playing in the background when doing a fully concentrated work?

zeynephan
Автор

I recreated your solution using much shorter syntax.
Here it is ...
class Solution
{
public int CalPoints(string[] ops)
{
var opsList = new List<int>();
var count = 0;
foreach(var op in ops)
{
if (op.Equals("+"))
{
var val = opsList[opsList.Count -1] + opsList[opsList.Count - 2];
opsList.Add(val);
}
else if (op.Equals("D"))
{
var val = opsList[opsList.Count - 1] * 2;
opsList.Add(val);
}
else if (op.Equals ("C"))
{
- 1);
}
else
{
opsList.Add(int.Parse(op));
}
}
foreach(var op in opsList)
{
count += op;
}
return count;
}
}
class CalPoints
{
public static void Main(string[] args)
{
var solution = new Solution();
var space = new Char[] { ' ' };
string[] ops =
int output = solution.CalPoints(ops);

}
}

nelsonmudanya
Автор

This is great content. Looking forward to try the live coding challenge in Kotlin

frankjames
Автор

Im looking forward to develop my skills in programming from your channel.Thanks

okechukwuihechukwu
Автор

why didnt u make integer list you couldve only needed to parse once while adding right?

lordgreat
Автор

I try to write before watching your solution. Still refactor should be done. Here it is:

public int Calculate(string[] scores)
{
const string CANCEL = "C";
const string DOUBLE = "D";
const string ADD = "+";

if (scores == null || scores.Length == 0)
{
return 0;
}

var records = new List<int>();
foreach (var score in scores)
{
switch (score)
{
case CANCEL:
if(records.Count == 0) throw new Exception($"{CANCEL} operator cannot be used first!");
- 1);
continue;
case DOUBLE:
if (records.Count == 0) throw new Exception($"{DOUBLE} operator cannot be used first!");
records.Add(records[^1] * 2);
continue;
case ADD:
if (records.Count < 2) throw new Exception($"{ADD} operator cannot be used for first two values!");
records.Add(records[^1] + records[^2]);
continue;
default:
if (!int.TryParse(score, out int number))
{
throw new ArgumentException($"Unknown score specifier: {score}");
}
records.Add(number);
continue;
}
}

return records.Sum();
}

huseyinsarioglu
Автор

I did something like that yesterday, un VS works ok but un the turing web give me a sintaxis error in " value.Count - 1 " for get the last member, specificly un the -

robertovillavicencio
Автор

Ty for the video! Your explanation is great and is helping me to prepare for a C# challenge. Just a question, why not to put the foreach (var val in opsList) [...] segment outside all if statements instead of copying and repeating it for each of them? I may be missing something.

tsunablast
Автор

A stack version for fun

var opList = new[] { "5", "2", "C", "D", "+" };
var stk = new Stack<int>();

foreach(var op in opList)
{
switch(op)
{
case "+":
var lVal = stk.Pop();
var rVal = stk.Peek();
stk.Push(lVal);
stk.Push(lVal + rVal);
break;
case "C":
stk.Pop();
break;
case "D":
stk.Push(stk.Peek() * 2);
break;
default:
stk.Push(int.Parse(op));
break;
}
}
Console.WriteLine(stk.Sum());

clearz
Автор

Thanks it was great! and here is an other solution:
public static int CalPoints(string[] ops)
{
Stack<int> stack = new Stack<int>();

foreach (string op in ops)
{
if (int.TryParse(op, out int score))
{
stack.Push(score);
}
else if (op == "+")
{
int prev1 = stack.Pop();
int prev2 = stack.Peek();
int newScore = prev1 + prev2;
stack.Push(prev1);
stack.Push(newScore);
}
else if (op == "D")
{
int prev = stack.Peek();
stack.Push(prev * 2);
}
else if (op == "C")
{
stack.Pop();
}
}

int finalScore = 0;
while (stack.Count > 0)
{
finalScore += stack.Pop();
}

return finalScore;
}

digitalizedhorizon
Автор

My solution, nice and clean
public int CalPoints(string[] ops)
{
var record = new List<int>();
foreach (var item in ops)
{
switch (item)
{
case var _ when int.TryParse(item, out int value):
record.Add(value);
break;
case "+":
record.Add(record[^2] + record[^1]);
break;
case "D" or "d":
record.Add(2 * record[^1]);
break;
case "C" or "c":
record.RemoveAt(record.Count - 1);
break;
}
}
return record.Sum();
}
}

mgadriescu
Автор

Both can be done using single loop.

Solution 1 :
List<int> result = new List<int>();
for (int i = 0; i < ops.Length; i++)
{
if (ops[i] == "+")
{
- 1] + result[result.Count - 2]);
}
else if (ops[i] == "D")
{
- 1] * 2);
}
else if (ops[i] == "C")
{
result.RemoveAt(result.Count - 1);
}
else
{

}
}

return result.Sum();



Solution 2:
char[] chars = s.ToCharArray();
Stack<char> openParenthesis = new Stack<char>();

foreach (var c in chars)
{
if(c == '(' || c == '{' || c == '[')
{
openParenthesis.Push(c);
}

else if(c == ')')
{
if(openParenthesis.Peek() == '(')
{
openParenthesis.Pop();
}
}
else if(c == '}')
{
if (openParenthesis.Peek() == '{')
{
openParenthesis.Pop();
}
}
else if (c == ']')
{
if (openParenthesis.Peek() == '[')
{
openParenthesis.Pop();
}
}
}

return openParenthesis.Count == 0;

abdulrafeyartist