In [1]:
## 8.1

def fancy_print( text ):
    print("*****")
    print( text )
    print("*****")
    
fancy_print("Testing my fancy_print function")
*****
Testing my fancy_print function
*****
In [2]:
## 8.2

def my_function( number ):
    return 2 * number

print( my_function(3) )
6
In [3]:
## 8.3

def largest_item( list_of_things ):
    largest = -1
    for item in list_of_things:
        if item > largest:
            largest = item
    return largest

print( largest_item( [1, 5, 4, 9, 14, 2 ] ) )
14
In [4]:
## 8.4

def mathematics( x, y ):
    if x > y:
        return x * y - y
    else:
        return 2 * y - x
In [5]:
## 8.5

def is_leap_year( year ):
    if year % 100 == 0:
        if year % 400 == 0: ## if it is divisible by 100, it is a leap year only if it can also be divided by 400.
            return True
        else:
            return False
    else:
        if year % 4 == 0: ## A year is a leap year if it can be divided by 4.
            return True
        else:
            return False
In [6]:
## 8.6

## is_leap_year is defined in 8.5

def number_of_days( year1, year2 ):
    total_days = 0
    for year in range( year1, year2 ):
        if is_leap_year( year ):
            total_days = total_days + 366
        else:
            total_days = total_days + 365
    return total_days
In [7]:
print( number_of_days(2023, 2024) )
print( number_of_days(2023, 2030) )
365
2557
In [8]:
## 8.7

import networkx

graph = networkx.read_edgelist("edgelist.txt")
print( graph.degree() )
[('A', 2), ('B', 2), ('C', 2)]
In [9]:
## 8.8

## This is an R-only exercise

8.9

  • when factorial is set a 0 in the begining, the result is 0 (0 times anything is still zero)
  • the calculation factorial * current is not stored to any variable, thus the current stored in factorial does not work
  • printing factorial within the for-loop prints it many times

8.10

  • the statement current = current + 1 does not make any sense and should be removed
  • the sum variable should increase the sum variable, not reset it - i.e. it should be sum = sum + current
In [10]:
## 8.12

## define known leap years between 1900 and 2022

leap_years = [1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020]

for potential_leap_year in leap_years:
    if is_leap_year ( potential_leap_year ) == False:
        print("Is leap year is not working correctly")
        
for year in range(1900, 2023):
    if year not in leap_years:
        if is_leap_year( year ) == True:
            print("Is leap year is not working correcty")
            
print("If nothing printed above, is leap year correctly identified leap years and not-leap-years between 1900 and 2022")
If nothing printed above, is leap year correctly identified leap years and not-leap-years between 1900 and 2022
In [11]:
## 8.13

def tax_rate( income, children ):
    taxes = 0.2 * income
    taxes = taxes - 10 * children
    if taxes < 0:
        taxes = 10
    return taxes

def tax_rate( income, children ):
    taxes = 0.2 * income
    if children > 0:
        taxes = 2/3 * taxes
    return taxes
In [12]:
## 8.14

stockA = [100]
stockB = [100]

def inflowA( stockA, stockB ):
    increaseA = 0.05
    increaseA_B = 0.05
    return increaseA * stockA[-1] + increaseA_B * stockB[-1]

def outflowA( stockA, stockB ): ## while this does not use stockB, it might be better to make the function so that in case we use it, it is there
    decreaseA = 0.05
    return decreaseA * stockA[-1]

def inflowB( stockA, stockB ):
    increaseB = 0.05
    increaseB_A = 0.05
    return increaseB * stockB[-1] + increaseB_A * stockA[-1]

def outflowB( stockA, stockB ):
    decreaseB = 0.05
    return decreaseB * stockB[-1]

for i in range(0, 50):
    changeA = inflowA( stockA, stockB ) - outflowA( stockA, stockB )
    stockA.append( stockA[-1] + changeA )
    
    changeB = inflowB( stockA, stockB ) - outflowB( stockA, stockB )
    stockB.append( stockB[-1] + changeB )
    
print( stockA )
print( stockB )
[100, 105.0, 110.2625, 115.80065625, 121.62831414062501, 127.76004281660157, 134.21117159961963, 140.99782831163674, 148.13697959443294, 155.64647332621524, 163.54508324131308, 171.8525558645142, 180.5896598773766, 189.77823803993243, 199.4412617975881, 209.60288870973775, 220.28852284366175, 231.5248782846949, 243.34004592143975, 255.76356367298823, 268.82649033371916, 282.5614832202844, 297.0028798149004, 312.1867836090536, 328.1511543622294, 344.93590300131086, 362.58299139789557, 381.13653727297503, 400.6429244912369, 421.15091902072686, 442.7117908477686, 465.3794421519298, 489.21054206147085, 514.2646683261655, 540.6044562616756, 568.29575533784, 597.4077938023489, 628.0133517513636, 660.1889430797569, 694.0150067658494, 729.5761079688566, 766.9611494417859, 806.2635937883197, 847.5816971193243, 891.0187546931272, 936.683359153663, 984.6896720120828, 1035.157709050533, 1088.2136403616094, 1143.99010577359, 1202.6265464500043]
[100, 105.25, 110.763125, 116.5531578125, 122.63457351953126, 129.02257566036133, 135.7331342403423, 142.78302565592415, 150.1898746356458, 157.97219830195655, 166.1494524640222, 174.74208025724792, 183.77156325111676, 193.2604751531134, 203.2325382429928, 213.7126826784797, 224.72710882066278, 236.30335273489752, 248.4703550309695, 261.2585332146189, 274.69985773130486, 288.8279318923191, 303.67807588306414, 319.28741506351685, 335.69497278162834, 352.9417679316939, 371.07091750158867, 390.12774436523745, 410.1598905897993, 431.21743654083565, 453.35302608322405, 476.62199819082053, 501.0825252938941, 526.7957587102023, 553.8259815232861, 582.2407692901782, 612.1111589802956, 643.5118265678637, 676.5212737218516, 711.222024060144, 747.7008294585869, 786.0488869306762, 826.3620666200922, 868.7411514760583, 913.2920892107147, 960.1262571683978, 1009.360740769002, 1061.1186262215285, 1115.529308239609, 1172.7288135282886, 1232.8601408507889]
In [13]:
## 8.16

def sum( list_of_things ):
    sum = 0
    for thing in list_of_things:
        sum = sum + thing
    return sum

def mean( list_of_things ):
    return sum( list_of_things ) / len( list_of_things )

experimental = {}
experimental['A'] = []
experimental['B'] = []

for line in open('experiment.txt'):
    line = line.strip().split(',')
    condition = line[1]
    
    for i in range(2,6):
        experimental[ condition ].append( float( line[i] ) )
        
## means

print("Mean for A", mean( experimental['A'] ) )
Mean for A 6.0
In [14]:
## 8.17

def make_science_happen( index ):
    
    conditionA = 0
    conditionB = 0
    
    for line in open('experiment.txt'):
        line = line.strip().split(',')
        condition = line[1]
        experimental_value = float( line[1+index] ) ## index 0 is for username, index 1 is for condition, thus starting from that
        
        if condition == 'A':
            conditionA = conditionA + experimental_value
        if condition == 'B':
            conditionB = conditionB + experimental_value
        
    if conditionA - conditionB > 1:
        return True
        
    return False

make_science_happen( 1 )
Out[14]:
False
In [ ]: