Split string by every new instance of a letter - javascript

Couldn't find anything while searching but I need to split a string by each new instance of a letter. I know split() would output: (M,B,A,M,M,D,X,X,M,M,M,G,G,M,M,Z) but can't figure out how to group them.
Exmaple: "MBAMMDXXMMMGGMMZ" should output to:
M
B
A
MM
D
XX
MMM
GG
MM
Z
str = "MBAMMDXXMMMGGMMZ"
newStr = str.split("")
console.log(newStr)

One regex approach would be to do the following find and replace:
Find: (.)(?!\1)
Replace: $1[ ]
This regex pattern will capture each letter which is not followed by the same letter, and in that case insert a space after the captured letter:
(.) capture each letter
(?!\1) which is NOT followed by the same letter (i.e. is followed by a different letter)
Sample script:
var input = "MBAMMDXXMMMGGMMZ";
var output = input.replace(/(.)(?!\1)/g, "$1 ");
console.log(input + "\n" + output);

If you'd like to avoid regular expressions you may convert the string into an array and use the reduce method.
var input = "MBAMMDXXMMMGGMMZ";
Array.from(input).reduce((prev, curr, index, array) => {
let next;
if (prev.includes(curr)) {
// If char matches previous group, add it.
next = prev + curr;
} else {
// If it differs, log previous group and begin a new one.
console.log(prev);
next = curr;
}
// If that's the last iteration, log the final group.
if (index == array.length - 1) {
console.log(next);
}
return next;
});
It is also possible to use this method to get an array of grouped string, it just requires changing the console logging to pushing groups into some array.
var input = "MBAMMDXXMMMGGMMZ";
var output = [];
Array.from(input).reduce((prev, curr, index, array) => {
let next;
if (prev.includes(curr)) {
// If char matches previous group, add it.
next = prev + curr;
} else {
// If it differs, add previous group to an output array and begin a new one.
output.push(prev);
next = curr;
}
// If that's the last iteration, push the final group.
if (index == array.length - 1) {
output.push(next);
}
return next;
});
console.log(output);

Related

vowelFrequency JavaScript function

I need to iterate over an input string and return a string with the count of the frequency of the different vowels in the string. The vowels in the return string should be in the order they appear in the input string. So, if 'hello world' is the function's parameter, the function should return e1o2. The code I have so far is below. It returns e1o2o3. For some reason, it is not stopping the count of o after it hits the o in hello, and seems to be counting the o in world as a separate count. I think it is, finalString += char + sum;, that is causing this. But, I do not know how to create this function in the first place. Thank you for your time.
function vowelFrequency(str) {
let finalString = '';
let sum = 0;
for (let char of str) {
if ('aeiou'.includes(char)) {
sum += 1;
finalString += char + sum;
}
}
return finalString;
};
The main problem is your sum counter. It counts all vowels together.
Better appoach would be to create a dictionary of vowels
where we add +1 every time we met a match.
In short the idea is:
if (char === 'e') {
dic['e'] += 1;
}
const text = 'hello world';
function vowelFrequency(str) {
let finalString = '';
let dic = {};
for (let char of str) {
if ('aeiou'.includes(char)) {
//check if dictionary has no certain vowel
//it happens when we first time meet a vowel
if (!(char in dic)) {
dic[char] = 0;
}
//increment our counter
dic[char]+=1;
}
}
//by the end of the loop
//we have object with { e: 1, o: 2 }
//now we need to gather values into string
//loop through the object
for ([char, count] of Object.entries(dic)) {
finalString += char + count;
}
return finalString;
};
console.log(vowelFrequency(text));
Shorter version of the same solution would be:
function vowelFrequency(str) {
const vowels = 'aeiou';
let dic = [...str].reduce((dic, char) => {
if (vowels.includes(char))
dic[char] = dic[char] + 1 || 1;
return dic;
}, {});
return Object.entries(dic)
.map(([char, count]) => `${char}${count}`)
.join('');
};
One concise approach would be to transform the string via String.prototype.replaceAll (evaluating every character in the string). The following code searches the original string (which you may wish to normalize beforehand with .toLowerCase() for better results) for any character.
"hello world".replaceAll(/./g, ( char, index, str ) =>
!'aeiou'.includes( char ) || str.lastIndexOf( char ) > index
? "" : char + [ ...str ].filter( o => o == char ).length
);
Each character is checked against a list of vowels. We also check to see if the character index is the last index of this character (does it appear multiple times) in the original string. If either of these conditions fail, an empty string is returned in the character's place.
If our character is in our vowel list, and is the last instance of itself, then we split the original string, filter-out non-matching characters, and return the final count of character instances.
The above approach is somewhat of a gimmick. It's concise, but probably not very self-explanatory or maintainable. Realistically, you'd want to take a slightly more verbose approach (see below).
Note that Map is preferred over a standard object to ensure that key-insertion order is preserved.
function charInstanceString ( input, chars = "aeiou" ) {
/**
* Cycle over each character in our string, checking
* if it appears in our `chars` string. If the character
* appears in our `chars` string, we'll update our map
* to reflect the number of instances for the character.
*/
const charMap = new Map();
for ( const char of input ) {
if ( !chars.includes( char ) ) continue;
charMap.set( char, charMap.get( char ) + 1 || 1 );
}
/**
* Cycle over our map, adding each character (and its
* corresponding count) to an output string.
*/
let output = "";
for ( const [ char, count ] of charMap ) {
output += `${ char }${ count }`;
}
return output;
}

Insert Spaces into string at an index

I'm trying to do this Codewars problem.
Task
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.
Rules
The input string will always be lower case but maybe empty.
If the character in the string is whitespace then pass over it as if it was an empty seat.
Example
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
My code so far is hosted on this repl.it
My thought process is as follows:
Turn argument into array
manipulate each index of the array at index and then readjust previous index to make a wave pattern
turn array into string
reinsert spaces before logging it to console and restarting the loop
I'm pretty stuck and my mind is stuck on how to use
for(var j = 0; j < indexSpaceNumber.length; j++){
//join and add in the spaces at their former index before returning string
strToArray[indexSpaceNumber[j]].slice(0, " ");
}
to insert the spaces into the string.
If there's any guidance or tips it would be much appreciated. I feel like I'm close, but so frustratingly far.
The main idea would be:
Iterate the characters
Replace the character in the original string with an uppercase version
You can use Array.from() to convert the string to an array, and map each item to a new string. If the character is a space return something falsy (en empty string in the example). After the creating the array, filter all falsy values:
const wave = str =>
Array.from(str, (c,i) => // convert the string to an array
// replace the character with an uppercase version in the original string
c === ' ' ?
''
:
`${str.substring(0, i)}${c.toUpperCase()}${str.substring(i + 1)}`
).filter(c => c)
const result = wave("hello")
console.log(result)
For string with spaces
function wave(str) {
let res = []
str.toLowerCase().split('').forEach((v, i) => {
if(v == ' ') return;
res.push( str.substr(0, i) + v.toUpperCase() + str.substr(i + 1) )
});
return res
}
console.log(wave("hello hello"))
I'd go recursive ;)
You know that for a string of length n you need an array of the same length. That's your exit condition.
You can use the length of the array at each iteration to work out the shape of the next string:
hello [] [Hello] 0: uppercase 1st char and append
hello [Hello] [Hello hEllo] 1: uppercase 2nd char and append
hello [Hello hEllo] [Hello hEllo heLlo] 2: uppercase 3rd char and append
...
const wave =
(str, arr = []) =>
str.length === arr.length
? arr
: wave
( str
, [ ...arr
, str.slice(0, arr.length)
+ str[arr.length].toUpperCase()
+ str.slice(arr.length + 1)
]
);
console.log(wave('hello'));
Go over each char in string and build
Slice str from start till current char + current char to upper case + Slice str from current char to end
const wave = str => {
const res = [];
for (let i = 0; i < str.length; i++) {
res.push(`${str.slice(0, i)}${str[i].toUpperCase()}${str.slice(i + 1)}}`);
}
return res;
};
console.log(wave("hi my name is rylan"));
// Alternate way to do with Array.splice
const wave2 = str => {
const res = [];
for (let i in str) {
const temp = Array.from(str);
temp.splice(i, 1, temp[i].toUpperCase());
res.push(temp)
}
return res.map(x => x.join(''));
};
console.log(wave2("hi my name is rylan"));

How to get odd and even position characters from a string?

I'm trying to figure out how to remove every second character (starting from the first one) from a string in Javascript.
For example, the string "This is a test!" should become "hsi etTi sats!"
I also want to save every deleted character into another array.
I have tried using replace method and splice method, but wasn't able to get them to work properly. Mostly because replace only replaces the first character.
function encrypt(text, n) {
if (text === "NULL") return n;
if (n <= 0) return text;
var encArr = [];
var newString = text.split("");
var j = 0;
for (var i = 0; i < text.length; i += 2) {
encArr[j++] = text[i];
newString.splice(i, 1); // this line doesn't work properly
}
}
You could reduce the characters of the string and group them to separate arrays using the % operator. Use destructuring to get the 2D array returned to separate variables
let str = "This is a test!";
const [even, odd] = [...str].reduce((r,char,i) => (r[i%2].push(char), r), [[],[]])
console.log(odd.join(''))
console.log(even.join(''))
Using a for loop:
let str = "This is a test!",
odd = [],
even = [];
for (var i = 0; i < str.length; i++) {
i % 2 === 0
? even.push(str[i])
: odd.push(str[i])
}
console.log(odd.join(''))
console.log(even.join(''))
It would probably be easier to use a regular expression and .replace: capture two characters in separate capturing groups, add the first character to a string, and replace with the second character. Then, you'll have first half of the output you need in one string, and the second in another: just concatenate them together and return:
function encrypt(text) {
let removedText = '';
const replacedText1 = text.replace(/(.)(.)?/g, (_, firstChar, secondChar) => {
// in case the match was at the end of the string,
// and the string has an odd number of characters:
if (!secondChar) secondChar = '';
// remove the firstChar from the string, while adding it to removedText:
removedText += firstChar;
return secondChar;
});
return replacedText1 + removedText;
}
console.log(encrypt('This is a test!'));
Pretty simple with .reduce() to create the two arrays you seem to want.
function encrypt(text) {
return text.split("")
.reduce(({odd, even}, c, i) =>
i % 2 ? {odd: [...odd, c], even} : {odd, even: [...even, c]}
, {odd: [], even: []})
}
console.log(encrypt("This is a test!"));
They can be converted to strings by using .join("") if you desire.
I think you were on the right track. What you missed is replace is using either a string or RegExp.
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.
Source: String.prototype.replace()
If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g) modifier
Source: JavaScript String replace() Method
So my suggestion would be to continue still with replace and pass the right RegExp to the function, I guess you can figure out from this example - this removes every second occurrence for char 't':
let count = 0;
let testString = 'test test test test';
console.log('original', testString);
// global modifier in RegExp
let result = testString.replace(/t/g, function (match) {
count++;
return (count % 2 === 0) ? '' : match;
});
console.log('removed', result);
like this?
var text = "This is a test!"
var result = ""
var rest = ""
for(var i = 0; i < text.length; i++){
if( (i%2) != 0 ){
result += text[i]
} else{
rest += text[i]
}
}
console.log(result+rest)
Maybe with split, filter and join:
const remaining = myString.split('').filter((char, i) => i % 2 !== 0).join('');
const deleted = myString.split('').filter((char, i) => i % 2 === 0).join('');
You could take an array and splice and push each second item to the end of the array.
function encrypt(string) {
var array = [...string],
i = 0,
l = array.length >> 1;
while (i <= l) array.push(array.splice(i++, 1)[0]);
return array.join('');
}
console.log(encrypt("This is a test!"));
function encrypt(text) {
text = text.split("");
var removed = []
var encrypted = text.filter((letter, index) => {
if(index % 2 == 0){
removed.push(letter)
return false;
}
return true
}).join("")
return {
full: encrypted + removed.join(""),
encrypted: encrypted,
removed: removed
}
}
console.log(encrypt("This is a test!"))
Splice does not work, because if you remove an element from an array in for loop indexes most probably will be wrong when removing another element.
I don't know how much you care about performance, but using regex is not very efficient.
Simple test for quite a long string shows that using filter function is on average about 3 times faster, which can make quite a difference when performed on very long strings or on many, many shorts ones.
function test(func, n){
var text = "";
for(var i = 0; i < n; ++i){
text += "a";
}
var start = new Date().getTime();
func(text);
var end = new Date().getTime();
var time = (end-start) / 1000.0;
console.log(func.name, " took ", time, " seconds")
return time;
}
function encryptREGEX(text) {
let removedText = '';
const replacedText1 = text.replace(/(.)(.)?/g, (_, firstChar, secondChar) => {
// in case the match was at the end of the string,
// and the string has an odd number of characters:
if (!secondChar) secondChar = '';
// remove the firstChar from the string, while adding it to removedText:
removedText += firstChar;
return secondChar;
});
return replacedText1 + removedText;
}
function encrypt(text) {
text = text.split("");
var removed = "";
var encrypted = text.filter((letter, index) => {
if(index % 2 == 0){
removed += letter;
return false;
}
return true
}).join("")
return encrypted + removed
}
var timeREGEX = test(encryptREGEX, 10000000);
var timeFilter = test(encrypt, 10000000);
console.log("Using filter is faster ", timeREGEX/timeFilter, " times")
Using actually an array for storing removed letters and then joining them is much more efficient, than using a string and concatenating letters to it.
I changed an array to string in filter solution to make it the same like in regex solution, so they are more comparable.

Find a number of occurrences of character in string

I have a string and I want to remove the first 3 occurrences of the letter a (for example).
Seems regex has no solution for a specific number of occurrences. And I can't use replace without loops, which I want to avoid.
Nice challenge.
This works:
var str = "I have a string and I want to remove the first 3 occurrences of the letter a (for example).";
str = str.replace(/^([^a]*a){3}/, function(match) {
return match.replace(/a/g, '!');
});
document.body.innerHTML = str;
Result:
I h!ve ! string !nd I want to remove the first 3 occurrences of the letter a (for example).
The first replace matches a continuous string containing exactly 3 as. Its inner replace changes the a to a ! for clarity; you can safely replace it with nothing as well.
This is scaleable to any number because you can change the {3} to the required number of changes.
I have a string and I want to remove the first 3 occurrences of the
letter a (for example).
If regex is not mandatory and looping is not a huge concern, then try this single line loop
str = str.split(/(?=a)/).map(function(value,index){ if (index < 3){value=value.replace("a", "")} return value }).join(""); // 3 and "a" can be passed on as input
make it a function like this
function removeNOccurences(str, toBeRepalced, n)
{
return str.split(/(?=a)/).map(function(value,index){ if (index < n){value=value.replace(toBeRepalced, "")} return value }).join("");
}
document.body.innerHTML += removeNOccurences("asdas3sdfw3aar23rzas", "a", 3);
With replace callback counting the matches:
function replaceFirstN(str, regex, n, replacement) {
var i = 0;
return str.replace(regex, function(match) {
return i++ < n ? replacement : match;
});
}
output.innerText = replaceFirstN("a a a a a a a a", /a/g, 3, "b");
<pre id="output"></pre>
This is a solution which utilises the thisArg of Array#filter.
var string = 'a1a2a3a4a5a6a7a8a9a'.split('').filter(function (a) {
return a !== 'a' || ++this.c > 3;
}, { c: 0 }).join('');
document.write(string);
var str = "What a beautiful day is today";
var strA = str.split('a',4).join('a');
var strRemaining = str.replace(strA,'');
// Do global replace and append remaining string
var resultStr = strA.replace(/a/g,'') + strRemaining;
alert(resultStr);
JSFiddle
Without Loop, String#replace and regex
var str = "abcabcabcabc";
function replace(s, symbol, replacer, times) {
if (times == 0) return s;
var i = s.indexOf(symbol);
var a = s.split('');
a[i] = replacer;
return replace(a.join(''), symbol, replacer, --times)
}
var result = replace(str, 'a', '_', 3);
document.write(result);

Why can't I swap characters in a javascript string?

I am trying to swap first and last characters of array.But javascript is not letting me swap.
I don't want to use any built in function.
function swap(arr, first, last){
var temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
}
Because strings are immutable.
The array notation is just that: a notation, a shortcut of charAt method. You can use it to get characters by position, but not to set them.
So if you want to change some characters, you must split the string into parts, and build the desired new string from them:
function swapStr(str, first, last){
return str.substr(0, first)
+ str[last]
+ str.substring(first+1, last)
+ str[first]
+ str.substr(last+1);
}
Alternatively, you can convert the string to an array:
function swapStr(str, first, last){
var arr = str.split('');
swap(arr, first, last); // Your swap function
return arr.join('');
}
Let me offer my side of what I understood: swapping items of an array could be something like:
var myFish = ["angel", "clown", "mandarin", "surgeon"];
var popped = myFish.pop();
myFish.unshift(popped) // results in ["surgeon", "angel", "clown", "mandarin"]
Regarding swaping first and last letters of an strings could be done using Regular Expression using something like:
"mandarin".replace(/^(\w)(.*)(\w)$/,"$3$2$1")// outputs nandarim ==> m is last character and n is first letter
I just ran your code right out of Chrome, and it seemed to work find for me. Make sure the indices you pass in for "first" and "last" are correct (remember JavaScript is 0-index based). You might want to also try using console.log in order to print out certain variables and debug if it still doesn't work for you.
EDIT: I didn't realize you were trying to manipulate a String; I thought you just meant an array of characters or values.
function swapStr(str, first, last) {
if (first == last) {
return str;
}
if (last < first) {
var temp = last;
last = first;
first = temp;
}
if (first >= str.length) {
return str;
}
return str.substring(0, first) +
str[last] +
str.substring(first + 1, last) +
str[first] +
str.substring(last + 1);
}
Swap characters inside a string requires the string to convert into an array, then the array can be converted into string again:
function swap(arr, first, last){
arr = arr.split(''); //to array
var temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
arr = arr.join("").toString() //to string
return arr;
}
Following usage:
str = "ABCDE"
str = swap(str,1,2)
console.log(str) //print "ACBDE"
I hope this piece of code will help somebody.
var word = "DED MOROZ";
var arr = word.split('');
for (var i = 0; i < arr.length/2; i++) {
var temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[word.length - i - 1] = temp;
}
console.log(arr.join(""));
Run this code and your characters will be swapped.

Categories

Resources