+ 18

How to read and analyze other's code?

Is it an important ability to analyze other's creations? What kind of stuff is going on? Because, I'm solving an assignment named as Adjacent Clone Letters and I'm able to fill the matrix with random letters but I don't know how to check for the clones? I checked out others code as well but yeah, while analyzing others code, I realized one that I just don't know how to analyze all the stuff going there!!! I don't know how it is working! And I was feeling like a stupid as the solution is in my hand but I'm unable to solve the problem. I can tell you, this kind of feeling is not good!!!! My confidence is going down whenever I see that I haven't solve that assignment!!! Please guide!! Basically, I have two questions to ask: - 1. Is to analyze others code is an important ability in programming? 2. What is the logic to solve that assignment? https://code.sololearn.com/c8zW4D40Ucyg/?ref=app https://www.sololearn.com/learn/4682/?ref=app

20th Mar 2019, 4:02 AM
Ayush Sinha
Ayush Sinha - avatar
16 Antworten
+ 12
The feeling isn't that good indeed...just have a collection of what stumbles you, then break them down into mini problems, solve each one at a time and eventually the whole concept will make sense. Infact never stop reviewing because when such thing happens and you work around it, that's when you become even good at it.
20th Mar 2019, 2:16 PM
Dan Rhamba
Dan Rhamba - avatar
+ 9
Ayush Sinha For me it is hard to read a code if the coder uses non-english variable names. And I have problems when all is written together (without blank lines). I think the ability to analize other code is important but I know that it is not easy and need a lot of experience. Assignment: Lets take an example: A B D A C D C A B How many different letters are used? 4 A, B, C, D I solved it with a HashMap: key = letter, value = clone A hashmap don't add duplicates. So I got this map: [A=0, B=0, C=0, D=0] Now check the matrix line per line: check the neighbors of your current letter: up, down, left, right, bottom left, bottom right, upper left, upper right. Matrix[x][y] - positions: down: x+1, y (x+1 < matrix.length) up: x-1,y (x-1 >= 0) left: x, y-1 (y-1 >= 0) right: x, y+1 (y+1 < matrix[x].length) upper left: x-1, y-1 upper right: x-1, y+1 bottom left: x+1, y-1 bottom right: x+1, y+1 loop through your matrix: matrix[0][0] = A left: nothing right: B down: A --> clone found
20th Mar 2019, 4:10 PM
Denise Roßberg
Denise Roßberg - avatar
+ 7
Dan Rhamba, the feeling of being bored is also a thing which is a problem with me! Do you have any suggestion on that?
20th Mar 2019, 3:09 PM
Ayush Sinha
Ayush Sinha - avatar
+ 7
Ayush Sinha This is what I do when I feel bored... from my videos collection or YouTube videos I watch those ones that demonstrate some programming skills. I can't tell how but watching someone explaining some concepts gets me motivated again...
20th Mar 2019, 10:51 PM
Dan Rhamba
Dan Rhamba - avatar
+ 6
Denise Roßberg, Now check the matrix line per line: check the neighbors of your current letter: up, down, left, right, bottom left, bottom right, upper left, upper right. Matrix[x][y] - positions: down: x+1, y (x+1 < matrix.length) up: x-1,y (x-1 >= 0) left: x, y-1 (y-1 >= 0) right: x, y+1 (y+1 < matrix[x].length) upper left: x-1, y-1 upper right: x-1, y+1 bottom left: x+1, y-1 bottom right: x+1, y+1 loop through your matrix: matrix[0][0] = A left: nothing right: B down: A --> clone found Can you explain it more deeply please!!!!! 😊
21st Mar 2019, 5:42 AM
Ayush Sinha
Ayush Sinha - avatar
+ 5
Part two: I have a method for checking all neighbors which returns true if two neighbors are the same. If a clone is found, I add the letter and increment the value of this letter: [A=1, B=0, C=0] Second letter is B: no clone found. Third letter D ([0][3] = [1][3] --> clone found, add D and increment value of D) [A=1, B=0, C=0, D=1] Next line: A [1][0] = [0][0] --> clone found And so on. HashMap after the loop: [A=3, B=0, C=2, D=2] I hope it helps you a little bit. https://code.sololearn.com/cy5oNTefTXIb/?ref=app
20th Mar 2019, 4:22 PM
Denise Roßberg
Denise Roßberg - avatar
+ 5
Reading other people's code can be a lot harder than reading your own, especially when it doesn't have enough comments or contains badly named variables etc. This may apply to your own code that you revisit months later as well. If any non-trivial code of others is not well documented/commented as to the algorithm used, for example, it can be a real struggle to analyse. Often you need a lot of patience and effort to analyse it. Remember to include useful and pertinent comments in your code and we can only hope that others would do the same. Sometimes for larger pieces of code, debuggers and software which generates call-graphs may help you. For larger programs, debugger and software that generates call graphs may help you.
21st Mar 2019, 12:07 PM
Sonic
Sonic - avatar
+ 5
https://code.sololearn.com/ciV82WjqlnQD/?ref=app If you run the code you will see a matrix and the positions of each letter. I think it makes clear how I found down/up/left/right positions. Matrix[i][j]: left/right i-1/i+1 up/down j-1/j+1 And I think it is clear that your matrix has borders: i-1 and j-1 >= 0 i+1 < matrix.length j+1 < matrix[i].length Imagine a nested for loop to get all letters and inside it a method: public boolean checkNeighbor(int i, int j){ //left if(j - 1 >= 0 && matrix[i][j] == matrix[i][j-1] ){ return true; //right if(j + 1 < matrix[i].length && matrix[i][j] == matrix[i][j+1]){ return true //do it for all neighbor positions return false //no clone found } For(int i = 0; i < matrix.length; i++){ For(int j = 0; j < matrix[i].length; j++){ If(checkNeighbor(i,j) == true){ //see my last post with the hashmap //get the value of matrix[i][j] in the hashMap --> value++ }//end if }//end for But I don't know how to find a solution without using a HashMap ;) Maybe you need two arrays.
21st Mar 2019, 3:23 PM
Denise Roßberg
Denise Roßberg - avatar
+ 5
i find the following helpful when analysing others code. 1) print a physical copy of the code. there is nothing better than physical manipulation. 2) mark the printout by labelling the functions and numbering them. 3) you must now understand the code in two ways. firstly, you should understand how each function works. secondly, understand how each function relates to other functions. this is called within elaboration and between elaboration (craik and lockhart 1975). if you want a visual way of representing information then it is recommended that you understand how to use tools like psudo code, flowcharts and uml
21st Mar 2019, 8:42 PM
Logomonic Learning
Logomonic Learning - avatar
21st Mar 2019, 9:35 PM
Anna
Anna - avatar
+ 3
That is a great question! I started programming when the company's programmer gave me the Business BASIC language reference for the Data General Nova Architecture minicomputer that ran their applications. Having the language reference handy to look up what the code does is key. Writing your own code snippets and seeing what the code does helps. It's painful at the start, but if you keep at it you will get it. Back when I started there was no community like SoloLearn, so keep asking questions and continue the journey 🤓
21st Mar 2019, 3:55 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
I would use Frida or Ghidra(NSA just released this) Which are decompilers (or fuzzers) to basically run hooks and scripts that act like a logCat for the apps they are used upon. .....you see what functions and methods are in play and their values and then you can use python or java to CHANGE (hack) the values and change or fix or experiment (mostly hack or ostensibly hunt down malware) So you see a password function that is encrypted and decide to make the value of the password 0...or 1234 You just defeated the password. Just an example. But also.....you can closely watch your on apps as they execute and trace everything which can be very helpful In debugging your work.
20th Mar 2019, 4:18 PM
Gregory Daerr
Gregory Daerr - avatar
+ 2
I feel your frustration. While I can't help you solve your situation. You have demonstrated the importance of using descriptive function names and variables as well as comments while developing code. This is particularly important for a learning experience that will engage the student and allow the student to succeed. In this course I have seen questions that rely on knowledge that has not been imparted in order for the question to be answered correctly only to have that information explained in a later lesson. That can be very discouraging to the student. I can answer your first question. Yes. Analyze code when you can. You can gain knowledge, techniques and further your own skill set by observing how another solved a given task.
21st Mar 2019, 1:06 PM
Michael Williams
Michael Williams - avatar
+ 2
Here on Sololearn you have the advantage that you can ask the people personally if you don't understand a solution. If you try on your own... well, I would try to follow the program step by step, keeping track of the values and how they change over the course of the program on paper, maybe visualising it with a draft. Of course it helps if the code is readably written and commented. I tried to comment my own attempt understandably... wanna give it a try? (And as I said, you can ask.) https://code.sololearn.com/c30wK7XUZeo3/?ref=app
22nd Mar 2019, 2:11 PM
HonFu
HonFu - avatar
+ 1
To all those who are saying "keep track of each variable and function of what they are doing" I think this approach helps only when the code is written in c++ or python or some other non web languages and that too when it is short. But what can one do if he is reading a code that is written in web languages (here on sololearn)? It's equivalent to reading three codes in 3 different languages at the same time and all of them interwoven in a beautiful and ugly way at the same time. When each of the section (html, css, js) contains more than 300 lines and I can't even count how many local or global variables and functions are used there, it becomes a terrible mess. Is there any suggestions on this? How do you guys read and understand such huge codes? I know you don't read the whole code (obviously), but some of you just look at some parts of the code and get the idea of what and how it is doing something. How you guys do it? Thanks for this thread
29th Feb 2020, 3:34 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
0
I want to get my phone rooted, I have the kingroot apk and the purify app, and I got the bootloader unlocked. <redacted> Patricia Collier, please refrain from posting contact information here, it may be unsafe.
21st Mar 2019, 9:26 PM
Patricia Collier
Patricia Collier - avatar