This question already has answers here:
How to stop the execution of a method after a specific time?
(2 answers)
Run and stop a method for a minute
(6 answers)
Closed 2 years ago.
I wanted to know if there was any way I could cause a method to "time-out" or stop executing if nothing is returned. For example, in the code provided below, I'm passing a BigInteger through a method that determines if an integer is probable prime or not. When I call the method, I am using an array of Big Integers to test the method several times.
class PrimeChecker {
public static bool isPrime(BigInteger num) {
if (num <= 1)
{
return false;
}
if (num == 2)
{
return true;
}
for (int i = 3; i <= num / 2; ++i)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
}
However I am running in to an issue where some instances of num, such as:
35201546659608842026088328007565866231962578784643756647773109869245232364730066609837018108561065242031153677 (is Prime)
are stuck calculating in what feels like forever. Is there a way, either in my main or in the class itself, that I can stop this function after a certain period of time and move on to the next Big Integer in the array.
*I understand that the class itself may not be perfect and is most likely what is causing this test to be looping forever. However, I just wanted to see if there was any way to stop the function if it is calculating for x seconds/minutes.
Related
This question already has answers here:
Captured variable in a loop in C#
(10 answers)
Closed 2 years ago.
I need to run tasks in parallel and transfer about 3-5 parameters to them, but now I transfer 2 parameters to the task, and as a result, I always see the value 100 in the console.
Tell me what am I doing wrong? and how to correctly pass parameters to the task?
class Program
{
static void Main(string[] args)
{
/// Init
string file_name = "unknown.dat";
Action<string, int> action = (msg, count) => Load(msg, count);
/// For
for (int i = 0; i < 100; i++)
{
Task.Factory.StartNew(() => action(file_name, i));
}
/// End
Console.Read();
}
public static void Load(string aFileName, int aCount)
{
Console.WriteLine("Index: {0} ", aCount);
}
}
This is a "captured variable" problem; try instead:
for (int i = 0; i < 100; i++)
{
var copy = i;
Task.Factory.StartNew(() => action(file_name, copy));
}
The fundamental problem here is that your action is capturing the variable i, not the value of i at a particular time. So what happens is: your loop finishes really quickly (before the thread-pool has even got its shoes on), and i ends up at 100. At some indeterminate time the thread-pool starts processing your work items, and the i is sat at 100 for all of them. Note: it is technically possible to get earlier numbers, but that is ultimately a massive race condition.
The fix here moves the declaration of the captured variable to inside the loop; the declaration of a variable defines the scope for the purposes of captured variables, so now each copy is independent of the others.
This question already has answers here:
What is the yield keyword used for in C#?
(19 answers)
Closed 4 years ago.
I have the code below:
static IEnumerable<int> YieldReturn()
{
yield return 1;
yield return 2;
yield return 3;
}
static void Main(string[] args)
{
// Lets see how yield return works
foreach (int i in YieldReturn())
{
Console.WriteLine(i);
}
}
I have a couple of questions:
1-How many times does YieldReturn() get called? one or three times?
2-If YieldReturn() get called three times, how does IEnumerable store value 1, 2 and 3?
yield is used for lazy evaluation - it is only executed in enumeration. Your method YieldReturn() will be called once and then be enumerated by your foreach loop, providing the values in the order in which you yielded them.
Note that this example gains nothing from yield - it works best when you can defer execution of something expensive to an eventual enumeration. To immediately enumerate on the method call gains nothing for you.
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 am wondering can someone please tell me where I am going wrong with this FizzBuzz.
I get an error "not all code returns a value and struggling to find out exactly how to fix it.
My code is below:
for (int i = 0; i < input; i++)
{
if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else
{
Console.WriteLine(i);
}
}
And the test case is this:
[Test]
public void Test1()
{
var solution = new Solution();
Assert.AreEqual(solution.PrintFizzBuzz(15), "FizzBuzz");
}
First of like other people have said, not all the code is present so it's difficult for us to help. But I'll try anyway.
When testing with Assert.AreEqual the function PrintFizzBuzz should return something, in this case the string "FizzBuzz". But if you are using the Console.WriteLine method it will return nothing. The Console.Writeline method will print the string to the console, but the console is not actually the 'program', it's just for visualising some things like logging or debugging.
So to fix your issue you should use return instead of Console.Writeline and let your method PrintFizzBuzz return a string instead of voiding and printing it to the console. It's probably also better to rename your method in this case, because it doesn't print FizzBuzz anymore.
Your code has also another issue and that's when you input 15 it will print out "Fizz", because the check you do is modulo 3 and 15 % 3 = 0. You should order your check the otherway around, from specific to less specific.
An example for returning a string would be:
string SomeMethod()
{
return "Some String"
}
not all code returns a value
Means your method declared having a return type and not void but you are not returning anything from your method. One example case:
int Add(int a, int b)
{
int c = a + b;
// no return specified would through the same error
}
This question already has answers here:
Is there an elegant way to repeat an action?
(11 answers)
Closed 5 years ago.
A common pattern in coding is to use a iteration variable to do something certain number of times. In many cases, the iteration variable is only used as loop termination condition but adds several lines of code for the purpose.
Example,
int counter=100;
while ( counter > 0)
{
//Do something
counter--;
}
Does the .NET framework provide a way to define a coding block (Do something in example) and then execute it a given number of times, thereby removing the condition checking and decrementing statements above. This is to reduce the number of visible statements.
Edit: I am looking for way to reduce the number of codes, not make the underlying call efficient.
whats wrong with
for (int counter = 0; counter < limit; counter++)
{
//Do something
}
its an instantly recognizable idiom
There is no option in .NET Framework to do something N times without keeping track of number of times you already did something.
But you can write your own extension which will hide all this stuff. E.g.
public static void Times(this int count, Action action)
{
if (count < 0)
throw new ArgumentException(nameof(count));
for(int i = 0; i < count; i++)
action();
}
Usage:
100.Times(() => Console.WriteLine("hello"))
Note that if you'll extract the code which you want to repeat into method which matches Action signature, then the code above becomes cleaner:
100.Times(DoSomething);
You could use the Enumerable.Repeat to do the trick, not sure if it's really what you thought of elegance
Edit :
Little example how you can generate 100 different random values with this method. With a little adaptation it could suit your need
var ran = new Random();
var results = Enumerable.Repeat<Func<int>>(ran.Next, 100)
.Select(f => f())
.ToArray();
Edit:
I thought you could make it on your own, but some people need clarifications :
Enumerable.Repeat(100, () =>
{
// Do what you want
});
Source (for more details):
http://geekswithblogs.net/BlackRabbitCoder/archive/2012/05/03/c.net-little-wonders-the-enumerable.repeat-static-method.aspx
This question already has answers here:
Are ILists passed by value?
(6 answers)
Closed 6 years ago.
I am trying to write a function for creating a prime factors list. For this - I am using a recursive function.
Here are the calls:
private int Problem003()
{
//The prime factors of 13195 are 5, 7, 13 and 29.
//What is the largest prime factor of the number 600851475143 ?
return GeneratePrimeFactorsList(15).Last();
}
private List<int> GeneratePrimeFactorsList(int n)
{
List<int> _primefactors = new List<int>();
_primefactors.Add(2);
int i = 3;
while(i <= n)
{
if (CheckIfIntIsPrime(_primefactors, i))
{
_primefactors.Add(i);
}
i=i+2;
}
return _primefactors;
}
private bool CheckIfIntIsPrime(List<int> _primefactors, int i)
{
if (_primefactors.Count() == 0)
{
return true;
}
else
{
if(i % _primefactors.First() != 0)
{
_primefactors.Remove(_primefactors.First());
return CheckIfIntIsPrime(_primefactors, i);
}
else
{
return false;
}
}
}
The problem is that, when I am calling for CheckIfIntIsPrime(List, i), which have bool return type - it modifies the List. That means, that after the check - the passed argument into the GeneratePrimeFactorsList(int) is getting empty after each while loop iteration.
The CheckIfPrime function works correctly, but modifies the passed argument, when it should not - I dont relate them.
Very strange case, but I feel, that I missing knowledge about some of the List properties.
An object is a reference type. All variables that are typed as a reference type don't contain the value itself; they each contain a reference or pointer to the value (or object). This is true even when the variable is passed as a parameter.
The function that receives the object parameter is not capable of modifying the variable itself (which, again, is just a pointer). This means it cannot cause the variable to point somewhere else. However, it can use the pointer to get a reference to the object and modify the object itself.
If you want to pass an object but want to make absolutely sure it doesn't get modified, you can pass a clone of it.