Archive | Coding

python-logo

Python Q&A – Functional Programming

Question 1: Writing Lambda Functions A lambda function is an anonymous function. The syntax for writing a lambda function is straightforward enough: variable_name = lambda argument_one, argument_two: return_value For example, we could write a simple lambda function which increments its argument: increment = lambda x: x + 1 We can then call this in exactly […]

Continue Reading 0
python-logo

Python Q&A – Recursion

Question 1: Recursion – Extracting Digits Recursion is a very useful way to iterate a condition over large amounts of data. In this example, it will be the simple use of recursion to extract the digits of the number inputted/analyzed. Task: Write a recursive function getdigits(n) that returns the list of digits in the positive […]

Continue Reading 0
python-logo

Python Q&A – Inheritance

Question 1: Using Inheritance A class which inherits from a superclass is called a subclass. The reason for using inheritance and subclasses is because some class can be broken down further (for example, an employee in a company is very general, and it is useful to have more information like what type of employee he […]

Continue Reading 2
python-logo

Python Q&A – Class Design

Question 1: Using Classes A class is a user-defined prototype for an object that defines a set of attributes that characterize any object in the class. Task: write a function print_friend_info(person) which accepts a single argument, of type Person, and: prints out their name prints out their age if the person has any friends, prints […]

Continue Reading 0
python-logo

Python Q&A – Classes and Objects

Question 1: Using Range Python has a built in function range, which returns an iterable sequence of numbers. Task: Write a function sum_range(start, end) which uses range to return the sum of the numbers from start up to, but not including, end. (Including start but excluding end is the default behaviour of range.) Write a […]

Continue Reading 0
python-logo

Python: Basic Questions – Functions & If Statements

Question 1: Using Functions Using a function to allow a “block of code” to take in arguments/values and returns a result. Task: Print 2*(n+1) to m using the double and increment functions provided. ‘2’ -> ‘6’ ‘0’ -> ‘2’ ‘-3’ -> ‘-4’ When given the functions: def increment(x): “”” Increment the argument (ie, return the […]

Continue Reading 0
python-logo

Python: Intro – Basic Questions

*Please note that the comments in the code, started by the hashtag, are for educational purposes and do not change the code. Question 1: Introduction: Hello, World! Printing text (e.g. Hello World) Task: Output the string ‘Hello, World!’ Answer: Explanation: Quotations are required because we want to print a string. However, if the task required […]

Continue Reading 0

Powered by WordPress. Designed by WooThemes

Free WordPress Themes