C# Comparing List objects to passed parameters - c#

What I am trying to do is get the number of right angled triangles between 1 and 20 on both sides.
Most of the logic is fine, but when I want to check for 3, 4 and 5 this is one triangle, while 4, 3 and 5 would not be valid as it 3, 4, 5 in a different order.
Here is the code that I have written for the problem area
public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
{
bool breakLoop = false;
Int32 length = triangleList.Count;
for (int index = 0; index < length && breakLoop != false; index++)
{
//This is to compare an existing adjacent that is stored in the list to the
//supplied opposite, this is to prebent the 3, 4, 5 and 4, 3, 5 issue
var response = triangleList.Find(r => r.IntAdjacent == intOpp);
if (response !=null)
{
//This is to compare an existing opposite that is stored in the list to the
//supplied adjacent, this is to prebent the 3, 4, 5 and 4, 3, 5 issue
var otherResponse = triangleList.Find(r => r.IntOpposite == intAdj);
if (otherResponse != null)
{
breakLoop = true;
}
}
}
return breakLoop;
}
Just in case anybody needs the Triangle code, here it is
public class Triangle
{
private int intAdjacent;
private int intOpposite;
private int intHypotenuse;
public Triangle(int intAdjacent, int intOpposite, int intHypotenuse)
{
this.intAdjacent = intAdjacent;
this.intOpposite = intOpposite;
this.intHypotenuse = intHypotenuse;
}
public int IntAdjacent
{
get { return intAdjacent; }
}
public int IntOpposite
{
get { return intOpposite; }
}
public int IntHypotenuse
{
get { return intHypotenuse; }
}
}
Could some one spot to see where I am making a mistake in the logic or have made an error in the code itself?
Keith

You can simplify this quite a lot like this:
public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
{
if(triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
return true;
return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);
}
It first looks for any matches where the passed in values are a match, then reverses the search if they don't. It's slightly different to your code in that it looks for both adjacent and opposite at the same time which is where you went wrong. Additionally, it uses Any which returns a boolean value if any item is found that matches.
Thinking about this further, I would change the function to make it an extension method like this:
public static bool isAlreadyValidTriangle(this List<Triangle> triangleList, int intAdj, int intOpp)
{
if(triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
return true;
return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);
}
This means you can call it with a little more readability:
List<Triangle> triangleList = new List<Triangle>();
... fill list with triangles ...
if(triangleList.isAlreadyValidTriangle(adjacent, opposite)
{
...
}

First of all thanks for the advice and help.
Here is the complete code from start to finish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
public class Program
{
private static double doubleHypotenuse = 0;
private static int adjacent = 1;
private static int opposite = 1;
private static int limit = 200;
private static int count = 0;
public static void Main(string[] args)
{
TriangleLogic triLogic = new TriangleLogic();
List<Triangle> triangleList = new List<Triangle>();
List<Triangle> trianglePlus1 = new List<Triangle>();
while (adjacent < limit)
{
opposite = 1;
while (opposite < limit)
{
doubleHypotenuse = triLogic.intRightAngle(adjacent, opposite);
if (doubleHypotenuse % 1 == 0)
{
if (!triLogic.isAlreadyValidTriangle(adjacent, opposite, triangleList))
{
triangleList.Add(new Triangle(adjacent, opposite, (int)Convert.ToInt32(doubleHypotenuse)));
}
count++;
}
opposite++;
}
adjacent++;
}
Console.WriteLine("The following are integer triangles");
triangleList.ForEach(delegate(Triangle pytag)
{
if ((pytag.IntHypotenuse - pytag.IntOpposite) == 1)
{
trianglePlus1.Add(new Triangle(pytag.IntAdjacent, pytag.IntOpposite, pytag.IntHypotenuse));
}
Console.WriteLine(pytag.IntAdjacent + ", " + pytag.IntOpposite + " and " + pytag.IntHypotenuse);
});
Console.WriteLine("the number of squares is " + count);
Int32 length = triangleList.Count;
Console.WriteLine("the length of the list is " + length);
Console.WriteLine("");
Console.WriteLine("the List of triangles with the hypotenuse 1 ");
Console.WriteLine("more than the opposite");
trianglePlus1.ForEach(delegate(Triangle pytagPlus1)
{
Console.WriteLine(pytagPlus1.IntAdjacent + ", " + pytagPlus1.IntOpposite + " and " + pytagPlus1.IntHypotenuse);
});
Int32 lengthPlus1 = trianglePlus1.Count;
Console.WriteLine("the length of the list is " + lengthPlus1);
}
}
}
Here is the Triangle Class
public class Triangle
{
private int intAdjacent;
private int intOpposite;
private int intHypotenuse;
public Triangle(int intAdjacent, int intOpposite, int intHypotenuse)
{
this.intAdjacent = intAdjacent;
this.intOpposite = intOpposite;
this.intHypotenuse = intHypotenuse;
}
public int IntAdjacent
{
get { return intAdjacent; }
}
public int IntOpposite
{
get { return intOpposite; }
}
public int IntHypotenuse
{
get { return intHypotenuse; }
}
}
And finally the TriangleLogic class
public class TriangleLogic
{
private double squareAdjacent = 0;
private double squareOpposite = 0;
private double squareSum = 0;
public TriangleLogic()
{
}
public double intRightAngle(int intAdjacent, int intOpposite)
{
squareAdjacent = Math.Pow(Convert.ToDouble(intAdjacent), 2);
squareOpposite = Math.Pow(Convert.ToDouble(intOpposite), 2);
squareSum = squareAdjacent + squareOpposite;
return Math.Sqrt(squareSum);
}
public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
{
if (triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
return true;
return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);
}
}
Thanks again for the support

Related

If/else statement is returning false, even though the condition should be true. (C#)

I'm currently trying to build a tic-tac-toe game in C# as of right now. Now, I've gotten most of the whole game set up. Below is the complete code for my project as of rn:
// Main code.
Game newGame = new();
newGame.RunGame();
// Classes
internal class Game
{
private Player[] Players;
private Updater GameUpdater;
private Board GameBoard;
public Game()
{
Players = new Player[2] { new(PlayerSymbol.X), new(PlayerSymbol.O) };
GameUpdater = new();
GameBoard = new();
}
public void RunGame()
{
while (true)
{
GameBoard.DisplayBoard();
int currentPlayer = GameUpdater.SendPlayerTurnInfo();
int playerInput = Players[currentPlayer].GetUserInput(); // The position the player wants to place their X/O
bool playerInputtedValidNumber = GameUpdater.VerifyUserHasInputValidNumber(playerInput);
if (playerInputtedValidNumber)
{
bool playerInputtedUnusedNumber = GameUpdater.VerifyUserHasInputUnusedSpot(GameBoard.SendBoardPiecesData(--playerInput));
if (playerInputtedUnusedNumber)
{
PlayerSymbol currentPlayerSymbol = Players[currentPlayer].SendPlayerSymbol(); // The symbol of the current player.
GameBoard.UpdateBoardPiecesData(playerInput, currentPlayerSymbol);
}
else
{
Console.WriteLine("This position has already been used!");
continue;
}
}
else
{
Console.WriteLine("Inputted an invalid position!");
continue;
}
}
}
}
internal class Board
{
private string[]? BoardPieces;
private string? BoardDisplay;
public Board()
{
BoardPieces = new string[9] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
BoardDisplay = $" {BoardPieces[0]} | {BoardPieces[1]} | {BoardPieces[2]} \n---+---+---\n {BoardPieces[3]} | {BoardPieces[4]} | {BoardPieces[5]} \n---+---+---\n {BoardPieces[6]} | {BoardPieces[7]} | {BoardPieces[8]} ";
}
public void DisplayBoard()
{
Console.WriteLine(BoardDisplay);
}
public string SendBoardPiecesData(int userInput)
{
return BoardPieces[userInput]; // No issue with null since the number will be automatically checked for null beforehand.
}
public void UpdateBoardPiecesData(int userInput, PlayerSymbol playerSymbol)
{
BoardPieces[userInput] = $"{playerSymbol}";
BoardDisplay = $" {BoardPieces[0]} | {BoardPieces[1]} | {BoardPieces[2]} \n---+---+---\n {BoardPieces[3]} | {BoardPieces[4]} | {BoardPieces[5]} \n---+---+---\n {BoardPieces[6]} | {BoardPieces[7]} | {BoardPieces[8]} ";
}
}
internal class Updater
{
private int PlayerIndicator;
public Updater()
{
PlayerIndicator = 1;
}
public int SendPlayerTurnInfo()
{
if (PlayerIndicator == 1)
{
PlayerIndicator = 0;
return PlayerIndicator;
} else
{
PlayerIndicator = 1;
return PlayerIndicator;
}
}
public bool VerifyUserHasInputValidNumber(int userInput)
{
Console.WriteLine(userInput);
if (userInput >= 0 || userInput <= 10)
{
return false;
} else
{
return true;
}
}
public bool VerifyUserHasInputUnusedSpot(string userInput)
{
if (userInput == "X" || userInput == "O") return false;
else return true;
}
}
internal class Player
{
private PlayerSymbol PlayerSymbol;
public Player(PlayerSymbol playerSymbol)
{
PlayerSymbol = playerSymbol;
}
public int GetUserInput()
{
Console.Write($"It is currently {PlayerSymbol}'s turn. Which position would you like to play at? ");
string? input = Console.ReadLine();
bool isNumerical = int.TryParse(input, out int _);
while (!isNumerical)
{
Console.Write("Invalid input, please input a number. ");
input = Console.ReadLine();
isNumerical = int.TryParse(input, out int _);
}
return int.Parse(input);
}
public PlayerSymbol SendPlayerSymbol()
{
return PlayerSymbol;
}
}
// Enumerations
enum PlayerSymbol
{
X,
O
}
Right now, the main issue with this code will be inside the Updater class.
I have a VerifyUserHasInputValidNumber(int userInput) method, taking in the number input that the player has taken, and returning a true or false depending on whether it fits a range of being a value between 1-9.
public bool VerifyUserHasInputValidNumber(int userInput)
{
if (userInput >= 0 || userInput <= 10)
{
return false;
} else
{
return true;
}
}
For some reason though, even though I can confirm that the userInput parameter is being inputted correctly (the Console.WriteLine in the 1st line of the method), it will still produce a false and will tell the user that they've inputted an invalid position.
public void RunGame()
{
while (true)
{
GameBoard.DisplayBoard();
int currentPlayer = GameUpdater.SendPlayerTurnInfo();
int playerInput = Players[currentPlayer].GetUserInput(); // THIS IS THE NUMBER THAT'S BEING INPUTTED INTO THE METHOD THAT I'M HAVING AN ISSUE WITH (the VerifyUserHasInputValidNumber(playerInput); method)
bool playerInputtedValidNumber = GameUpdater.VerifyUserHasInputValidNumber(playerInput); // Should be true if I input any number between 1-9.
if (playerInputtedValidNumber)
{
bool playerInputtedUnusedNumber = GameUpdater.VerifyUserHasInputUnusedSpot(GameBoard.SendBoardPiecesData(--playerInput));
if (playerInputtedUnusedNumber)
{
PlayerSymbol currentPlayerSymbol = Players[currentPlayer].SendPlayerSymbol(); // The symbol of the current player.
GameBoard.UpdateBoardPiecesData(playerInput, currentPlayerSymbol);
}
else
{
Console.WriteLine("This position has already been used!");
continue;
}
}
else
{
Console.WriteLine("Inputted an invalid position!"); // THIS, however, is what is being logged into the console.
continue;
}
}
}
I don't know if it may be a memory leak issue, or if I may have some sort of incorrect formatting, but if I could get any help with this issue, I would greatly appreciate it.
this test
if (userInput >= 0 || userInput <= 10)
is always true. What value do you think will be false?
I assume you want
if (userInput >= 0 && userInput <= 10)
return true;
else return false;

Is there a way to count the index values of duplicate numbers in a binary search?

I want to be able to count multiple entries in an already sorted array. In previous attempts, I've tried to use a list like this.
public static List<int> duplicate = new List<int>();
However, it wouldn't count properly and kept printing the same index number. I'm pretty much stumped on what to do at this point any help would be appreciated.
class binarysearch
{
public static int lowestIndex = 0;
public static int highestIndex;
public static int middleIndex = 0;
public static int indexValue = -1;
public static int binarySearch(double[] data, double target)
{
int highestIndex = data.Length - 1;
while (lowestIndex < highestIndex)
{
middleIndex = lowestIndex + (highestIndex - lowestIndex) / 2;
if (data[middleIndex] == target)
{
indexValue = middleIndex;
highestIndex = middleIndex;
}
else if (data[middleIndex] > target)
{
highestIndex = middleIndex;
}
else
{
lowestIndex = middleIndex + 1;
}
}
return indexValue;
}
}
edit
public static List<int> duplicate = new List<int>();
is declared earlier in the code in
class dataset
{
public static List<int> duplicate = new List<int>();
}
then printed later in the main method
foreach (object dupes in dataset.duplicate)
{
Console.WriteLine(dupes);
}

OOP approach for the Triangle Check scenario

Recently I had a practical test for Software developer position , in that test they asked following question.
Write me a function that receives three integer inputs for the lengths
of the sides of a triangle and returns one of four values to determine
the triangle type (1=scalene, 2=isosceles, 3=equilateral, 4=error)
Also they asked to use enums and OOP approach for this solution,
but without OOP approach I built that on following way
using System;
using System.Linq;
namespace triangleSolution
{
class Program
{
static void Main(string[] args)
{
int[] values = new int[3];
for (int i = 1; i <= 3; i++)
{
Console.WriteLine("Please enter side " + i +" value");
values[i - 1] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine(GetTriangleType(values[0], values[1], values[2]));
Console.WriteLine();
}
public enum TriangleType
{
Scalene = 1, // no two sides are the same length
Isosceles = 2, // two sides are the same length and one differs
Equilateral = 3, // all sides are the same length
Error = 4 // inputs can't produce a triangle
}
public static TriangleType GetTriangleType(int a, int b, int c)
{
// There should also be a side length check
if (a <= 0 || b <= 0 || c <= 0)
{
return TriangleType.Error;
}
if (a == b && a == c) // These could also be their own methods
{
return TriangleType.Equilateral;
}
else if (a == b || a == c || b == c)
{
return TriangleType.Isosceles;
}
else
{
return TriangleType.Scalene;
}
}
}
}
I wish to extend above solution in OOP way though I failed that interview :)
There are many ways to do this (based on the vague description)
e.g.: use a,b,c in a class Triangle that has a property TriangleType
But I have to say, the wording
Write me a function that ...
Is very misleading if OOP was what they were after.
public enum TriangleType
{
Scalene = 1, // no two sides are the same length
Isosceles = 2, // two sides are the same length and one differs
Equilateral = 3, // all sides are the same length
Error = 4 // inputs can't produce a triangle
}
public class Triangle
{
public TriangleType TriangleType {get; private set;}
public int SideA {get; private set;}
public int SideB {get; private set;}
public int SideC {get; private set;}
public Triangle(int a, int b, int c)
{
SideA = a;
SideB = b;
SideC = c;
TriangleType = GetTryangleType(a,b,c);
}
public static TriangleType GetTriangleType(int a, int b, int c)
{
// There should also be a side length check
if (a <= 0 || b <= 0 || c <= 0)
{
return TriangleType.Error;
}
if (a == b && a == c) // These could also be their own methods
{
return TriangleType.Equilateral;
}
else if (a == b || a == c || b == c)
{
return TriangleType.Isosceles;
}
else
{
return TriangleType.Scalene;
}
}
}
Usage:
static void Main(string[] args)
{
int[] values = new int[3];
for (int i = 1; i <= 3; i++)
{
Console.WriteLine("Please enter side " + i +" value");
values[i - 1] = Int32.Parse(Console.ReadLine());
}
Triangle triangle = new Triangle(values[0], values[1], values[2]);
Console.WriteLine(triangle.TriangleType);
Console.WriteLine();
}
You could use a triangle class, something like this
public class Triangle
{
private int[] sideLength;
public Triangle(int side1, int side2, int side3)
{
sideLength = new int[3];
sideLength[0] = side1;
sideLength[1] = side2;
sideLength[2] = side3;
}
public TriangleType GetTriangleType()
{
//Your code here ;)
}
}

Knights Tour recursive C# Im getting something wrong in my way of doing the stuff

class Knight
{
public static readonly double LegalDistance = Math.Sqrt(5);
public Stack<Field> Steps { get; set; }
private static readonly List<Field> board = Board.GameBoard;
private static List<Field> fields;
private static readonly Random random = new Random();
private static readonly object synLock = new object();
public Knight(Field initial)
{
Steps = new Stack<Field>();
Steps.Push(initial);
}
public void Move()
{
Field destination = Choose();
if (destination == null)
{
return;
}
Console.WriteLine("Moving from " + GetPosition().GetFieldName() + " to " + destination.GetFieldName());
Steps.Push(destination);
}
public Field Back()
{
Field from = Steps.Pop();
Console.WriteLine("Moving back from " + from.GetFieldName() + " to " + GetPosition().GetFieldName());
return from;
}
public Field Choose()
{
List<Field> legalMoves = Behaviour();
legalMoves.RemoveAll(field => Steps.Contains(field, new FieldValueComparer()));
if (legalMoves.Count == 0)
{
return null;
}
Field theChoosenOne;
int index;
lock (synLock)
{
index = random.Next(0, legalMoves.Count);
}
theChoosenOne = legalMoves.ElementAt(index);
return theChoosenOne;
}
private List<Field> Behaviour()
{
fields = new List<Field>();
fields.AddRange(board);
for (int i = fields.Count - 1; i >= 0; i--)
{
double actualDistance = fields[i].GetDistance(GetPosition());
if (!actualDistance.Equals(LegalDistance))
{
fields.Remove(fields[i]);
}
}
return fields;
}
public List<Field> GetSteps()
{
return Steps.ToList();
}
public Field GetPosition()
{
return Steps.Peek();
}
}
So this is how I'd do the stuff. The problem is, I am missing some key functionality, because on low given stepcount it backtracks to the start, on high stepcount, it causes StackOverFlow.
Here are some other functions to let you understand what I want to do:
Calculating distance:
public double GetDistance(Field other)
{
return Math.Sqrt(Math.Pow(other.X - X, 2) + Math.Pow(other.Y - Y, 2));
}
Finding the path:
class PathFinder
{
public static void FindPath(Knight knight)
{
if (knight.Steps.Count != 20)
{
knight.Move();
FindPath(knight);
knight.Back();
}
}
}
Your path search is essentially random walk. On large board, this may take a while anyway.
Now about StackOverflow: notice that you don't push anything on Move() when there are no places to go. So, on recursive call to FindPath() there still will be the same knight.Steps.Count, the same position, the same null return on Choose()... and so on, until you're out of stack space.
Obvious fix would be to add bool return value to Move() indicating if there was any move. Unless there is actual reason behind using random moves, more deterministic search algorithm is recommended.

What is the best way to find the digit at n position in a decimal number?

Background
I'm working on a symmetric rounding class and I find that I'm stuck with regards to how to best find the number at position x that I will be rounding. I'm sure there is an efficient mathematical way to find the single digit and return it without having to resort to string parsing.
Problem
Suppose, I have the following (C#) psuedo-code:
var position = 3;
var value = 102.43587m;
// I want this no ↑ (that is 5)
protected static int FindNDigit(decimal value, int position)
{
// This snippet is what I am searching for
}
Also, it is worth noting that if my value is a whole number, I will need to return a zero for the result of FindNDigit.
Does anyone have any hints on how I should approach this problem? Is this something that is blaringly obvious that I'm missing?
(int)(value * Math.Pow(10, position)) % 10
How about:
(int)(double(value) * Math.Pow(10, position)) % 10
Basically you multiply by 10 ^ pos in order to move that digit to the one's place, and then you use the modulus operator % to divide out the rest of the number.
using System;
public static class DecimalExtensions
{
public static int DigitAtPosition(this decimal number, int position)
{
if (position <= 0)
{
throw new ArgumentException("Position must be positive.");
}
if (number < 0)
{
number = Math.Abs(number);
}
number -= Math.Floor(number);
if (number == 0)
{
return 0;
}
if (position == 1)
{
return (int)(number * 10);
}
return (number * 10).DigitAtPosition(position - 1);
}
}
Edit:
If you wish, you may separate the recursive call from the initial call, to remove the initial conditional checks during recursion:
using System;
public static class DecimalExtensions
{
public static int DigitAtPosition(this decimal number, int position)
{
if (position <= 0)
{
throw new ArgumentException("Position must be positive.");
}
if (number < 0)
{
number = Math.Abs(number);
}
return number.digitAtPosition(position);
}
static int digitAtPosition(this decimal sanitizedNumber, int validPosition)
{
sanitizedNumber -= Math.Floor(sanitizedNumber);
if (sanitizedNumber == 0)
{
return 0;
}
if (validPosition == 1)
{
return (int)(sanitizedNumber * 10);
}
return (sanitizedNumber * 10).digitAtPosition(validPosition - 1);
}
Here's a few tests:
using System;
using Xunit;
public class DecimalExtensionsTests
{
// digit positions
// 1234567890123456789012345678
const decimal number = .3216879846541681986310378765m;
[Fact]
public void Throws_ArgumentException_if_position_is_zero()
{
Assert.Throws<ArgumentException>(() => number.DigitAtPosition(0));
}
[Fact]
public void Throws_ArgumentException_if_position_is_negative()
{
Assert.Throws<ArgumentException>(() => number.DigitAtPosition(-5));
}
[Fact]
public void Works_for_1st_digit()
{
Assert.Equal(3, number.DigitAtPosition(1));
}
[Fact]
public void Works_for_28th_digit()
{
Assert.Equal(5, number.DigitAtPosition(28));
}
[Fact]
public void Works_for_negative_decimals()
{
const decimal negativeNumber = -number;
Assert.Equal(5, negativeNumber.DigitAtPosition(28));
}
[Fact]
public void Returns_zero_for_whole_numbers()
{
const decimal wholeNumber = decimal.MaxValue;
Assert.Equal(0, wholeNumber.DigitAtPosition(1));
}
[Fact]
public void Returns_zero_if_position_is_greater_than_the_number_of_decimal_digits()
{
Assert.Equal(0, number.DigitAtPosition(29));
}
[Fact]
public void Does_not_throw_if_number_is_max_decimal_value()
{
Assert.DoesNotThrow(() => decimal.MaxValue.DigitAtPosition(1));
}
[Fact]
public void Does_not_throw_if_number_is_min_decimal_value()
{
Assert.DoesNotThrow(() => decimal.MinValue.DigitAtPosition(1));
}
[Fact]
public void Does_not_throw_if_position_is_max_integer_value()
{
Assert.DoesNotThrow(() => number.DigitAtPosition(int.MaxValue));
}
}
Edited: Totally had the wrong and opposite answer here. I was calculating the position to the left of the decimal instead of the right. See the upvoted answers for the correct code.
I found this one here working:
public int ValueAtPosition(int value, int position)
{
var result = value / (int)Math.Pow(10, position);
result = result % 10;
return result;
}
And also this one to know the full value (i.e.: 111, position 3 = 100 , sorry I don't know the proper name):
public int FullValueAtPosition(int value, int position)
{
return this.ValueAtPosition(value, position) * (int)Math.Pow(10, position);
}
How about this:
protected static int FindNDigit(decimal value, int position)
{
var index = value.ToString().IndexOf(".");
position = position + index;
return (int)Char.GetNumericValue(value.ToString(), position);
}
None of the previous solutions worked for me, so here is a working one :
var result = value / Math.Pow(10, Math.Truncate((Math.Log10(value) + 1) - position));
return (int)(result % 10);

Categories

Resources