├── Baseball Game ├── README.md └── Solution.py ├── LICENSE ├── README.md └── Valid_Parentheses ├── README.md └── Solution.py /Baseball Game/README.md: -------------------------------------------------------------------------------- 1 | ## Baseball Game: 2 | 3 | You are keeping score for a baseball game with strange rules. The game consists of several rounds where the scores of past rounds may affect future rounds' scores. 4 | 5 | At the beginning of the game, you start with an empty record. you are given a list of strings `ops`, where `ops[i]` is the `ith` operation you must apply to the record and is one 6 | of the following: 7 | 8 | - An integer `x` - Record a new score of `X`. 9 | - `"+"` - Record a new score that is the sum of the previous two scores. It is guaranteed that there will always be two previous scores. 10 | - `"D"` - Record a new score that is double the previous score. It is guaranteed that there will always be a previous score. 11 | - `"C"` - Invalidate the previous score, removing it from the record. It is guaranteed that there will always be a previous score. 12 | 13 | Return *the sum of all the scores on the record*. 14 | 15 | Example 1: 16 | 17 | `Input: ops = ["5", "2", "C", "D", "+"]` 18 | 19 | `Output: 30` 20 | 21 | Example 2: 22 | 23 | `Input: ops = ["5", "-2", "4", "C", "D", "9", "+", "+"]` 24 | 25 | `Output: 27` 26 | 27 | Example 3: 28 | 29 | `Input: ops = ["1"]` 30 | 31 | `Output: 1` 32 | 33 | 34 | ## Constraints 35 | 36 | - `1 <= ops.lenght <= 1000` 37 | - `ops[i]` is `"C"`, `"D"`, `"+"`, or a string representing an integer in the range `[-3 * 104, 3 * 104]` 38 | - For operation `"+"`, there will always be two previous scores on the record. 39 | - For operation `"C"` and `"D"`, there will always be at least one previous score on the record. 40 | -------------------------------------------------------------------------------- /Baseball Game/Solution.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Apr 11 17:48:44 2022 4 | 5 | @author: Noah 6 | """ 7 | 8 | def calPoints(ops) -> int: 9 | 10 | records = [] 11 | 12 | for op in ops: 13 | 14 | if op.isdigit() == True or '-' in op: 15 | records.append(int(op)) 16 | 17 | elif op == 'C': 18 | del records[-1] 19 | 20 | elif op == 'D': 21 | records.append(int(2*records[-1])) 22 | 23 | elif op == '+': 24 | records.append(int(records[-1]+records[-2])) 25 | 26 | result = sum(records) 27 | return result 28 | 29 | if __name__ == "__main__": 30 | line = '5 -2 C D +' 31 | ops = line.strip().split() 32 | 33 | print(calPoints(ops)) 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Turing-Coding-Challenges 2 | I'm sick of so much coding challenges to get a job, so I decided to create this repository to save the answers for later copypasting. 3 | -------------------------------------------------------------------------------- /Valid_Parentheses/README.md: -------------------------------------------------------------------------------- 1 | ## Valid Parentheses: 2 | 3 | Given a string `s` containing just the characters `'('`, `')'`, `'['`, `']'`, `'{'`, `'}'`, determine if the input string is valid. 4 | 5 | An input string is valid if: 6 | 7 | - Open brackets must be closed by the same type of brackets 8 | - Open brackets must be closed in the correct order 9 | 10 | ## Constraints 11 | 12 | - `1 <= s.lenght <= 104` 13 | - `s` consists of parentheses only `'()[]{}'` 14 | 15 | Example 1: 16 | 17 | `Input: s = "()"` 18 | 19 | `Output: valid` 20 | 21 | Example 2: 22 | 23 | `Input: s = "()[]{}"` 24 | 25 | `Output: valid` 26 | 27 | Example 3: 28 | 29 | `Input: s = "(]"` 30 | 31 | `Output: invalid` 32 | 33 | Example 4: 34 | 35 | `Input: s = "([)]"` 36 | 37 | `Output: invalid` 38 | 39 | Example 5: 40 | 41 | `Input: s = "{[]}"` 42 | 43 | `Output: valid` 44 | -------------------------------------------------------------------------------- /Valid_Parentheses/Solution.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Apr 11 17:48:44 2022 4 | 5 | @author: Noah 6 | """ 7 | 8 | def isValid(s: str) -> bool: 9 | bracket_couples = ['()', 10 | '[]', 11 | '{}' 12 | ] 13 | 14 | while s: 15 | initial_lenght = len(s) 16 | 17 | for bracket_couple in bracket_couples: 18 | s = s.replace(bracket_couple, '') 19 | 20 | final_lenght = len(s) 21 | 22 | if initial_lenght == final_lenght: 23 | result = False 24 | return result 25 | 26 | result = True 27 | return result 28 | 29 | if __name__ == "__main__": 30 | line = '(){}()[({})]' 31 | 32 | if isValid(line): 33 | print("valid") 34 | 35 | else: 36 | print("invalid") 37 | --------------------------------------------------------------------------------