Andela Bootcamp Day 3: Diving into code
So today is day three and has been the most exciting for me so far (probably for most of us) as we got to solve a couple of problems requiring us to collaborate with peers to some degree. However, lets start from the very begining.
The day started with a soft skills session where a method of communication referred to as “Yes and” was demonstrated to the class and the class separated into groups of two to practice its use. First we saw how frustrating it was if every attempt at connecting with a fellow is met with an outright NO. Next, we considered how receiving a yes response with an excuse would be no more better than the first. Finally, we practiced the “Yes and” and immediately appreciated how much better it is communicate in this way. This approach ensures that even if a not so smart idea was proposed initially, the conversation could end with a very brilliant solution to the problem.
Following was an opportunity for each member of the class to discuss his/her findings on the topic: How do we ensure our code works. Colleagues raised very good points showing that each student seriously researched the problem. Testing code vigorously using unit tests, professional QA testers, and other forms of structured testing were recommended by all fellows. Understanding the requirements and developing a pseudocode/algorithm was also advocated. Others pushed for coding conventions to be followed to ensure easy debugging for other programmers and for the author to easily understand his code in future reviews. Other ways include peer review, use of IDEs, inviting users to test your program, testing on production systems among others.
Then the trainer introduced Test Driven Design — defined as a process where test cases are defined before writing code. Benefits include: enabling programmers understand the software at a deeper level before diving into code, the creation of more modular, testable and easily maintainable code etc. However, TDD has some disadvantages with giving programmers false confidence should the test cases not be comprehensive and adding maintenance and cost overhead. *Integration Testing was discussed as tests that are carried out on a program in relation to its interaction with another program.
Then came our first programming exercise of the day; writing a function called twoSum that takes in two parameters: a list and a target number. The function finds two numbers in the list that sum up to the target and return the positions of these numbers. We were told a run time of N would be preferred over N^^2. However, mine was effectively an N^^2 program although it offered slight improvement over a full N^^2 solution. The instructor introduced a brilliant N solution that made use of the python’s dictionary capability to be accessed in constant time. So instead of searching through a list of the second number which has a runtime of n. The algorithm goes through the list once, each time subtracting current number from target and checking if the difference exists as a key in the dictionary. If the key doesn’t exists, the current number is created as a key and the value becomes its position on the original list. Thus, whenever we search for a difference in the dictionary and find it, we know we have the difference and therefore our answer. Cool ehh?
Finally, lambda, filter, map and reduce were explained to the class and examples demonstrated. The above are used to implement the functional programming paradigm in python. Lambda is known in other languages as the anonymous function. It could take in parameters which could include functions and return a result. Filter takes two parameters: a function and a list and returns a list. It could be used to filter values that meet a certain requirement from a new list. The function part of the argument typically returns a boolean. Map is similar to filter except that it function returns an element of the list. Both return lists. Reduce takes a list and reduces it to a single value; hence, the name reduce. It could be used as an accumulator and to calculate factorial. To find factorial with reduce:
factorial = reduce(lambda x,y: x * y range(1, num + 1)) and voila you have a factorial. Just one line and statement. SWEET!
I need to study more about these four commands. They are humble in appearance but really powerful.