Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved Assignment-1 Successfully #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 120 additions & 8 deletions Tavisca.Bootcamp.LanguageBasics.Exercise1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tavisca.Bootcamp.LanguageBasics.Exercise1
{
class Program
class FixMultiplication
{
static void Main(string[] args)
{
Expand All @@ -14,16 +14,128 @@ static void Main(string[] args)
Console.ReadKey(true);
}

private static void Test(string args, int expected)
{
private static void Test(string args, int expected)
{
var result = FindDigit(args).Equals(expected) ? "PASS" : "FAIL";
Console.WriteLine($"{args} : {result}");
Console.WriteLine($"{args} : {result}");
}

public static int FindDigit(string equation)
{
// Add your code here.
throw new NotImplementedException();
public static int FindDigit(string equation)
{
/*
Step 1 :-
Divide the given equation string into 3 differnt strings, each representing a number/operand.
Here s1,s2 and s3 are corresponding string representation of the 3 numbers.
*/

string s1 = "";
string s2 = "";
string s3 = "";
bool is_mult_sign_encountered = false;
bool is_equal_sign_encountered = false;
int question_mark = 0;
for (int i = 0; i < equation.Length; i++)
{
if (equation[i] == '*')
{
is_mult_sign_encountered = true;
continue;
}
if (equation[i] == '=')
{
is_equal_sign_encountered = true;
continue;
}
if (!is_mult_sign_encountered)
{
s1 = s1 + equation[i];
if (equation[i] == '?')
{
question_mark = 1;
}
}
if (is_mult_sign_encountered && !is_equal_sign_encountered)
{
s2 = s2 + equation[i];
if (equation[i] == '?')
{
question_mark = 2;
}
}
if (is_mult_sign_encountered && is_equal_sign_encountered)
{
s3 = s3 + equation[i];
if (equation[i] == '?')
{
question_mark = 3;
}
}
}

/*
Step 2 :-
Determine the faulty string/number and perform the corresponding operation of the rest
two string/number.
*/

string res = "";
string faulty_string = "";
if (question_mark == 1)
{
res = Double.Parse(s3) / Double.Parse(s2) + "";
faulty_string = s1;
}
else if (question_mark == 2)
{
res = Double.Parse(s3) / Double.Parse(s1) + "";
faulty_string = s2;
}
else
{
res = Double.Parse(s1) * Double.Parse(s2) + "";
faulty_string = s3;
}

/*
Step 3 :-
if the length of the resulted string/number which is obtained by the operation of the two string/numbers
except the faulty string/number, differs in the length as of the string/number given in the equation,
gives an indication for the following cases :
1.) Either the resulted number is not an integer i.e. contains decimal fraction.
2.) It contains leading zeros.
*/

if (res.Length != faulty_string.Length)
{
return -1;
}
else
{
for (int i = 0; i < res.Length; i++)
{
/*
Step 4 :-
if the resultant string/number obtained mismatches with the string/number provided in the
equation at any position except '?', we simply return -1
*/

if (res[i] != faulty_string[i] && faulty_string[i] != '?')
{
return -1;
}

/*
Step 5 :-
if we encounter a '?', we return the corresponding digit.
*/

if (res[i] != faulty_string[i] && faulty_string[i] == '?')
{
return Int32.Parse(res[i].ToString());
}
}
}
return -1;
}
}
}