This week in class, we went over recursion a bit more when talking about tree lists with their nodes, which could be a root, a leaf, or an internal node. The recursive code occurs when calculating the height of the tree, or the number of children or leaves a tree has. There were many terms discussed in class pertaining to trees which were interesting, but something I found even more interesting since week one was list comprehension. List comprehensions are lines of codes that are read more easily, as if like a regular English sentence, which is why I noticed it since week one of this course because we were not used to this kind of coding in CSC108. They also shorten codes of multiple lines into one by writing both the 'if' statement and the 'else' statement in one line. When I first saw this kind of coding, it confused me and took a bit of getting used to, but the lab this week aided me in reading list comprehensions as we were to rewrite them in the structure we were used to. I may need a bit more practice with this way of coding, but in the end, it should be more efficient.
An example of list comprehension:
new_lst = [x**2 for x in origin_lst if x > 0 else abs(x)]
No comments:
Post a Comment