Javascript regular expressions problem - javascript

I am creating a small Yahtzee game and i have run into some regex problems. I need to verify certain criteria to see if they are met. The fields one to six is very straight forward the problem comes after that. Like trying to create a regex that matches the ladder. The Straight should contain one of the following characters 1-5. It must contain one of each to pass but i can't figure out how to check for it. I was thinking /1{1}2{1}3{1}4{1}5{1}/g; but that only matches if they come in order. How can i check if they don't come in the correct order?

If I understood you right, you want to check if a string contains the numbers from 1 to 5 in random order. If that is correct, then you can use:
var s = '25143';
var valid = s.match(/^[1-5]{5}$/);
for (var i=1; i<=5; i++) {
if (!s.match(i.toString())) valid = false;
}
Or:
var s = '25143';
var valid = s.split('').sort().join('').match(/^12345$/);

Although this definitely can be solved with regular expressions, I find it quite interesting and educative to provide a "pure" solution, based on simple arithmetic. It goes like this:
function yahtzee(comb) {
if(comb.length != 5) return null;
var map = [0, 0, 0, 0, 0, 0];
for(var i = 0; i < comb.length; i++) {
var digit = comb.charCodeAt(i) - 48;
if(digit < 1 || digit > 6) return null;
map[digit - 1]++;
}
var sum = 0, p = 0, seq = 0;
for(var i = 0; i < map.length; i++) {
if(map[i] == 2) sum += 20;
if(map[i] >= 3) sum += map[i];
p = map[i] ? p + 1 : 0;
if(p > seq) seq = p;
}
if(sum == 5) return "Yahtzee";
if(sum == 23) return "Full House";
if(sum == 3) return "Three-Of-A-Kind";
if(sum == 4) return "Four-Of-A-Kind";
if(seq == 5) return "Large Straight";
if(seq == 4) return "Small Straight";
return "Chance";
}
for reference, Yahtzee rules

For simplicity and easiness, I'd go with indexOf.
string.indexOf(searchstring, start)
Loop 1 to 5 like Max but just check indexOf i, break out for any false.
This also will help for the small straight, which is only 4 out of 5 in order(12345 or 23456).
Edit: Woops. 1234, 2345, 3456. Sorry.
You could even have a generic function to check for straights of an arbitrary length, passing in the maximum loop index as well as the string to check.

"12543".split('').sort().join('') == '12345'

With regex:
return /^([1-5])(?!\1)([1-5])(?!\1|\2)([1-5])(?!\1|\2|\3)([1-5])(?!\1|\2|\3|\4)[1-5]$/.test("15243");
(Not that it's recommended...)

A regexp is likely not the best solution for this problem, but for fun:
/^(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5).{5}$/.test("12354")
That matches every string that contains exactly five characters, being the numbers 1-5, with one of each.
(?=.*1) is a positive lookahead, essentially saying "to the very right of here, there should be whatever or nothing followed by 1".
Lookaheads don't "consume" any part of the regexp, so each number check starts off the beginning of the string.
Then there's .{5} to actually consume the five characters, to make sure there's the right number of them.

Related

Find longest word in Javascript array

So I'm working on this codility coding challenge and I cannot get the code to work for all inputs, particularly large ones. The rules for this are here.
To summarize, I need each word in the string to be tested for: alpha-numeric characters only, even number of letters, and odd number of digits.
For the sample input - "test 5 a0A pass007 ?xy1", this solution effectively ignores "test" (it has an even number of digits, 0 digits) and "?xy1" (special character, ?). From the leftover options, it chooses pass007 as the longest word and returns 7 (length of word).
I start by splitting the string into separate words and then generating if statements to check if each word in my new array meets the requirements, isAlpha, isAlphaEven (remainder 0 for even # of letters), isNumeric (remainder 1 for odd numbers).
Any idea what I am doing wrong? Thanks much! :)
// you can write to stdout for debugging purposes,
// e.g. console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
// you can write to stdout for debugging purposes,
// e.g. console.log('this is a debug message');
// write your code in JavaScript (Node.js 8.9.4)
var words = S.split(" ");
var isAlpha = /^[0-9a-zA-z]*$/;
var isAlphaEven = /^[a-zA-Z]/;
var isNumeric = /^[0-9]/;
var maxLength = -1;
for(var i = 0; i <= words.length - 1; i++) {
if(words[i].match(isAlpha)
&& words[i].replace(isAlphaEven, '').length % 2 == 0
&& words[i].replace(isNumeric, '').length % 2 == 1
|| words[i].match(isNumeric)
) {
maxLength = Math.max(maxLength, words[i].length);
//console.log(words[i], maxLength);
}
}
return maxLength;
}
One problem is that the patterns
var isAlphaEven = /^[a-zA-Z]/;
var isNumeric = /^[0-9]/;
can only match characters at the start of the string: ^ anchors to the beginning. It also isn't a global match, so it'll only replace one character. Another problem is that you're replacing the matches with the empty string, rather than testing the number of matches. To test the number of matches, use .match instead, with the global flag, and check the length of the resulting array (or null if there are no matches):
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
// you can write to stdout for debugging purposes,
// e.g. console.log('this is a debug message');
// write your code in JavaScript (Node.js 8.9.4)
var words = S.split(" ");
var allAlphaNumeric = /^[\da-z]*$/i;
var alpha = /[a-z]/gi;
var numeric = /\d/g;
var maxLength = -1;
for (var i = 0; i <= words.length - 1; i++) {
if (words[i].match(allAlphaNumeric) &&
(words[i].match(alpha) || []).length % 2 == 0 &&
(words[i].match(numeric) || []).length % 2 == 1
) {
maxLength = Math.max(maxLength, words[i].length);
}
}
return maxLength;
}
console.log(solution("test 5 a0A pass007 ?xy1"));
Note that you can use the case-insensitive flag instead of repeating a-zA-Z, and you can use \d instead of [0-9] if you wish.
While you could use .replace to figure out the number of matches, it'd be convoluted: you'd have to replace everything that doesn't match with the empty string, which would make the code's intent somewhat confusing.
You already have the answer why your approach didn't work as expected.
So I thought I could add a slightly different approach with multiple .filter() steps
function findLongestWord(input) {
const isAlphaNumericOnly = /^[a-z0-9]+$/i;
const numbersOnly = /\d/g;
const alphaOnly = /[a-z]/gi;
const validWords = input.split(/\s/)
.filter(word => isAlphaNumericOnly.test(word))
.filter(word => (word.match(numbersOnly) || []).length % 2 === 1)
.filter(word => (word.match(alphaOnly) || []).length % 2 === 0)
.sort((a, b) => b.length - a.length);
return {
word: validWords[0],
length: validWords[0] ? validWords[0].length : -1
};
}
console.log(findLongestWord("test 5 a0A pass007 ?xy1"));

How does indexOf method on string work in JavaScript

I was doing a question on codility and came across this problem for which I wrote something like this:
function impact(s) {
let imp = 4; // max possible impact
for (let i = 0; i < s.length; i++) {
if (s[i] === 'A') return 1;
else if (s[i] === 'C') imp = Math.min(imp, 2);
else if (s[i] === 'G') imp = Math.min(imp, 3);
else if (s[i] === 'T') imp = Math.min(imp, 4);
}
return imp;
}
function solution(S, P, Q) {
const A = new Array(P.length);
for (let i = 0; i < P.length; i++) {
const s = S.slice(P[i], Q[i] + 1);
A[i] = impact(s);
}
return A;
}
And it failed all the performance tests
Now I changed it to the following code which I thought would be slower but to my surprise it scored 100%:
function solution(S, P, Q) {
let A = []
for (let i = 0; i < P.length; i++) {
let s = S.slice(P[i], Q[i] + 1)
if (s.indexOf('A') > -1) A.push(1)
else if (s.indexOf('C') > -1) A.push(2)
else if (s.indexOf('G') > -1) A.push(3)
else if (s.indexOf('T') > -1) A.push(4)
}
return A
}
Which to me made no sense, because I was using 4 indexOf which should be slower than 1 linear iteration of the same string. But it's not.
So, how does String.indexOf() work and why are 4 .indexOf so much faster than 1 iteration?
In your first solution you have two loops. The second loop is in impact. That second loop corresponds roughly to the four indexOf you have in the second solution.
One iteration of the second loop will do at most 4 comparisons, and there will be at most n iterations. So this makes at most 4n comparisons. The same can be said of the indexOf solution. Each of these four indexOf may need to scan the whole array, which represents n comparisons. And so that also amounts to a worst case of 4n comparisons.
The main difference however, is that the scanning that an indexOf performs, is not implemented in JavaScript, but in highly efficient pre-compiled code, while the first solution does this scanning with (slower) JavaScript code. As a rule of thumb, it is always more efficient to use native String/Array methods (like there are indexOf, slice, includes,...) than implementing a similar functionality with an explicit for loop.
Another thing to consider is that if there is an "A" in the data at position i, then the second solution will find it after i comparisons (internal to the indexOf implementation), while the first solution will find it after 4i comparisons, because it also makes the comparisons for the other three letters during the same iterations in which it looks for an "A". This extra cost decreases for when there is no "A", but a "C" somewhere, ...etc.

How complete a string without exceed a determinated limit?

I have a sequence of 47 numbers, where i must complete with 0 (zeros) to right if lenght is lower that 47.
var numbers = "42297115040000195441160020034520268610000054659";
var numbers_lenght = numbers.length;
if (numbers_lenght < 47)
numbers = numbers.concat("0");
alert(numbers);
But i want know of a way where the complement not exceed to > 47 independent of lenght < 47 is the string.
How make this?
On modern browsers, you can use padEnd here:
The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
var numbers = "54321";
const result = numbers.padEnd(47, '0')
console.log(result + ' :: ' + result.length);
You don't want to just concatenate 1 0 you want to add 47 - string_length zeros, right? You can use repeat() for that:
var numbers = "123456789123";
var numbers_length = numbers.length;
if (numbers_length < 47) {
numbers = numbers.concat('0'.repeat(47 - numbers_length))
}
console.log(numbers)
console.log("length: ", numbers.length)
There are several ways. You're on the right track. You are concatenating "0" to pad. What you are missing is the repetition.
There are a few ways to solve this. Since you seem to be learning, I'll start with the brute force approach to make sure you understand how it all works.
var numbers = "42297115040000195441160020034520268610000054659";
while (numbers.length < 47) {
numbers = numbers.concat("0");
}
alert(numbers);
All I did was change your if to while.
That said, there are methods built in to JavaScript which can do this for you.
var numbers = "42297115040000195441160020034520268610000054659";
numbers += "0".repeat(47 - numbers.length);
Even more expressive:
var numbers = "42297115040000195441160020034520268610000054659";
numbers = numbers.padEnd(47, "0");
Seems like you can accomplish what you're trying to do if you just stick your logic inside a while loop:
var number1 = "42297115040000195441160020034520268610000054659";
var number2 = "4229711504000019";
console.log(padString(number1, 47, "0"));
console.log(padString(number2, 47, "0"));
function padString(str, minimumLength, paddingCharacter) {
while (str.length < minimumLength) {
str = str.concat(paddingCharacter);
}
return str;
}

Longest Common Subsequence, required length on contiguous parts

I think I have enough grasp of the LCS algorithm from this page. Specifically this psedo-code implementation: (m and n are the lengths of A and B)
int lcs_length(char * A, char * B) {
allocate storage for array L;
for (i = m; i >= 0; i--)
for (j = n; j >= 0; j--) {
if (A[i] == '\0' || B[j] == '\0') L[i,j] = 0;
else if (A[i] == B[j]) L[i,j] = 1 + L[i+1, j+1];
else L[i,j] = max(L[i+1, j], L[i, j+1]);
}
return L[0,0];
}
The L array is later backtracked to find the specific subsequence like so:
sequence S = empty;
i = 0;
j = 0;
while (i < m && j < n) {
if (A[i]==B[j]) {
add A[i] to end of S;
i++; j++;
}
else if (L[i+1,j] >= L[i,j+1]) i++;
else j++;
}
I have yet to rewrite this into Javascript, but for now I know that the implementation at Rossetta Code works just fine. So to my questions:
1. How do I modify the algorithm to only return the longest common subsequence where the parts of the sequence are of a given minimum length?
For example, "thisisatest" and "thimplestesting" returns "thistest", with the contiguous parts "thi", "s" and "test". Let's define 'limit' as a minimum requirement of contiguous characters for it to be added to the result. With a limit of 3 the result would be "thitest" and with a limit of 4 the result would be "test". For my uses I would like to not only get the length, but the actual sequence and its indices in the first string. It doesn't matter if that needs to be backtracked later or not.
2. Would such a modification reduce the complexity or increase it?
From what I understand, analysing the entire suffix tree might be a solution to find a subsequence that fits a limit? If correct, is that significantly more complex than the original algorithm?.
3. Can you optimize the LCS algorithm, modified or not, with the knowledge that the same source string is compared to a huge amount of target strings?
Currently I'm just iterating through the target strings finding the LCS and selecting the string with the longest subsequence. Is there any significant preprocessing that could be done on the source string to reduce the time?
Answers to any of my questions are welcome, or just hints on where to research further.
Thank you for your time! :)

Finding a pattern in a binary string

I'm trying to find a repeating pattern in a string of binary digits.
eg. 0010010010 or 1110111011 = ok
not. 0100101101 = bad
The strings are 10 digits long (as above) & i guess 2 iterations of the 'pattern' are the minimum.
I started manually setting a 'bank' of patterns that the program could match it with but there must be a better way using an algorithm?
Searching got me nowhere - i think the language & terminology i'm using is incorrect..
Quite a challenge. What about this function?
function findPattern(n) {
var maxlen = parseInt(n.length/2);
NEXT:
for(var i=1; i<=maxlen; ++i) {
var len=0, k=0, prev="", sub;
do {
sub = n.substring(k,k+i);
k+= i;
len = sub.length;
if(len!=i) break;
if(prev.length && sub.length==i && prev!=sub) continue NEXT;
if(!prev.length) prev = sub;
} while(sub.length);
var trail = n.substr(n.length-len);
if(!len || len && trail==n.substr(0,len)) return n.substr(0,i);
}
return false;
}
It even works for any length strings with any contents. See the fiddle
Inspired by Jack's and Zim-Zam's answer, here is the list for brute force algorithm:
var oksubs =
["001","010","011","100","101","110",
"0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110",
"00000","00001","00011","00101","00110","00111","01000",
"01001","01010","01011","01100","01101","01110","01111",
"10000","10001","10011","10101","10110","10111","11000","11001",
"11010","11011","11100","11101","11110","11111"];
Thanks to Jack's comment, here is both short and effective code:
function findPattern(n) {
var oksubs = [n.substr(0,5),n.substr(0,4),n.substr(0,3)];
for(var i=0; i<oksubs.length; ++i) {
var sub = oksubs[i];
if((sub+sub+sub+sub).substr(0,10)==n) return sub;
}
return false;
}
You've only got 2^10 patterns, that's a small enough number that you can just precompute all of the valid strings and store the results in a 1024-element boolean array; if a string is valid, then convert it to an integer (e.g. "0000001111" = 15) and store "true" in the resulting array index. To check if a string is valid, convert it to an integer and look up the index in the precomputed boolean array.
If your strings are longer than 10 digits then you'll need to be more clever about determining if a string is valid, but since you only have 1024 strings you might as well be lazy about this.
maintaining an array of 2^10 wont help becuase it wont indicate which strings have repeating patterns.
To have a repeating pattern the pattern length can be only be <= 5
there can be a pattern of length 1.but pattern of length five will cover it. [STEP EDITED]
if there a pattern with length 2, there is always a pattern ith length 4.
from (1),(2), (3) and (4) , it is only necessary to check patterns of length 3,4 and 5
that means if first three digits match with next three digits proceed till end of string else break and go to 7
else match first four digits with next four if match proceed till end of string
else break and go to 8
else match first five digits with next four if match proceed till end of string
else break and go to 9
if one of 6, 7,8 is false, return failure
My brute-force approach would be:
by example
givenString: 0010010010:
create list of possible patterns for givenString 0010010010:
possiblePatterns = [00, 010, 0010, 00100, 01, 001, 0100, 10, 100]
repeat them to make strings with length >= 10
testPattern0 = 0000000000 // 00 00 00 00 00
testPattern1 = 010010010010 // 010 010 010 010
testPattern2 = 001000100010 // 0010 0010 0010
...
and check...
for each testPattern:
if '0010010010' is a substring of testPattern ==> pattern found
one of the matching strings:
testPattern2: 010010010010
givenString : 0010010010
found patterns:
foundPatterns = [010, 001, 100]
As one can see, this is a possibly redundant list as all patterns are basically the same, just shifted. But depending on the use case, this may actually be what you want.
Code:
function findPatterns(str){
var len = str.length;
var possible_patterns = {}; // save as keys to prevent duplicates
var testPattern;
var foundPatterns = [];
// 1) create collection of possible patterns where:
// 1 < possiblePattern.length <= str.length/2
for(var i = 0; i <= len/2; i++){
for(var j = i+2; j <= len/2; j++){
possible_patterns[str.substring(i, j)] = 0;
}
}
// 2) create testPattern to test against given str where:
// testPattern.length >= str.length
for(var pattern in possible_patterns){
testPattern = new Array(Math.ceil(len/pattern.length)+1).join(pattern);
if(testPattern.indexOf(str) >= 0){
foundPatterns.push(pattern);
}
}
return foundPatterns;
}
==> fiddle
As far as I can tell, there are 62 patterned binary strings of length 10 => 2^1 + 2^2 + 2^3 + 2^4 + 2^5. Here's some code that lists them and matches a patterned string:
function binComb (n){
var answer = []
for (var i=0; i<Math.pow(2,n);i++){
var str = i.toString(2)
for (var j=str.length; j<n; j++){
str = "0" + str
}
answer.push(str)
}
return answer
}
function allCycles(){
var answer = {}, cycled = ""
for (var i=1; i<=5; i++){
var arr = binComb(i)
for (var j=0; j<arr.length; j++){
while(cycled.length < 10){
cycled += arr[j]
if (10 - cycled.length < arr[j].length)
cycled += arr[j].substr(0,10 - cycled.length)
}
if (answer[cycled])
answer[cycled].push(arr[j])
else answer[cycled] = [arr[j]]
cycled = ""
}
}
return answer
}
function getPattern (str){
var patterns = allCycles()
if (patterns[str])
return patterns[str]
else return "Pattern not found."
}
OUTPUT:
console.log(getPattern("0010010010"))
console.log(getPattern("1110111011"))
console.log(getPattern("0100101101"))
console.log(getPattern("1111111111"))
["001"]
["1110"]
Pattern not found.
["1", "11", "111", "1111", "11111"]
In Python (again) but without regexps:
def is_repeated(text):
'check if the first part of the string is repeated throughout the string'
len_text = len(text)
for rep_len in range(len_text // 2, 0, -1):
reps = (len_text + rep_len) // rep_len
if (text[:rep_len] * reps).startswith(text):
return rep_len # equivalent to boolean True as will not be zero
return 0 # equivalent to boolean False
matchstr = """\
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
"""
for line in matchstr.split():
print('%r has a repetition length of %i' % (line, is_repeated(line)))
Output:
'1001110011' has a repetition length of 5
'1110111011' has a repetition length of 4
'0010010010' has a repetition length of 3
'1010101010' has a repetition length of 4
'1111111111' has a repetition length of 5
'0100101101' has a repetition length of 0
This answer uses a Python regular expression to be compiled with the VERBOSE flag set which allows multi-line regexps with comments and non-significant spaces. I also use named groups in the regexp.
The regexp was developed with the aid of the Kodos tool.
The regexp searches for the longest repeating strings of length five then four then three repeating characters. (two repeating characters is redundant as it is equal to the longer four; and similarly one repeating is made redundant by five).
import re
rawstr = r"""
(?P<r5> .{5}) (?P=r5) |
(?P<r4>(?P<_42> .{2}) .{2}) (?P=r4) (?P=_42) |
(?P<r3>(?P<_31> .{1}) .{2}) (?P=r3){2} (?P=_31)
"""
matchstr = """\
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
"""
for matchobj in re.finditer(rawstr, matchstr, re.VERBOSE):
grp, rep = [(g, r) for g, r in matchobj.groupdict().items()
if g[0] != '_' and r is not None][0]
print('%r is a repetition of %r' % (matchobj.group().strip(), rep))
Gives the output:
'1001110011' is a repetition of '10011'
'1110111011' is a repetition of '1110'
'0010010010' is a repetition of '001'
'1010101010' is a repetition of '1010'
'1111111111' is a repetition of '11111'

Categories

Resources