<- function(x){
get_digit_after_dec return (((x * 10) %% 10) %/% 1)
}
<- function(x){
get_digit_after_dec2 return ((x %% 1 * 10) %/% 1)
}
<- function(x){
get_digit_after_dec3 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: 0
Hint: 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] 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:
12.34) get_digit_after_dec(
3.0
0.6123) get_digit_after_dec(
6.0
213) get_digit_after_dec(
0
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.
<- function(x, pos){ get_digit return ((x %/% 10**pos) %% 10) } <- function(x, pos){ get_digit2 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 <- function(x, pos){ get_digit return ((x ** 10** -pos) %% 10) } # change division to multiplication <- function(x, pos){ get_digit2 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)
612.34, 2) get_digit(
6.0
612.34, 1) get_digit(
1.0
612.34, 0) get_digit(
2.0
612.34, -1) get_digit(
3.0
612.34, -2) get_digit(
4.0
612.34, 4) get_digit(
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)
A simple function to determine maximum of two numbers:
def my_max(x, y): return [x,y][y > x]
Now we could run:
3, 10) my_max(
10
19, 3) my_max(
19
12,-4) my_max(
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 functionsmax
andmin
t<- function(x, y){ my_min <- (x < y) + 1 index 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]
3, 10) my_min(
3
19, 3) my_min(
3
12,-4) my_min(
-4
Other ways of writing the max function could be:
- \(x^iy^{1-i}\) where \(i = x > y\)
def my_max1(x,y): = x > y i return x**i * y**(1-i)
3, 10) my_max1(
10
19, 3) my_max1(
19
12,-4) my_max1(
12
\(xi + y(1-i)\) where \(i = x > y\)
Implement this method.
<- function(x,y){ my_max <- x > y i return (x*i + y*(1 - i)) }
def my_max(x,y): = x > y i 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
sign
that outputs the sign of the input. ie a negative number has a sign of-1
and a positive number has1
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
<- function(x) (-1)**(x < 0) sign <- function(x) 2*(x > 0) - 1 sign2 # TRUE SIGN FUNCTION IMPLEMENTATION <- function(x) (x > 0) - (x < 0) sign3
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)
-9) sign3(
-1
23) sign3(
1
0) sign3(
0
Take note that the functions
sign
andsign2
as implemented above give incorrect results for 0. But still acceptable.sign3
is the correct implementationWrite 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
<- function(x)sign(x)* x absolute
absolute(-9)
[1] 9
absolute(23)
[1] 23
def absolute(x): return sign(x) * x
-9) absolute(
9
23) absolute(
23
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
<- function(x)sign(x)* absolute(x)^(1/3) cbrt
cbrt(-8)
[1] -2
cbrt(27)
[1] 3
def cbrt(x): return sign(x)* absolute(x)**(1/3)
-8) cbrt(
-2.0
27) cbrt(
3.0