As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have a text file with characters like this(eg.):
Hi man. how is going. cool. lool. love this thing. fo real. yep.no way.it's real.haha.
The character for this is predetermined and in this case is '.' I'm supposed to be replacing 40% of this character with another character. The 40% of characters are to be chosen at random (only choosing from '.'). How would I go about finding these characters, and then replacing them?
You find the number of ., example: 20 and store the indices of . into an array
You find how many of those you want to delete: example: 8
Then you loop through the array of indices 8 times, get a random one from that array and replace that index with space. Btw, you'll need to remove the updated index from the array at each update.
What you're wanting to do is find all of the indexes of the '.' and store them somewhere, in this case an array. Once you have all of those indexes you take the largest index and use that as the maximum for the random number generator. Divide the largest index by 40 and you have how many times you need to replace a character. Take the random number you've generated and start replacing characters at those indexes.
int i = 0, chr[] = 0;
while (chr[i] != -1)
{
if (string.indexof(specifiedChar, chr[i]) != -1)
{
chr[i] = string.indexof(specifiedChar, chr);
i ++;
}
else
{
chr = -1;
}
}
chr[] will have the indexes of all of your .'s (assumingi specificedChar = '.'). i = number of '.'s. Don't take the above code as ready to roll out, but it's generally correct for your problem.
endInt = i;
int x = RandomNumber.Next(startInt, endInt);
Then replace the character at index[x] with ' '
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
How can I remove specific text from a string?
for example I have this string:
string file = "43 , 2000-12-12 003203";
I need to remove the text after the comma, including comma, so I can have as a final result:
string file = "43";
thank you,
string file = "43 , 2000-12-12 003203";
string number = file.Split(',')[0].Trim();
You can do this:
string output = file.Substring(0, file.IndexOf(',')).Trim();
However, that might fail if the string doesn't contain a comma. To be safer:
int index = file.IndexOf(',');
string output = index > 0 ? file.Substring(0, index).Trim() : file;
You can also use Split as others have suggested, but this overload would provide better performance, since it stops evaluating the string after the first comma is found:
string output = file.Split(new[] { ',' }, 2)[0].Trim();
Possibly by using Split?
file.Split(',')[0].Trim();
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I have a integer array list int[] marks = {2,3,4,5,4,5,6,2,9};
I want to get all duplicate in this list and the array shold traverse only once.
No inbuild function should use becasue every inbuild function also traverse for every serch.
Expected result should be 2,4,5
Well, as long as the qualification is that you can only traverse THIS list once, you could create two more lists, 1 non-duplicates and 1 duplicates.
Iterate through the input list, and then check if the non-duplicates has the int. If not, then add it, if so, then add it to the dups if it doesn't already contain it. In the end, the dups should have your requested result.
int[] marks = { 2, 3, 4, 5, 4, 5, 6, 2, 9 };
Dictionary<int, bool> flags = new Dictionary<int, bool>();
HashSet<int> result = new HashSet<int>();
for (int index = 0; index < marks.Length; index++)
{
if (flags.ContainsKey(marks[index])) result.Add(marks[index]);
else flags.Add(marks[index], true);
}
Follow this find Duplicates in linear time
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I need regular expression to retrieve no of doubles, triplets, tetras etc from a telephone number
following is the example,
number is 1001055522
it should return me
group Numbers
=============================
Doubles 00
22
Triplets 555
This regex when used with Regex.Matches will produce exact double or triple (not part of longer consecutive sequence). This is due to the greediness of the quantifier.
(\d)\1+
Demo
Well, the rest is to check the length of the string and count... I will leave it to you.
To find doubles, use a backreference:
(.)\1
Here's a demo: http://regex101.com/r/zC3fM1
To find triplets, just repeat the backreference:
(.)\1{2}
Here's a demo: http://regex101.com/r/cJ4lJ8
If you want to match all consecutive numbers regardless of how many there are, then use + on the backreference:
(.)\1+
Here's a demo: http://regex101.com/r/pL8sB3
Dim n = "1001055522"
Dim doubles = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1")
Dim triples = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1{2}")
'Doubles
For Each d As System.Text.RegularExpressions.Match In doubles
Console.WriteLine(d.Value)
Next
'Triples
For Each t As System.Text.RegularExpressions.Match In triples
Console.WriteLine(t.Value)
Next
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In this table I want to insert values in the weight column that is of float type with decimal
values 00 like 100.00, 150.00, 25.00 .
In this table if I am going to insert 25.5 then it will show decimal value bit if I am storing 135.00 it will store 135 only not showing decimal if 00.
I'm not sure from your question what you are actually looking for. Storage and Display are two separate things.
STORAGE
If you want to store a number with a fixed precision in SQL SERVER use DECIMAL
CREATE TABLE MyTable
(
MyNumber DECIMAL(5,2)
)
The above will store 5 digits, 3 before the decimal point, and two after.
Here's the documentation: http://msdn.microsoft.com/en-gb/library/ms187746.aspx
If you are storing currency values, then there is the MONEY datatype too: http://msdn.microsoft.com/en-us/library/ms179882.aspx
DISPLAY
If you are more interested in the display of values rather than the storage (You've already mentioned they are floats in the database) then in your C# application you can use something like this:
string display = string.Format("{0:0.00}", myNumber);
Here's the documentation for custom formatting of numbers into strings: http://msdn.microsoft.com/en-gb/library/0c899ak8.aspx
If it is of type float, then X.00f = Xf where X is any integral number.
Yes, it stores 135 or 135.00 - that is the same if it is a float, really.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am fresher and just gave my first interview on friday.
In a machine test they ask me to write a program to add first ten numbers i.e 1-10 without using any for loop. I tried it alot but can not find the solution. How can we write a program logic to get the sum of first 10 natural numbers without using for loop.
if they asked you not to use for loop , then you could have use while or do while.
There is another way to do it if you dont want to use any knd of loop.You can use the formula
1+2+3+.........+(n-1)+n=(n*(n+1))/2 .
you had to add first 10 numbers so you could have use it like
(10*(10+1))/2.
Console.WriteLine("{0}",(10*(10+1))/2);
You can make it more general by asking the user the value of n etc.I hope this will help you.
If I understand what you're saying correctly, you could just use simple mathematics.
x = firstnaturalnumber;
You want to have:
(x + 0) + (x + 1) + (x + 2) ... (x + 9)
Natural numbers are whole integers, which makes this mathematically sound. The final equation is:
sum = 10x + 45
The natural numbers are a special case of an arithmetic series. The sum of any arithmetic series can be computed with a simple formula, without requiring a loop.
S = (n / 2) * (a1 + an)
Personally, I think it would kind of unfair for an interviewer to expect you to just remember this formula off the top of your head. However, you probably would impress an interviewer a great deal by working through the series and figuring out the formula yourself!
if they specified not to use 'for' loop then there are other loops available such as do-while or while. Recursive function is also a good choice.
Actually, they have already specified the numbers to be added. So, instead of being a smart arse, use '+' to add them directly. Yeah I one it is dumb answer but it is an answer.
static void Main(string[] args) {
Console.WriteLine("{0}", SumRecursive(1,10));
}
static int SumRecursive(int min, int max) {
return _SumRecursive(min, max);
}
static int _SumRecursive(int min, int val) {
if (val == min)
return val;
return val + _SumRecursive(min, val - 1);
}
You can achieve it with a recursive method , and there is no for loop either
public int AddDown(int i)
{
return i += (i >= 1 ? AddDown(--i) : 0);
}
x= 1 + 2 + 3+.....+n-2 +n-1+n
x =n +n-1+ n-2+ ....3+2 +1 just reversing
sum up both sides
2x= (n+1) +(n+1) +(n+1)+ .....+(n+1) ///n times
2x= n(n+1)
x= n(n+1)/2
so in your case x= 10(10+1)/2 = 55