morry329#
morry329#
CC#
Created by morry329# on 6/14/2024 in #help
KMP-Algorithm in C#
No description
10 replies
CC#
Created by morry329# on 5/12/2024 in #help
Fighting with the longest common prefix puzzle
No description
44 replies
CC#
Created by morry329# on 4/14/2024 in #help
IndexOutOfBoundsException triggered | LeetCode Where will the ball fall
Link to the puzzle description https://leetcode.com/problems/where-will-the-ball-fall/description/ I have been fighting with this puzzle for more than a month. Still my code cannot output correctly. I tried this testcase Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]] Output: [1,-1,-1,-1,-1] And my WIP code (it triggers IndexOutOfBoundsException at line int currRow = 0. Could anyone kindly point me in the right direction?:
public int[] FindBall(int[][] grid){
int col = grid[0].Length;
int row= grid.Length;
int[] result = new int[col];

for(int i=0; i< col; i++){
result [i]= Helper(grid,row,col);
}

return result;
}

public int Helper(int[][] grid, int row, int col){
int currCol = 0;
int currRow = 0; //OutOfBoundsException
while(currRow < row){
if (grid[currRow][currCol]==1){
if(currCol == col - 1 || grid[currRow][currCol+1]==-1){
return -1;
break;
} else {
currCol++;
currRow++;
}
} else if (grid[currRow][currCol] ==-1){
if(currCol <0 || grid[currRow][currCol-1]==1){
return -1;
break;
} else {
currCol--;
currRow++;
}
}
currRow++;
}
if(currRow==row){
currCol = grid[currRow][currCol];
}
return currCol;
}
public int[] FindBall(int[][] grid){
int col = grid[0].Length;
int row= grid.Length;
int[] result = new int[col];

for(int i=0; i< col; i++){
result [i]= Helper(grid,row,col);
}

return result;
}

public int Helper(int[][] grid, int row, int col){
int currCol = 0;
int currRow = 0; //OutOfBoundsException
while(currRow < row){
if (grid[currRow][currCol]==1){
if(currCol == col - 1 || grid[currRow][currCol+1]==-1){
return -1;
break;
} else {
currCol++;
currRow++;
}
} else if (grid[currRow][currCol] ==-1){
if(currCol <0 || grid[currRow][currCol-1]==1){
return -1;
break;
} else {
currCol--;
currRow++;
}
}
currRow++;
}
if(currRow==row){
currCol = grid[currRow][currCol];
}
return currCol;
}
7 replies
CC#
Created by morry329# on 4/6/2024 in #help
A small hiccup for the LeetCode puzzle Where the ball will fall
Link to the original puzzle https://leetcode.com/problems/where-will-the-ball-fall/ My code does not print out the right output just yet
public class ZigZagBallFallsTry
{

public int[] FindBall(int[][] grid)
{
int col = grid[0].Length-1;
int row = grid.Length-1;
int[] result = new int[col];

for (int i = 0; i < col; i++)
{
result[i] = BallHelp(grid, row, col);
}

return result;

}

private int BallHelp(int[][] grid, int row, int col)
{
int currRow = 0;
int currCol = col;

while (currRow <= row)
{
if (grid[currRow][currCol] == 1)
{
if (currCol == col || grid[currRow][currCol + 1] == -1)
{
return -1;
}
else
{
currCol++;
currRow++;
}
}
else if (grid[currRow][currCol] == -1)
{
if (currCol == 0 || grid[currRow][currCol - 1] == 1)
{
return -1;
}
else
{
currCol--;
currRow++;
}
}
else
{
currRow++;
}
}

return currCol;
}

}
public class ZigZagBallFallsTry
{

public int[] FindBall(int[][] grid)
{
int col = grid[0].Length-1;
int row = grid.Length-1;
int[] result = new int[col];

for (int i = 0; i < col; i++)
{
result[i] = BallHelp(grid, row, col);
}

return result;

}

private int BallHelp(int[][] grid, int row, int col)
{
int currRow = 0;
int currCol = col;

while (currRow <= row)
{
if (grid[currRow][currCol] == 1)
{
if (currCol == col || grid[currRow][currCol + 1] == -1)
{
return -1;
}
else
{
currCol++;
currRow++;
}
}
else if (grid[currRow][currCol] == -1)
{
if (currCol == 0 || grid[currRow][currCol - 1] == 1)
{
return -1;
}
else
{
currCol--;
currRow++;
}
}
else
{
currRow++;
}
}

return currCol;
}

}
The output is -1, -1, -1, -1 whilst it is supposed to be 1, -1, -1, -1, -1. The input here is grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]` Could anyone kindly point me in the right direction?
1 replies
CC#
Created by morry329# on 3/17/2024 in #help
Not able to pass the test case correctly
The link to the original task https://leetcode.com/problems/where-will-the-ball-fall/ This is my WIP code
public class ZigZagBallFallsTry
{
public int[] FindBall(int[][] grid)
{
int m = grid.Length - 1;
int n = grid[0].Length - 1;

int[] result = new int[grid[0].Length]; // Initialize result array with correct size

for (int row = 0; row <= m; row++) // Loop until row <= m
{
result[row] = RecordBallMovements(grid, m, n); // Pass row index to RecordBallMovements
}

return result;
}

private int RecordBallMovements(int[][] grid, int m, int n)
{
int helperCol = 0; // Initialize helperCol here
for (int helperRow = 0; helperRow < m; helperRow++) // Loop until helperRow <= row
{
for (; helperCol >= 0 && helperCol <= n; helperCol++) // Correct loop condition and increment helperCol
{
if (grid[helperRow][helperCol] == 1)
{
if (helperCol == n || grid[helperRow][helperCol + 1] == -1) // Use == instead of >
{
return -1;
break;
}
}
else if (grid[helperRow][helperCol] == -1)
{
if (helperCol == 0 || grid[helperRow][helperCol - 1] == 1) // Use == instead of <
{
return -1;
break;
}
helperCol--; // Decrement helperCol for left movement
}
}
}
return helperCol;
}


}
public class ZigZagBallFallsTry
{
public int[] FindBall(int[][] grid)
{
int m = grid.Length - 1;
int n = grid[0].Length - 1;

int[] result = new int[grid[0].Length]; // Initialize result array with correct size

for (int row = 0; row <= m; row++) // Loop until row <= m
{
result[row] = RecordBallMovements(grid, m, n); // Pass row index to RecordBallMovements
}

return result;
}

private int RecordBallMovements(int[][] grid, int m, int n)
{
int helperCol = 0; // Initialize helperCol here
for (int helperRow = 0; helperRow < m; helperRow++) // Loop until helperRow <= row
{
for (; helperCol >= 0 && helperCol <= n; helperCol++) // Correct loop condition and increment helperCol
{
if (grid[helperRow][helperCol] == 1)
{
if (helperCol == n || grid[helperRow][helperCol + 1] == -1) // Use == instead of >
{
return -1;
break;
}
}
else if (grid[helperRow][helperCol] == -1)
{
if (helperCol == 0 || grid[helperRow][helperCol - 1] == 1) // Use == instead of <
{
return -1;
break;
}
helperCol--; // Decrement helperCol for left movement
}
}
}
return helperCol;
}


}
The output for this code is ==> -1 ==> -1 ==> -1 ==> -1 ==> -1 But it was supposed to be ==> 1 ==> -1 ==> -1 ==> -1 ==> -1` instead I don't understand why my code stores -1 instead of 1 at the first index on my array. Could anyone kindly point me in the right direction?
3 replies
CC#
Created by morry329# on 3/4/2024 in #help
Create Migration always fail
So I wanted to create a SQL Server database table based on your C# model. I have typed down dotnet ef migrations add InitialCreate but it never gets to migrate. The dotnet build shows no noticeable errors like this
PM> dotnet ef migrations add InitialCreate
Build started...
Build failed. Use dotnet build to see the errors.
PM> dotnet build
MSBuild version 17.7.1+971bf70db for .NET
Determining projects to restore...
All projects are up-to-date for restore.
CRUD_HomeFinder -> C:\Users\Mami Kawamura\Source\Repos\CRUD_HomeFinder\CRUD_HomeFinder\bin\Debug\net6.0\MVCCore.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.35
PM> dotnet ef migrations add InitialCreate
Build started...
Build failed. Use dotnet build to see the errors.
PM> dotnet build
MSBuild version 17.7.1+971bf70db for .NET
Determining projects to restore...
All projects are up-to-date for restore.
CRUD_HomeFinder -> C:\Users\Mami Kawamura\Source\Repos\CRUD_HomeFinder\CRUD_HomeFinder\bin\Debug\net6.0\MVCCore.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.35
` The link to all other database-related code https://pastebin.com/qFyNtM04 Could anyone point me in the right direction?
4 replies
CC#
Created by morry329# on 2/25/2024 in #help
✅ MvC | IAction edit is not able to edit data
So I am following an YT tutorial for creating a CRUD operation with ASP.NET. My tutorial code can create and read data but not update ( or edit). Whenever I tried to edit it pops the custom error message
Employee not found
Employee not found
` It was supposed to be the success message
"Employee edited successfully!"
"Employee edited successfully!"
`` Here's my code, could anyone point me in the right direction? Controller https://pastebin.com/fHLMY3jr Edit.cshtml https://pastebin.com/EmNXeEZ0
43 replies
CC#
Created by morry329# on 2/21/2024 in #help
ASP.NET | The table headers don't want to be centered
No description
1 replies
CC#
Created by morry329# on 2/18/2024 in #help
IndexOutOfRangeException? Why??
I am analysing a solution for Spiral Matrix puzzle on LeetCode: https://leetcode.com/problems/spiral-matrix/ But I keep getting an IndexOutOfRange exception at this line if (x >= r && !bol[y, x]) Could anyone kindly tell me why? Here is the whole solution code
public IList<int> SpiralOrder3(int[][] arr2D)
{
var ls = new List<int>();
var count = 0;

var bol = new bool[arr2D.Length,arr2D[0].Length];

int x = 0, y = 0;
var r = 0;

while (count < arr2D.Length * arr2D[0].Length)
{
for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x < arr2D[0].Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y][x++]);
count++;
}
}

y++;
x--;

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y < arr2D.Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y++][x]);
count++;
}
}

y--;
x++;

for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x >= r && !bol[y, x]) //EXCEPTION

{
bol[y, x] = true;
ls.Add(arr2D[y][x--]);
count++;
}
}

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y >= r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y--][x]);
count++;
}
}

x++;
y++;
r++;
}

return ls;
}
public IList<int> SpiralOrder3(int[][] arr2D)
{
var ls = new List<int>();
var count = 0;

var bol = new bool[arr2D.Length,arr2D[0].Length];

int x = 0, y = 0;
var r = 0;

while (count < arr2D.Length * arr2D[0].Length)
{
for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x < arr2D[0].Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y][x++]);
count++;
}
}

y++;
x--;

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y < arr2D.Length - r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y++][x]);
count++;
}
}

y--;
x++;

for (int i = 0; i < arr2D[0].Length - r; i++)
{
if (x >= r && !bol[y, x]) //EXCEPTION

{
bol[y, x] = true;
ls.Add(arr2D[y][x--]);
count++;
}
}

for (int i = 0; i < arr2D.Length - r; i++)
{
if (y >= r && !bol[y, x])
{
bol[y, x] = true;
ls.Add(arr2D[y--][x]);
count++;
}
}

x++;
y++;
r++;
}

return ls;
}
`
6 replies
CC#
Created by morry329# on 2/15/2024 in #help
RuntimeBinderException: '' does not contain 'Any()'
Would like another set of eyes on this exception. I am following a YT tutorial to create a CRUD application via ASP.NET. Once I ran the code, this error popped out. https://pastebin.com/7LqfcshB Seems to me like this exception points out at those two lines in my cshtml file @if(Model != null && Model.Any()){ (line 26) and <h3>@ViewData["Title"]</h3> (line 8). I have added (at)using System.Linq in cshtml file and all over classes with the name of Employee, but the exception persists. Does anyone have an idea where it went wrong and triggered the exception? EmployerController.cs https://pastebin.com/VndL9jjG cshtml file https://pastebin.com/9FLYLwZc EmployerViewModel.cs https://pastebin.com/cVcw9vyQ Employees.cs https://pastebin.com/PWmaiNTC
1 replies
CC#
Created by morry329# on 11/21/2023 in #help
The helper method and the char count do not work as I wanted
I wanted to solve this leetcode puzzle with recursion https://leetcode.com/problems/decode-string/ This is my WIP, the count variable (to count chars in the bracket) and the recursion line do not seem to work. For example, this code prints out aabc while it was supposed to be print out like aaabcbc (for the case 3[a]2[bc]). Could anyone kindly point me in the right direction?
public class DecodeStringRCTry
{
public string DecodeString(string s) {
var sb = new StringBuilder();
for(int i = 0; i < s.Length; i++){

int count = 0;
if(Char.IsDigit(s[i])){
count = 10*count + (s[i]-'0');

} else if (s[i] == '[')
{
count++;
string teilStr = Solver(count, s, sb);

string decodedString = DecodeString(teilStr);

sb.Append(decodedString);
//count = 0;
} else if (Char.IsLetter(s[i])){
count++;
sb.Append(s[i]);

}
}
return sb.ToString();
}

private string Solver(int count, string s, StringBuilder sb)
{

string stringToAppend = "";
int openBracket = 0;
for(int i = 1; i < count; i++){
openBracket++;
sb.Append(s[i]);
}
openBracket--;

return sb.ToString();
}
}
public class DecodeStringRCTry
{
public string DecodeString(string s) {
var sb = new StringBuilder();
for(int i = 0; i < s.Length; i++){

int count = 0;
if(Char.IsDigit(s[i])){
count = 10*count + (s[i]-'0');

} else if (s[i] == '[')
{
count++;
string teilStr = Solver(count, s, sb);

string decodedString = DecodeString(teilStr);

sb.Append(decodedString);
//count = 0;
} else if (Char.IsLetter(s[i])){
count++;
sb.Append(s[i]);

}
}
return sb.ToString();
}

private string Solver(int count, string s, StringBuilder sb)
{

string stringToAppend = "";
int openBracket = 0;
for(int i = 1; i < count; i++){
openBracket++;
sb.Append(s[i]);
}
openBracket--;

return sb.ToString();
}
}
``
4 replies
CC#
Created by morry329# on 10/6/2023 in #help
❔ Only one testcase failed
No description
13 replies
CC#
Created by morry329# on 9/30/2023 in #help
❔ Why is Pop() necessary here??
So I found a sample solution for the LeetCode decode string puzzle https://leetcode.com/problems/decode-string/
public class Solution
{
public string DecodeString(string s)
{
Stack<string> strings = new Stack<string>();
Stack<int> numbers = new Stack<int>();

string currentString = "";
int currentNumber = 0;

foreach (char c in s)
{
if (c == '[')
{
strings.Push(currentString);
numbers.Push(currentNumber);
currentString = "";
currentNumber = 0;
}
else if(c == ']')
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
else if ( c-'0'<=9 && c - '0' >= 0)
currentNumber = currentNumber * 10 + (c - '0');
else
currentString += c;
}

return currentString;
}
}
public class Solution
{
public string DecodeString(string s)
{
Stack<string> strings = new Stack<string>();
Stack<int> numbers = new Stack<int>();

string currentString = "";
int currentNumber = 0;

foreach (char c in s)
{
if (c == '[')
{
strings.Push(currentString);
numbers.Push(currentNumber);
currentString = "";
currentNumber = 0;
}
else if(c == ']')
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
else if ( c-'0'<=9 && c - '0' >= 0)
currentNumber = currentNumber * 10 + (c - '0');
else
currentString += c;
}

return currentString;
}
}
` And I am confused with this line
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
` Why is it strings.Pop() needed here?? ChatGPT says it's not needed and it's redundant, I do not think that is the correct answer. Does the Pop remove the bracket [, so the code goes on to check part of the string??
5 replies
CC#
Created by morry329# on 9/9/2023 in #help
✅ why are they considered int instead of string?
So I was working on this puzzle https://leetcode.com/problems/backspace-string-compare/submissions/ I would like some advice on the compiler error/tipo (not the semantic/logical error to resolve this puzzle). My code is as follows, I am getting error CS1503: Argument 1: cannot convert from 'string' to 'int' (in Solution.cs) for two string initialisations inside the foreach-loop at the bottom. I had tried ToString()/Convert.ToString(), but to no avail. Could anyone kindly point me in the right direction? public class Solution { public bool BackspaceCompare(string s, string t) { var dic1 = new Dictionary<string, string>(){ {" ", "#"}, {" ", "##"}, {" ", "###"} }; var dic2 = new Dictionary<string, string>(){ {" ", "#"}, {" ", "##"}, {" ", "###"} }; for(int i = 0; i < s.Length; i++){ string sStr = s[i].ToString(); string tStr = t[i].ToString(); if(!dic1.ContainsKey(sStr)){ dic1.Add(sStr, " "); } if(!dic2.ContainsKey(tStr)){ dic2.Add(tStr, " "); } } foreach(var k in dic1.Keys){ string sForEach = s[k].Convert.ToString(); //ERROR HERE string tForEach = t[k].Convert.ToString(); //ALSO HERE if(dic2.ContainsKey(k) && sForEach == tForEach){ return true; } } return false; } }
15 replies
CC#
Created by morry329# on 8/31/2023 in #help
❔ Getting a wrong result on LeetCode although the IDE shows the correct output
So I believe I still make some mistake in setting cows' Count for this puzzle https://leetcode.com/problems/bulls-and-cows/submissions/ For the test case string secret = "1807", guess = "7810";I get the wrong output 1A0B on the website while getting the correct one (1A3B) on Rider (IDE). Can anyone please point me in the right direction or is LC malfunctioning? public class Solution { public string GetHint(string secret, string guess) { var dic = new Dictionary<char, int>(); int bulls = 0, cows = 0; for (int i = 0; i < secret.Length; i++) { if (!dic.ContainsKey(secret[i])) { dic.TryAdd(secret[i], 0); } } for (int i = 0; i < secret.Length; i++) { if (secret[i] == guess[i]) { bulls++; dic[secret[i]]++; } } foreach (var k in dic) { /*if (!dic.TryGetValue(k.Key, out int val)) { continue; }*/ cows += Math.Min(k.Value, dic.GetValueOrDefault(guess[k.Key],0)); } Console.WriteLine(bulls + "A" + cows + "B"); return $"{bulls}A{cows}B"; } }
16 replies
CC#
Created by morry329# on 8/17/2023 in #help
❔ ✅ My code is pretty close to the right solution, but one test case still fails
5 replies
CC#
Created by morry329# on 7/30/2023 in #help
✅ The code prints out no list elements
So I have been experimenting with this sample solution for this puzzle https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/?currentPage=1&orderBy=hot&query=&tag=c-2 public class Solution2 { public List<int> FindAnagrams(string s, string p) { if (p.Length > s.Length) { return new List<int>(); } List<int> result = new List<int>(); int[] mapP = new int[26]; foreach (var symbol in p) { mapP[symbol - 'a']++; } for (int i = 0; i < p.Length; i++) { if (IsAnagram(mapP)) { result.Add(i - p.Length); } } for (int i = p.Length; i < s.Length; i++) { mapP[s[i-p.Length] - 'a']++; mapP[s[i] - 'a']--; if (IsAnagram(mapP)) { result.Add(i-p.Length+1); } } Console.WriteLine($"return the list 2 "); result.ForEach(Console.WriteLine); return result; } bool IsAnagram(int[] map) { foreach (var count in map) { if (count != 0) { return false; } } return true; } } This code returns no list elements whilst it returns no exception/errors. Maybe can anyone explain to me what went wrong with it?
7 replies
CC#
Created by morry329# on 7/29/2023 in #help
✅ Why does my list get zeroed?
5 replies
CC#
Created by morry329# on 6/30/2023 in #help
❔ Why wrong answer?
19 replies
CC#
Created by morry329# on 6/23/2023 in #help
✅ What is the function of the Dictionary here?
public class Solution { Dictionary<int,int>cached=new Dictionary<int,int>(); public int ClimbStairs(int n) { if(cached.ContainsKey(n)) { return cached[n]; } int output = 1; if(n<=3){ output = n; } else { output = ClimbStairs(n-1) + ClimbStairs(n-2);
} cached[n]=output; return output; } } So I wanted to understand the above solution for this LeetCode puzzle https://leetcode.com/problems/climbing-stairs/description/ I am still wondering what the Dictionary is doing here. My guess is that it prevents the code from exceeding the time limit (as my code went to exceed the limit without the Dictionary). But I appreciate any other plausible explanation for the Dictionary's presence here.
7 replies