Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,900 members, 7,814,048 topics. Date: Wednesday, 01 May 2024 at 03:21 AM

Pattern Chaser In Javascript: Coding Challenge - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Pattern Chaser In Javascript: Coding Challenge (6274 Views)

Coding Challenge For Fun / Help With Javascript Coding On My Website / Cookie Programming In Javascript (2) (3) (4)

(1) (Reply) (Go Down)

Pattern Chaser In Javascript: Coding Challenge by Kodejuice: 4:29pm On Jul 20, 2015
Using the JavaScript language, have the function
PatternChaser(str) take str which will be a string and return the longest pattern within the string. A pattern for this challenge will be defined as: if at least 2 or more adjacent characters within the string repeat at least twice. So for example "aabecaa" contains the pattern aa, on the other hand "abbbaac" doesn't contain any pattern. Your program should return yes/no pattern/null . So if str were "aabejiabkfabed" the output should be yes abe. If str were "123224" the output should return no null. The string may either contain all characters (a through z only), integers, or both. But the parameter will always be a string type. The maximum length for the string being passed in will be 20 characters. If a string for example is "aa2bbbaacbbb" the pattern is "bbb" and not "aa". You must always return the longest pattern possible.

example
patternChaser("abcddeddb"wink; //=> "dd"



my try, having little erros sha

functions patternChaser(str){
for (i = 0; i < str.lengh; i++) {
var letter = str[i], list = [];
if (letter == str[i + 1]) { //to 20
if (str[i + 1] == str[i + 2]) {
list.push(str[i], str[i + 1], str[i + 2]);
i = i + 2;
} else {
list.push(str[i], str[i + 1]);
i = i + 1;
}
}
}
//... incomplete
}


For the JavaScript ninjas only.
Re: Pattern Chaser In Javascript: Coding Challenge by thewebcraft(m): 4:52pm On Jul 20, 2015
Kodejuice:
Using the JavaScript language, have the function
PatternChaser(str) take str which will be a string and return the longest pattern within the string. A pattern for this challenge will be defined as: if at least 2 or more adjacent characters within the string repeat at least twice. So for example "aabecaa" contains the pattern aa, on the other hand "abbbaac" doesn't contain any pattern. Your program should return yes/no pattern/null . So if str were "aabejiabkfabed" the output should be yes abe. If str were "123224" the output should return no null. The string may either contain all characters (a through z only), integers, or both. But the parameter will always be a string type. The maximum length for the string being passed in will be 20 characters. If a string for example is "aa2bbbaacbbb" the pattern is "bbb" and not "aa". You must always return the longest pattern possible.

example
patternChaser("abcddeddb"wink; //=> "dd"



my try, having little erros sha

functions patternChaser(str){
for (i = 0; i < str.lengh; i++) {
var letter = str[i], list = [];
if (letter == str[i + 1]) { //to 20
if (str[i + 1] == str[i + 2]) {
list.push(str[i], str[i + 1], str[i + 2]);
i = i + 2;
} else {
list.push(str[i], str[i + 1]);
i = i + 1;
}
}
}
//... incomplete
}


For the JavaScript ninjas only.


Lemme try....

function PatternChaser(str) {
var l, i, pattern, count;

for (l = Math.floor(str.length / 2); l >= 2; l--) {

for (i = 0; i < str.length - l; i++) {
pattern = str.substr(i, l);
count = str.match(new RegExp(pattern,'g')).length;
if (count > 1) return 'yes ' + pattern;
}
}
return 'no null';
}
console.log(PatternChaser("Can this work" ) );

1 Like

Re: Pattern Chaser In Javascript: Coding Challenge by FincoApps(m): 5:49pm On Jul 20, 2015
Niceeeeee smiley
thewebcraft:



Lemme try....

function PatternChaser(str) {
var l, i, pattern, count;

for (l = Math.floor(str.length / 2); l >= 2; l--) {

for (i = 0; i < str.length - l; i++) {
pattern = str.substr(i, l);
count = str.match(new RegExp(pattern,'g')).length;
if (count > 1) return 'yes ' + pattern;
}
}
return 'no null';
}
console.log(PatternChaser("Can this work" ) );
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 6:08pm On Jul 20, 2015
Na money-chaser script i need
Re: Pattern Chaser In Javascript: Coding Challenge by thewebcraft(m): 9:44am On Jul 21, 2015
dhtml18:
Na money-chaser script i need
lolz....
function makeMoney( dollars ) {
var money = '';
for ( var i = 0; i < dollars; i++ ) {
money += '$';
}
return money;
};
makeMoney((100000) );
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 10:56am On Jul 21, 2015
^^^Now that is what we are talking about
Re: Pattern Chaser In Javascript: Coding Challenge by thewebcraft(m): 1:35pm On Jul 21, 2015
dhtml18:
^^^Now that is what we are talking about
Yeah just run the script on your bank server under ur account... cheesy wink

1 Like

Re: Pattern Chaser In Javascript: Coding Challenge by bizycodin: 3:55pm On Jul 24, 2015
I have test this it worked fine. But the algorithmic complexity is O(n^2) due to the nested for loop. The effect of this is the larger the input, the slower the time of execution. I would love to see other solutions and let's compare our time of execution. This should also pass most test cases.

function patternChaser(str){

//If string is less than 4 return No Pattern
if(str.length < 4) return "No Pattern";

//IF there are mulitple occurence of decimals return No Pattern ss
if(/^\d+/.test(str)) return "NO Pattern";

var newString, newString2, nmatch, maxTestCase, testRes = "", len = str.length;

for (var i = 0; i < len; i++) {

//If iteration has gotten to the second to the last string break out of the loop
// and return testResult
//NOte: if there are no match an empty string is returned.
if (i === str.length-2) break;

/*maxTestCase is used to evaluate the maximum test case for each iteration
* The maxTestCase is gotten by dividing the length of the string into 2
* IF the value is odd ad one before division
* The maxTestCase reduces for every iteration*/
maxTestCase = ((len - i) % 2 == 0) ? (((len - i)/2)+1) : (((len -i) + 1)/2);

for (var j = 2; j < maxTestCase; j++) {

//Get a substring from the string
newString = str.substr(i, j);

var regEx = new RegExp(newString, "g"wink;

//CHeck for matches for this substring in the whole string.
nmatch = str.match(regEx);

//If there is match and match is greater than one
if (nmatch.length > 1) {

newString2 = nmatch[0];

//If length of string is greater than the existing testREs length
//testRes = newString
if (newString2.length > testRes.length) {
testRes = newString2;
}
}
}
}
return testRes;
}

console.log(patternChaser("howarekddkdkdieeialskdoeiedkdklskdiamaboyandeiei383838iamaboyexx,x"wink);

returns "iamaboy"....
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 6:57am On Jul 25, 2015
Craigston:
Bizycodin, you did great. Almost same approach as mine.
That means the two of you have the same brain power which is quite impressive for NOOBS!
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 10:35am On Jul 25, 2015
Craigston:

Abi? Please show me some other approach that can advance my skill. We dey look ur hand o. Baba we dey look ur hand o.
Lol, your approaches are fine as far as I can see - just that i thought i could tweak it slightly:

<script>
function patternChaser(str){
var newString, newString2, nmatch, maxTestCase, testRes = '', len = str.length;
if(str.length < 4 || /^\d+/.test(str)) {
testRes='Please supply a valid string';
} else {
for (var i = 0; i < len; i++) {
if (i === str.length-2) break;
maxTestCase = ((len - i) % 2 == 0) ? (((len - i)/2)+1) : (((len -i) + 1)/2);
for (var j = 2; j < maxTestCase; j++) {
nmatch = str.match(new RegExp(str.substr(i, j), 'g'));
if (nmatch.length > 1 && nmatch[0].length > testRes.length) {
testRes = nmatch[0];
}}}
}
return testRes=='' ? 'No match found' : testRes;
}

document.write(patternChaser('howarekddkdkdieeialskdoeiedkdklskdiamaboyandeiei383838iamaboyexx,x')+'<br/>');
document.write(patternChaser('how')+'<br/>');
document.write(patternChaser('howarekdd,x')+'<br/>');

</script>
The above will return 3 results:
- iamaboy
- Please supply a valid string
- No match found

But technically it is still the same code written above just reformatted slightly. . .and compressed to actually look shorter
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 3:43pm On Jul 25, 2015
Yeah, i was your own code it was the one with the rainbowcolor UI abi na linearbackground. Before I declare that it didn't work, you need to provide us with a manual of how to use it.
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 8:02pm On Jul 25, 2015
I downloaded it, well I did not know it uses onblur, and yes it works fine. Good job, but it brings out undefined error sometimes.
Re: Pattern Chaser In Javascript: Coding Challenge by Craigston: 9:11pm On Jul 25, 2015
dhtml18:
I downloaded it, well I did not know it uses onblur, and yes it works fine. Good job, but it brings out undefined error sometimes.
It returns undefined when no pattern was found. I didn't bother fixing that since it's for this thread and you guys would understand what's the cause.
Lemme play with something else. Maybe a technique that will output the patterns found continuously as the user enters a string.
Re: Pattern Chaser In Javascript: Coding Challenge by bizycodin: 11:54pm On Jul 25, 2015
dhtml18:

Lol, your approaches are fine as far as I can see - just that i thought i could tweak it slightly:


The above will return 3 results:
- iamaboy
- Please supply a valid string
- No match found

But technically it is still the same code written above just reformatted slightly. . .and compressed to actually look shorter

@dhtml. Kudos for d clean-up. Is there anyway solution can be improved?
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 7:04am On Jul 26, 2015
^^^That will now depend on your usage. You might convert the code into a prototype, or even a jquery plugin.

Check out the prototype below and the usage. It is just like adding a new string method, I use stuffs like these in my code libraries to make life easier.


<script>
String.prototype.patternChaser = function() {
var newString, newString2, nmatch, maxTestCase, testRes = '', len = this.length;
if(this.length < 4 || /^\d+/.test(this)) {
testRes='Please supply a valid string';
} else {
for (var i = 0; i < len; i++) {
if (i === this.length-2) break;
maxTestCase = ((len - i) % 2 == 0) ? (((len - i)/2)+1) : (((len -i) + 1)/2);
for (var j = 2; j < maxTestCase; j++) {
nmatch = this.match(new RegExp(this.substr(i, j), 'g'));
if (nmatch.length > 1 && nmatch[0].length > testRes.length) {
testRes = nmatch[0];
}}}
}
return testRes=='' ? 'No match found' : testRes;
}

document.write('howarekddkdkdieeialskdoeiedkdklskdiamaboyandeiei383838iamaboyexx,x'.patternChaser()+'<br/>');
document.write('how'.patternChaser()+'<br/>');
document.write('howarekdd,x'.patternChaser()+'<br/>');

</script>
Re: Pattern Chaser In Javascript: Coding Challenge by bizycodin: 12:51pm On Jul 26, 2015
dhtml18:
^^^That will now depend on your usage. You might convert the code into a prototype, or even a jquery plugin.

Check out the prototype below and the usage. It is just like adding a new string method, I use stuffs like these in my code libraries to make life easier.


Cool protoyping. But by improvement I mean rewriting the code to improve performance. A different way of thinking abt the problem! And have u tried inspecting the execution time?
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 1:16pm On Jul 26, 2015
Too damn busy for that, notice I did not even write any code as such, just picked one of the codes someone wrote above and modified it. . . .right now I am battling out an android chat app writing JAVA like no-man's business. . .
Re: Pattern Chaser In Javascript: Coding Challenge by bizycodin: 1:33pm On Jul 26, 2015
dhtml18:
Too damn busy for that, notice I did not even write any code as such, just picked one of the codes someone wrote above and modified it. . . .right now I am battling out an android chat app writing JAVA like no-man's business. . .

Whao! Ok. Cool! I would also love to pick up android again but 4 now I am crzy digging the web.. All d best on your app bro!
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 2:03pm On Jul 26, 2015
Thanks, I am not restricted to one side - I try to combine all platforms in my work, but am just more popular with web.
Re: Pattern Chaser In Javascript: Coding Challenge by bizycodin: 2:29pm On Jul 26, 2015
dhtml18:
Thanks, I am not restricted to one side - I try to combine all platforms in my work, but am just more popular with web.

Ok.! It's good to be vast.
Re: Pattern Chaser In Javascript: Coding Challenge by makinde06(m): 10:12am On Jul 28, 2015
var PatternChaser = function(caseMe)
{
var response = '';
var alphabet = '';
var number = 0;

if (caseMe !='')
{
var innerNumber = 0;
var list = caseMe.split('');

for (var i = 0; i < list.length; i++)
{

if (i == 0)
{
alphabet = list[i].toString();
number++;
}
else
{
if (list[i - 1] == list[i])
{
if (list[i - 1].toString() == alphabet)
number++;
else
innerNumber++;

if (innerNumber > number)
{
alphabet = list[i].toString();
number = innerNumber;
}
}
else
{
innerNumber = 1;
}
}
}
}
return alphabet;
};

var tt = PatternChaser('aaabbdddee444rrttt');

console.log(tt);
a
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 10:19am On Jul 28, 2015
makinde06:
var PatternChaser = function(caseMe)
{
var response = '';
var alphabet = '';
var number = 0;

if (caseMe !='')
{
var innerNumber = 0;
var list = caseMe.split('');

for (var i = 0; i < list.length; i++)
{

if (i == 0)
{
alphabet = list[i].toString();
number++;
}
else
{
if (list[i - 1] == list[i])
{
if (list[i - 1].toString() == alphabet)
number++;
else
innerNumber++;

if (innerNumber > number)
{
alphabet = list[i].toString();
number = innerNumber;
}
}
else
{
innerNumber = 1;
}
}
}
}
return alphabet;
};

var tt = PatternChaser('aaabbdddee444rrttt');

console.log(tt);
d

Nice, but when you test it with howarekddkdkdieeialskdoeiedkdklskdiamaboyandeiei383838iamaboyexx,x - it gives only i unlike the other pattern chasers above.
Re: Pattern Chaser In Javascript: Coding Challenge by makinde06(m): 10:31am On Jul 28, 2015
dhtml18:


Nice, but when you test it with howarekddkdkdieeialskdoeiedkdklskdiamaboyandeiei383838iamaboyexx,x - it gives only i unlike the other pattern chasers above.

Are you saying it should return something like this 'ddd' instead of 'd'?
Re: Pattern Chaser In Javascript: Coding Challenge by Nobody: 11:58am On Jul 28, 2015
If you test the pattern with the patternchaser of the other guys, it will return i am a boy. Test the one i modified for example.

<script>
String.prototype.patternChaser = function() {
var newString, newString2, nmatch, maxTestCase, testRes = '', len = this.length;
if(this.length < 4 || /^\d+/.test(this)) {
testRes='Please supply a valid string';
} else {
for (var i = 0; i < len; i++) {
if (i === this.length-2) break;
maxTestCase = ((len - i) % 2 == 0) ? (((len - i)/2)+1) : (((len -i) + 1)/2);
for (var j = 2; j < maxTestCase; j++) {
nmatch = this.match(new RegExp(this.substr(i, j), 'g'));
if (nmatch.length > 1 && nmatch[0].length > testRes.length) {
testRes = nmatch[0];
}}}
}
return testRes=='' ? 'No match found' : testRes;
}

document.write('howarekddkdkdieeialskdoeiedkdklskdiamaboyandeiei383838iamaboyexx,x'.patternChaser()+'<br/>');
document.write('how'.patternChaser()+'<br/>');
document.write('howarekdd,x'.patternChaser()+'<br/>');

</script>
Re: Pattern Chaser In Javascript: Coding Challenge by ehtesham17: 11:13am On May 14, 2018
You can convert string to number or integer using javascript.

http://www.phpcodify.com/convert-string-to-integer-using-jquery-parseint/

(1) (Reply)

[Help Request ] I Want To Learn Programming, what are the requirements / Certified-drugs App: List Of All Drugs Approved For Use In Nigeria By NAFDAC / Free Research Project/thesis Topics In Computer Science (PART 1)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 51
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.