Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Given a string S how can we check that weather we can convert it to palindrome or not by just changing one single letter. Please help a fast solution to do it.
Example : Let us have S = "mixem". Then the answer is YES as we can change "e" to "i" to make it palindrome.
String of length can be 1000 at max.
Assuming 0-based indexing, it suffices to check, for each 0 <= i < your_string_length, if the number of positions for which the characters at positions i and your_string_length - i - 1 are different is at most 2. If yes then the answer is also yes, otherwise it's not.
Basically, this checks how many pairs of characters with equal distance from the sides of the string are different: if only one such pair is different, then we can change one of its characters and make it equal. If more pairs, then we'd need to change more characters.
If I understood your example correctly it means that you can change a single letter to ANY other letter. This makes the algorythm embarrassingly simple. You need to parse the word from sides into center and calculate number of mistakes. If it is <=1 then you can convert into a palindrome with 1 change or it is already a palindrome.
var s = "mixen", errors = 0;
for(var i = 0; i < s.length/2; i++){
if(s[i] != s[s.length-1-i]){
errors++;
}
var isPalindrom = errors == 0;
var isPalindromConvertible = errors <= 1;
PS. You didn't specify language, so i've written in most convenient for me now: javascript
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to compare two big ints. I'm looking at the docs: https://pkg.go.dev/math/big#Int and I don't see an inequality operator, but I do see an equality one (Cmp).
How am I meant to compare a big int a to a big int b?
Am I meant to subtract b from a and compare the sign? Or is there something I am missing? E.g.
c := (new.bigInt).Sub(a, b)
i := c.Sign()
if i < 0 {
fmt.Println("a < b")
}
It seems a little odd to me there is not an inequality operator, nor much about this online so I think I am doing something wrong.
Cmp returns: -1 if x < y, 0 if x == y, +1 if x > y
Go Playground Example
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How can I generate a matrix in Matlab that has 5 rows and some specific number of columns and the elements may only be binary numbers and the column sum has to be less or equal to 3?
Some possibilites without loops:
Using strings:
D = 5;
S = 3;
numbers = str2mat(dec2bin(0:2^D-1))-'0';
numbers = numbers(sum(numbers,2)<=S,:);
Using combinatorial numbers, one line:
numbers = [zeros(1,D); cell2mat(arrayfun(#(s) fliplr(full(sparse((1:nchoosek(D,s)).'*ones(1,s), nchoosek(1:D,s), 1))), 0:S, 'uni', 0).')];
How about this: The maximum binary number, that you can represent by 5bit is 2^5-1 = 31 and skip through these to find the ones with sum of digits <= 3.
Something like
n = 1:1:31;
for ii = 1:length(ii)
bin = dec2bin(ii)
digitSum = 0
for d = 1:length(bin)
digitSum = digitSum + str2num(bin(d))
end
if (digitSum <= 3)
%store results
end
end
Here is a vecotorized solution to provide all occurences efficiently:
Bstr =dec2bin(1:31);
Bstr(sum(dec2bin(0:31),2)<=sum('00111'),:)=='1'
Inspired by the solution of #pyStarter
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
could you please help me with my homework? I am just a beginner and know very little about Pascal :(
I am supposed to write a program for division but have special conditions depending on their decimal numbers. The outputs should look something like this:
no decimals then 20/5=4 (there can only be the 4; it can't be like 4.00)
if decimals then only 1 decimal number 9/4=2.3
if the decimal is an infinite of the same number then 1/3=0.(3)
How can I do it? I was thinking about putting it into an array but I don't know how to find where the decimal point is nor I know how to write the brackets into an output.
I can't manage to solve the 3rd point but here is the code for the first 2:
Var
x,y : integer;
z : real;
Begin
z := x / y;
if z = x div y
then Write(z:4:0) //Write a real var without any decimals
else Write(z:4:1); //Write a real var with just 1 decimal
End.
You'll have to change things around , like reading x and y , but from this code you can almost do your homework .
I'll try to solve the 3rd time , check the answer again after some time.
To solve 3rd point: think about x and y as about a fraction. Here is the basic idea:
A:=A/GCD(A,B);B:=B/GCD(A,B)
Integer part := A div B; A:=A mod B
Non-periodical part: while A mod 2=0 do BEGIN C:=C*2;A:=A div 2; END; while A mod 5=0 do BEGIN C:=C*5;A:=A div 5; END
Find M so that A*M=999..999.
Suppose I have a string like 'meetateight' and I need to segment it into meaningful words like 'meet' 'at' 'eight' using dynamic programming.
To judge how “good” a block/segment "x = x1x2x3" is, I am given a black box that, on input x, returns a real number quality(x) such that: A large positive value for quality(x) indicates x is close to an English word, and a large negative number indicates x is far from an English word.
I need help with designing an algorithm for the same.
I tried thinking over an algorithm in which I would iteratively add letters based on their quality and segment whenever there is a dip in quality.
But this fails in the above example because it cuts out me instead of meet.
I need suggestions for a better algorithm.
Thanks
What about building a Trie using an English Dictionary and navigating it down scanning your string with all the possible path to leaf (backtracking when you have more than one choice).
You can use dynamic programming, and keep track of the score for each prefix of your input, adding one letter at a time. Each time you add a letter, see if any suffixes can be added on to a prefix you've already used (choosing the one with the best score). For example:
m = 0
me = 1
mee = 0
meet = 1
meeta = 1 (meet + a)
meetat = 1 (meet + at)
meetate = 1 (meet + ate)
meetatei = 1 (meetate + i)
meetateig = 0
meetateigh = 0
meetateight = 1 (meetat + eight)
To handle values between 0 and 1, you can multiply them together. Also save which words you've used so you can split the whole string at the end.
I wrote a program to do this at my blog; it's too long to include here. The basic idea is to chop off prefixes that form words, then recursively process the rest of the input, backtracking when it is not possible to split the entire string.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Some applications (or websites) compute a password's complexity as you type.
They typically display a red bar which turns orange, then green, then even greener as your password gets longer, and contains more classes of characters (e.g., lowercase, uppercase, punctuation, digits).
How can I reliably calculate the complexity of a password?
I've come up with the following algorithm, but I'm concerned by the fact that it rates Password1! as "very strong" and ]#feé:m as "weak" because it's only 7 characters long.
private int GetPasswordComplexity(string password)
{
if (password.Length <= 4)
return 1;
int complexity = 0;
int digit = 0;
int letter = 0;
int cap = 0;
int other = 0;
for (int i = 0; i < password.Length; i++)
{
if (char.IsDigit(password[i]) && i!=password.Length-1)
digit = 1;
else if (char.IsLower(password[i]))
letter = 1;
else if (char.IsUpper(password[i]) && i!=0)
cap = 1;
else
other = 1;
}
complexity = digit + letter + cap + other;
if (password.Length <= 7)
complexity = Math.Min(3, complexity);
return complexity;
}
Using something like cracklib is very good if you can afford the time of checking against all of the potential rules. If you just want something quick -- say for a javascript-based strength meter -- then consider estimating the number of potential guesses that would be required for a brute force attack. For every character type seen update a multiplier based on the number of potential characters of that type. So if you have only digits, then the multiplier would be 10. If you have only lowercase, then the multiplier is 26. If both, then the multiplier is 36 -- that is for each character in the password, a brute force attack would need to try up to 36 different characters. A password containing both upper and lowercase characters, digits, and punctuation, then would have a multiplier of 10 + 26 + 26 + 32 = 94 (more or less depending on the allowable punctuation).
To estimate the maximum number of permutations a brute force method would take, raise the multiplier to the power equal to the number of digits in the password. This gives you then maximum number of guesses it would take to break the password using a brute force attack. Assume that each guess takes one cpu cycle and given the fastest processor calculate how long it would take to break a password given a certain number of permutations. For example, if my multiplier was 10 and the password was 10 characters long, then I would have 10,000,000,000 potential combinations. On 3GHz processor, this ought to take 10/3 * k or 3k seconds (where k is the number of cycles per guess, typically small). Clearly, this is a weak password.
Now, establish some ranges that represent reasonable password strengths. For example, if you think that an 8 character password with upper and lowercase characters is minimally required for medium strength, then your cutoff would be 52^8 or approximately 1.5 years on a 3GHz processor (assuming k = 1). If you add in digits, then the cutoff becomes 62^8 or approximately 8 years on 3GHz processor.
To put it in use, then you only need keep track of which kinds of characters you see, construct the appropriate multiplier, calculate the expected permutations based on password length, and compare it against your predefined cutoffs to determine what strength the password has.
I'd recommend using cracklib for this.
I would not simply set a flag when you see a digit, capital etc. but give points for them. Something like a score system. A normal letter counts 1, a digit 2 and a special character 3.
Now your total number accounts for both the number of characters and how the password is made up. You only have to draw lines for what is weak and what is strong.
You should also check against a dictionary. I think apple does this in it's built-in password checker.