Last active
August 30, 2023 19:34
-
-
Save sibu-github/65b943bc5db69e5bbf6841206982a4dc to your computer and use it in GitHub Desktop.
Reverse each word in a sentence
This file contains 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
/** | |
* PROBLEM: Given a sentence reverse each word and print it back. | |
* For example if the given sentence is "Mother India" | |
* then output would be "rehtoM aidnI" | |
* If given sentence is "hare-krishna hare-rama" | |
* then output would be "erah-anhsirk erah-amar" | |
*/ | |
// define a list by chars that we need to split the string with | |
// word bounderies are space, comma, colon, dash, semi colon | |
const split_by_chars = [' ', ',', ':', '-', ';', '']; | |
// define a function which will split the string with given string | |
// reverse it only if it is splitted by = '' | |
// call the same function recursively with next splitBy char in the sequence | |
// join the result | |
function splitReverseJoin(str, splitBy) { | |
// if input param is not string | |
// or str is empty or single char | |
// then end recursion | |
if (typeof str !== 'string' || str.length === 0 || str.length === 1) { | |
return str; | |
} | |
const splitted = str.split(splitBy); | |
// when splitBy = '' that means no more splitting and all chars need to reversed | |
if (splitBy === '') { | |
return splitted.reverse().join(''); | |
} | |
// otherwise get the nextSplitBy char from the sequence | |
const nextSplitBy = split_by_chars[split_by_chars.findIndex((ch) => ch === splitBy) + 1]; | |
// call recursively with nextSplitBy for each splitted parts | |
// join the result and return the value | |
return splitted.map((part) => splitReverseJoin(part, nextSplitBy)).join(splitBy); | |
} | |
{ | |
const testStr = 'I am Chanchal'; | |
const expected = 'I ma lahcnahC'; | |
const actual = splitReverseJoin(testStr, ' '); | |
console.log(actual === expected ? 'PASSED' : 'FAILED'); | |
} | |
{ | |
const testStr = 'come, v cat,closer'; | |
const expected = 'emoc, v tac,resolc'; | |
const actual = splitReverseJoin(testStr, ' '); | |
console.log(actual === expected ? 'PASSED' : 'FAILED'); | |
} | |
{ | |
const testStr = 'mon-manush:matal'; | |
const expected = 'nom-hsunam:latam'; | |
const actual = splitReverseJoin(testStr, ' '); | |
console.log(actual === expected ? 'PASSED' : 'FAILED'); | |
} | |
{ | |
const testStr = 'dabanol-hati:ghora-gari dabanol-hati:ghora-gari'; | |
const expected = 'lonabad-itah:arohg-irag lonabad-itah:arohg-irag'; | |
const actual = splitReverseJoin(testStr, ' '); | |
console.log(actual === expected ? 'PASSED' : 'FAILED'); | |
} | |
{ | |
const testStr = 'Mother India'; | |
const expected = 'rehtoM aidnI'; | |
const actual = splitReverseJoin(testStr, ' '); | |
console.log(actual === expected ? 'PASSED' : 'FAILED'); | |
} | |
{ | |
const testStr = 'hare-krishna hare-rama'; | |
const expected = 'erah-anhsirk erah-amar'; | |
const actual = splitReverseJoin(testStr, ' '); | |
console.log(actual === expected ? 'PASSED' : 'FAILED'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment