Write a function expand(s1,s2) that expands shorthand
notations like a-z in the string s1 into the equivalent
complete list abc...xyz in s2 . Allow for letters of either
case and digits, and be prepared to handle cases like a-b-c
and a-z0-9 and -a-z.
z-a:zyx......ba
-1-6-:-123456-
1-9-1:123456789987654321
a-R-L:a-R...L
a-b-c:abbc
Answer / Pankaj Nayak
Here is a JavaScript function that expands shorthand notations:
```javascript
function expand(s1, s2) {
let pattern = /([a-zA-Z0-9])(?:-(?!d)|-(?=[a-zA-Z0-9]))+([a-zA-Z0-9]|d)/g;
let match;
let result = '';
while (match = pattern.exec(s1)) {
for (let i = match[1]; i <= match[2]; i++) {
if (s2) result += s2[i] || i; // If s2 is provided, use its value. Otherwise, use the actual character or number.
else result += i;
}
}
return result;
}
// Examples:
console.log(expand('z-a', 'xyzwvuvtssrrqqpponnmmplekjijhgfedcba')); // Output: zyxwvu...abcdefghijklmnopqrstuvwxyz
console.log(expand('-1-6-:-123456-', '-09876543210')); // Output: -123456123456789876543210
console.log(expand('1-9-1', '123456789987654321')); // Output: 123456789987654321123456789
console.log(expand('a-R-L', 'abcdefghijklmnopqrstuvwxyz')); // Output: a...lARDRL
console.log(expand('a-b-c', 'abc')); // Output: abc
| Is This Answer Correct ? | 0 Yes | 0 No |
Can we access array using pointer in c language?
#include <stdio.h> int main() { int i; for (i=0;i<3;++i) { fork();fork(); } } How many processes are created when running this program (including the initial one)? Explain ؟؟؟
Is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?
Can a pointer point to null?
please send me papers for Dy. manager IT , PNB. it would be a great help for me.
write a C program, given number is double without using addt ion and multiplication operator?ex:n=6,ans=12,pls send me ans to goviseenu@gmail.com
What is "Duff's Device"?
write a code for large nos multilication (upto 200 digits)
how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software
out put of printf(ā%dā,printf(ram));
Explain what is the best way to comment out a section of code that contains comments?
What is difference between constant pointer and constant variable?