Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I need to scramble chars in word. And this scrambling must not be random. In other words, each time of scrambling (on the same word) the new result must be equal to last scramble result (on the same word). Very simple example is XOR. But XOR is very easy to decode and I need something more strength. Could you recommend library for such purpose that identically works on C# and Javascript?
Thank you for any advice!:)
You can use random with fixed seed if you really want to scramble characters:
string input = "hello";
char[] chars = input.ToArray();
Random r = new Random(2011); // Random has a fixed seed, so it will always return same numbers (within same input)
for (int i = 0 ; i < chars.Length ; i++)
{
int randomIndex = r.Next(0, chars.Length);
char temp = chars[randomIndex];
chars[randomIndex] = chars[i];
chars[i] = temp;
}
return new string(chars);
Although I don't really agree about what you were trying to do, here's a link to MD5 javascript library (assuming what you're trying to do is some kind of encryption). As for the C# part, it's a built in feature.
You can use any of the built in .NET classes to generate random numbers and use these to scramble your string. For all the subsequent scramble attempts you can use the result from the first scramble operation. This assumes that the result from the first call is stored somewhere.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Ok so i'm halfway into the semester in a C# coding class and I just have a simple question.
When Im reading others code I see people do something like this:
string exampleString = new string();
StreamReader exampleSr = new StreamReader();
Int exampleInt = new int();
char a = new Char();
I have a few questions:
For like the String one, wouldn't it be the same if I just did
string exampleString = " ";?
What do people use these for?
And also what goes inside the parenthesis?
Just a few simple questions, im not sure what to search up to my find the answer to my questions so im asking here, Thanks!
In the case of the string, string a = ""; is not the same as string b = new string();.
Why:
a is now set to literally "", that means an empty string.
trying to define b in the way shown above will result in an error, because the constructor of string is not defined for 0 arguments.
you can declare a cariable like this, too:
string c;
this will result in an empty variable with no value in it (yet).
you'll have to assign it a value before you can use it, though (otherwise you'll get an error).
I'm not sure if i gave you a useful answer, so please ask me if you have any further questions.
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 5 years ago.
Improve this question
I'm reading some documentation (under NDA) where is says to do the following operation:
Rnd1[11..7] <+> Rnd2[11..7]
Rnd1 and Rnd2 represent an array of bytes.
What should operator <+> do here?
I've browsed through entire documentation and can't find an explanation.
It is likely an attempt to make an ASCII representation of the symbol ⊕, that symbol represents XOR.
So the documentation is likely telling you to take bytes 11 through 7 of Rnd1 and xor them with the same bytes in Rnd2
public byte[] YourOperator(byte[] rnd1, byte[] rnd2)
{
byte[] result = new byte[5];
for(int i = 7; i <= 11; i++)
{
result[i - 7] = rnd1[i] ^ rnd2[i];
}
return result;
}
Be careful to check if the 7..11 is a inclusive or exclusive upper bound, that will change the number of bytes you are working with from a 5 to a 4 and the <= to a <.
The fact you signed a NDA to get the documentation means you should have working relationship with the company that gave it to you, the best option is to contact them and ask for clarification so you know for sure what they meant.
I like the Linq way of doing things, e.g.
rnd1.Zip(rnd2, (r1, r2) => r1 ^ r2).Skip(7).Take(4);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am looking to create a small windows app that will take a text file of hundreds, maybe thousands of lines of text and then randomize the text and print 5 lines seperated by a line break. I should be able to copy from the app and each time I hit the "generate" button it should delete the previous 5 text outputs.
Here's an example:
https://www.random.org/lists/
The difference is that this app randomizes and prints all lines. Could someone point me to some resources on how to do this exact thing?
Microsoft own c# developer portal has examples of how to read from a text file
How to: Read From a Text File (C# Programming Guide) which can get you started with loading the text.
Microsoft's own Developer Network also has information on random number generation and examples at Random Class
finally Microsoft's own ASP.Net ASP.NET Samples has load of examples and information about building web (or desktop) applications.
You should be able to find working examples and API information on all three of these locations, that will help you with your quest of development of C# applications.
//Initialize variables
static Random rnd;
static StreamReader reader;
static List<string> list;
//here we load the text file into a stream to read each line
using (reader = new StreamReader("TextFile1.txt"))
{
string line;
list = new List<string>();
rnd = new Random();
int index;
//read each line of the text file
while (!reader.EndOfStream)
{
line = reader.ReadLine();
//add the line to the list<string>
list.Add(line);
}
//pull 5 lines at random and print to the console window
for (int i = 0; i < 5; i++)
{
index = rnd.Next(0, list.Count);
Console.WriteLine(list[index]);
}
}
Console.ReadKey();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I've been researching on finding an efficient solution to this. I've looked into diffing engines (google's diff-match-patch, python's diff) and some some longest common chain algorithms.
I was hoping on getting you guys suggestions on how to solve this issue. Any algorithm or library in particular you would like to recommend?
I don't know what "longest common [[chain? substring?]]" has to do with "percent difference", especially after seeing in a comment that you expect a very small % difference between two strings that differ by one character in the middle (so their longest common substring is about one half of the strings' length).
Ignoring the "longest common" strangeness, and defining "percent difference" as the edit distance between the strings divided by the max length (times 100 of course;-), what about:
def levenshtein_distance(first, second):
"""Find the Levenshtein distance between two strings."""
if len(first) > len(second):
first, second = second, first
if len(second) == 0:
return len(first)
first_length = len(first) + 1
second_length = len(second) + 1
distance_matrix = [[0] * second_length for x in range(first_length)]
for i in range(first_length):
distance_matrix[i][0] = i
for j in range(second_length):
distance_matrix[0][j]=j
for i in xrange(1, first_length):
for j in range(1, second_length):
deletion = distance_matrix[i-1][j] + 1
insertion = distance_matrix[i][j-1] + 1
substitution = distance_matrix[i-1][j-1]
if first[i-1] != second[j-1]:
substitution += 1
distance_matrix[i][j] = min(insertion, deletion, substitution)
return distance_matrix[first_length-1][second_length-1]
def percent_diff(first, second):
return 100*levenshtein_distance(a, b) / float(max(len(a), len(b)))
a = "the quick brown fox"
b = "the quick vrown fox"
print '%.2f' % percent_diff(a, b)
The Levenshtein function is from Stavros' blog. The result in this case would be 5.26 (percent difference).
In addition to difflib and other common subsequence libraries, if it's natural language text, you might look into stemming, which normalizes words to their root form. You can find several implementations in the Natural Language Toolkit ( http://www.nltk.org/ ) library. You can also compare blobs of natural language text more semantically by using N-Grams ( http://en.wikipedia.org/wiki/N-gram ).
Longest common chain? Perhaps this will help then: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
Another area of interest might be the Levenshtein distance described here.
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 9 years ago.
Improve this question
How can I print different length of spaces in C#?
Write(number*" ");
doesn't work here.
EDIT: But it works in Python.
Indeed, there's no * operator taking a string and an integer.
The simplest option is probably to use the string(char, int) constructor:
Write(new string(' ', number));
Depending on your actual use case, you might want to look at string.PadLeft and string.PadRight too, if you're actually trying to pad an existing string.
You can do it like that:
Write(new String(' ', number)); // <- ' ' character, number times
That's, probably the simplest way. You may also want to look String.Format esp. if you try to print out a padded data. For instance:
// Prints out myData padded left up to 10 characters
Write(String.Format("{0,10}", myData));
Try with this snippet:
int index = 0;
while(index<number)
{
System.Console.Write(" ");
index++;
}
You can encapsulate this in a wrapper function, and call it as
printblank(number);
using:
void printblank(int number)
{
int index = 0;
while(index<number)
{
System.Console.Write(" ");
index++;
}
}
Thereby you can use this function repeatedly, as per your requirement.