This week, CSC148 had a test. I studied before the test, so I felt pretty comfortable while writing the test. However, there was a part where I got little confused, that part is about recursive functions. I think the questions involved the list. First few functions were simple to trace, but on last question, I got an answer that I did not expected, and I could not doubt that tracing that I did for other functions were right. I need to understand what is recursive function inorder to understand it properly.
So what is a recursive function? On the course note it says recursive means"defining something in terms of itself." And I also read that functions can call its function to solve the sub problems. The course note about Koch fractal gave me an example and I felt like great example. It explains that 3 koch fractal is consist of four 2 koch fractals and 2 koch fractal is also consist of four 1 koch fractals, and so on. So basically it is doing same drawing pattern in each fractals. This "drawing pattern" will be a function as in programming, so it is calling self multiple times. I thought that it is similar to calculating the nested list.
I feel like tracing the functions just need a lot of practices. The lab 3 had a lot of problems, so it would be an great ways to review and get comfortable with it. Soon we might start coding the recursive function, so getting used to tracing it would make writing process easier.
CSC148 SLog Kento
Sunday, February 8, 2015
Sunday, February 1, 2015
The Post
For the first few weeks of the class, we have reviewed about python classes, learned about subclass, and recursive functions. So far I feel more comfortable with python classes than beginning of the semester, but I still have trouble with implementing the recursive functions.
In the beginning of the semester, I did not understand the python classes well. I knew the process of creating and defining classes from CSC108, but I had trouble writing the type contract. For example, storing data. If I wanted to initialize the data for budget, my type contract would be something like (Budget, int) -> nonetype. Since I thought that something has to be returned, I always wrote it like (Budeget, int) -> int. Another thing that I had difficulty was difference between __str__ and __repr__. Since they both return strings, I thought they are identical methods. Then I just implemented __init__ and __str__ in the class, However, when I set a = Budget(12) and type a again in the shell, it returned something like <__main__.Budget object at 0x0000000003038978>. I expected "a" to return "Budget(12)". Then in the lecture next day, I was explained the __str__ was meant for reading purposes and __repr__ was needed so that python evaluates it and returns in string form. I get to have a lot of practice with these methods in assignment 1.
Right now, I do not have a lot of trouble tracing the recursive functions. I can trace the function that is called again and how does it evaluate the inputs. When it comes to implementing it, it gives me some trouble. In the lab 3, there is a question that asked me to make a function from given example. Following the examples and looking at the function I have traced before, I tried it, but it always give back an answer that I did not expect. This might be a sign that I am not tracing recursive function correctly. I should go back and review how recursive functions are working, then I might get an answer that I expected.
In the beginning of the semester, I did not understand the python classes well. I knew the process of creating and defining classes from CSC108, but I had trouble writing the type contract. For example, storing data. If I wanted to initialize the data for budget, my type contract would be something like (Budget, int) -> nonetype. Since I thought that something has to be returned, I always wrote it like (Budeget, int) -> int. Another thing that I had difficulty was difference between __str__ and __repr__. Since they both return strings, I thought they are identical methods. Then I just implemented __init__ and __str__ in the class, However, when I set a = Budget(12) and type a again in the shell, it returned something like <__main__.Budget object at 0x0000000003038978>. I expected "a" to return "Budget(12)". Then in the lecture next day, I was explained the __str__ was meant for reading purposes and __repr__ was needed so that python evaluates it and returns in string form. I get to have a lot of practice with these methods in assignment 1.
Right now, I do not have a lot of trouble tracing the recursive functions. I can trace the function that is called again and how does it evaluate the inputs. When it comes to implementing it, it gives me some trouble. In the lab 3, there is a question that asked me to make a function from given example. Following the examples and looking at the function I have traced before, I tried it, but it always give back an answer that I did not expect. This might be a sign that I am not tracing recursive function correctly. I should go back and review how recursive functions are working, then I might get an answer that I expected.
Subscribe to:
Posts (Atom)