I want to solve this problem of Possible three letter words
Here is following words. (17 characters)
19920620forestJSR
How many possible ways to make 3 length word with given characters?
Ex: 192, 162, Rer, ….
Rule:
Number 0 is different alpha o. (0 != o)
case-sensitive is available. (R != r)
same character repeat not available. (rr1 : wrong)
Hint:
17 16 15 : wrong
How can I solve this
I am trying with this code
function permute($str,$i,$n) {
if ($i == $n)
print "$str\n";
else {
for ($j = $i; $j < $n; $j++) {
swap($str,$i,$j);
permute($str, $i+1, $n);
swap($str,$i,$j); // backtrack.
}
}
}
function swap(&$str,$i,$j) {
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
$str='19920620forestJSR';
permute($str,0,strlen($str));
But in output have some error with numeric characters
Output look like (when str=19920620forestJSR)
n���Z��뢿�Yh��fzj+�ȳz��ߍ�シo+^��aj�-y�k��m��e�ƭ{�6�ټ�zȧo�h���j���Z�ǫ���������z�a���X�y�����
Output look like (when str=forestJSR)
foresSJtR
foresSJRt
foresStJR
foresStRJ
foresSRtJ
foresSRJt
foresRJSt
foresRJtS
The rules says you need combinations of 3 characters, whithout repeating letters. Your code is generating all combinations for 17 letters. So, the script is looping for millions of possibilities (355,687,428,096,000).
The number of permutations of n distinct objects, taken r at a time is
nPr = n! / (n - r)!
So, for "19920620forestJSR" (14 different letters only), using 3 at a time:
14P3 = 14! / (14 - 3)! = 14! / 11! = (14)(13)(12) = 2184
Related
Premises / What you want to achieve
I want to know the algorithm for the minimum number of trials of inversion processing in a character string with n characters.
Realized without using standard functions and libraries such as strrev()
What I tried ・ Source code
The number of trials was n times and n / 2 times.
The number of trials is even smaller than that (I don't know if there is an algorithm)
n times
$string = '123456789';
$result ='';
for ($i = 0; $i < strlen($string); $i++) {
$result = $string[$i] . $result;
}
echo $result. "\n"; // 987654321
echo "count: {$i} times \n"; // count: 9 times
n / 2 times
Strictly speaking, even digits are n / 2 times, odd digits are n / 2 + 1 times.
$string = '123456789';
$result = '';
for ($i=0, $j=strlen($string)-1; $i<$j; $i++, $j--) {
[$result[$i], $result[$j]] = [$string[$j], $string[$i]];
}
if (strlen($string) % 2 === 1) {
$result[$i] = $string[$i];
$i++;
}
echo $result."\n"; // 987654321
echo "count: {$i} times\n"; // count: 5 times
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
one of the rules in our password creation is, it shouldn't contain a sequence of number or alphabets.
ex.12345, pwd45678, pwd_abcdef, pwd_abc123
all of these are not allowed.
Any suggestion how to check for sequence?
By sequence meaning it shouldn't be order like for numbers 1-10 or letters in the alphabet. So the password shouldn't contain alphabet sequence or order and numbers in 1-10 order. So password containing ABC or DEF or HIJK is not allowed and passwords containing number orders like 1234 or 4567 are not allowed but passwords containing ABNOE or 19334 is ok.
TIA
A specific rule for no 2 adjacent digits or letters:
if (preg_match("#(\d{2,}|[[:alpha:]]{2,})#u", $input)) {
return false;
}
You can try it out here.
However, there are packages available specifically for password strength checking. They will have configurable rules or tests.
you can use the code below,I used the "asci code" to resolve the problem, it is already tested for your examples :
<?php
$passwords = [
'12345',
'pwd45678',
'pwd_abcdef',
'pwd_abc123',
];
var_dump(check_password_sequence($passwords[3], 4));
function check_password_sequence($password, $max) {
$j = 0;
$lenght = strlen($password);
for($i = 0; $i < $lenght; $i++) {
if(isset($password[$i+1]) && ord($password[$i]) + 1 === ord($password[$i+1])) {
$j++;
} else {
$j = 0;
}
if($j === $max) {
return true;
}
}
return false;
}
I have problem with my code while finding permutation of string for string with length greater than 7. For eg 'abcdefgh'. I have to find the permutation of word up to 12 length. Please review my code and suggest if any optimization can be done.
function permute($str)
{
if (strlen($str) < 2)
{
return array($str);
}
$permutations = array();
$tail = substr($str, 1);
foreach (permute($tail) as $permutation)
{
$length = strlen($permutation);
for ($i = 0; $i <= $length; $i++)
{
$permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
}
}
/* Return the result */
return $permutations;
}
$arr = permute('abcdefghijkl');
It seems like the core of the solution should lie in not checking everything. For example, if you give me the word "earthquakes", it should be immediately evident that there are no dictionary words for anything beginning with "qk", thus checking all permutations of the form q k _ _ _ _ _ _ _ _ _ is unnecessary. This type of check will save you a lot of comparison and lookup operations. This "qk" example alone would rule out 362,880 (9!) permutations that you would have needed to check otherwise.
So instead of calculating ALL the permutations, then pulling from the dictionary if it matches one of the permutations, I would make sure that each permutation you're generating is actually a possible word first.
I want to calculate Frequency (Monobits) test in PHP:
Description: The focus of the test is
the proportion of zeroes and ones for
the entire sequence. The purpose of
this test is to determine whether that
number of ones and zeros in a sequence
are approximately the same as would be
expected for a truly random sequence.
The test assesses the closeness of the
fraction of ones to ½, that is, the
number of ones and zeroes in a
sequence should be about the same.
I am wondering that do I really need to calculate the 0's and 1's (the bits) or is the following adequate:
$value = 0;
// Loop through all the bytes and sum them up.
for ($a = 0, $length = strlen((binary) $data); $a < $length; $a++)
$value += ord($data[$a]);
// The average should be 127.5.
return (float) $value/$length;
If the above is not the same, then how do I exactly calculate the 0's and 1's?
No, you really need to check all zeroes and ones. For example, take the following binary input:
01111111 01111101 01111110 01111010
. It is clearly (literally) one-sided(8 zeroes, 24 ones, correct result 24/32 = 3/4 = 0.75) and therefore not random. However, your test would compute 125.0 /255 which is close to ½.
Instead, count like this:
function one_proportion($binary) {
$oneCount = 0;
$len = strlen($binary);
for ($i = 0;$i < $len;$i++) {
$intv = ord($binary{$i});
for ($bitp = 0;$bitp < 7;$bitp++) {
$oneCount += ($intv>>$bitp) & 0x1;
}
}
return $oneCount / (8 * $len);
}
Given a list of common words, sorted in order of prevalence of use, is it possible to form word combinations of an arbitrary length (any desired number of words) in order of the 'most common' sequences. For example,if the most common words are 'a, b, c' then for combinations of length two, the following would be generated:
aa
ab
ba
bb
ac
bc
ca
cb
cc
Here is the correct list for length 3:
aaa
aab
aba
abb
baa
bab
bba
bbb
aac
abc
bac
bbc
aca
acb
bca
bcb
acc
bcc
caa
cab
cba
cbb
cac
cbc
cca
ccb
ccc
This is simple to implement for combinations of 2 or 3 words (set length) for any number of elements, but can this be done for arbitrary lengths? I want to implement this in PHP, but pseudocode or even a summary of the algorithm would be much appreciated!
Here's a recursive function that might be what you need. The idea is, when given a length and a letter, to first generate all sequences that are one letter shorter that don't include that letter. Add the new letter to the end and you have the first part of the sequence that involves that letter. Then move the new letter to the left. Cycle through each sequence of letters including the new one to the right.
So if you had gen(5, d)
It would start with
(aaaa)d
(aaab)d
...
(cccc)d
then when it got done with the a-c combinations it would do
(aaa)d(a)
...
(aaa)d(d)
(aab)d(d)
...
(ccc)d(d)
then when it got done with d as the 4th letter it would move it to the 3rd
(aa)d(aa)
etc., etc.
<?php
/**
* Word Combinations (version c) 6/22/2009 1:20:14 PM
*
* Based on pseudocode in answer provided by Erika:
* http://stackoverflow.com/questions/1024471/generating-ordered-weighted-combinations-of-arbitrary-length-in-php/1028356#1028356
* (direct link to Erika's answer)
*
* To see the results of this script, run it:
* http://stage.dustinfineout.com/stackoverflow/20090622/word_combinations_c.php
**/
init_generator();
function init_generator() {
global $words;
$words = array('a','b','c');
generate_all(5);
}
function generate_all($len){
global $words;
for($i = 0; $i < count($words); $i++){
$res = generate($len, $i);
echo join("<br />", $res);
echo("<br/>");
}
}
function generate($len, $max_index = -1){
global $words;
// WHEN max_index IS NEGATIVE, STARTING POSITION
if ($max_index < 0) {
$max_index = count($words) - 1;
}
$list = array();
if ($len <= 0) {
$list[] = "";
return $list;
}
if ($len == 1) {
if ($max_index >= 1) {
$add = generate(1, ($max_index - 1));
foreach ($add as $addit) {
$list[] = $addit;
}
}
$list[] = $words[$max_index];
return $list;
}
if($max_index == 0) {
$list[] = str_repeat($words[$max_index], $len);
return $list;
}
for ($i = 1; $i <= $len; $i++){
$prefixes = generate(($len - $i), ($max_index - 1));
$postfixes = generate(($i - 1), $max_index);
foreach ($prefixes as $pre){
//print "prefix = $pre<br/>";
foreach ($postfixes as $post){
//print "postfix = $post<br/>";
$list[] = ($pre . $words[$max_index] . $post);
}
}
}
return $list;
}
?>
I googled for php permutations and got: http://www.php.happycodings.com/Algorithms/code21.html
I haven't looked into the code if it is good or not. But it seems to do what you want.
I don't know what the term is for what you're trying to calculate, but it's not combinations or even permutations, it's some sort of permutations-with-repetition.
Below I've enclosed some slightly-adapted code from the nearest thing I have lying around that does something like this, a string permutation generator in LPC. For a, b, c it generates
abc
bac
bca
acb
cab
cba
Probably it can be tweaked to enable the repetition behavior you want.
varargs mixed array permutations(mixed array list, int num) {
mixed array out = ({});
foreach(mixed item : permutations(list[1..], num - 1))
for(int i = 0, int j = sizeof(item); i <= j; i++)
out += ({ implode(item[0 .. i - 1] + ({ list[0] }) + item[i..], "") });
if(num < sizeof(list))
out += permutations(list[1..], num);
return out;
}
FWIW, another way of stating your problem is that, for an input of N elements, you want the set of all paths of length N in a fully-connected, self-connected graph with the input elements as nodes.
I'm assuming that when saying it's easy for fixed length, you're using m nested loops, where m is the lenght of the sequence (2 and 3 in your examples).
You could use recursion like this:
Your words are numbered 0, 1, .. n, you need to generate all sequences of length m:
generate all sequences of length m:
{
start with 0, and generate all sequences of length m-1
start with 1, and generate all sequences of length m-1
...
start with n, and generate all sequences of length m-1
}
generate all sequences of length 0
{
// nothing to do
}
How to implement this? Well, in each call you can push one more element to the end of the array, and when you hit the end of the recursion, print out array's contents:
// m is remaining length of sequence, elements is array with numbers so far
generate(m, elements)
{
if (m == 0)
{
for j = 0 to elements.length print(words[j]);
}
else
{
for i = 0 to n - 1
{
generate(m-1, elements.push(i));
}
}
}
And finally, call it like this: generate(6, array())