Pregunta de entrevista

Entrevista de Full Stack Developer

-

monday.com

Given a formula (string) and a dict, replace each key that appears in the formula with its value. "CONCATENATE("#first_name#", " - ", 30)" { 'first_name': "Moshe", 'age': 20, 'status': "Done"} Result: "CONCATENATE("Moshe", " - ", 30)"

Respuesta

Respuestas de entrevistas

9 respuestas

5

column_id_to_value = {'first_name': "Moshe", 'age': 20, 'status': "Done"} formula = 'CONCATENATE("#first_name#", " - ", #age#)' dotIt(formula, column_id_to_value) def dotIt(formula, dictionary): answer = [] formula_list = formula.split("#") for form in formula_list: if form in dictionary: answer.append(str(dictionary[form])) else: answer.append(form) print(''.join(answer))

Roy en

1

let formula = "CONCATENATE('#first_name#', ' - ', '#age#')"; let user = { first_name: "Moshe", age: 20, status: "Done"}; let keys = Object.keys(user); function getKeys() { const keys = Object.keys(user); keys.forEach( key => formula = formula.replace(new RegExp('#'+key+'#'), user[key])); console.log(formula); } getKeys();

Anónimo en

0

def calculate_for_row(formula, column_id_to_value): result = [] replaced_last_part = False new_formula_parts = formula.split('#') for index, part in enumerate(new_formula_parts): if part in column_id_to_value: result.append(str(column_id_to_value[part])) replaced_last_part = True else: if index > 0 and not replaced_last_part: result.append('#') result.append(part) replaced_last_part = False parse(''.join(result)) def parse(formula): print(formula) column_id_to_value = {'first_name': "Moshe", 'age': 20, 'status': "Done"} formula = 'CONCATENATE("#first_name#", " - ", #age#)' calculate_for_row(formula, column_id_to_value)

Anónimo en

0

const formula = "=CONCATENATE('Name: ','#fiRst_name#','; Age: ','#aGe#','; Status: ','#sTATUS#')"; const user = { 'first_name': 'Moshe', 'age': 20, 'status': "Done" }; const extractedKeys = formula.match(/#[a-z_]+#/gi); console.log(extractedKeys); let adjustedFormula = formula; extractedKeys.forEach( key => { objKey = key.replaceAll('#','').toLowerCase(); adjustedFormula = user[objKey] ? adjustedFormula.replaceAll(key,user[objKey]) : adjustedFormula; }); console.log(adjustedFormula);

Anónimo en

0

const formula = "=CONCATENATE('Name: ','#fiRst_name#','; Age: ','#aGe#','; Status: ','#sTATUS#')"; const user = { 'first_name': 'Moshe', 'age': 20, 'status': "Done" }; const extractedKeys = formula.match(/#[a-z_]+#/gi); console.log(extractedKeys); let adjustedFormula = formula; extractedKeys.forEach( key => { objKey = key.replaceAll('#','').toLowerCase(); adjustedFormula = user[objKey] ? adjustedFormula.replaceAll(key,user[objKey]) : adjustedFormula; }); console.log(adjustedFormula);

Anónimo en

0

const formula = "=CONCATENATE('Name: ','#fiRst_name#','; Age: ','#aGe#','; Status: ','#sTATUS#')"; const user = { 'first_name': 'Moshe', 'age': 20, 'status': "Done" }; const extractedKeys = formula.match(/#[a-z_]+#/gi); console.log(extractedKeys); let adjustedFormula = formula; extractedKeys.forEach( key => { objKey = key.replaceAll('#','').toLowerCase(); adjustedFormula = user[objKey] ? adjustedFormula.replaceAll(key,user[objKey]) : adjustedFormula; }); console.log(adjustedFormula);

Anónimo en

0

const formula = "=CONCATENATE('Name: ','#fiRst_name#','; Age: ','#aGe#','; Status: ','#sTATUS#')"; const user = { 'first_name': 'Moshe', 'age': 20, 'status': "Done" }; const extractedKeys = formula.match(/#[a-z_]+#/gi); console.log(extractedKeys); let adjustedFormula = formula; extractedKeys.forEach( key => { objKey = key.replaceAll('#','').toLowerCase(); adjustedFormula = user[objKey] ? adjustedFormula.replaceAll(key,user[objKey]) : adjustedFormula; }); console.log(adjustedFormula);

Anónimo en

0

const formula = 'CONCATENATE("#first_name#", " - ", #age#)' const obj = { "first_name" : "Moshe", "age" : 30, "status" : "done" }; const replace = (formula, obj)=> { const strArr = formula.split(','); for(const [i, section] of strArr.entries()) { const result = section.match(/#.+#/ig); if(result) { const removeAt = section.split('#')[1] strArr[i] = section.replace(result[0], obj[removeAt]); } } return strArr.join(''); } console.log(replace(formula, obj))

asnwer en

0

const dict = { 'first_name': "Moshe", 'age': 20, 'status': 'Done' } const formula = 'CONCATENATE("#first_name#", " - ", #age#)' const replace = (formula, dict) => { const formulaSplitted = formula.split(','); const newFormula = formulaSplitted .map(k => { let match = k.match(/#.+#/ig); if (match) { return match[0].replace(/#/g, ''); } }) .filter(key => dict[key]) .reduce((str, key) => str.replace(`#${key}#`, dict[key]) , formula ) return newFormula } console.log(replace(formula, dict));

functional answer en

Añadir respuestas o comentarios

Para publicar un comentario sobre esto, inicia sesión o regístrate.