vendredi 1 avril 2016

if and/or printf doesn't work right. C

This is my code.

  void change_matrix(){
    scanf ("%s %s", p1, p2);
    int exists1, exists2, change1, change2;
    exists1 = verify(p1);
    exists2 = verify(p2);
    if (exists1 == -1 && exists2 == -1)
        printf("%s does not exist\n %s does not exist\n", p1, p2);
    else{ 
        if (exists1 == -1)
           printf("%s does not exist\n", p1);
         else {
            if (exists2 == -1)
                 printf(""%s does not exist\n", p2);        
            else {
                 change1 = matrix[exists1][exists2];
                 printf("change made (%s,%s)%d\n", p1, p2, change1);}}}}

The problem is that if both exists1 and exists2 don't exist it ignores the fact that exist1 equals -1 and just says that exists2 does not exist. Why is that?

Why I'm getting "maximum recursion depth exceeded"

I just saw an alternative construction for python's if-else statements like (0, 1)[ x > 5] and wanted to try it with recursion but for some reason it's not working. Just forget for a moment that it's unpythonic.

Here is an original code which I'm trying to replace with alternative:

def f(n):
    return 1 if n == 1 else n * f(n - 1)

Alternative, which give recursion problem:

def f(n):
    return (n * f(n - 1), 1)[n == 1]

What is the problem with alternative code?

Countif with multiple criterias in R

I would like to count all rows where two criterias are matched. My approach was:

a <- c(2,1,2,2,3,4,2,1,9)
b <- c(2,1,2,1,4,4,5,6,7)
c <- data.frame(a,b)
sum((c['a']==2) && (c['b']==2))

but for some reason this gives 1 instead of two. How could I count rows if multiple criterias are matched?

Use an 'apply' function to perfrom code with conditional statements in R

I have been working on a project for which I need to find peaks and valleys in a dataset (not just the highest numbers per column, but all of the peaks and valleys). I did manage to get it to work on 1 column, but I use a for-loop for that and I need to do this for about 50 columns, so I think I should use an 'apply' function. I just don't know how to do so. Can I put 'if' statements and such in an 'apply' function?

Here is what I used for checking one column: ('First' is the name of the dataset and 'Seq1' is the first column)

Lowest = 0
Highest = 0
Summits = vector('numeric')
Valleys = vector('numeric')

for (i in 1:length(First$Seq1))
{
    if (!is.na(First$Seq1[i+1]))
    {
        if (First$Seq1[i] < Lowest) {Lowest = First$Seq1[i]}
        if (First$Seq1[i] > Highest) {Highest = First$Seq1[i]}  

        if (First$Seq1[i] > 0 && First$Seq1[i+1] < 0) 
        { Summits <- append(Summits, Highest, after=length(Summits)) }
        if (First$Seq1[i] < 0 && First$Seq1[i+1] > 0) 
        { Valleys <- append(Valleys, Lowest, after=length(Summits)) }
    }
}

if(x=y){...} else{...} takes else path

I know it's a basic and trivial questions but when coding in C, why doesn't it go into the second if-statement? I know there's only one equal sign, so this should set x = 0 and then go into the code? Is it because the compiler understands x = 0 as False so it doesn't enter the chunk? And why does it execute the else statement? Thank you!

#include <stdio.h>

void main()
{
    int x=1, y=0;

    if (x >= y){
       x = 0;

    if (x = y)
       x = x + 1;

    else{
       x = 4;
       printf(" %d",x);
    }
}

Meteor template each loop, provide other template each X results

Is it possible to insert specific data when meteor parse a collection with {{#each}} template logic only each X collection entry?

Exemple:

HTML
    {{#each collection}}
                 <div>
                    <p>{{collectionparam1}} - {{collectionparam2}}</p>
                    {{#if **collection[only multiple of 2]**}}
                       <p>I show this {{collectionparam3}} only each 2 entries</p>
                    {{/if}}
                 </div>      
    {{/each}}

Results:

Billy - 24
Joe - 12
I show this banana only each 2 entries
Bob - 88
Maria - 5
I show this cherry only each 2 entries
Samantha - 102
Henry - 43
I show this apple only each 2 entries

If possible, what do I have to put on {{#if}} logic?

Thanks for your help.

Optimize PHP If Statement Logic

I would like to optimize this:

if (isset($value->videoURL) && empty($value->videoURL)) {
  unset($value->videoURL);
}
if (isset($value->videoID) && empty($value->videoID)) {
  unset($value->videoID);
}

In a single PHP if logic

if ((isset($value->videoID) && empty($value->videoID)) || isset($value->writer) && empty($value->writer)) {
  unset($value->videoID);
  unset($value->writer);
}

I know this is totally incorrect, hence I'm looking for some suggestions.