Skip to content
New Workbook
Sign up
Quizpad
## work out mean
def ar_mean (a_list):
    """ returns the arithmetic mean of a list """
    num_a = sum(a_list)
    num_b = len(a_list)
    return num_a/num_b
## work out median
def median (a_list):
    """ works out the median value of a list """
    
    place = len(a_list)+1
    place = place//2
    if len(a_list)%2 == 1:
        return a_list[place-1]
    else:
        hold = a_list[place] + a_list[place - 1]
        hold = hold/2
        return hold
def branch_to_leaf (an_int, branch):
    """ take numbers from stem plot, convert to string and then
        convert to proper literal integers"""
    
    List_1 = str(an_int)
    List_2 = []
    ##  this bit cycles through the string and makes a new list of single integers
    for i in range (len(List_1)):
        List_2.append(int(List_1[i]))
    ##  this bit adds 20, as this would be the branch for the 20s
    for j in range(len(List_2)):
        List_2[j] = List_2[j] + 10*branch
    List_2.sort()
    return List_2


##  trying to work out, from the stemplot of cage times in freds, how to add
##  as a string, the numbers on each branch, then get code to cycle through
##  the string, make a new list with seperate ints, and turn them into numbers.
##  also to make print a stemplot.
## set up a place to input the numbers from each branch. This will be entered as a string of numbers.
## cannot start with a zero if there are other numbers after. This would be a problem later.
branch0 = 0
branch1 = 0
branch2 = 5089
branch3 = 189759546
branch4 = 202430
branch5 = 1
branch6 = 4
branch7 = 6
branch8 = 7
## list of branches
tree = [branch0, branch1, branch2, branch3, branch4, branch5, branch6, branch7, branch8]
## final concatenated list of all the integers created from the stemplot
big_list = []
## check input
for tree_check in range(len(tree)):
    if tree[tree_check] > 0:
        branch = tree_check
        holder = branch_to_leaf(tree[tree_check], tree_check)
        ## print(holder)
        big_list = big_list + holder
#####
#       now have string inputs
#       and have lists with the full number values
#       strings can be used to prints a stemplot
#       full values can now be used to work out central tendency and spread
#       next step is to write code to work out:
#                                               median absolute deviation
#                                               Q1 and Q3
print(big_list)
print('n = ', len(big_list))
print('total = ', sum(big_list))
print('mean is ', ar_mean(big_list))
print('median is ', median(big_list))
""" code for turning a list of integers into a stemplot.
    so far only tested on 2-digit positive integers.
"""

#initiate a list of zeroes
tree = [0]
inputa = [45, 20, 14, 15, 19, 22, 7, 20, 20, 49]

for i in inputa:
    tree = tree + [0]

def leaf_maker(int_a, list):
    leaf = int_a%10
    stem = int_a//10
    list[stem] = (list[stem]*10) + leaf
    
    
for index in range(len(inputa)):
    leaf_maker(inputa[index], tree)

for search in range(len(tree)):
    if tree[search] == 0:
        tree[search] = ' '

    
for ex in range(len(tree)):
    print(ex, '|', tree[ex], end = ' ')
    print()