NEWTON

NEWTON


Popular tags

    How to make conditional expressions if..then..else in Cairo lang?

    Asked

    4 months ago

    146

    views


    1

    Hello! This is day 3 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Conditionals”. Perhaps you can help me?

    // The function `abs_eq` should return 1 if its inputs // are equal or differ only in sign and 0 otherwise. // Its current implementation is incorrect. // // 1. Click on "Run" to see where it fails. // 2. Fix the body of `abs_eq`, so that the // program terminates successfully. // Returns 1 if |x| = |y| (that is, x = y or x = -y), // 0 otherwise. func abs_eq(x: felt, y: felt) -> (bit: felt) { if (x == -y) { return (bit=1); } else { return (bit=0); } } func main() { let (bit) = abs_eq(2, -2); assert bit = 1; let (bit) = abs_eq(2, 3); assert bit = 0; let (bit) = abs_eq(2, 2); assert bit = 1; return (); }

    Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton.

    Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    4 months ago


    10 answers

    3

    Accepted answer

    One of the ways to implement the required function would be to add a check on the situation where x == y. You could also remove the else which is redundant and just return bit = 0 if your function reaches the end of execution. You would get something like this:

    func abs_eq(x: felt, y: felt) -> (bit: felt) {
        if (x == y) {
            return(bit=1);
        }
        if (x == -y) {
            return (bit=1);
        }
        return(bit=0);
    }
    

    answered

    4 months ago

    0

    You need a forest...

    ...of IFs. Yes Cairo doesn't support logic operators. (like ||)

    So you need to add:

    if (x == y) {
        return (bit=1);
    }
    

    victorforissier.eth

    answered

    3 months ago

    0

    Unfortunately seems like Cairo doesn't support logical operators (|| , &&, !) so something like this is not possible:

    if (x == -y || x == y) {
        return (bit=1);
    }
    

    So the solution is to use multiple if-s (or one if and use abs_value as other users pointed out)

    func abs_eq(x: felt, y: felt) -> (bit: felt) {
        if (x == -y) {
            return (bit=1);
        }
        if (x == y) {
            return (bit=1);
        } else { // else can be omitted, but if it's not, it must be in the same line as {
            return (bit=0); 
        }
    }
    

    1

    I wondered that such a basic function as abs should exist on cairo. Therefore, I went through the cairo-lang repository and found the function I wanted.

    However, I had a problem with the implementation of the abs function. But thanks to the help from the cairo community I was able to solve it. Here is the error

    My answer for the day 3

    %builtins range_check
    
    from starkware.cairo.common.math import abs_value
    
    func abs_eq{range_check_ptr}(x: felt, y: felt) -> (bit: felt) {
        if (abs_value(x) == abs_value(y)) {
            return (bit=1);
        } else {
            return (bit=0);
        }
    }
    
    func main{range_check_ptr}() {
        let (bit) = abs_eq(2, -2);
        assert bit = 1;
    
        let (bit) = abs_eq(2, 3);
        assert bit = 0;
    
        let (bit) = abs_eq(2, 2);
        assert bit = 1;
    
        return ();
    }
    

    Repository challenge

    0

    Problem

    // The function `abs_eq` should return 1 if its inputs
    // are equal or differ only in sign and 0 otherwise.
    // Its current implementation is incorrect.
    // Returns 1 if |x| = |y| (that is, x = y or x = -y),
    // 0 otherwise.
    func abs_eq(x: felt, y: felt) -> (bit: felt) {
        if (x == -y) { 
            return (bit=1);
        } else {
            return (bit=0);
        }
    }
    
    func main() {
        let (bit) = abs_eq(2, -2);
        assert bit = 1;
    
        let (bit) = abs_eq(2, 3);
        assert bit = 0;
    
        let (bit) = abs_eq(2, 2);
        assert bit = 1;
    
        return ();
    }
    

    Solution

    func abs_eq(x: felt, y: felt) -> (bit: felt) {
        if (x == y) { //2===2 or -2 ==. -2 
            return (bit=1);
        }
        if (x == -y) { // 2 == -(-2) or -2 == -2 
            return (bit=1);
        } 
    return (bit=0);
    }
    func main() {
        let (bit) = abs_eq(2, -2);
        assert bit = 1;
    
         let (bit) = abs_eq(2, 2);
        assert bit = 1;
        
         let (bit) = abs_eq(-2, 2);
        assert bit = 1;
        
         let (bit) = abs_eq(-2, -2);
        assert bit = 1;
    
        let (bit) = abs_eq(2, 3);
        assert bit = 0;
        
        return ();
    }
    

    Explanation

    abs_eq function accepts two arguments x and y, if x and y equal in magnitude or differ only in sign it will return 1 otherwise 0.

    First if statement checks the both magnitude and signs should be equal to return true that is (2,2), (-2,-2)

    Second if statement checks the both magnitude should be same but return true only if signs are different that is (-2,2) , (2,-2)

    main function uses assert to verifies the computation of all the test cases possible for the conditions.

    0

    // The function abs_eq should return 1 if its inputs // are equal or differ only in sign and 0 otherwise. // Its current implementation is incorrect. // // 1. Click on "Run" to see the where it fails. // 2. Fix the body of abs_eq, so that the // program terminates successfully.

    // Returns 1 if |x| = |y| (that is, x = y or x = -y), // 0 otherwise. func abs_eq(x: felt, y: felt) -> (bit: felt) { if( x == y){ return (bit=1); } if (x == -y) { return (bit=1); } else { return (bit=0); } }

    func main() { let (bit) = abs_eq(2, -2); assert bit = 1;

    let (bit) = abs_eq(2, 3);
    assert bit = 0;
    
    let (bit) = abs_eq(2, 2);
    assert bit = 1;
    
    return ();
    

    }

    answered

    4 months ago

    1

    func abs_eq(x: felt, y: felt) -> (bit: felt) {
        //return 1 if x = -y
        if (x == -y) {
            return (bit=1); 
        }
    
        //return 1 if x = y
        if (x == y) {
            return (bit=1);
        }  
    
        //otherwise  
        return (bit=0);
    }
    

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON