get_digit_after_dec <- function(x){
return (x %/% 0.1 %% 10)
}
get_digit_after_dec1 <- 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)
}solutions exercise 1 questions 1-6
Exercise 1
Submit work here
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: 0Hint: Use math operations. eg modulus operator, integer division, multiplication etc
Any of the following
Running using any of the functions above:
get_digit_after_dec(12.34)[1] 3get_digit_after_dec(0.6123)[1] 6get_digit_after_dec(213)[1] 9Any 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) % 10Running using any of the functions above:
get_digit_after_dec(12.34)3.0get_digit_after_dec(0.6123)6.0get_digit_after_dec(213)0Given a number
xand 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: 0Hint: 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] 6get_digit(612.34, 1)[1] 1get_digit(612.34, 0)[1] 2get_digit(612.34, -1)[1] 3get_digit(612.34, -2)[1] 4get_digit(612.34, 4)[1] 0Extra: 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.0get_digit(612.34, 1)1.0get_digit(612.34, 0)2.0get_digit(612.34, -1)3.0get_digit(612.34, -2)4.0get_digit(612.34, 4)0.0Extra: 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)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)10my_max(19, 3)19my_max(12,-4)12Take a good look at the code. Now write a function
my_minthat will take in two arguments and output the minimum of the two. Note that there are built in functionsmaxandmintmy_min <- function(x, y){ index <- (x < y) + 1 return (c(y, x)[index]) }my_min(3, 10)[1] 3my_min(19, 3)[1] 3my_min(12,-4)[1] -4def my_min(x, y): return [y,x][x < y]my_min(3, 10)3my_min(19, 3)3my_min(12,-4)-4Other ways of writing the max function could be:
- \(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)10my_max1(19, 3)19my_max1(12,-4)12\(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?
Write a function
signthat outputs the sign of the input. ie a negative number has a sign of-1and a positive number has1Input: -9 Output: -1 Input: 23 Output: 1Hint1: 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] -1sign3(23)[1] 1sign3(0)[1] 0Any 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)-1sign3(23)1sign3(0)0Take note that the functions
signandsign2as implemented above give incorrect results for 0. But still acceptable.sign3is the correct implementationWrite a program
absolutethat returns the absolute value of a number. Hint: Use the sign function you wrote in question 4.Input: -9 Output: 9 Input: 23 Output: 23absolute <- function(x)sign(x)* xabsolute(-9)[1] 9absolute(23)[1] 23def absolute(x): return sign(x) * xabsolute(-9)9absolute(23)23Write a program
cbrtto 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: 3cbrt <- function(x)sign(x)* absolute(x)^(1/3)cbrt(-8)[1] -2cbrt(27)[1] 3def cbrt(x): return sign(x)* absolute(x)**(1/3)cbrt(-8)-2.0cbrt(27)3.0