I know what I’m supposed to do for this fucking swift project its ridiculously easy. I just can’t do it for some reason. It’s a program that takes in numbers and outputs the min, max, average, and standard deviation. It stores all the numbers in an array until it’s time to output them and then does the calculations and outputs. It’s not hard. However I just can’t get it to work or to code it at all and I don’t know what to do. I’m having a break down over here because I can’t even program an input function to get the first number needed.
Like here’s what I’ve got so far maybe one of you knows swift and can tell me what the fuck I’m doing wrong.
*/ Declerations *\ (I don’t actually have the word delerations so don’t worry about it not being commented out incorrectly)
var minimum:Double = 0.0
var maximum:Double = 0.0
var average:Double = 0.0
var standardDev:Double = 0.0
var runningTotal:Double = 0.0
var count:Double = 0.0
*/ Calculations */
fund get_num() -> Double {
if let num = readLine() {
if let num = num(Double) {return num}
}
return 0
}
print(”Please enter Test Scores”)
print(”enter -1 when you are done entering numbers”)
let num = get_num()
if num = -1 {
minimum = 0
maximum = 0
average - runningTotal/count
standardDev = 0
}
else {
print (”Please enter another score”)
num = get_num()
runningTotal =+ num
count += count
}
*/ output */
print(”Minimum:\(minimum)”)
print(”Maximum:\( maximum )”)
print(”Average:\( average )”)
print(”Standard Deviation:\(standardDev)”)
I know I’m missing the array and the standard dev stuff, I don’t remember why I have so many 0s either. I think they were just placeholders. I can do c++ , c#, some HTML, visual basic, some css, but swift is kicking my ass.
1 note
·
View note
HackingWithSwift Day 7
Closures Part 2
Attempt to pass a closure as a parameter with the closure accept parameter as well
travel { (place: String) in
print("I'm going to \(place) in my car")
}
func travel(action: (String) -> Void) {
print("I'm getting ready to go.")
action("London")
print("I arrived!")
}
=============================
Practical example
To give you a practical example, imagine you were building a car. The car needs to know what engine it has, what steering wheel it has, how many seats it has, and so on. Sure, the engine could just be a string of information, but really it should be able to actually accelerate or decelerate to a certain speed.
let changeSpeed = { (speed: Int) in
print("Changing speed to \(speed)kph")
}
And now we can create a buildCar() function that accepts any sort of closure for the engine, as long as that closure can be told to accelerate or decelerate to a specific integer value:
func buildCar(name: String, engine: (Int) -> Void) {
// build the car
}
=============================
Attempt to pass a closure as a parameter with the closure accept parameter and return value as well
func travel(action: (String) -> String) {
print("I'm getting ready to go.")
let description = action("London")
print(description)
print("I arrived!")
}
travel { (place: String) -> String in
return "I'm going to \(place) in my car"
}
https://www.hackingwithswift.com/quick-start/understanding-swift/when-would-you-use-closures-with-return-values-as-parameters-to-a-function
The sudden introduce of “reduce” might be a bit too sudden for the example
Create an array of [10, 20, 30], then summarise it to single value
func reduce(_ values: [Int], using closure: (Int, Int) -> Int) -> Int {
// start with a total equal to the first value
var current = values[0]
// loop over all the values in the array, counting from index 1 onwards
for value in values[1...] {
// call our closure with the current value and the array element, assigning its result to our current value
current = closure(current, value)
}
// send back the final current value
return current
}
let numbers = [10, 20, 30]
let sum = reduce(numbers) { (runningTotal: Int, next: Int) in
runningTotal + next //if just 1 line of code, ,and it’s inside closure, no need to write “return”
}
print(sum)
//Most simplify version
let sum = reduce(numbers, using: +)
===================
Shorthand parameters
Just when I thought swift already make everything “simple” (can be very complicated when read it the first time) and still there’s shorthand params…..geezzzzz
func travel(action: (String) -> String) {
print("I'm getting ready to go.")
let description = action("London")
print(description)
print("I arrived!")
}
travel { (place: String) -> String in
return "I'm going to \(place) in my car"
}
Then chop some more - removed data type for pass in param
travel { place -> String in
return "I'm going to \(place) in my car"
}
And…..continue chopping - removed the need to indicator need return param
travel { place in
return "I'm going to \(place) in my car"
}
And chopped - removed “return”
travel { place in
"I'm going to \(place) in my car"
}
And chopped - no more param name but just $0
travel {
"I'm going to \($0) in my car"
}
It can be $0, $1, $2, depends on how many params are there and the param sequence, in this case, there’s only 1 param, name “place” and since it’s the “first param” to pass in, so by normal index, it will be $0
Don’t overuse it because it may helps or screwed up the readability at the same time.
==================
I made passed the THE BASIC
Now it’s the INTERMEDIATE LEVEL
SHIT XD
func travel(action: (String, Int) -> String) {
print("I'm getting ready to go.")
let description = action("London", 60)
print(description)
print("I arrived!")
}
travel {
"I'm going to \($0) at \($1) miles per hour."
}
So here is the example of $0 and $1, $0 will be the string, which is London, $1 is 60
==================
Returning Closure from Function
“The syntax for this is a bit confusing a first, because it uses -> twice: once to specify your function’s return value, and a second time to specify your closure’s return value.”
The author is RIGHT! NOT JUST A BIT, BUT VERY CONFUSING XD
func travel() -> (String) -> Void { //return a closure that return a string
return {
print("I'm going to \($0)")
}
}
let result = travel()
result("London")
Cheat Sheet
http://goshdarnclosuresyntax.com/
======================
Capturing value
func travel() -> (String) -> Void {
return {
print("I'm going to \($0)")
}
}
let result = travel()
result("London")
func travel() -> (String) -> Void {
var counter = 1
return {
print("\(counter). I'm going to \($0)")
counter += 1
}
}
Counter create inside the closure, it’s like a static variable , so if I call travel 3 times, the counter will keep increase instead of stay at 1 or 2
—————
Up to this part, I think out of these 2 parts, I can only confident says that I understand maybe 50% of it. Maybe more practice at later chapter will helps
0 notes
runningtotal Oracle
SELECT BAL.*,
SUM(AMOUNT_BALANCE) OVER(PARTITION BY COMPANY, ACCOUNTING_YEAR,ACCOUNT ORDER BY COMPANY, ACCOUNTING_YEAR,ACCOUNT, ACCOUNTING_PERIOD RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) ACUM_AMOUNT_BALANCE
FROM (
SELECT COMPANY, ACCOUNTING_YEAR, ACCOUNT, ACCOUNTING_PERIOD,SUM(AMOUNT_BALANCE) AMOUNT_BALANCE
FROM ACCOUNTING_BALANCE_AUTH WHERE ACCOUNT LIKE ('1500000%0')
AND COMPANY = 'CI'
AND ACCOUNTING_YEAR = 2018 AND ACCOUNTING_PERIOD <= 8
GROUP BY COMPANY, ACCOUNTING_YEAR, ACCOUNTING_PERIOD, ACCOUNT
ORDER BY 1,2,3,4) BAL
0 notes