Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) - Programming (2) - Nairaland
Nairaland Forum › Science/Technology › Programming › Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) (5884 Views)
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by jacob05(m): 7:07am On Jun 24, 2013 |
@authurowen what is my implementation not doing? Or should have done? |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by kambo(m): 7:35am On Jun 24, 2013*. Modified: 8:45am On Jun 24, 2013 |
authurowen: Good try. I took a quick glance at your code, didn't test it out but It can BETTER be refactored and Improved.You'll have to be more explicit when you say "refactored and improved". The abstract class ? not needed? guy u serious. This is the S.E way of writing this? it's extensible and maintainable. The unnecessaries ? okay. i'll admit 2 methods were overkill , but not a "lot of unnecessaries as you say". the methods are - allset() - not necessary . i cant see any other that was un necessary. anyway imagine this scenario. List<matrixPrinters> list_of_printers ; printMatrices() // addPrinter(matrixPrinter mp) //adds a matrix printer to a class. addPrinter(spiralPrinter sp); addPrinter(columnPrinter mp); addPrinter(skipPrinter sp); // printMatrices() //calls all the matrice printers... or this: Create a class that prints a polygonal object in a particular way. you could go short and write: class polygon{ public void print(){} } later on you can add more codes to it. or you can go long hand: abstract class Shape{ public abstract void draw(); } abstract class Polygon extends Shape{} or more completely. abstract class Polygon extends Shape{} class Rect extends Polygon{} class Rhombos extends polygon, class trapezoid extends polygon etc... One is more professional and maintainable than the faster hacks. without sufficient polymorphism - read: root class u'll have chaos.!! Anyway-- i expect a more thorough critique. ".. give me the position of the array index to avoid getting an index out of bound exception " This wont happen. The task is to print the array in spiral form - which is what the method does. for explanation of what is going on i left comments in the printer method indicating what printing is taking place. "printing right most column," "printing bottom row " etc. If the matrix isnt a square one - the printing function would necessarily be more complex than if it wasnt. Thats why i state the implied assumption that a square matrix is assumed. On the s.E aspect , at the risk of appearing defensive, the norm is maintainability at the cost of a lil efficiency. modularity. meaningfulness at the cost of a lil verbosity. see java for example , System.out.println() against cout<< cuz with one, there's lil need for re-explanation. donald knuth - "software should be written to be read by humans and in some rare cases to be executed by a computer .. " (paraphrase). Anyway, compile the code and see if it does what u want. i tested it and it worked. Thanks for the exercise anyway. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 7:50am On Jun 24, 2013*. Modified: 8:35am On Jun 24, 2013 |
authurowen: Brother, the solution is not as complicated as you have made it out to be. There is no need for the Mathematical / logical Gibberish (for lack of better words) you've just written.No bro... its NOT about software engineering... My python solution is not an Optimal solution in terms of speed... but I made it expose simple grid transversing to anyone that cares to understand it!! ....and I thought its so that we could all learn something? Nonetheless, HERE is my OPTIMAL CODE in C++: Its currently the FASTEST implementation I reasoned... ---this code is what I presume you may call 'software engineering'?.
I would really love to see yours in shorter lines... so that I can learn. I wouldn't bother explaining this snippet since my Python code was discarded.... BUT it works, if anyone wants, I could upload a working program.... cheers.... |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by danmc: 9:21am On Jun 24, 2013*. Modified: 9:48am On Jun 24, 2013 |
WhiZTiM: HERE is my OPTIMAL CODE in C++: Its currently the FASTEST implementation I reasoned...man, u can't just copy and paste some portion of ur code and tell us to feed it with blah blah blah. it compiled fine but couldn't run it. crash!!! i beg check ur algo well and pls show us a working proggie. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 2:27pm On Jun 24, 2013 |
danmc: man, u can't just copy and paste some portion of ur code and tell us to feed it with blah blah blah. it compiled fine but couldn't run it. crash!!! i beg check ur algo well and pls show us a working proggie.Hmmn...(I wrote that snippet from my mobile) Ohhkkkay pal... I'll post a working program... |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 2:32pm On Jun 24, 2013*. Modified: 5:56pm On Jun 24, 2013 |
Here is a closely coupled working program that solves authurowens problem. Its only 30lines;... I could explain the code and make the it more readable on anybody's request... so ask if you wanna understand ....NB: I made slight modification to the initially rough snippet I wrote from my mobile phone... #EDIT... k value overshoot...FIXED
USage: all inputs are via stdin so that input values can be entered manually or by piping a file into the program.... **Input must be guaranteed to be a square matrix! **my code automatically determines the size of the grid from the first line. - - - My algorithm here is pretty simple. - - Also, try to understand my Python code, you may learn a few lazy-man's tricks... |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by danmc: 5:22pm On Jun 24, 2013 |
WhiZTiM: Here is a closely coupled working program that solves authurowens problem.tanx boss for the proggie but it still isn't perfect yet. try inputting a 3x3 matrix e.g. "1 2 3", "4 5 6" and "7 8 9". the output misses the last item in the spiral i.e. "5"! pls fix it ![]() |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by jacob05(m): 5:47pm On Jun 24, 2013 |
:/ @danmc jacob05: @authurowen what is my implementation not doing? Or should have done? |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 5:47pm On Jun 24, 2013 |
danmc: tanx boss for the proggie but it still isn't perfect yet. try inputting a 3x3 matrix e.g. "1 2 3", "4 5 6" and "7 8 9". the output misses the last item in the spiral i.e. "5"! pls fix it. . .lol!... Thanks man. You are following very well sir!.... Err, my bros... If you cared to check out the logical part, you would have noticed that there was an overshoot with 'k'... THE FIX IS SIMPLE: REMOVE THE LAST "k++;" that is at the very end of the line in the last for loop... |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 6:29pm On Jun 24, 2013 |
@danmc. I have removed the last k++;... So try that code again... The algorithm can be implemented recursively to further beat down the SLoC by a meagre factor... @authurowen... I don't really believe in writing programs alone and calling myself a software engineer. . . . I inherently mean, I wouldnt call myself a Software Engineer. But rather a Software Engineering hobbyist.... |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by danmc: 9:20pm On Jun 24, 2013 |
WhiZTiM: The algorithm can be implemented recursively to further beat down the SLoC by a meagre factor...i removed it like u said and now it works well. just to quote linus torvalds: "talk is cheap. show me the code", pls implement what u just said. i'd love to see it. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 10:34pm On Jun 24, 2013 |
danmc: i removed it like u said and now it works well. just to quote linus torvalds: "talk is cheap. show me the code", pls implement what u just said. i'd love to see it.like seriously bro??!! You are trying to put me to a show using Torvald's qoute... ...not cool. Nope. I would NOT implement it.... Nonetheless, the version I did, has 4 for loops to transverse in 4 directions. My thought to the recursive version is to have a function with several control args, transverse in only 2 directions or one preferential direction... While subsequent calls recursively controls the rest of the direction... C'mon, not everything will be given to you... think!! Haba Oga danmc At The Top!... Though... As for me... I have a knack for Robotics and Control Systems... |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by kambo(m): 2:07am On Jun 25, 2013 |
Anoda passve poster in d programmg room. He shud ,provde his own implementation, justifyg his design choices, for all to see and possibly learn from not just vanish into thin air after postg a Q. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by jacob05(m): 2:44pm On Jun 25, 2013 |
kambo: Anoda passve poster in d programmg room.Seconded ![]() |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by jacob05(m): 2:51pm On Jun 25, 2013 |
But I'm greatly concerned about what is wrong with my implementation.(PHP) ![]() |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 4:07pm On Jun 25, 2013 |
kambo: Anoda passve poster in d programmg room.already seconded, thus, I am Third in line! |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by authurowen(op): 4:52pm On Jun 25, 2013 |
WhiZTiM: already seconded, thus, I amSorry guys, I went quiet for a while (Got caught up). I will provide my own implementation so you all can criticize as much as you want (we're all here to learn). I'll give one more day for others to post their solution and then post mine. Mine should be up by Wednesday. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by authurowen(op): 6:50pm On Jun 26, 2013 |
Find below, my Implementation to the print spiral Question written in JAVA (There is very little difference btween C# and Java so C# Developers will be able to relate to it). Pls Criticize to the very lowest details as much as you please.
Test Code Fragments written in Java
Test Output:
|
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by authurowen(op): 6:51pm On Jun 26, 2013 |
Be on the look out for the second part of the programming questions. I will open another thread for it. Thanks for participating |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by jacob05(m): 7:10pm On Jun 26, 2013 |
@authurowen and how is your algo different from mine . ![]() |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by authurowen(op): 9:21pm On Jun 26, 2013 |
jacob05: @authurowen and how is your algo different from mineJacob, you are a developer. Can you take a closer look at your implementation and mine and see the difference? Also try parsing the other test case i provided in my solution to your own solution and see what it returns. secondly, Using your algorithm (which is wrong) after you're done printing the array, Your Master Array It will become empty (at a quick glance) |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 2:38am On Jun 27, 2013 |
authurowen: Find below, my Implementation to the print spiral Question written in JAVA (There is very little difference btween C# and Java so C# Developers will be able to relate to it). Pls Criticize to the very lowest details as much as you please.Its TOTALLY not my pleasure to criticize your code... I hate critics and I hate to criticize... I am going to criticize you from an Engineering POV.. on the fact that you used the Phrase in your original Post. N.B: Be Genuine in your solutions, Real S.E(s) are.That phrase annoyed me so much. . . Like I said, I don't call myself a Software Engineer... I rather consider myself a Software Engineering hobbyist... ANYWAY... my small review NB: Your code is ACCEPTABLE for this exact problem in a very localized manner Also NOTE: I am Not a Java Programmer 1 I thought I was a Sappy Programmer when it comes to OOP skills and patterns until I saw what you called OOP.... I am No Java Programmer but IMHO, Your code is NOT close to being truly OOP::: You used Objects... why not have the algorithm generic for a Matrix of objects... in a generalist perspective... having all this in One object is a flawed design! Advice:: SOMETIMES, write bloated/insane codes to learn something against tomorrow... but write tight, solid, highly efficient(and probably localized) codes for production; "This code is acceptable for tight conditions like time constraints, etc" Localized implementations are always usually fastest and efficient for any problem. Broadened implementations are costly, but if and only if done well, You'll never regret doing it 2 If I ask you of these, you'll need way too much modification:: *The Inputed Array are NOT strings, but objects with features on a grid... Print them out in spiral form *the ARRAY is NOT a Square Matrix.. but print it in a deterministic manner as defined by some rules. *Print the array in ---- reverse manner starting from the center ---- Anticlockwise manner; ---- Spiral manner skipping some things... I need a callback for each element you are on... and this callback determines whether you should print the next item or not... ADVICE (2): stop hardcoding your test-run inputs... except for real testing. NB: "SUBJECTIVELY" Rough benchmark... ---The algorithm will be rated on the scale of 1 to 10.. 10 is best: Comprehension: 9 Generalization(Generic): 2 Need to Modify for other purpose: 1 Speed: 7 Memory on small input: 7 memory on large input: 1 Defense to Offense ratio: 5 That's good GOOD THINGS: Your algorithm is pretty descent and really easy to comprehend... good one, I loved it... ---the use of printing functions was a good and future-proof idea... ---Recursion is pretty good here... NB: When dealing with Matrix data that might be large... prefer iterative algorithms over recursive algorithms. ...Stop stacking up memory ---You have good approach to keeping things simple ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 2:42am On Jun 27, 2013 |
Here is a third Solution from me.... Very bloated and fun to grasp..... Not about professionalism, but writing and drafting out toy algorithms with respect to tomorrow(no matter how stupid) pays off.... err(Don't make it too stupid though))) ...lol. I had several choices of algorthim, some, as short as my 7lines C++ algorithm version... and a recursive 6line version... BUT having 3 solutions should hopefully pass something to someone out there... I have other problems I am solving... again its for my to figure out somestuff... This solution is presented in C#.... partially 'generalistic'... Again, I make no use of optimally localized algorithm... just some very very basic stuff on grid travellers... NB: "SUBJECTIVELY" Rough benchmark for my C# code... ---The algorithm will be rated on the scale of 1 to 10.. 10 is best: Comprehension: 4 Generalization(Generic): 8 Need to Modify for other purpose: 7 Speed: 5 Memory on small input: 7 memory on large input: 7 Defense to Offense ratio: 2 NB: "SUBJECTIVELY" Rough benchmark for my C++ code... ---The algorithm will be rated on the scale of 1 to 10.. 10 is best: Comprehension: 7 Generalization(Generic): 2 Need to Modify for other purpose: 4 Speed: 10 Memory on small input: 10 memory on large input: 10 Defense to Offense ratio: 6 |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 2:52am On Jun 27, 2013 |
Here is the link ...http://pastebin.com/GAK00hF8 C#4.0 or later ........ Notice the class Program.. where the Main() function resides... look into the Main... that is how you should build up things in OOP;;; Again... this uses standard input.. (stdin) for input... @everyone: Please Criticize my code also... especially the C# PROFESSIONALS... who know any specifics of C# that I am not harnessing
|
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by kambo(m): 4:02am On Jun 27, 2013*. Modified: 5:06am On Jun 27, 2013 |
authurowen: Find below, my Implementation to the print spiral Question written in JAVA (There is very little difference btween C# and Java so C# Developers will be able to relate to it).critiquing your code: s.e code defensively. >> case: imagine this scenerio. Object[][] matrix = new Object[x][y]; matrix[0][1]= new HashTable(); matrix[0][2]= new File("/.." ;matrix[0][3]= new String("" ;//etc...silly, yes - but possible on your code!!! or generic codes like that!!. preferred : Number[][] matrix = new Number[][] or Char[][] matrix = new Character[][] 2.) jagged arrays cripple the algorithm. case: Integer[][] matrix = new Integer[x]; for(int u=0;u<matrix.length;u++) matrix[u]=new Integer[Math.random()*u];//set different array sub lengths!!! cud thoroughly mess up the algorithm. Such abuse can be injected into undefensive code. 3.) PrintMatrix(Object[][] obj) abusive case: PrintMatrix prm = new PrintMatrix(null);//oops - wrong arg or : obj[][] = new obj{{}};//empty matrix. printMatrix(obj);// wrong args -- defensive leaks ..
thanks for sharing.. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by Nobody: 10:18am On Jun 27, 2013 |
OP, you had better pay WHIZTIM for those sweet stunts up there. Nice quiz, OOP approach means the spiral could any size and it will still work. am battling with 2 projects with 2mrw as deadline. when am through with them, I'll attempt this in actionscript 3.0, just that Seun seems to have removed the [flash][/flash] embed thing so you may not be able to test it here. brb. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by jacob05(m): 3:57pm On Jun 27, 2013 |
authurowen: Jacob, you are a developer. Can you take a closer look at your implementation and mine and see the difference?I don't know the version of PHP you're using but my code(Unedited) works perfectly well and correct providing the values you used in your code;
|
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by tobsbola: 8:21am On Jul 04, 2013 |
Hi Hi Hi pls ao can i develop CBT test app using swing with Java? I did one bt i nid d questions numbers to b generated randomly from d database. Tnx |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by authurowen(op): 10:49pm On Jul 04, 2013*. Modified: 5:29am On Aug 10, 2020 |
abc |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 8:42am On Jul 05, 2013 |
@authurowen.... I think you should read more books to increase your comprehension skills. Lemme clear out a few things... 1. I said its sometimes a plus to write bloated codes when you are have NO concrete goal in mind... 2. Your citation with the mathematics expression shows your poor understanding of my intent... If you are damn good in maths, you could be solving a COMPLEX problem, you could express a subproblem of that problem as parametric equations etc... just to get the results you need and continue with more subproblems till you are done! But assuming it was only that subproblem that existed and you have nothing else doing, you could represent it as ODE's or some more complex model and plot graphs to investigate behavior at certain values.... Ofcause... The knowledge is just for keeps. . FOR THE RECORD: as a result of writing crazy overkills in free time, I have implemented a few algorithms that I never knew it formally existed... This started few years back, when I implemented a shortest part algorithm in a bloated solution to a code challenge.... only to later realize that it was 95% Edgar Dijkstra's algorithim! And any fair solution to that problem did not even require a graph algorithm! . . . 3. I would usually go for optimal solution in real life or any software routine... 4. Yes. I am Software tester with experience limited to GUI Applications (I use Python to automate mouse and keyboards), and C++ programs using a few arsenels... e.g Valgrind tools is my fave for blackbox testing. 6. Yes. I have participated in some programming contests, most notably, I am an ex-regionals contestant of the Worlds most renowned programming contest, ACM-ICPC... I am yet to sign up for codeforces etc, cause I know I wouldnt rise up the ranks quickly even in years. I have lots of responsibilites than programming all day or weekend. 7. If you are talking of solving problems in exact. my C++ solution beats yours in ALL endeavors. 8. Your problem is not an Engineering problem. in this context, PLEASE consider Droping the phrase "Software Engineer" or "engineering", BECAUSE your problem and solution is an insult of the first order to Engineering! ...or qualify it with "trainee", "prospective" etc. 9. Recursion builds up stack! Compilers happen to be clever than you that's why the stack size of recursive fuctions MAY be smaller compared to what it was supposed to be...! Peace.... I think we'll make good friends bro. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by WhiZTiM(m): 8:58am On Jul 05, 2013 |
I would stop derailing from the goal of your thread. . . And BTW, I have been a subscriber to code simplicity, it actually reduces maintenance burden and upgrades by the next maintainer. . . . . .and thank you for the problem. . awaiting the next problem. Cheers. |
| Re: Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) by prakharmishra04: 3:07am On May 19, 2016 |
/* hows this import java.util.*; class spiralfill { static int a[][]; public static void main(String args[]) { int k=0,c=1,i,j,l; Scanner sc=new Scanner(System.in); System.out.println("enter the size of matrix" ; l=sc.nextInt(); a=new int[l][l]; while(c<=l*l) { for(j=k;j<l-k;j++) a[k][j]=c++; for(i=k+1;i<l-k;i++) a[i][l-1-k]=c++; for(j=l-2-k;j>=k;j--) a[l-1-k][j]=c++; for(i=l-2-k;i>=k+1;i--) a[i][k]=c++; k++; } arraydisplay(l); } public static void arraydisplay(int l) { int i,j; for(i=0;i<l;i++) { for(j=0;j<l;j++) System.out.print(a[i][j]+"\t" ;System.out.println(); } } } |
Programming Challenge 1 (any Language). • . • Coding Challenge 1: Permutations • 2 • 3 • 4
I'm Leaving Nairaland But Not Without A Gift For ALL Programmers! • Moat Academy Partners Moscow Innovation Agency To Drive Innovation • Friendosphere.com is there for you, developer-inclined website
....

;