Created
September 26, 2018 16:12
-
-
Save IvanSok/d9fad66f9f690556de1ca2c9e7fa4ac8 to your computer and use it in GitHub Desktop.
HelloFresh test exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Link(object): | |
def __init__(self, parent_name, child_name): | |
self.parent_name = parent_name | |
self.child_name = child_name | |
def find_venture(list_of_links, account): | |
for comp in list_of_links: | |
# if child_name is the same as we are looking for, then proceed for checking its parent | |
if comp.child_name == account: | |
if comp.parent_name != "Root": | |
venture = find_venture(list_of_links, comp.parent_name) | |
return(venture) | |
else: | |
return(comp.child_name) | |
def main(): | |
list_of_links = [] | |
list_of_links.append(Link("Root", "HelloFresh UK")) | |
list_of_links.append(Link("Root", "HelloFresh US")) | |
list_of_links.append(Link("HelloFresh UK", "account1")) | |
list_of_links.append(Link("HelloFresh UK", "account2")) | |
list_of_links.append(Link("HelloFresh US", "account3")) | |
list_of_links.append(Link("account3", "account4")) | |
list_of_links.append(Link("account3", "account5")) | |
list_of_links.append(Link("account4", "account6")) | |
# Printing the venture of "account5": | |
print(find_venture(list_of_links, "account5")) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ivan Sokolenko | |
# Imports | |
from random import seed | |
import math | |
import numpy as np | |
from scipy.stats import binom | |
import matplotlib.pyplot as plt | |
#setting variales: (set your variables here) | |
N = 100 | |
a = 0.5 | |
b = 0.5 | |
# setting a seed value of 42: | |
seed(42) | |
# defining a range of x: | |
k = np.arange(0,N+1) | |
# defining intra cluster correlation p: | |
p = 1/(a+b+1) | |
# calculating the expected value of X, i.e. E[x]: | |
mean = N*p | |
# calculating variance of X, i.e. Var(x): | |
variance = N*p*(1-p) | |
# standard deviation of X: | |
std = math.sqrt(variance) | |
# 95% confidence intervals: | |
lower_bound = p - 1.96*std | |
upper_bound = p + 1.96*std | |
confidence_interval = [lower_bound, upper_bound] | |
print("The expected value of x is %d when N = %d, a = %d and b = %d. For the 95%% confidence interval the lower bound is %d and upper bound is %d." % ( mean, N, a, b ,lower_bound, upper_bound) ) | |
# Plots: | |
binomial = binom.pmf(k, N, p) | |
binomial | |
plt.plot(k, binomial, "o-", label = "pmf") | |
plt.title("Binomial: N=%i, p=%.2f" % (N,p), fontsize=15) | |
plt.xlabel("Number of successes") | |
plt.ylabel("Probability of successes", fontsize=15) | |
plt.axvline(mean,color="red", label = "Mean") | |
plt.legend() | |
plt.show() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
# importing class to test | |
from mainClass import Link | |
from mainClass import find_venture | |
class LinkTest(unittest.TestCase): | |
""" | |
Our basic test class | |
""" | |
def setUp(self): | |
""" | |
Setting up values | |
""" | |
self.list_of_links = [] | |
self.list_of_links.append(Link("Root", "HelloFresh UK")) | |
self.list_of_links.append(Link("Root", "HelloFresh US")) | |
self.list_of_links.append(Link("HelloFresh UK", "account1")) | |
self.list_of_links.append(Link("HelloFresh UK", "account2")) | |
self.list_of_links.append(Link("HelloFresh US", "account3")) | |
self.list_of_links.append(Link("account3", "account4")) | |
self.list_of_links.append(Link("account3", "account5")) | |
self.list_of_links.append(Link("account4", "account6")) | |
def test_UK(self): | |
""" | |
The actual test. | |
Any method which starts with ``test_`` will considered as a test case. | |
Testing for vendor - HelloFresh UK | |
""" | |
res = find_venture(self.list_of_links, "account2") | |
self.assertEqual(res, "HelloFresh UK") | |
def test_US(self): | |
""" | |
Testing for vendor - HelloFresh US | |
""" | |
res = find_venture(self.list_of_links, "account5") | |
self.assertEqual(res, "HelloFresh US") | |
if __name__ == '__main__': | |
unittest.main(argv=['first-arg-is-ignored'], exit=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment