-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarramedaHelenMary_MP.R
273 lines (223 loc) · 7.43 KB
/
BarramedaHelenMary_MP.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
##Note: Kindly run using Source because it does not do the interactive prompt when clicking Run only.
##Thank you.
#Define an R function that removes NA values from a vector. -- WORKING
walana <- function(a) {
return(a[!is.na(a)])}
testdata <- c(NA,7,22,3,2,15,67,9,NA,NA)
walana(testdata)
print(testdata)
#Define an R function that computes the factorial of given an integer argument. The output should be a vector of length 1. -- WORKING
inputtest<-as.numeric(readline(prompt="Gimme your factorial!"))
FactorialFun <- function(x) {
if (x==1) {
x=1
} else {
return(x*FactorialFun(x-1));
}
}
print(FactorialFun(inputtest))
#Define an R function that computes the determinant of a given matrix. The output should be a vector of length 1.
#Work in Progress
#Test variables so I can check my computation
testdata <- matrix(nrow = 3, ncol = 3)
testdata
testdata[1,1]=0
testdata[1,2]=2
testdata[1,3]=1
testdata[2,1]=3
testdata[2,2]=-1
testdata[2,3]=2
testdata[3,1]=4
testdata[3,2]=-4
testdata[3,3]=1
print(det(testdata))
testdata2 <- matrix(nrow=4,ncol=4)
testdata2[1,1]=0
testdata2[1,2]=2
testdata2[1,3]=1
testdata2[1,4]=3
testdata2[2,1]=-1
testdata2[2,2]=2
testdata2[2,3]=-1
testdata2[2,4]=2
testdata2[3,1]=4
testdata2[3,2]=-4
testdata2[3,3]=1
testdata2[3,4]=0
testdata2[4,1]=34
testdata2[4,2]=1
testdata2[4,3]=4
testdata2[4,4]=-1
det(testdata2)
#Get the dimension of the matrix first
SqMtrxDmnsn <-as.numeric(readline(prompt="Dimension of square matrix"))
inputmatrix <- matrix(0,nrow=SqMtrxDmnsn,ncol=SqMtrxDmnsn)
#Initializer that prompts user to enter the elements of the matrix
#Matrix Writer Function for input matrix
for(i in 1:SqMtrxDmnsn)
{for(j in 1:SqMtrxDmnsn)
{
inputmatrix[i,j] <- as.numeric(readline(prompt="Enter element (L to R then Top to Bottom)"))
j=j+1
}
i=i+1
}
detrmntdos <- function(x) {
#Basic determinant formula
dos<-x[1,1]*x[2,2]-x[1,2]*x[2,1]
return(dos)
}
detrmnttres <- function(x) {
#Basketweave Method formula for a 3x3 Matrix:
tres <- x[1,1]*x[2,2]*x[3,3]+x[1,2]*x[2,3]*x[3,1]+x[1,3]*x[2,1]*x[3,2]-x[3,1]*x[2,2]*x[1,3]-x[3,2]*x[2,3]*x[1,1]-x[3,3]*x[2,1]*x[1,2]
return(tres)
}
#Matrix by Cofactors Loop Method (I will always use row 1 for expansion to simplify the algo)
#If I still have time: Scale down until it's a 3x3 matrix to apply detrmnttres function from above and return the ultimate determinant
#If Murphy's law at work: I'll use det for matrices higher than 3x3 but improve this code before end of term.
detrmntgeneral <- function(x) {
totalna=0
col=1
cofactorsum=0
while(col<=ncol(x)) {
cofactorthing<-x[-1,(-1*col)]
print(det(cofactorthing))
multiplier<-(-1)^(1+col)
element<-x[1,col]
print(multiplier)
print(element)
print(element*multiplier*det(cofactorthing))
yup <- element*multiplier*det(cofactorthing)
cofactorsum = cofactorsum + yup
print(cofactorsum)
col=col+1
}
return(cofactorsum)
}
detrmntgeneral(testdata2)
if (SqMtrxDmnsn==2) {
detrmntdos(inputmatrix)
} else if (SqMtrxDmnsn==3) {
detrmnttres(inputmatrix)
} else if (SqMtrxDmnsn>3) {
detrmntgeneral(inputmatrix)
} else print("Hey, invalid matrix yan, buddy!")
#Define an R function that sorts a given vector in decreasing order. The output should be a vector of the same length. It should accept both numeric or character vectors.
#Work in Progress
#Provided given vectors
numericvector <- c(3,33,25,46,12,8,9,1,2)
charactervector <- c("Luke Skywalker","Han Solo", "Chewbacca", "Darth Vader", "Princess Leia", "Obi Wan Kenobi")
#Bubble Sort for Numeric Vector
SortThisThing <- function(x){
n<-length(x)
for(j in 1:(n-1)){
for(i in 1:(n-j)){
if(x[i]<x[i+1]){
temp<-x[i]
x[i]<-x[i+1]
x[i+1]<-temp
}
}
}
return(x)
}
#Working
numberoutput <- SortThisThing(numericvector)
print(numberoutput)
#Not working yet =(
charoutput <- SortThisThing(charactervector)
print(charactervector)
#Define an R function that accepts a Date (POSIXct) as argument and outputs the day of the week as characters. Use modulo operator. -- WORKING
FlyLikeaPosix <-as.numeric(readline(prompt="I am the oracle that tells you the day of the POSIXct. Enter the number here."))
# Origin is a Thursday - Jan 1 1970
PosixDayPredictor <- function(x) {
days<-x/86400
print(days)
dayslang <- trunc(days)
newx <- as.POSIXct(x,tz="UTC", origin = '1970-01-01')
if(x<86400) {
day<-"Thursday"
} else if (x>86400&&x<172800) {
day<-"Friday"
} else if(x>604800&&dayslang%%8==0) {
day<- "Friday"
} else if(dayslang%%7==0){
day<-"Thursday"
} else if(dayslang%%6==0) {
day<-"Wednesday"
} else if(dayslang%%5==0) {
day<-"Tuesday"
} else if(dayslang%%4==0) {
day<-"Monday"
} else if(dayslang%%3==0) {
day<-"Sunday"
} else if(dayslang%%2==0) {
day<-"Saturday"
}
print(day)
print(newx)
return(day)
}
PosixDayPredictor(FlyLikeaPosix)
#Required: Create a function to compute for your net pay at work. (Actual rate is confidential.) -- WORKING
## Base pay calculation for 50k per month
## Ideal: Code TRAIN LAW; Murphy's Law Version: Just a single bracket
monthlygross=50000
SalaryCalculator <- function(y) {
#SSS Philhealth and PAGIBIG
taxableincome <- y-581.30-437.50-100
#15% deduction
netpay <- 0.85*taxableincome
print(netpay)
return(netpay)
}
SalaryCalculatorv2(monthlygross)
##Version 2 Annual and with TRAIN Law
SalaryCalculatorv2 <- function (basic.monthly, allowance.nontax = 0, allowance.tax = 0) {
annual.taxable = (basic.monthly + allowance.tax) * 12
if (annual.taxable <= 250000) {tax = 0}
else if (annual.taxable <= 400000) {tax = (annual.taxable - 250000) * 0.2}
else if (annual.taxable <= 800000) {tax = 30000 + (annual.taxable - 400000) * 0.25}
else if (annual.taxable <= 2000000) {tax = 130000 + (annual.taxable - 800000) * 0.3}
else if (annual.taxable <= 8000000) {tax = 490000 + (annual.taxable - 2000000) *0.32}
else {tax = 2410000 + (annual.taxable - 8000000) * 0.35}
net.monthly = basic.monthly + allowance.nontax + allowance.tax - (tax/12)
return(net.monthly)
}
SalaryCalculator(monthlygross)
#Create a function that accepts a vector and and integer n and returns nth highest number
numericvector <- c(3,33,25,46,12,8,9,1,2)
NthHighestNumber <- function(x,y) {
x<-sort(x,decreasing=TRUE)
element<-x[y]
return(element)
}
NthHighestNumber(numericvector,2)
#Create a function that computes the compound interest of an investment given the rate, time, and initial amount or principal. -- WORKING
Rate <-as.numeric(readline(prompt="Nominal interest rate in decimal"))
Time <-as.numeric(readline(prompt="Time of investment in years"))
Compounds <-as.numeric(readline(prompt="Compounding periods per year"))
Principal<-as.numeric(readline(prompt="Principal amount"))
CompoundInterest <- function(x,y,z,w) {
Acompound <- w*(1+(x/z))^(y*z)
cat("Accrued amount or compounded amount is ", Acompound)
return(Acompound)
}
CompoundInterest(Rate,Time,Compounds,Principal)
#Create a function isPrime(n) that accepts an integer and outputs a Boolean value (TRUE or FALSE) depending whether the integer is a prime number or not.
#Work in Progress
numero <-as.integer(readline(prompt="Prime or Not? Enter the integer."))
isPrime <- function(n) {
m<-ceiling(sqrt(n))
if(n==1) {return(FALSE)}
else if(n==2|n==3) {return(TRUE)}
else if(n>3) {
while(!is.integer(n/m)&&m>=2){
saywhat<-
print(saywhat)
return(saywhat)
m=m-1
}
}
}
isPrime(numero)