I feel kinda retarded

I been going through my codewars account and completing the problems i skipped one of them was called "Are they the same?" You have a function that takes two inputs a1 and a2, the job you need to do is see if a1 inputs to the power of 2 are the same as a2 inputs <which are just a1 units to the power of two>.


It took around an hour just to get by the test on the problem and when i hit the attempt button, I realise that codewars hates jesus.To my dismay a realise that the attempt button generated a input which is bigger than the other.I spent the whole day just raging at the computer as i try and figure the solution.I finally did and tested it on my machine which worked perfectly fine.


But in codewars it outputs a bool and the code in my computer just Print false or true.So i spent a good 10 minutes trying to figure out the problem and the solution was so simple 

i had to add one if statement and boom everything was good  .


Ounce i look at other peoples solutions i see about 5 lines of code at minimum and 20 at max, Mine how ever has about a little over 60 LINES!!!


I wrote the problem in rust and the other solutions were totally dependent on rust built in macros i think is what they are called. This is one of the other solutions



____________________________________________________________________________________

use itertools::Itertools;


fn comp(a: Vec<i64>, b: Vec<i64>) -> bool {

    println!("a: {:?}, b: {:?}", a, b);

    let a: Vec<_> = a

        .into_iter()

        .map(|i| i.pow(2))

        .sorted()

        .enumerate()

        .collect();

    b.into_iter().sorted().enumerate().all(|i| a.contains(&i))

}

______________________________________________________________________________



I didnt know you could use external libraries but even if i did i would prob not have think of it 


Well thats all from me heres my solution 


_________________________________________________________________________________

fn comp(a: Vec<i64>, b: Vec<i64>) -> bool {

    //copy vectors

    let mut newa: Vec<i64> = vec![]; // storea

    let mut newb: Vec<i64> = vec![]; //store b

    

    //pushes b value into newb

    for value in b {

        newb.push(value);

    }

    

    //pushes a values into newa 

    for value in a {

        let mut value2 = value * value;

        newa.push(value2);

    }

    newa.sort();

    newb.sort();

    

    let mut bo: Vec<i32> = vec![]; // store the trues

    let mut bo2: Vec<i32> = vec![]; // store the falses

    

    if newa.len() > newb.len() {

        /*

        The sum variable takes the amount of numbers

        That newb wikk need to equal newa and pushes

        random numbers until newb is the same size as newa

        I do this so i can compare the to vectors in a series 

        of for loops

        */

        let sum = newa.len() - newb.len();

        //For loop to make newb.len == newa.len

        for i in 0..sum {

            newb.push(i.try_into().unwrap());

        }

        for i in 0..newa.len() {

            if newa[i] == newb[i] {

                bo.push(1);

            } if newa[i] != newb[i] {

                bo2.push(0);

            }

        }

        

    } else if newb.len() > newa.len() {

        let sum = newb.len() - newa.len();

        for i in 0..sum {

            newa.push(i.try_into().unwrap());

        }

        for i in 0..newb.len() {

            if newa[i] == newb[i] {

                bo.push(1);

            } if newa[i] != newb[i] {

                bo2.push(0);

            } 

        }

    } else if newa.len() == newb.len() {

        for i in 0..newa.len() {

            if newa[i] == newb[i] {

                bo.push(1);

            }if newa[i] != newb[i] {

                bo2.push(0);

            }

        }

    }

    

   

    if bo2.len() > 0 {

        return false

    } else {

        return true

    }


}

_________________________________________________________________


0 Kudos

Comments

Displaying 0 of 0 comments ( View all | Add Comment )