typeofweb-org / typeofweb-org/devfaq

Mistake in test case Question #230

Open
#331 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
40
Forks
10
PR merge metrics
No merged PRs in 30d

Description

There is a mistake in this question.

image

It is written that "{dklf(df(kl))d]{}" is a valid test case while it is not due to the not opened "[".

Please update the question.

The code I used to test the cases:

const sum = (arr: number[]): number => arr.reduce((acc, el) => acc+el, 0);

type OpeningBracket = "(" | "[" | "{";
type BracketStackMap = Record<OpeningBracket, number>;

const checkBrackets = (str: string) => {
  const brackets = ["(",")", "[","]", "{","}"]; // "()[]{}".split("");
  
  const filtered = str.split("").filter(character => brackets.includes(character));
  const bracketsStack = new Map<BracketStackMap>([["(", 0], ["[", 0], ["{", 0]]);

  for(let i=0; i<filtered.length; i++){
    // could be simplified with closed/opening bracket key/value mapping
    const key = filtered[i] === ")" ? "("
          : filtered[i] === "]" ? "["
          : filtered[i] === "}" ? "{"
          : filtered[i];
    
    const currentVal = bracketsStack.get(key);
    
    // console.log({i, str: filtered[i], key})
    
    switch(filtered[i]){
      case "(":
      case "[":
      case "{":
        bracketsStack.set(filtered[i], currentVal + 1);
        break;
      case ")":
      case "]":
      case "}":
        bracketsStack.set(key, currentVal - 1);
        break;
      default: throw new Error(`Unhandled bracket: ${filtered[i]}`);
    }
    
    if(bracketsStack.get(key) < 0){
      // console.log("negative", bracketsStack.get(key))
      return false;
    };
  }
  
  return (sum([...bracketsStack.values()]) === 0);
};

const tests = {
  "{ac[bb]}": true, 
  "{dklf(df(kl))d]{}": true, //fails, should pass according to task description
  "{[[[]]]}": true,
  "{3234[fd": false,
  "{df][d}": false
};

Object.entries(tests).forEach(([test, expected]) => {
  const result = (checkBrackets(test) === expected) ? "✅" : "❌";
  
  console.log(`${result} "${test}" `);
});
// results
"✅ '{ac[bb]}' "
"❌ '{dklf(df(kl))d]{}' "
"✅ '{[[[]]]}' "
"✅ '{3234[fd' "
"✅ '{df][d}' "

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open question 230 from the linked DevFAQ page and compare the stated valid test case with the issue’s bracket analysis and TypeScript reproduction. Update the question so the example with the unmatched closing bracket is represented correctly, then verify that the displayed test cases and expected results are consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
content
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.