Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -0,0 +1,15 @@
|
|||
def sma(n) Proc(Float64, Float64)
|
||||
a = Array(Float64).new
|
||||
->(x : Float64) {
|
||||
a.shift if a.size == n
|
||||
a.push x
|
||||
a.sum / a.size.to_f
|
||||
}
|
||||
end
|
||||
|
||||
sma3, sma5 = sma(3), sma(5)
|
||||
|
||||
# Copied from the Ruby solution.
|
||||
(1.upto(5).to_a + 5.downto(1).to_a).each do |n|
|
||||
printf "%d: sma3 = %.3f - sma5 = %.3f\n", n, sma3.call(n.to_f), sma5.call(n.to_f)
|
||||
end
|
||||
|
|
@ -1,3 +1 @@
|
|||
SMA(N) = let buffer = Number[]
|
||||
x -> (push!(buffer, x) ; mean(buffer[max(1,end-N+1):end]))
|
||||
end
|
||||
using Statistics
|
||||
|
|
|
|||
|
|
@ -1,6 +1,22 @@
|
|||
SMA(N, buffer = Number[]) =
|
||||
x -> begin
|
||||
push!(buffer, x)
|
||||
if length(buffer) == N+1 shift!(buffer) end
|
||||
mean(buffer)
|
||||
end
|
||||
function movingaverage(::Type{T} = Float64; lim::Integer = -1) where T<:Real
|
||||
buffer = Vector{T}(0)
|
||||
if lim == -1
|
||||
# unlimited buffer
|
||||
return (y::T) -> begin
|
||||
push!(buffer, y)
|
||||
return mean(buffer)
|
||||
end
|
||||
else
|
||||
# limited size buffer
|
||||
return (y) -> begin
|
||||
push!(buffer, y)
|
||||
if length(buffer) > lim shift!(buffer) end
|
||||
return mean(buffer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test = movingaverage()
|
||||
@show test(1.0) # mean([1])
|
||||
@show test(2.0) # mean([1, 2])
|
||||
@show test(3.0) # mean([1, 2, 3])
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
function movingaverage(::Type{T} = Float64; lim::Integer = -1) where T<:Real
|
||||
buffer = Vector{T}(0)
|
||||
if lim == -1
|
||||
# unlimited buffer
|
||||
return (y::T) -> begin
|
||||
push!(buffer, y)
|
||||
return mean(buffer)
|
||||
end
|
||||
else
|
||||
# limited size buffer
|
||||
return (y) -> begin
|
||||
push!(buffer, y)
|
||||
if length(buffer) > lim shift!(buffer) end
|
||||
return mean(buffer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test = movingaverage()
|
||||
@show test(1.0) # mean([1])
|
||||
@show test(2.0) # mean([1, 2])
|
||||
@show test(3.0) # mean([1, 2, 3])
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#This version allows a user to enter numbers one at a time to figure this into the SMA calculations
|
||||
|
||||
$inputs = @() #Create an array to hold all inputs as they are entered.
|
||||
$period1 = 3 #Define the periods you want to utilize
|
||||
$period2 = 5
|
||||
|
||||
Write-host "Enter numbers to observe their moving averages." -ForegroundColor Green
|
||||
|
||||
function getSMA ($inputs, [int]$period) #Function takes a array of entered values and a period (3 and 5 in this case)
|
||||
{
|
||||
if($inputs.Count -lt $period){$period = $inputs.Count} #Makes sure that if there's less numbers than the designated period (3 in this case), the number of availble values is used as the period instead.
|
||||
|
||||
for($count = 0; $count -lt $period; $count++) #Loop sums the latest available values
|
||||
{
|
||||
$result += $inputs[($inputs.Count) - $count - 1]
|
||||
}
|
||||
|
||||
return ($result | ForEach-Object -begin {$sum=0 }-process {$sum+=$_} -end {$sum/$period}) #Gets the average for a given period
|
||||
}
|
||||
|
||||
while($true) #Infinite loop so the user can keep entering numbers
|
||||
{
|
||||
try{$inputs += [decimal] (Read-Host)}catch{Write-Host "Enter only numbers" -ForegroundColor Red} #Enter the numbers. Error checking to help mitigate bad inputs (non-number values)
|
||||
|
||||
"Added " + $inputs[(($inputs.Count) - 1)] + ", sma($period1) = " + (getSMA $inputs $Period1) + ", sma($period2) = " + (getSMA $inputs $period2)
|
||||
}
|
||||
|
|
@ -9,7 +9,6 @@ if n=='' | n=="," then n= 10 /* " " " " "
|
|||
|
||||
do k=n%2 by -1 to 1; @.j= k; j= j+1 /* " 2nd " " " decreasing " */
|
||||
end /*k*/
|
||||
|
||||
say ' number ' " SMA with period" p' ' " SMA with period" q
|
||||
say ' ──────── ' "───────────────────" '───────────────────'
|
||||
pad=' '
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
struct SimpleMovingAverage {
|
||||
var period: Int
|
||||
var numbers = [Double]()
|
||||
|
||||
mutating func addNumber(_ n: Double) -> Double {
|
||||
numbers.append(n)
|
||||
|
||||
if numbers.count > period {
|
||||
numbers.removeFirst()
|
||||
}
|
||||
|
||||
guard !numbers.isEmpty else {
|
||||
return 0
|
||||
}
|
||||
|
||||
return numbers.reduce(0, +) / Double(numbers.count)
|
||||
}
|
||||
}
|
||||
|
||||
for period in [3, 5] {
|
||||
print("Moving average with period \(period)")
|
||||
|
||||
var averager = SimpleMovingAverage(period: period)
|
||||
|
||||
for n in [1.0, 2, 3, 4, 5, 5, 4, 3, 2, 1] {
|
||||
print("n: \(n); average \(averager.addNumber(n))")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue