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 6 years ago.
Improve this question
In my organization, users must generate a password from numbers only (keypads are used to access), minimum length is 8 numbers. How can I make sure the password the user generats is not too weak (using c# on server procossing password change request), applying the following rule:
3 following numbers (even a part of password) are not sequential or repeated (9451238401 or 543555784)
The regular expression is:
^((?!(?<ch>.)\k<ch>\k<ch>)(?!012|123|234|345|456|567|678|789|890)[0-9]){8,}$
The (?!(?<ch>.)\k<ch>\k<ch>) will check for the same character repeated thrice. Note that for the various contiguous sequences I had to put them in a list of possible sequences, (?!012|123|234|345|456|567|678|789|890). [0-9] is the character that will be accepted as valid. The {8,} is for the minimum length.
If you want a general-purpose approach which tells you the number of repeated, ascending and descending digits:
static void checkStrength(string text, out int maxRepeats, out int maxAscending, out int maxDescending)
{
maxRepeats = 0;
maxAscending = 0;
maxDescending = 0;
int currRepeats = 0;
int currAscending = 0;
int currDescending = 0;
for (int i = 1; i < text.Length; ++i)
{
char curr = text[i];
char prev = text[i-1];
if (curr - prev == -1)
maxDescending = Math.Max(maxDescending, ++currDescending);
else
currDescending = 1;
if (curr - prev == 1)
maxAscending = Math.Max(maxAscending, ++currAscending);
else
currAscending = 1;
if (curr == prev)
maxRepeats = Math.Max(maxRepeats, ++currRepeats);
else
currRepeats = 1;
}
}
You would have to call this and then do what you want with the results:
int maxRepeats, maxAscending, maxDescending;
checkStrength(text, out maxRepeats, out maxAscending, out maxDescending);
if (maxRepeats > REPEAT_LIMIT || maxAscending > ASCENDING_LIMIT || maxDescending > DESCENDING_LIMIT)
{
// ... report error or whatever
}
If you don't need to vary the allowed number of repeated or ascending digits, then xanatos' regex is clearly by far the shortest code. This code is only needed if you need to vary the allowed counts at runtime.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
It's a little confusing but if you look at the example below you get it!
I have a special "Console.Write" method that takes a string, for example "§cHello %mThere!" and when printing to the Console, this method ignores the §c and %m (it changes the color of the Console).
Now, I have another method to print text by aligning it, by breaking the string into an array of strings every nth char. That means, if I pass a string of 100 chars and a LineLength of 10, it will break my string into an array of 10 strings with 10 chars each, then my Write method will print each one in a new line.
The problem is, when I split the text every nth char, it count the '§' and 'c' chars, and when I print it (the method for printing will remove those two), so the output is off by two chars.
So... I need a method that:
Splits a string into an array of strings every nth character.
However, it must not count '§' and the following char, or '%' and the next char as characters in that math.
The output must have those extra chars in the string array.
Example:
string Text = "§cOnce upon a time there was a §R%mnice girl named Cinderella. She was very poor and her father passed way."
int LineLength = 6;
return string[] Output = {
"§conce u" //[o n c e space u], thats 6 chars ignoring the first two.
"pon a " //[p o n space a space], thats 6 chars.
"time t" // etc
//etc
}
If someone help me write this, thanks in advance!!!
If I understand what you're saying this seems about right.
public static string[] ConsoleChunk(string input, int length){
List<string> Output = new List<string>();
int k = 0;
string currentString = "";
for(int i = 0; i < input.Length; i++){
if(k == 6){
Output.Add(currentString);
currentString = input[i].ToString();
k = 1;
}
else if(input[i] == '§' || input[i] == '%'){
currentString += input[i];
currentString += input[++i];
}
else{
k++;
currentString += input[i];
}
}
Output.Add(currentString);
return Output.ToArray();
}
Input
string test = "§cOnce upon a time there was a §R%mnice girl named Cinderella. She was very poor and her father passed way.";
Output
§cOnce u
pon a
time t
here w
as a §R%mn
ice gi
rl nam
ed Cin
derell
a. She
was v
ery po
or and
her f
ather
passed
way.
Given
public static IEnumerable<string> FunkyChunk(string source, int size)
{
var index = 0;
while (index < source.Length)
{
var sb = new StringBuilder(size*2);
for (int count = 0; count<size && index < source.Length; index++)
{
sb.Append(source[index]);
if (source[index] != '§' && source[index] != '%')
count++;
}
yield return sb.ToString();
}
}
Note : This is O(n) and used StringBuilder for less allocations even though there would be more succinct solutions. Using a fixed buffer and another index would likely be better
Usage
var input = "012345§678901234567%890123%4567890123456§§789012§345678901234";
foreach (var result in FunkyChunk(input,10))
Console.WriteLine(result);
Output
012345§6789
01234567%89
0123%456789
0123456§§789
012§3456789
01234
Full Demo Here
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to count sum of the numbers found in a string, not digits. For example, there are string = "abc12df34", and the answer must be 46 (12+34), not 10. Also maybe negative numbers for string = "abc10gf-5h1" answer must be 6. I can not understand how to implement this.
RegEx approach:
string input = "abc10gf-5h1";
int result = Regex.Matches(input, "-?[0-9]+").Cast<Match>().Sum(x =>int.Parse(x.Value));
While the above answer is elegant, it's not really something to understand what to do or how to approach the problem. Here is another, more explicit solution.
In words, you iterate through your string, collect digits as long as there are any, if you find a nondigit, your number is finished, you convert the number to integer and sum it up, clear the number string. The same you do if you find the end of the string. On the next found digit you start collecting digits again.
This algorithm will fail on any number larger than than 10 digits in multiple ways (as the other answer will also), but this is just for demonstration anyway.
string input = "abc10gf-5-1h1";
var number = new char[10];
int numberlength = 0;
int pos = 0;
int sum = 0;
while (pos < input.Length)
{
char c = input[pos++];
if (char.IsDigit(c))
{
number[numberlength++]=c;
}
else
{
if (numberlength > 0)
{
sum += int.Parse(new String(number, 0, numberlength));
numberlength = 0;
}
if (c=='-')
number[numberlength++]=c;
}
}
if (numberlength > 0)
sum += int.Parse(new String(number, 0, numberlength));
Console.WriteLine(sum);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So I am doing an online coding challenge and have come across this issue that has me stumped:
This is my code:
static void Main(String[] args)
{
int noOfRows = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < noOfRows; i++)
{
string odds = "";
string evens = "";
//get the input word from console
string word = Console.ReadLine();
for (int j = 0; j < word.Length; j++)
{
//if the string's current char is even-indexed...
if (word[j] % 2 == 0)
{
evens += word[j];
}
//if the string's current char is odd-indexed...
else if (word[j] % 2 != 0)
{
odds += word[j];
}
}
//print a line with the evens + odds
Console.WriteLine(evens + " " + odds);
}
}
Essentially, the question wants me to get the string from the console line and print the even-indexed characters (starting from index=0) on the left, followed by a space, and then the odd-indexed characters.
So when I try the word 'Hacker', I should see the line printed as "Hce akr". When I debugged it, I saw the code successfully put the letter 'H' on the left (because it is index=0, thus even), and put the letter 'a' on the right (odd index). But then when it got to the letter 'c', instead of going through the first IF path (even index), it skips it and goes to the odd index path, and places it on the right hand side?
The funny thing is when I try the word 'Rank' it works fine and prints the correct statement: "Ra nk", yet other words do not.
Its just bizarre that I'm getting different results.
What am I missing?
word[j] is a character in your string; j is the index you want to check the evenness of.
if (j%2) should provide the correct path. You're using if( word[j] %2) which is doing modular arithmetic on a character, not an index. Most likely using modulo on the ASCII value. Hope this helps.
you want to check if the index is even,yet you compare word[j] % 2 == 0 which is not an index.
what you should do:
if(j % 2 == 0){
}
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 4 years ago.
Improve this question
I have a large group of Hindi numbers which i want to convert into numeric values but i don't know how to convert them . Please suggest me appropriate way to achieve this.
Note Please don't suggest me replace method.
eg. convert this number २०७४ to equivalent to 2074.
I believe this is what you're after but be aware that this code is written by someone who doesn't speak Hindi, read Hindi or know Hindi.
I found the digits on the wikipedia page but I absolutely have no idea what I'm doing.
The google page (which I found by just googling for the individual digits from the original string in the question) seems to indicate the following:
The digits for 0-9 are ०१२३४५६७८९
I clicked on a link and used the last character of the url as the digit
Note that 4 had to be gotten as the second digit of 14, and there seems to be a disambiguity suffix on that link as well
They have unicode code points ranging from 2406 through 2415, in that order
The double digits numbers follow the system to a tee, so it seems to be just a 10-digit numeric system using different code points
But note that there are far too few examples for me to be absolutely certain this holds true for all numbers
If anyone pokes hole in this answer I will take it down but feel free to grab all the code from it first if you think it can be improved.
Also bear in mind that the OP explicitly asked for a non-replace method. The whole thing can probably be written in a oneliner with that but since that doesn't seem to be an acceptable answer then here we are.
With all that said, here's a non-string-replace version that mimicks basic numeric parsing using different symbols:
Note: There's about 7 tons of error-handling that isn't present here, such as empty strings, etc.
public static bool TryParseHindiToInt32(string text, out int value)
{
const int codePointForZero = 2406;
const int codePointForNine = codePointForZero + 9;
int sign = +1;
int index = 0;
if (index < text.Length && text[index] == '-') // todo: hindi minus?
{
index++;
sign = -1;
}
value = 0;
while (index < text.Length)
{
char c = text[index];
if (c < codePointForZero || c > codePointForNine)
{
value = 0;
return false;
}
if ((uint)value > 214748364u)
{
value = 0;
return false;
}
value *= 10;
value += (c - codePointForZero);
index++;
}
value *= sign;
return true;
}
Test:
string digits = "२०७४";
TryParseHindiToInt32(digits, out int i);
Console.WriteLine(i);
Outputs:
2074
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
I'm trying to do a Prime Number finder but cant see why it's not working.
When i run the debug test nothing show in the console. Could someone check it and tell me what i do wrong?
List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
int maxPrime = 11; //The maximum found Primes
int primeCount = primes.Count; //Current Number of Primes
int num = 4; //Current Number
int x = 0; //
int curPrime = primes[x];
while (primeCount < maxPrime)
{
if (x != primeCount)
{
if (num % primes[x] == 0)
{
num++;
x = 0;
}
else
{
x++;
}
}
else
{
primes.Add(num);
primeCount=primes.Count;
x = 0;
}
}
primes.ForEach(i => Console.Write("{0}\t", i));
You have an infinite loop.
Since you never modify primeCount or maxPrime, this will always be true:
while (primeCount < maxPrime)
In order to end that loop, you need to modify one of those two values in such a way that the condition will evaluate to false.
(Note: There appear to also be other bugs/problems in the code aside from this. For example, num = num++; doesn't do what you probably think it does.)