c# - codehunt 1.06 code puzzle has too many contingencies -


even though i'm @ beginning of game every level kindergarten easy, found myself stumped 1 problem. easy solve can't figure out how 3 points. it's obvious solution quite ugly problem quite ugly , can't think of more elegant way solve it.

the function supposed come expected results predefined list of inputs. there doesn't seam pattern had wright 7 different return statements cover scenarios. can figure out elegant solution problem?

here results, listing each of expected results each input:

x       expected result     result  success     29      0       0            success     4       1       1            success     -2      -2      -2           success     3       1       1            success     -4      -1      -1           success     2       2       2            success     -95     0       0            success     -1      -4      -4           success     1       4       4    

here's ugly code wrote solution:

using system; public class program {     public static int puzzle(int x) {         if (x==1) return 4;         if (x==-1) return -4;         if (x<-4 || x>4) return 0;         if (x<-2) return -1;         if (x<0) return x;         if (x>3) return 1;         return 4-x;     } } 

ignore this. way obvious guess reason decided try easy , worked found solution.

return 4/x; 

seriously. easy.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -