Skip to main content

Learning Python: Introductory Excercises

These are rudimentary functions created in Python, working through the exercises in Computational Statistics in Python from Duke.


 def power_function(num, power):  
   """Funcion to caculate power, number ** power."""  
   return num**power  
   
 def fizz_buzz(begin, end):  
   """Write a program that prints the numbers from 1 to 100.  
   But for multiples of three print “Fizz” instead of the number  
   and for the multiples of five print “Buzz”.  
   For numbers which are multiples of both three and five print “FizzBuzz"""  
   for i in range(begin, end):  
     if i%3 == 0 & i%5 == 0:  
       print("FizzBuzz")  
     elif i%5 == 0:  
       print("Buzz")  
     elif i%3 == 0:  
       print("Fizz")  
     else:  
       print(1)  
   
 def euclidean(UPoint, VPoint, ShortCalc):  
   """Write a function that calculates and returns the euclidean distance   
   between two points u and v, where u and v are both 2-tuples (x,y).   
   For example, if u=(3,0) and v=(0,4) the function should return 5."""  
   try:  
     if ShortCalc == False:  
       hyp = UPoint[0]**2 + VPoint[1]**2  
       return hyp**.5  
     elif ShortCalc == True:  
       return math.hypot(UPoint[0], VPoint[1])  
   except:  
     return 0  
   
 def CountCharacters(someString):  
    """Using a dictionary, write a program to calculate the number times   
    each character occurs in the given string s. Ignore differneces in   
    capitalization - i.e ‘a’ and ‘A’ should be treated as a single key.   
    For example, we should get a count of 7 for ‘a’."""  
    arr = list(someString.lower())  
    arrUnique = dict.fromkeys(set(arr), 0)  
    for item in arrUnique:  
      arrUnique[item] = arr.count(item)  
    return arrUnique  
   
 def CountCharactersAlt(someString):  
    """Using a dictionary, write a program to calculate the number times   
    each character occurs in the given string s. Ignore differneces in   
    capitalization - i.e ‘a’ and ‘A’ should be treated as a single key.   
    For example, we should get a count of 7 for ‘a’."""  
    newDict = {}  
    for item in list(someString.lower()):  
      try:  
        val = newDict.get(item)  
        newDict[item] = val + 1  
      except:  
        newDict[item] = 1  
    return newDict  
   
 def CountCharactersAlt2(someString):  
    """Using a dictionary, write a program to calculate the number times   
    each character occurs in the given string s. Ignore differneces in   
    capitalization - i.e ‘a’ and ‘A’ should be treated as a single key.   
    For example, we should get a count of 7 for ‘a’."""  
    newDict = {}  
    for item in someString.lower():  
      newDict[item] = newDict.get(item, 0) + 1  
    return newDict  
   
 #import string  
 def Sliding_Windows_Percentage(someString, findThis):  
    """Write a program that finds the percentage of sliding windows   
    of length 5 for the sentence s that contain at least one ‘a’.   
    Ignore case, spaces and punctuation. For example, the first   
    sliding window is ‘write’ which contains 0 ‘a’s, and the second   
    is ‘ritea’ which contains 1 ‘a’."""  
    exclude = set(string.punctuation)  
    out = ''.join(ch for ch in someString if ch not in exclude)  
    found_Counter = 0  
    for i in range(0, len(out)-4):  
      if findThis in out[i: i+4]:  
        found_Counter += 1  
    return found_Counter / (len(out)-4)  
   


Popular posts from this blog

Decision Tree in R, with Graphs: Predicting State Politics from Big Five Traits

This was a continuation of prior explorations, logistic regression predicting Red/Blue state dichotomy by income or by personality. This uses the same five personality dimensions, but instead builds a decision tree. Of the Big Five traits, only two were found to useful in the decision tree, conscientiousness and openness.

Links to sample data, as well as to source references, are at the end of this entry.

Example Code

# Decision Tree - Big Five and Politics library("rpart") # grow tree input.dat <- read.table("BigFiveScoresByState.csv", header = TRUE, sep = ",") fit <- rpart(Liberal ~ Openness + Conscientiousness + Neuroticism + Extraversion + Agreeableness, data = input.dat, method="poisson") # display the results printcp(fit) # visualize cross-validation results plotcp(fit) # detailed summary of splits summary(fit) # plot tree plot(fit, uniform = TRUE, main = "Classific…

Mean Median, and Mode with R, using Country-level IQ Estimates

Reusing the code posted for Correlations within with Hofstede's Cultural Values, Diversity, GINI, and IQ, the same data can be used for mean, median, and mode. Additionally, the summary function will return values in addition to mean and median, Min, Max, and quartile values:

Example Code
oecdData <- read.table("OECD - Quality of Life.csv", header = TRUE, sep = ",") v1 <- oecdData$IQ # Mean with na.rm = TRUE removed NULL avalues mean(v1, na.rm = TRUE) # Median with na.rm = TRUE removed NULL values median(v1, na.rm = TRUE) # Returns the same data as mean and median, but also includes distribution values: # Min, Quartiles, and Max summary(v1) # Mode does not exist in R, so we need to create a function getmode <- function(v) { uniqv <- unique(v) uniqv[which.max(tabulate(match(v, uniqv)))] } #returns the mode getmode(v1)
Example Results
> oecdData <- read.table("OECD - Quality of L…

Chi-Square in R on by State Politics (Red/Blue) and Income (Higher/Lower)

This is a significant result, but instead of a logistic regression looking at the income average per state and the likelihood of being a Democratic state, it uses Chi-Square. Interpreting this is pretty straightforward, in that liberal states typically have cities and people that earn more money. When using adjusted incomes, by cost of living, this difference disappears.

Example Code
# R - Chi Square rm(list = ls()) stateData <- read.table("CostByStateAndSalary.csv", header = TRUE, sep = ",") # Create vectors affluence.median <- median(stateData$Y2014, na.rm = TRUE) affluence.v <- ifelse(stateData$Y2014 > affluence.median, 1, 0) liberal.v <- stateData$Liberal # Solve pol.Data = table(liberal.v, affluence.v) result <- chisq.test(pol.Data) print(result) print(pol.Data)
Example Results
Pearson's Chi-squared test with Yates' continuity correction data: pol.Data X-squared = 12.672, df …