solutions exercise 1 questions 1-6

Exercise 1

Submit work here

  1. Given a number x ,write a program that would obtain the digit immediately after the decimal point.

    Input: 12.34
    Output: 3
    
    Input: 0.6123
    Output: 6
    
    Input: 213
    Output: 0 

    Hint: Use math operations. eg modulus operator, integer division, multiplication etc

    Any of the following

    get_digit_after_dec <- function(x){
      return (((x * 10) %% 10) %/% 1)
    }
    
    get_digit_after_dec2 <- function(x){
      return ((x %% 1 * 10) %/% 1)
    }
    
    get_digit_after_dec3 <- function(x){
      return (as.integer(x * 10) %% 10)
    }

    Running using any of the functions above:

    get_digit_after_dec(12.34)
    [1] 3
    get_digit_after_dec(0.6123) 
    [1] 6
    get_digit_after_dec(213)
    [1] 0

    Any of the three methods:

    def get_digit_after_dec(x):
      return x * 10 % 10 // 1
    
    def get_digit_after_dec2(x):
      return x % 1 * 10 // 1
    
    def get_digit_after_dec3(x):
      return int(x * 10) % 10

    Running using any of the functions above:

    get_digit_after_dec(12.34)
    3.0
    get_digit_after_dec(0.6123)
    6.0
    get_digit_after_dec(213)
    0
  2. Given a number x and position, write a program that would obtain digit at the specified position. In this exercise we will assume that positions to the left of the decimal points are positive while those to the right of the decimal point are negative.

    Example:

    Input: 612.34, pos = 2
    Output: 6
    
    Input: 612.34, pos = 1
    Output: 1
    
    Input: 612.34, pos = 0
    Output: 2
    
    Input: 612.34, pos = -1
    Output: 3
    
    Input: 612.34, pos = -2
    Output: 4
    
    Input: 612.34, pos = 4
    Output: 0

    Hint: Use math operations. eg modulus operator, integer division, multiplication etc.

    get_digit <- function(x, pos){
      return  ((x %/% 10**pos) %% 10)
    }
    
    get_digit2 <- function(x, pos){
      return  (as.integer((x / 10**pos) %% 10))
    }

    Running using any of the functions above:

    get_digit(612.34, 2)
    [1] 6
    get_digit(612.34, 1) 
    [1] 1
    get_digit(612.34, 0)
    [1] 2
    get_digit(612.34, -1)
    [1] 3
    get_digit(612.34, -2)
    [1] 4
    get_digit(612.34, 4)
    [1] 0

    Extra: What would change if the positions to the left were given as negative while those to the right as positive?

    # Change sign of position 
     get_digit <- function(x, pos){
      return  ((x ** 10** -pos) %% 10)
     }
    
    # change division to multiplication
    get_digit2 <- function(x, pos){
      return  (as.integer((x * 10**pos) %% 10))
    }

    Any of the following

    def get_digit(x, pos):
      return  x // 10**pos % 10
    
    def get_digit2(x, pos):
      return  int(x / 10**pos % 10)
    get_digit(612.34, 2)
    6.0
    get_digit(612.34, 1) 
    1.0
    get_digit(612.34, 0)
    2.0
    get_digit(612.34, -1)
    3.0
    get_digit(612.34, -2)
    4.0
    get_digit(612.34, 4)
    0.0

    Extra: What would change if the positions to the left were given as negative while those to the right as positive?

    # Change sign of position 
    def get_digit(x, pos):
      return  x // 10**-pos % 10
    
    # change division to multiplication
    def get_digit2(x, pos):
      return int(x * 10**pos % 10)
  3. A simple function to determine maximum of two numbers:

    def my_max(x, y):
      return [x,y][y > x]

    Now we could run:

    my_max(3, 10)
    10
    my_max(19, 3)
    19
    my_max(12,-4)
    12

    Take a good look at the code. Now write a function my_min that will take in two arguments and output the minimum of the two. Note that there are built in functions max and mint

    my_min <- function(x, y){
      index <- (x < y) + 1
      return (c(y, x)[index])
    }
    my_min(3, 10)
    [1] 3
    my_min(19, 3)
    [1] 3
    my_min(12,-4)
    [1] -4
    def my_min(x, y):
      return [y,x][x < y]
    my_min(3, 10)
    3
    my_min(19, 3)
    3
    my_min(12,-4)
    -4

    Other ways of writing the max function could be:

    1. \(x^iy^{1-i}\) where \(i = x > y\)
    def my_max1(x,y):
      i = x > y
      return x**i * y**(1-i)
    my_max1(3, 10)
    10
    my_max1(19, 3)
    19
    my_max1(12,-4)
    12
    1. \(xi + y(1-i)\) where \(i = x > y\)

      Implement this method.

    my_max <- function(x,y){
      i <- x > y
      return  (x*i + y*(1 - i))
    }
    def my_max(x,y):
       i = x > y
       return  x*i + y*(1 - i)

    Could you explain as to how the two methods above are able to compute the maximum?

  4. Write a function sign that outputs the sign of the input. ie a negative number has a sign of -1 and a positive number has 1

    Input: -9
    Output: -1
    
    Input: 23
    Output: 1

    Hint1: Use math operations. Recall \(x^0 = 1\). Select a good \(x\) and then think of what your exponent should be.

    Hint2: Use math operations. Use a logical \(x\) and the expression \(2x - 1\)

    Any of the following is acceptable

    sign <- function(x) (-1)**(x < 0)
    
    sign2 <- function(x) 2*(x > 0) - 1
    
    # TRUE SIGN FUNCTION IMPLEMENTATION
    sign3 <- function(x) (x > 0) - (x < 0)
    sign3(-9)
    [1] -1
    sign3(23)
    [1] 1
    sign3(0)
    [1] 0

    Any of the following is acceptable

    def sign(x):
      return (-1)**(x < 0)
    
    def sign2(x):
      return 2*(x > 0) - 1
    
    # TRUE SIGN FUNCTION IMPLEMENTATION
    def sign3(x):
      return (x > 0) - (x < 0)
    sign3(-9)
    -1
    sign3(23)
    1
    sign3(0)
    0

    Take note that the functions sign and sign2 as implemented above give incorrect results for 0. But still acceptable. sign3 is the correct implementation

  5. Write a program absolute that returns the absolute value of a number. Hint: Use the sign function you wrote in question 4.

    Input: -9
    Output: 9
    
    Input: 23
    Output: 23
    absolute <- function(x)sign(x)* x
    absolute(-9)
    [1] 9
    absolute(23)
    [1] 23
    def absolute(x):
      return sign(x) * x
    absolute(-9)
    9
    absolute(23)
    23
  6. Write a program cbrt to compute the cuberoot: Hint \(\sqrt[3]{x} = sign(x)\sqrt[3]{|x|}\) Where \(|\cdot|\) is the absolute function and \(sign(\cdot)\) is the sign function.

    Input: -8
    Output: -2
    
    Input: 1
    Output: 1
    
    Input: 27
    Output: 3
    cbrt <- function(x)sign(x)* absolute(x)^(1/3)
    cbrt(-8)
    [1] -2
    cbrt(27)
    [1] 3
    def cbrt(x):
      return sign(x)* absolute(x)**(1/3)
    cbrt(-8)
    -2.0
    cbrt(27)
    3.0
Back to top