C
C#11mo ago
Chukwuma

I would cherish if someone would share an idea on how to solve the Simple Algebra Problem.

You are given a mathematical function f(x) and a value of a. Your task is to determine if the equation f(x) = a has a solution in real numbers. Although this problem may seem simple at first, be prepared for a challenge, as the function you are given is longer and much more complex than what you may have encountered in a typical algebra class. The function is a big composition of exp(x) and log(x) functions. For a clearer understanding, refer to the examples provided. The exponential function exp(x) or e^x raises the constant e (approximately equal to 2.718281828459045) to the power of x, while the logarithmic function log(x) gives the power to which e must be raised to obtain the value x. Input The first line of input contains a string that represents the given function. It is a composition of between 1 and 10^5 exp(x) and log(x) functions. More specifically, exp(x) and log(x) are valid functions, and if f(x) is a valid function, so are exp(f(x)) and log(f(x)). The second line of input contains a decimal number a (−10^9 ≤ a ≤ 10^9), given with exactly two decimal places. Output Print “Yes” if f(x) = a has a solution in real numbers, or “No” otherwise. Examples standard input standard output exp(exp(x)) No 0.50
log(log(exp(log(x)))) Yes -2.39 exp(x) No 0.00 exp(log(x)) No 0.00
1 Reply
Chukwuma
ChukwumaOP11mo ago
using System;
using System.Collections.Generic;
using System.Linq;

public abstract class Expr
{
public class Exp : Expr
{
public Expr Inner { get; }
public Exp(Expr inner) => Inner = inner;
}
public class Log : Expr
{
public Expr Inner { get; }
public Log(Expr inner) => Inner = inner;
}
public class X : Expr { }
}

public class Parser
{
public (Expr, List<string>) ParseExpr(List<string> tokens)
{
if (!tokens.Any())
throw new Exception("Unexpected end of expression");
var rest = tokens.Skip(1).ToList();
switch (tokens.First())
{
case "exp":
var (innerExp, restExp) = ParseExpr(rest);
return (new Expr.Exp(innerExp), restExp);
case "log":
var (innerLog, restLog) = ParseExpr(rest);
return (new Expr.Log(innerLog), restLog);
case "(":
var (innerParen, restParen) = ParseExpr(rest);
if (!restParen.Any() || restParen.First() != ")")
throw new Exception("Missing closing parenthesis");
return (innerParen, restParen.Skip(1).ToList());
default:
return (new Expr.X(), tokens);
}
}

public string SolveExpr(Expr expr, float a)
{
if (a < 0.0)
return "No";
return expr switch
{
Expr.Exp _ when a == 0.0 => "No",
_ => "Yes",
};
}

public string Solve(string f, float a)
{
var tokens = f.Split('(').ToList();
var (expr, _) = ParseExpr(tokens);
return SolveExpr(expr, a);
}
}
using System;
using System.Collections.Generic;
using System.Linq;

public abstract class Expr
{
public class Exp : Expr
{
public Expr Inner { get; }
public Exp(Expr inner) => Inner = inner;
}
public class Log : Expr
{
public Expr Inner { get; }
public Log(Expr inner) => Inner = inner;
}
public class X : Expr { }
}

public class Parser
{
public (Expr, List<string>) ParseExpr(List<string> tokens)
{
if (!tokens.Any())
throw new Exception("Unexpected end of expression");
var rest = tokens.Skip(1).ToList();
switch (tokens.First())
{
case "exp":
var (innerExp, restExp) = ParseExpr(rest);
return (new Expr.Exp(innerExp), restExp);
case "log":
var (innerLog, restLog) = ParseExpr(rest);
return (new Expr.Log(innerLog), restLog);
case "(":
var (innerParen, restParen) = ParseExpr(rest);
if (!restParen.Any() || restParen.First() != ")")
throw new Exception("Missing closing parenthesis");
return (innerParen, restParen.Skip(1).ToList());
default:
return (new Expr.X(), tokens);
}
}

public string SolveExpr(Expr expr, float a)
{
if (a < 0.0)
return "No";
return expr switch
{
Expr.Exp _ when a == 0.0 => "No",
_ => "Yes",
};
}

public string Solve(string f, float a)
{
var tokens = f.Split('(').ToList();
var (expr, _) = ParseExpr(tokens);
return SolveExpr(expr, a);
}
}
Want results from more Discord servers?
Add your server