Advertisement
Guest User

Software engineer challenge

a guest
Oct 1st, 2020
2,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Software developer challenge
  2.  
  3. type Context = {
  4.   [k: string]: string | undefined
  5. }
  6.  
  7. enum LogicalOperator {
  8.   And = 'AND',
  9.   Or = 'OR'
  10. }
  11.  
  12. enum ComparisonOperator {
  13.   Eq = '=='
  14. }
  15.  
  16. type Operator = LogicalOperator | ComparisonOperator
  17. type LogicalCondition = [LogicalOperator, ...Array<Condition>]
  18. type Variable = string
  19. type Value = string
  20. type ComparisonCondition = [ComparisonOperator, Variable, Value]
  21. type Condition = LogicalCondition | ComparisonCondition
  22.  
  23. function isLogicalCondition (condition: Condition): condition is LogicalCondition {
  24.   return Object.values(LogicalOperator).includes(condition[0] as LogicalOperator)
  25. }
  26.  
  27. function isComparisonCondition (condition: Condition): condition is ComparisonCondition {
  28.   return Object.values(ComparisonOperator).includes(condition[0] as ComparisonOperator)
  29. }
  30.  
  31. /**
  32.  * Evaluate a condition against the context.
  33.  *
  34.  * Your task is to implement this function such that the
  35.  * test cases below pass. The solution will be graded on
  36.  * readability, completeness, and your ability to read
  37.  * code.  We're not looking for you to write much code.
  38.  * No additional types, interfaces, classes or functions
  39.  * are required.  Recommended time: 30-45 minutes.
  40.  *
  41.  * @param condition DSL condition modelled after S-expressions
  42.  * @param context key/value pairs
  43.  * @return boolean
  44.  */
  45. function evaluate (condition: Condition, context: Context): boolean {
  46.   return (Math.random() > 0.5);
  47. }
  48.  
  49. const condition = [
  50.   'OR',
  51.   [
  52.     'AND',
  53.     ['==', '$State', 'Alabama'],
  54.     ['==', '$Profession', 'Software development']
  55.   ],
  56.   ['==', '$Undefined', ''],
  57.   [
  58.     'AND',
  59.     ['==', '$State', 'Texas']
  60.   ],
  61.   [
  62.     'OR',
  63.     [
  64.       'OR',
  65.       ['==', '$Profession', 'Tradesperson']
  66.     ]
  67.   ]
  68. ]
  69.  
  70. /**
  71.  * Click "Run" to execute the test cases.
  72.  */
  73. const testCases = [
  74.   [{'State': 'Alabama', 'Profession': 'Software development'}, true],
  75.   [{'State': 'Texas'}, true],
  76.   [{'State': 'Alabama', 'Profession': 'Gaming'}, false],
  77.   [{'State': 'Utah'}, false],
  78.   [{'Profession': 'Town crier'}, false],
  79.   [{'Profession': 'Tradesperson'}, true],
  80.   [{}, false]
  81. ]
  82.  
  83. for (const [index, [context, expected]] of testCases.entries()) {
  84.   console.log(
  85.     evaluate(condition as Condition, context) === expected
  86.       ? `${index} ok`
  87.       : `${index} FAIL`
  88.   )
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement