Skip to content

Instantly share code, notes, and snippets.

@soedar
soedar / gist:6775790
Last active December 24, 2015 09:09
Data Abstraction
## Data Abstraction
# Variable Naming
# The key idea behind data abstraction is that we want to give meaning to data types. We already have
# seen how the naming your variables more meaninfully would help us give meaning to primitive data.
# For instance,
x = 24 # Not exactly clear what x refers to, and what does 24 mean
# Consider:
@soedar
soedar / gist:6640332
Created September 20, 2013 16:40
HOP Extra Practice Q7
# For this question, you are suppose to create a poly generator function, that would create
# a *function* that when passed in a value x, calculate the polynomial of using the coefficient
# that was used when the function is created.
# In short, we are given this function has already been defined for us:
def poly(coefficient, x):
# returns the following:
# (coeff[0] * x^0) + (coeff[1] * x^1) + (coeff[2] * x^2) + ...
# and we want to create a function which would create a function that looks like the above, but has fixed
@soedar
soedar / gist:6582905
Last active December 23, 2015 04:49
CS1010S Tutorial 4 GCD HOF
# GCD HOF Question
# For the filtered accumulate function, notice that the filtered function takes in 1 parameter, the
# term number, and returns True/False, depending on whether we want to accept the term or not.
# For the prime question, we can directly use the same is_prime function, because it takes in
# a number, and returns True if it is prime, and False otherwise. Since we want to sum the square
# of the prime numbers between a and b, is_prime works well as the filtered function.
# For the GCD question, it is not that straightforward. The function that is provided to us, gcd,
# takes in 2 values, and worse, it returns an integer which describes the GCD of the 2 numbers.
@soedar
soedar / gist:6410980
Last active December 22, 2015 03:29
CS1010S Tutorial 2 HOF Example
def bank_account(balance):
# Inner functions are created in the scope of the
# outer function, and as such have 'knowledge' of
# the scope.
#
# In this case, it knows about the balance.
# This function returns a string telling us
# whether the owner is rich or poor
def are_you_rich():
# The inner function can access/read the variables
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString *plistfile = [documentsDirectory stringByAppendingString:@"Global.plist"];
NSDictionary *dictionaryToSerialize = @{@"Hello": @"World", @"Dictionary": @{@"InnerHello": @"InnerWorld"}};
[dictionaryToSerialize writeToFile:plistfile atomically:YES];