library(tidyverse)
library(readxl)
= "Excel/629 Invert Sign.xlsx"
path = read_excel(path, range = "A1:A10", col_names = "Words")
input = read_excel(path, range = "B1:B10")
test
= input %>%
result mutate(`Answer Expected` = str_replace_all(Words, "([+-])(?=\\d)", function(m) ifelse(m == "+", "-", "+")))
all.equal(result$`Answer Expected`, test$`Answer Expected`)
#> [1] TRUE
Excel BI - Excel Challenge 629
Challenge Description
If a number is preceded by + or -, change + to - and - to +. Ex. qw3-4+ty+8- => -4 and +8 will be replaced with +4 and -8 => qw3+4+ty-8-
Download Practice File - https://lnkd.in/dKU3GxYK
Solutions
Logic:
The str_replace_all function effectively uses the regular expression ([+-])(?= to capture + or - immediately followed by a digit.
The anonymous function (function(m)) dynamically swaps the signs using a simple ifelse. Strengths:
Conciseness:
The pipeline (%>%) ensures a clean and readable workflow.
Verification: all.equal confirms correctness against the expected results.
Areas for Improvement:
Flexibility: If there are non-standard input formats (e.g., spaces around signs or digits), the solution might need additional handling.
Documentation: Brief comments explaining the regex pattern would make the code more beginner-friendly.
Gems:
- The regex is succinct and precisely targets the problem. The dynamic function for sign swapping (function(m)) is a nice touch.
import pandas as pd
import re
= "629 Invert Sign.xlsx"
path input = pd.read_excel(path, usecols="A", skiprows=0, nrows=10)
= pd.read_excel(path, usecols="B", skiprows=0, nrows=10)
test
def switch_sign(match):
= {"+": "-", "-": "+"}
signs return signs[match.group(1)]
= input['Words'].apply(lambda x: re.sub(r"([+-])(?=\d)", switch_sign, x))
result print(result.equals(test['Answer Expected'])) # True
Logic:
The regex pattern ([+-])(?= is identical to the R solution and functions in the same manner.
re.sub is used alongside a helper function switch_sign to replace the matched signs dynamically.
Strengths:
Modularity: The switch_sign function is a reusable and modular approach to handling the sign inversion.
Readability: The solution is straightforward, with logical steps for processing the data.
Areas for Improvement:
Edge Cases: Similar to the R solution, additional testing for unusual input formats would enhance robustness.
Performance: While the solution works efficiently for small datasets, larger datasets might benefit from vectorized operations in pandas rather than row-wise application.
Gems:
The use of re.sub with a custom function ensures flexibility in extending or modifying the logic.
Applying result.equals(test[‘Answer Expected’]) ensures validation of correctness.
Difficulty Level
This task is of moderate complexity:
- It involves knowledge of regular expressions, which can be challenging for beginners.
- The task requires dynamic replacement logic, which adds an extra layer of difficulty.