-
Notifications
You must be signed in to change notification settings - Fork 96
lesson2-AndrewKoldyrev-homework #160
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
Open
Raventus
wants to merge
1
commit into
nickovchinnikov:nick/lesson2
Choose a base branch
from
Raventus:homework1-AndrewKoldyrev
base: nick/lesson2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { bracketChecker } from "./bracketChecker"; | ||
|
|
||
| describe("bracketChecker correct cases", () => { | ||
| it("2 ( and 2 )", () => { | ||
| expect(bracketChecker("( 5 + ( 5 + 5 ) )")).toEqual(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("bracketChecker incorrect cases, ", () => { | ||
| it("to many )", () => { | ||
| expect(bracketChecker("( 5 + ( 5 + 5 ) ) )")).toEqual(false); | ||
| }); | ||
|
|
||
| it("to many (", () => { | ||
| expect(bracketChecker("( 5 + ( 5 + 5 )")).toEqual(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| export const openBracket = "("; | ||
| export const closeBracker = ")"; | ||
|
|
||
| export const bracketChecker = function (line: string): boolean { | ||
| const stackForBracketCheckArray = line.split(" "); | ||
| const stackForBreaket: string[] = []; | ||
|
|
||
| let correctOrder = true; | ||
|
|
||
| stackForBracketCheckArray.forEach((item) => { | ||
| if (item === openBracket) { | ||
| console.log("bracket push", item); | ||
| stackForBreaket.push(item); | ||
| } else if (item === closeBracker) { | ||
| const bracket = stackForBreaket.pop(); | ||
| console.log("bracket pop", bracket); | ||
| if (bracket !== openBracket) { | ||
| correctOrder = false; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return stackForBreaket.length === 0 && correctOrder; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { isNumber } from "./helpers"; | ||
| import { mathOperators } from "./mathOperators"; | ||
| import { mathOperators, singleOperators } from "./mathOperators"; | ||
| import { openBracket, closeBracker } from "./bracketChecker"; | ||
|
|
||
| export type ParsedLineType = (number | string)[]; | ||
|
|
||
|
|
@@ -9,19 +10,50 @@ export const parser = (line: string): ParsedLineType | null => { | |
| return stack.reduce<ParsedLineType>((result, item, key) => { | ||
| const prevItem = stack[key - 1]; | ||
|
|
||
| const isValidNumberPush = !isNumber(prevItem) && isNumber(item); | ||
| // for numbers | ||
| const isValidNumberPush = | ||
| !isNumber(prevItem) && | ||
| isNumber(item) && | ||
| prevItem !== closeBracker && | ||
| !singleOperators.hasOwnProperty(prevItem); | ||
|
|
||
| // for binary operators | ||
| const isValidOperatorPush = | ||
| isNumber(prevItem) && | ||
| !isNumber(item) && | ||
| mathOperators.hasOwnProperty(item); | ||
| isNumber(prevItem) && mathOperators.hasOwnProperty(item); | ||
|
|
||
| // for singleOperator | ||
| const isValidZeroOperator = | ||
| isNumber(prevItem) && singleOperators.hasOwnProperty(item); | ||
| const isSequenceOfZeroOperator = | ||
| singleOperators.hasOwnProperty(item) && | ||
| singleOperators.hasOwnProperty(prevItem); | ||
| const isOperatorAfterZeroOperator = | ||
| mathOperators.hasOwnProperty(item) && | ||
| singleOperators.hasOwnProperty(prevItem); | ||
|
|
||
| // for bracket | ||
| const isValidOpenBracket = | ||
| item === openBracket && | ||
| (mathOperators.hasOwnProperty(prevItem) || prevItem === undefined); | ||
| const isValidCloseBracket = | ||
| item === closeBracker && | ||
| (isNumber(prevItem) || singleOperators.hasOwnProperty(prevItem)); | ||
|
|
||
| if (isValidNumberPush) { | ||
| result.push(Number(item)); | ||
| } else if (isValidOperatorPush) { | ||
| } else if ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if без аргумента |
||
| isValidOperatorPush || | ||
| isValidZeroOperator || | ||
| isSequenceOfZeroOperator || | ||
| isOperatorAfterZeroOperator || | ||
| isValidOpenBracket || | ||
| isValidCloseBracket | ||
| ) { | ||
| result.push(item); | ||
| } else { | ||
| throw new TypeError("Unexpected string"); | ||
| } | ||
|
|
||
| return result; | ||
| }, []); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,54 @@ | ||
| import { parser } from "./parser"; | ||
| import { bracketChecker, closeBracker, openBracket } from "./bracketChecker"; | ||
|
|
||
| import { firstPrioritiesCalc, secondPrioritiesCalc } from "./engine"; | ||
| import { | ||
| firstPrioritiesCalc, | ||
| secondPrioritiesCalc, | ||
| zeroPrioritiesCalc, | ||
| } from "./engine"; | ||
|
|
||
| export const runner = (line: string): number => { | ||
| const isBracketCorrect = bracketChecker(line); | ||
|
|
||
| if (!isBracketCorrect) { | ||
| throw console.error("bad format for bracket"); | ||
| } | ||
|
|
||
| const stack = parser(line); | ||
|
|
||
| if (stack === null) { | ||
| throw new TypeError("Unexpected string"); | ||
| } | ||
|
|
||
| const firstPrioritiesRes = firstPrioritiesCalc(stack); | ||
| const bracerStack = stack; | ||
| while (bracerStack.find((index) => index == openBracket)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. данную операцию можно упростить попробовать |
||
| const startIndex = bracerStack.findIndex((index) => index == openBracket); | ||
| const stack = []; | ||
| let lastIndex = startIndex; | ||
| stack.push(openBracket); | ||
| while (stack.length > 0) { | ||
| lastIndex++; | ||
| if (bracerStack[lastIndex] == openBracket) { | ||
| stack.push(openBracket); | ||
| } else if (bracerStack[lastIndex] == closeBracker) { | ||
| stack.pop(); | ||
| } | ||
| } | ||
| const startBracer = startIndex + 1; | ||
| const bracerExpression = bracerStack.slice(startBracer, lastIndex); | ||
| const str = bracerExpression.join(" "); | ||
| const number = runner(str); | ||
|
|
||
| bracerStack.splice(startIndex, lastIndex - startIndex + 1, number); | ||
| } | ||
|
|
||
| const zeroPrioritiesRes = zeroPrioritiesCalc(stack); | ||
|
|
||
| if (zeroPrioritiesRes.length === 1) { | ||
| return Number(zeroPrioritiesRes[0]); | ||
| } | ||
|
|
||
| const firstPrioritiesRes = firstPrioritiesCalc(zeroPrioritiesRes); | ||
|
|
||
| if (firstPrioritiesRes.length === 1) { | ||
| return Number(firstPrioritiesRes[0]); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line.split('').reduce((acc, word) => word === openBracket && acc+1, 0)