Spekulant
Spekulant
Explore posts from servers
CC#
Created by Spekulant on 10/17/2023 in #help
❔ xUnit tests
using System;
using System.IO;

namespace TestWordCount;

public class WordCounterTests
{
[Fact]
public void Test1() {
var story = """
Jack and Jill went up the hill.
""";
var reader = new StringReader(story);

var counter = new WordCounter(reader);

counter.Execute();
var stringWriter = new StringWriter();
Console.SetOut(stringWriter);
counter.WriteResults();
string allConsoleOutput = stringWriter.ToString();
Console.WriteLine("Actual Output: " + allConsoleOutput);


Assert.Equal("14\n2", allConsoleOutput);

}



}
using System;
using System.IO;

namespace TestWordCount;

public class WordCounterTests
{
[Fact]
public void Test1() {
var story = """
Jack and Jill went up the hill.
""";
var reader = new StringReader(story);

var counter = new WordCounter(reader);

counter.Execute();
var stringWriter = new StringWriter();
Console.SetOut(stringWriter);
counter.WriteResults();
string allConsoleOutput = stringWriter.ToString();
Console.WriteLine("Actual Output: " + allConsoleOutput);


Assert.Equal("14\n2", allConsoleOutput);

}



}
i have this code that has function execute it reads text and appends the words in a paragraph into a list. then a function write results it writes the results onto a commandline for example
12
4
12
4
the code alone works perfectly fine when im debugging the xunit tests it execute counts the words correctly but when the writeResults function starts the values in the list are empty
17 replies
CC#
Created by Spekulant on 10/16/2023 in #help
❔ ✅ whitespaces are doing me dirty
public void Execute() {
StringBuilder word = new StringBuilder();
int wordCount = 0;
bool multipleNewLines = false;

int charValue;
while ((charValue = _reader.Read()) != -1) {
char character = (char)charValue;
if (!Char.IsWhiteSpace(character)) {
word.Append(charValue);
multipleNewLines = false;
} else {
if (word.Length > 0) {
wordCount++;
word.Clear();
}
if (character == '\n') {
multipleNewLines = true;}
if (character == '\n' && multipleNewLines) {
if (wordCount != 0) { _paragraphWordCount.Add(wordCount); }
wordCount = 0;
multipleNewLines = false;
}
}
}
if (word.Length > 0) {
wordCount++;
_paragraphWordCount.Add(wordCount);
}
}
public void Execute() {
StringBuilder word = new StringBuilder();
int wordCount = 0;
bool multipleNewLines = false;

int charValue;
while ((charValue = _reader.Read()) != -1) {
char character = (char)charValue;
if (!Char.IsWhiteSpace(character)) {
word.Append(charValue);
multipleNewLines = false;
} else {
if (word.Length > 0) {
wordCount++;
word.Clear();
}
if (character == '\n') {
multipleNewLines = true;}
if (character == '\n' && multipleNewLines) {
if (wordCount != 0) { _paragraphWordCount.Add(wordCount); }
wordCount = 0;
multipleNewLines = false;
}
}
}
if (word.Length > 0) {
wordCount++;
_paragraphWordCount.Add(wordCount);
}
}
Im reading char by char and i just want to count how many words are in paragraphs but it doesnt count correctly there is a problem with /n
13 replies
CC#
Created by Spekulant on 10/15/2023 in #help
❔ ✅ SreamReader vs. TextReader
Given i want to program a reader that is efficient so i will read by a character not by line or whole text file should i use stream or text reader i know from docs that TextReader -> StreamReader and they both have function .read() so how should i decide which one. And should i use a cobination with stringbuilder when i want to count the number of words
10 replies
CC#
Created by Spekulant on 5/1/2023 in #help
❔ basic maze solver but there is a bug that it moves how it should not
```cs
6 replies
CC#
Created by Spekulant on 4/10/2023 in #help
❔ reprotucing the kings path
can anybody help me fix this code https://paste.mod.gg/sgsvrbaqnmpi/0
the input is
number of obsticles
1
their x y position
2 1
the start of the king
1 1
the finish
3 3
the input is
number of obsticles
1
their x y position
2 1
the start of the king
1 1
the finish
3 3
i my code print out the number of steps the king needs but i also want the path the kings has to take but its not doing it alright
2 replies
CC#
Created by Spekulant on 4/3/2023 in #help
❔ forloops upon forloops
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
int m = int.Parse(Console.ReadLine());
List<char> gridContents = Console.ReadLine().Take(n*m).ToList();
List<char> word = Console.ReadLine().ToList();
List<Tuple<int, int>> coordList = new List<Tuple<int, int>>();

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
coordList.Add(new Tuple<int, int>(i,j));
}
}
findMinKeyStrokes(gridContents,coordList,word);

}

static void findMinKeyStrokes(List<char> gridContents,List<Tuple<int,int>> coordList,List<char> word)
{
List<int> indexPointers = new List<int>() {0};
int numberKeystrokes = 0;

foreach (char letter in word)
{
if (gridContents.Contains(letter))
{
numberKeystrokes += 1;
int minimalDis = int.MaxValue;
List<int> indexes = gridContents.Select((c, i) => letter == gridContents[i] ? i : -1)
.Where(i => i >= 0)
.ToList();

foreach (int indexPointer in indexPointers)
{
foreach (int index in indexes)
{
//calculating distance
int dist = Math.Abs(coordList[index].Item1 - coordList[indexPointer].Item1) +
Math.Abs(coordList[index].Item2 - coordList[indexPointer].Item2);
if (dist < minimalDis)
{
indexPointers.Clear();
minimalDis = dist;
indexPointers.Add(index);
}
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
int m = int.Parse(Console.ReadLine());
List<char> gridContents = Console.ReadLine().Take(n*m).ToList();
List<char> word = Console.ReadLine().ToList();
List<Tuple<int, int>> coordList = new List<Tuple<int, int>>();

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
coordList.Add(new Tuple<int, int>(i,j));
}
}
findMinKeyStrokes(gridContents,coordList,word);

}

static void findMinKeyStrokes(List<char> gridContents,List<Tuple<int,int>> coordList,List<char> word)
{
List<int> indexPointers = new List<int>() {0};
int numberKeystrokes = 0;

foreach (char letter in word)
{
if (gridContents.Contains(letter))
{
numberKeystrokes += 1;
int minimalDis = int.MaxValue;
List<int> indexes = gridContents.Select((c, i) => letter == gridContents[i] ? i : -1)
.Where(i => i >= 0)
.ToList();

foreach (int indexPointer in indexPointers)
{
foreach (int index in indexes)
{
//calculating distance
int dist = Math.Abs(coordList[index].Item1 - coordList[indexPointer].Item1) +
Math.Abs(coordList[index].Item2 - coordList[indexPointer].Item2);
if (dist < minimalDis)
{
indexPointers.Clear();
minimalDis = dist;
indexPointers.Add(index);
}
125 replies
CC#
Created by Spekulant on 3/20/2023 in #help
❔ just a quick help
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
static void Main(string[] args) {
string[] input = Console.ReadLine().Split(" ");
int[] limits = input[0].Split().Select(int.Parse).ToArray();
int[] initial = input[1].Split().Select(int.Parse).ToArray();


int m = limits.Max();
List<int> res = new List<int>();
List<int> resMoves = new List<int>();

for (int i = 0; i <= m; i++) {
int[] cur = initial.ToArray();
Queue<int[]> queue = new Queue<int[]>();
queue.Enqueue(cur);
Queue<int> depth = new Queue<int>();
depth.Enqueue(0);

List<int[]> history = new List<int[]>();

while (queue.Count > 0) {
cur = queue.Dequeue();
history.Add(cur);
int curDepth = depth.Dequeue();

if (cur.Contains(i)) {
res.Add(i);
resMoves.Add(curDepth);
break;
}
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
static void Main(string[] args) {
string[] input = Console.ReadLine().Split(" ");
int[] limits = input[0].Split().Select(int.Parse).ToArray();
int[] initial = input[1].Split().Select(int.Parse).ToArray();


int m = limits.Max();
List<int> res = new List<int>();
List<int> resMoves = new List<int>();

for (int i = 0; i <= m; i++) {
int[] cur = initial.ToArray();
Queue<int[]> queue = new Queue<int[]>();
queue.Enqueue(cur);
Queue<int> depth = new Queue<int>();
depth.Enqueue(0);

List<int[]> history = new List<int[]>();

while (queue.Count > 0) {
cur = queue.Dequeue();
history.Add(cur);
int curDepth = depth.Dequeue();

if (cur.Contains(i)) {
res.Add(i);
resMoves.Add(curDepth);
break;
}
11 replies
CC#
Created by Spekulant on 3/5/2023 in #help
❔ index out of range or non-negative
class find_path
{
public static int bfs(int[] start, int[] end,List<List<int>> board)
{
List<List<int>> Visited = new List<List<int>>();
Queue<List<int> > queue = new Queue<List<int>>();

Visited.Add(start.ToList());
queue.Enqueue(start.ToList());

while (queue.Count > 0)
{
var x = queue.Dequeue();
List<int[]> possiblePositions = new List<int[]>();
possiblePositions.Add(new int[] {x[0]-1, x[1], x[2]+1});
possiblePositions.Add(new int[] {x[0]-1, x[1]-1, x[2]+1});
possiblePositions.Add(new int[] {x[0]-1, x[1]+1, x[2]+1});
possiblePositions.Add(new int[] {x[0]+1, x[1]-1, x[2]+1});
possiblePositions.Add(new int[] {x[0]+1, x[1], x[2]+1});
possiblePositions.Add(new int[] {x[0]+1, x[1]+1, x[2]+1});
possiblePositions.Add(new int[] {x[0], x[1]+1, x[2]+1});
possiblePositions.Add(new int[] {x[0], x[1]-1, x[2]+1});

foreach (int[] position in possiblePositions)
{
if (position[0] > -1 && position[0] < 8 && position[1] > -1 && position[1] < 8)
{
if (board[position[0]][position[1]] == 2)
{
return position[2];
}
if (board[position[0]][position[1]] != 1)
{
if (!Visited.Contains(position.Take(2).ToList()))
{
queue.Enqueue(position.ToList());
Visited.Add(position.Take(2).ToList());
}
}
}
}
}

return -1;
}
}
class find_path
{
public static int bfs(int[] start, int[] end,List<List<int>> board)
{
List<List<int>> Visited = new List<List<int>>();
Queue<List<int> > queue = new Queue<List<int>>();

Visited.Add(start.ToList());
queue.Enqueue(start.ToList());

while (queue.Count > 0)
{
var x = queue.Dequeue();
List<int[]> possiblePositions = new List<int[]>();
possiblePositions.Add(new int[] {x[0]-1, x[1], x[2]+1});
possiblePositions.Add(new int[] {x[0]-1, x[1]-1, x[2]+1});
possiblePositions.Add(new int[] {x[0]-1, x[1]+1, x[2]+1});
possiblePositions.Add(new int[] {x[0]+1, x[1]-1, x[2]+1});
possiblePositions.Add(new int[] {x[0]+1, x[1], x[2]+1});
possiblePositions.Add(new int[] {x[0]+1, x[1]+1, x[2]+1});
possiblePositions.Add(new int[] {x[0], x[1]+1, x[2]+1});
possiblePositions.Add(new int[] {x[0], x[1]-1, x[2]+1});

foreach (int[] position in possiblePositions)
{
if (position[0] > -1 && position[0] < 8 && position[1] > -1 && position[1] < 8)
{
if (board[position[0]][position[1]] == 2)
{
return position[2];
}
if (board[position[0]][position[1]] != 1)
{
if (!Visited.Contains(position.Take(2).ToList()))
{
queue.Enqueue(position.ToList());
Visited.Add(position.Take(2).ToList());
}
}
}
}
}

return -1;
}
}
im pretty sure that im checking if it ok but i dont know what im doing wrong
167 replies
CC#
Created by Spekulant on 2/25/2023 in #help
❔ inputing numbers into list
I want to imput numbers into a list until -1 typed... and then i would like to have it as variable so i can do what ever with it
216 replies
CC#
Created by Spekulant on 2/25/2023 in #help
❔ ✅ Inputing numbers
I want to write a short code that gets random input. It reads two numbers between random inputs.
using System;
namespace hello_wolrd;
class Program
{
static void Main(string[] args)
{

int a = Inputer.ReadSymbol();
int b = Inputer.ReadSymbol();
Console.WriteLine(a + b);
}
}

class Inputer
{
public static int ReadSymbol()
{
int symbol = Console.Read();
while ((symbol < '0') || (symbol > 9))
symbol = Console.Read();
int x = 0;

while ((symbol >= '0') && (symbol <= '9'))
{
x = 10 * x + (symbol - '0');
symbol = Console.Read();
}


return x;
}
}
using System;
namespace hello_wolrd;
class Program
{
static void Main(string[] args)
{

int a = Inputer.ReadSymbol();
int b = Inputer.ReadSymbol();
Console.WriteLine(a + b);
}
}

class Inputer
{
public static int ReadSymbol()
{
int symbol = Console.Read();
while ((symbol < '0') || (symbol > 9))
symbol = Console.Read();
int x = 0;

while ((symbol >= '0') && (symbol <= '9'))
{
x = 10 * x + (symbol - '0');
symbol = Console.Read();
}


return x;
}
}
im somewhere around here.. but i havent figured the minus numbers also... the input can be something like:
115
aaa-12

a
115
aaa-12

a
42 replies