dimanche 28 juin 2015

multiple acceptable answers for an if statement on python 3 [duplicate]

This question already has an answer here:

run_program = (1)

while run_program==1:

 ###bulk of program goes here
user_answer = input('Do you have another item you would like to check')#request the answer of whether a user wishes to re run program

    if user_answer == 'yes' :#currently only working with the typed word yes 
        run_program = (1)# when statement is correct  run program continues to = 1 so the program continues to run

else:
        run_program =(2) # run program no longer =1 so program ends

How do I make the if statement recognise multiple answers as acceptable?

So yes is as acceptable as y or yeah or any other user input I list. I assumed it would be something like if user_answer == ['yes','yeah','y']: but it does not seem to work.

Multiple if statements in a foreach loop

I have 3 reviews in my database but somehow it only shows 1 of the 3 star ratings as checked and the other 2 reviews star ratings are empty

@foreach($recensies as $recensie)

                <fieldset class="rating">
                    <input type="radio" disabled  @if($recensie->rating == 5) checked="checked" @endif name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '4 and a half') checked="checked" @endif name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 4) checked="checked" @endif name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '3 and a half') checked="checked" @endif name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 3) checked="checked" @endif name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '2 and a half') checked="checked" @endif name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 2) checked="checked" @endif name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == '1 and a half') checked="checked" @endif name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
                    <input type="radio" disabled  @if($recensie->rating == 1) checked="checked" @endif name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
                    <input type="radio" disabled  @if($recensie->rating == 'half') checked="checked" @endif name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
                </fieldset>

@endforeach

Here is a picture to show you what I mean.

enter image description here

Why is the method, I think of to be the quicker one, the slower one?

I've currently got two methods for checking if a number is prime or not and another method to calculate the time both need.

IsPrime1:

bool IsPrime1(int i)
{
    if (i == 2 || i == 3 || i == 5 || i == 7) return true;
    return i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0 && i % 9 != 0;
}

IsPrime2:

bool IsPrime2(int i)
{
    if (i == 2 || i == 3 || i == 5 || i == 7) return true;
    if (i % 2 == 0) return false;
    if (i % 3 == 0) return false;
    if (i % 5 == 0) return false;
    if (i % 7 == 0) return false;
    return i % 9 != 0;
}

CheckForTicks:

string CheckForTicks(int ticks)
{
    var sw1 = Stopwatch.StartNew();
    for (var g = 0; g < ticks; g++)
    {
        var b = IsPrime1(g);
    }
    sw1.Stop();

    var sw2 = Stopwatch.StartNew();
    for (var g = 0; g < ticks; g++)
    {
        var b = IsPrime2(g);
    }
    sw2.Stop();

    return $"{ticks} ticks: IsPrime1: {sw1.ElapsedMilliseconds} ms / IsPrime2: {sw2.ElapsedMilliseconds} ms";
    //equal to the following:
    //return string.Format("{0} ticks: IsPrim1e: {1} ms / IsPrime2: {2} ms", ticks, sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds);
}

Results:

| CheckForTicks | IsPrime1 (in ms) | IsPrime2 (in ms) |
|---------------|------------------|------------------|
|        100000 |                3 |                4 |
|        500000 |               18 |               21 |
|       1000000 |               37 |               45 |
|       5000000 |              221 |              242 |
|      10000000 |              402 |              499 |
|      50000000 |             2212 |             2320 |
|     100000000 |             4377 |             4676 |
|     500000000 |            22125 |            23786 |

What I wonder is, why IsPrime2 is even slightly slower than IsPrime1.
From my point of view IsPrime2 should be much quicker as IsPrime1 because it only has to check once before the first possible return and IsPrime1 checks all possibilities.
Is there something I don't know about or is this related to .NET?

I'd be very appreciated if somebody can explain the cause of this to me.

Thanks in advance!

PS: I'm using Visual Studio 2015 RC and .NET 4.6.

android Strings and if statement

Im sure this is really simple but it bugging the hell out of me

I use the following code

    String name = Global.PicName2;
    String tempstr = name.substring(0, 3).trim();
    Toast.makeText(getBaseContext(), tempstr, Toast.LENGTH_LONG).show();


    if (tempstr == "Sou"){Toast.makeText(getBaseContext(), "Yes", Toast.LENGTH_LONG).show();}

Now the first Toast reports that tempstr is "Sou" I have checked the lenght of the string and it is 3 characters long I have trimmed any spaces

Yet it will not go through the if statement and toast yes

If I add the line

    tempstr="Sou";

after the first toast it goes through the if statement so that says its the tempstr that is wrong but I cant work out why

Its driving me nuts any ideas?

Any help appreciated

Mark

JS - If condition with only 1 else, does checking order affect performance?

I was working on a project and this problem popped into my head. I am not sure if there is already a post like this but I have found none.

Let say if I have this:

function checks(s) {
    if (s.length == 4 && s[0] == "a") {
        //action
    } else {
        //other action
    }
}
checks("a001");
checks("else");
checks("else");
checks("else");

and this:

function checkb(b) {
    if (b) {
        //action
    } else {
        //other action
    }
}
checkb(true);
checkb(false);
checkb(false);
checkb(false);

Since either way the if-statement is going to have to check once of the conditions, if the frequency of the actions are known (e.g. I know that the else part is performed most often). Does the checking for "not conditions" make the code run faster or does the checking of "not condition" don't even affect the performance?

Addition question: Do most programming languages do the same too?

samedi 27 juin 2015

condition based on a timer

I was thinking about using a Timer class to do this but I dont really know how to implement this. Here is what I need

Timer timer = new Timer();
While(true){
    if (condition1){
       do something
       timer.reset (set the timer back to 0)
       }
    if (timer >= 20 seconds){
       do something
       (something will happen here that will make condition1 to be true)
       }
     else {
        start the timer 
        }
}

How do I go about implementing something like this? It doesn't necessarily have to be using a Timer i guess.

php with not empty, if, else outputs both

I am running into a confusing problem with my php code edits within Wordpress. I am using a module which allows the the use of custom fields, called by the_field(custom_field).

My code is as follows:

<?php
$relatedlink = the_field('related_link');
if (!empty($lexilelink)) {
?>
    Related Link: <a href="<?php the_field('related_link'); ?>" target="_blank"><?php the_field('related_link_name'); ?></a> 
<?php
} else { echo "No related links";}  ?>

The field "related link" is populated. What ends up happening is it outputs both the if and the else. (I've tried using isset instead of !empty).