Optimizing Performance for Level Generation - ios
I'm generating simple game maps using a method described in this post. I'm using the second method of starting with a seed tile and randomly growing out. I'm testing it in a Swift playground on a 30x30 array. The problem is my method takes several minutes to generate a 30x30 map, and maps in my real game might exceed 100x100 tiles. How can I optimize the following code to run faster?
import UIKit
import SpriteKit
var map = [[String]](count: 30, repeatedValue:[String](count: 30, repeatedValue:" "))
var landTiles = [(y: Int,x: Int)]()
landTiles.append((15,x: 15))
let minX = 3
let maxX = 28
let minY = 3
let maxY = 28
var coastTiles = [(y: Int,x: Int)]()
var hasReachedBounds = false
while !hasReachedBounds {
coastTiles = []
for tile in landTiles {
let coastal = isCoastal(tile.y, x: tile.x)
if coastal {
coastTiles.append(tile)
}
}
let numTiles = UInt32(coastTiles.count)
print(map)
let randomTile = coastTiles[Int(arc4random_uniform(numTiles))]
if randomTile.x <= minX || randomTile.x >= maxX || randomTile.y <= minY || randomTile.y >= maxY {
hasReachedBounds = true
break
}
let coastal = isCoastal(randomTile.y, x: randomTile.x)
if !coastal{
continue
}
let randomPosition = arc4random_uniform(4) + 1
switch randomPosition {
case 1:
landTiles.append((randomTile.y + 1, randomTile.x))
map [randomTile.y + 1][randomTile.x] = "&"
case 2:
landTiles.append((randomTile.y, randomTile.x + 1))
map [randomTile.y][randomTile.x + 1] = "&"
case 3:
landTiles.append((randomTile.y - 1, randomTile.x))
map [randomTile.y - 1][randomTile.x] = "&"
case 4:
landTiles.append((randomTile.y, randomTile.x - 1))
map [randomTile.y][randomTile.x - 1] = "&"
default: break
}
}
func isCoastal (y: Int, x: Int) -> Bool{
if map[y + 1][x] == " " || map[y][x + 1] == " " || map[y - 1][x] == " " || map[y][x - 1] == " "{
return true
} else {
return false
}
}
print(map)
Related
Mathematic calculation using array of numbers and array of arithmetic operators in swift [duplicate]
I am doing a simple calculator, but when performing the multiplication and division, my code doesn't make them a priority over plus and minus. When doing -> 2 + 2 * 4, result = 16 instead of 10... How to conform to the math logic inside my switch statement? mutating func calculateTotal() -> Double { var total: Double = 0 for (i, stringNumber) in stringNumbers.enumerated() { if let number = Double(stringNumber) { switch operators[i] { case "+": total += number case "-": total -= number case "÷": total /= number case "×": total *= number default: break } } } clear() return total }
Assuming you want a generalised and perhaps extensible algorithm for any arithmetic expression, the right way to do this is to use the Shunting Yard algorithm. You have an input stream, which is the numbers and operators as the user typed them in and you have an output stream, which is the same numbers and operators but rearranged into reverse Polish notation. So, for example 2 + 2 * 4 would be transformed into 2 2 4 * + which is easily calculated by putting the numbers on a stack as you read them and applying the operators to the top items on the stack as you read them. To do this the algorithm has an operator stack which can be visualised as a siding (hence "shunting yard") into which low priority operators are shunted until they are needed. The general algorithm is read an item from the input if it is a number send it to the output if the number is an operator then while the operator on the top of the stack is of higher precedence than the operator you have pop the operator on the stack and send it to the output push the operator you read from input onto the stack repeat the above until the input is empty pop all the operators on the stack into the output So if you have 2 + 2 * 4 (NB top of the stack is on the left, bottom of the stack is on the right) start: input: 2 + 2 * 4 output: <empty> stack: <empty> step 1: send the 2 to output input: + 2 * 4 output: 2 stack: <empty> step 2: stack is empty so put + on the stack input: 2 * 4 output: 2 stack: + step 3: send the 2 to output input: * 4 output: 2 2 stack: + step 4: + is lower priority than * so just put * on the stack input: 4 output: 2 2 stack: * + step 5: Send 4 to output input: output: 2 2 4 stack: * + step 6: Input is empty so pop the stack to output input: output: 2 2 4 * + stack: The Wikipedia entry I linked above has a more detailed description and an algorithm that can handle parentheses and function calls and is much more extensible. For completeness, here is an implementation of my simplified version of the algorithm enum Token: CustomStringConvertible { var description: String { switch self { case .number(let num): return "\(num)" case .op(let symbol): return "\(symbol)" } } case op(String) case number(Int) var precedence: Int { switch self { case .op(let symbol): return Token.precedences[symbol] ?? -1 default: return -1 } } var operation: (inout Stack<Int>) -> () { switch self { case .op(let symbol): return Token.operations[symbol]! case .number(let value): return { $0.push(value) } } } static let precedences = [ "+" : 10, "-" : 10, "*" : 20, "/" : 20] static let operations: [String : (inout Stack<Int>) -> ()] = [ "+" : { $0.push($0.pop() + $0.pop()) }, "-" : { $0.push($0.pop() - $0.pop()) }, "*" : { $0.push($0.pop() * $0.pop()) }, "/" : { $0.push($0.pop() / $0.pop()) } ] } struct Stack<T> { var values: [T] = [] var isEmpty: Bool { return values.isEmpty } mutating func push(_ n: T) { values.append(n) } mutating func pop() -> T { return values.removeLast() } func peek() -> T { return values.last! } } func shuntingYard(input: [Token]) -> [Token] { var operatorStack = Stack<Token>() var output: [Token] = [] for token in input { switch token { case .number: output.append(token) case .op: while !operatorStack.isEmpty && operatorStack.peek().precedence >= token.precedence { output.append(operatorStack.pop()) } operatorStack.push(token) } } while !operatorStack.isEmpty { output.append(operatorStack.pop()) } return output } let input: [Token] = [ .number(2), .op("+"), .number(2), .op("*"), .number(4)] let output = shuntingYard(input: input) print("\(output)") var dataStack = Stack<Int>() for token in output { token.operation(&dataStack) } print(dataStack.pop())
If you only have the four operations +, -, x, and ÷, you can do this by keeping track of a pendingOperand and pendingOperation whenever you encounter a + or -. Then compute the pending operation when you encounter another + or -, or at the end of the calculation. Note that + or - computes the pending operation, but then immediately starts a new one. I have modified your function to take the stringNumbers, operators, and initial values as input so that it could be tested independently in a Playground. func calculateTotal(stringNumbers: [String], operators: [String], initial: Double) -> Double { func performPendingOperation(operand: Double, operation: String, total: Double) -> Double { switch operation { case "+": return operand + total case "-": return operand - total default: return total } } var total = initial var pendingOperand = 0.0 var pendingOperation = "" for (i, stringNumber) in stringNumbers.enumerated() { if let number = Double(stringNumber) { switch operators[i] { case "+": total = performPendingOperation(operand: pendingOperand, operation: pendingOperation, total: total) pendingOperand = total pendingOperation = "+" total = number case "-": total = performPendingOperation(operand: pendingOperand, operation: pendingOperation, total: total) pendingOperand = total pendingOperation = "-" total = number case "÷": total /= number case "×": total *= number default: break } } } // Perform final pending operation if needed total = performPendingOperation(operand: pendingOperand, operation: pendingOperation, total: total) // clear() return total } Tests: // 4 + 3 calculateTotal(stringNumbers: ["3"], operators: ["+"], initial: 4) 7 // 4 × 3 calculateTotal(stringNumbers: ["3"], operators: ["×"], initial: 4) 12 // 2 + 2 × 4 calculateTotal(stringNumbers: ["2", "4"], operators: ["+", "×"], initial: 2) 10 // 2 × 2 + 4 calculateTotal(stringNumbers: ["2", "4"], operators: ["×", "+"], initial: 2) 8 // 17 - 2 × 3 + 10 + 7 ÷ 7 calculateTotal(stringNumbers: ["2", "3", "10", "7", "7"], operators: ["-", "×", "+", "+", "÷"], initial: 17) 22
First you have to search in the array to see if there is a ÷ or × sign. Than you can just sum or subtract. mutating func calculateTotal() -> Double { var total: Double = 0 for (i, stringNumber) in stringNumbers.enumerated() { if let number = Double(stringNumber) { switch operators[i] { case "÷": total /= number case "×": total *= number default: break } //Remove the number from the array and make another for loop with the sum and subtract operations. } } clear() return total } This will work if you are not using complex numbers.
If you don't care speed, as it's running by a computer and you may use the machine way to handle it. Just pick one feasible calculate to do it and then repeat until every one is calculated. Just for fun here. I use some stupid variable and function names. func evaluate(_ values: [String]) -> String{ switch values[1] { case "+": return String(Int(values[0])! + Int(values[2])!) case "-": return String(Int(values[0])! - Int(values[2])!) case "×": return String(Int(values[0])! * Int(values[2])!) case "÷": return String(Int(values[0])! / Int(values[2])!) default: break; } return ""; } func oneTime(_ string: inout String, _ strings: [String]) throws{ if let first = try NSRegularExpression(pattern: "(\\d+)\\s*(\(strings.map{"\\\($0)"}.joined(separator: "|")))\\s*(\\d+)", options: []).firstMatch(in: string , options: [], range: NSMakeRange(0, string.count)) { let tempResult = evaluate((1...3).map{ (string as NSString).substring(with: first.range(at: $0))}) string.replaceSubrange( Range(first.range(at: 0), in: string)! , with: tempResult) } } func recursive(_ string: inout String, _ strings: [String]) throws{ var count : Int! repeat{ count = string.count ; try oneTime(&string, strings) } while (count != string.count) } func final(_ string: inout String, _ strings: [[String]]) throws -> String{ return try strings.reduce(into: string) { (result, signs) in try recursive(&string, signs) }} var string = "17 - 23 + 10 + 7 ÷ 7" try final(&string, [["×","÷"],["+","-"]]) print("result:" + string)
Using JeremyP method and the Shunting Yard algorithm was the way that worked for me, but I had some differences that had to do with the Operator Associativity(left or right priority) so I had to work with it and I developed the code, which is based on JeremyP answer but uses arrays. First we have the array with the calculation in Strings, e.g.: let testArray = ["10","+", "5", "*" , "4", "+" , "10", "+", "20", "/", "2"] We use the function below to get the RPN version using the Shunting Yard algorithm. func getRPNArray(calculationArray: [String]) -> [String]{ let c = calculationArray var myRPNArray = [String]() var operandArray = [String]() for i in 0...c.count - 1 { if c[i] != "+" && c[i] != "-" && c[i] != "*" && c[i] != "/" { //push number let number = c[i] myRPNArray.append(number) } else { //if this is the first operand put it on the opStack if operandArray.count == 0 { let firstOperand = c[i] operandArray.append(firstOperand) } else { if c[i] == "+" || c[i] == "-" { operandArray.reverse() myRPNArray.append(contentsOf: operandArray) operandArray = [] let uniqOperand = c[i] operandArray.append(uniqOperand) } else if c[i] == "*" || c[i] == "/" { let strongOperand = c[i] //If I want my mult./div. from right(eg because of parenthesis) the line below is all I need //-------------------------------- // operandArray.append(strongOperand) //---------------------------------- //If I want my mult./div. from left let lastOperand = operandArray[operandArray.count - 1] if lastOperand == "+" || lastOperand == "-" { operandArray.append(strongOperand) } else { myRPNArray.append(lastOperand) operandArray.removeLast() operandArray.append(strongOperand) } } } } } //when I have no more numbers I append the reversed operant array operandArray.reverse() myRPNArray.append(contentsOf: operandArray) operandArray = [] print("RPN: \(myRPNArray)") return myRPNArray } and then we enter the RPN array in the function below to calculate the result. In every loop we remove the numbers and the operand used before and we import the previous result and two "p" in the array so in the end we are left with the solution and an array of "p". func getResultFromRPNarray(myArray: [String]) -> Double { var a = [String]() a = myArray print("a: \(a)") var result = Double() let n = a.count for i in 0...n - 1 { if n < 2 { result = Double(a[0])! } else { if a[i] == "p" { //Do nothing else. Calculations are over and the result is in your hands!!! } else { if a[i] == "+" { result = Double(a[i-2])! + Double(a[i-1])! a.insert(String(result), at: i-2) a.remove(at: i - 1) a.remove(at: i - 1) a.remove(at: i - 1) a.insert("p", at: 0) a.insert("p", at: 0) } else if a[i] == "-" { result = Double(a[i-2])! - Double(a[i-1])! a.insert(String(result), at: i-2) a.remove(at: i - 1) a.remove(at: i - 1) a.remove(at: i - 1) a.insert("p", at: 0) a.insert("p", at: 0) } else if a[i] == "*" { result = Double(a[i-2])! * Double(a[i-1])! a.insert(String(result), at: i-2) a.remove(at: i - 1) a.remove(at: i - 1) a.remove(at: i - 1) a.insert("p", at: 0) a.insert("p", at: 0) } else if a[i] == "/" { result = Double(a[i-2])! / Double(a[i-1])! a.insert(String(result), at: i-2) a.remove(at: i - 1) a.remove(at: i - 1) a.remove(at: i - 1) a.insert("p", at: 0) a.insert("p", at: 0) } else { // it is a number so do nothing and go the next one } }//no over yet }//n>2 }//iterating return result }//Func
Hackerrank New Year Chaos Swift
I am trying to solve Hackerrank's New Year Chaos problem in Swift. https://www.hackerrank.com/challenges/new-year-chaos/problem It is about finding the number of bribes people made on a line waiting for a roller coaster ride. For example, there is a total of 3 bribes in this list [2, 1, 5, 3, 4]. Person 2 bribed person 1. Person 5 bribed person 3 and 4. If there are more than 2 bribes by a person, the line becomes "Too chaotic". I was able to get an exponential solution. However, I want to make it linear. func minimumBribes(q: [Int]) -> Void { var bribeCount = 0 var chaotic = false // for i in 0..<q.count { // if q[i] - (i + 1) > 2 { // chaotic = true // break // } // // for j in i + 1..<q.count { // if q[i] > q[j] { // bribeCount += 1 // } // } // } var i = 0 while i < q.count - 1 { if q[i] - (i + 1) > 2 { chaotic = true break } else if q[i] > i + 1 { bribeCount += (q[i] - (i + 1)) i += 1 } else if q[i] <= i + 1 && q[i] > q[i + 1] && q.indices.contains(i + 1) { bribeCount += 1 i += 1 } else { // q[i] < q[i + 1] i += 1 } } if chaotic { print("Too chaotic") } else { print(bribeCount) } } I commented out the exponential solution, which works. But the linear solution does not work and I cannot find out why. It works with the following arrays, [3,2,1,6,5,4], [2,5,1,3,4], [1,2,5,3,7,8,6,4], [1,3,4,2,7,6,5,9,8,11,10,14,13,12]. But there is a really long array in one of the test cases of the problem, which I do not get the correct answer with my linear solution. For this long array, I get 966 with my exponential solution but the linear solution prints 905. [2,1,5,6,3,4,9,8,11,7,10,14,13,12,17,16,15,19,18,22,20,24,23,21,27,28,25,26,30,29,33,32,31,35,36,34,39,38,37,42,40,44,41,43,47,46,48,45,50,52,49,51,54,56,55,53,59,58,57,61,63,60,65,64,67,68,62,69,66,72,70,74,73,71,77,75,79,78,81,82,80,76,85,84,83,86,89,90,88,87,92,91,95,94,93,98,97,100,96,102,99,104,101,105,103,108,106,109,107,112,111,110,113,116,114,118,119,117,115,122,121,120,124,123,127,125,126,130,129,128,131,133,135,136,132,134,139,140,138,137,143,141,144,146,145,142,148,150,147,149,153,152,155,151,157,154,158,159,156,161,160,164,165,163,167,166,162,170,171,172,168,169,175,173,174,177,176,180,181,178,179,183,182,184,187,188,185,190,189,186,191,194,192,196,197,195,199,193,198,202,200,204,205,203,207,206,201,210,209,211,208,214,215,216,212,218,217,220,213,222,219,224,221,223,227,226,225,230,231,229,228,234,235,233,237,232,239,236,241,238,240,243,242,246,245,248,249,250,247,244,253,252,251,256,255,258,254,257,259,261,262,263,265,264,260,268,266,267,271,270,273,269,274,272,275,278,276,279,277,282,283,280,281,286,284,288,287,290,289,285,293,291,292,296,294,298,297,299,295,302,301,304,303,306,300,305,309,308,307,312,311,314,315,313,310,316,319,318,321,320,317,324,325,322,323,328,327,330,326,332,331,329,335,334,333,336,338,337,341,340,339,344,343,342,347,345,349,346,351,350,348,353,355,352,357,358,354,356,359,361,360,364,362,366,365,363,368,370,367,371,372,369,374,373,376,375,378,379,377,382,381,383,380,386,387,384,385,390,388,392,391,389,393,396,397,394,398,395,401,400,403,402,399,405,407,406,409,408,411,410,404,413,412,415,417,416,414,420,419,422,421,418,424,426,423,425,428,427,431,430,429,434,435,436,437,432,433,440,438,439,443,441,445,442,447,444,448,446,449,452,451,450,455,453,454,457,456,460,459,458,463,462,464,461,467,465,466,470,469,472,468,474,471,475,473,477,476,480,479,478,483,482,485,481,487,484,489,490,491,488,492,486,494,495,496,498,493,500,499,497,502,504,501,503,507,506,505,509,511,508,513,510,512,514,516,518,519,515,521,522,520,524,517,523,525,526,529,527,531,528,533,532,534,530,537,536,539,535,541,538,540,543,544,542,547,548,545,549,546,552,550,551,554,553,557,555,556,560,559,558,563,562,564,561,567,568,566,565,569,572,571,570,575,574,577,576,579,573,580,578,583,581,584,582,587,586,585,590,589,588,593,594,592,595,591,598,599,596,597,602,603,604,605,600,601,608,609,607,611,612,606,610,615,616,614,613,619,618,617,622,620,624,621,626,625,623,628,627,631,630,633,629,635,632,637,636,634,638,640,642,639,641,645,644,647,643,646,650,648,652,653,654,649,651,656,658,657,655,661,659,660,663,664,666,662,668,667,670,665,671,673,669,672,676,677,674,679,675,680,678,681,684,682,686,685,683,689,690,688,687,693,692,691,696,695,698,694,700,701,702,697,704,699,706,703,705,709,707,711,712,710,708,713,716,715,714,718,720,721,719,723,717,722,726,725,724,729,728,727,730,733,732,735,734,736,731,738,737,741,739,740,744,743,742,747,746,745,750,748,752,749,753,751,756,754,758,755,757,761,760,759,764,763,762,767,765,768,766,771,770,769,774,773,776,772,778,777,779,775,781,780,783,784,782,786,788,789,787,790,785,793,791,792,796,795,794,798,797,801,799,803,800,805,802,804,808,806,807,811,809,810,814,812,813,817,816,819,818,815,820,821,823,822,824,826,827,825,828,831,829,830,834,833,836,832,837,839,838,841,835,840,844,842,846,845,843,849,847,851,850,852,848,855,854,853,857,856,858,861,862,860,859,863,866,865,864,867,870,869,868,872,874,875,871,873,877,878,876,880,881,879,884,883,885,882,888,886,890,891,889,893,887,895,892,896,898,894,899,897,902,901,903,905,900,904,908,907,910,909,906,912,911,915,913,916,918,914,919,921,917,923,920,924,922,927,925,929,928,926,932,931,934,930,933,935,937,939,940,938,936,943,944,942,941,947,946,948,945,951,950,949,953,952,956,954,958,957,955,961,962,963,959,964,966,960,965,969,968,971,967,970,974,972,976,973,975,979,977,981,982,978,980,983,986,984,985,989,988,987,990,993,991,995,994,997,992,999,1000,996,998] Please help me figure out what is wrong with my solution. Thanks in advance!!
Here is my solution which passes all the test cases :) func minimumBribes(q: [Int]) -> Void { var bCount = 0 var isChaotic = false for (key,value) in q.enumerated() { if (value - 1) - key > 2 { isChaotic = true break } for index in stride(from: max(0, value - 2), to: key, by: 1){ if q[index] > value { bCount += 1 } } } isChaotic ? print("Too chaotic") : print("\(bCount)") }
What you basically need to do is to first check if the element in each loop is on it's correct position. And if not you find out how much further is it from the right position if its greater than 2 you print "Too chaotic". Your solution is correct uptil this point. But if the difference is less than or equal to 2 then you need to increment the bribes and swap the indices to represent updated array. Furthermore if there are two swaps then you need to represent how the array would be effected by these 2 swaps and hence swap these values before the next iteration to ensure the array is in the condition it would be after these swaps. Please refer to my solution below. It passes for all test cases: func swapValues( arr:inout [Int],index:Int, times: Int, bribes:inout Int) -> Bool { if times == 0 { return false } if arr[index] > arr[index+1] { let temp = arr[index+1] arr[index+1] = arr[index] arr[index] = temp bribes = bribes + 1 return swapValues(arr: &arr, index: index+1, times: times-1,bribes: &bribes) }else{ var diff = abs(arr[index+1] - (index+2)) if diff > 2 { print("Too chaotic") return true } var tooChaotic = swapValues(arr: &arr, index: index+1, times: diff,bribes:&bribes) if tooChaotic { return true } return swapValues(arr: &arr, index: index, times: times, bribes: &bribes) } } func minimumBribes(q: [Int]) -> Void { var qC = q var bribes = 0 var i = 0 while i <= qC.count-1{ if i+1 == qC[i] { i = i + 1 continue } let diff = abs(qC[i] - (i+1)) if diff > 2 { print("Too chaotic") return } var tooChaotic = swapValues(arr: &qC, index: i, times: diff, bribes: &bribes) if tooChaotic { return } } print(bribes) }
I found this short and easy solution. func minimumBribes(q: [Int]) -> Void { var ans = 0 var shouldShow = true for i in stride(from: (q.count - 1), through: 0, by: -1) { if (q[i] - (i+1) > 2) { shouldShow = false break; } for j in stride(from: max(0, q[i] - 2), to: i, by: 1){ if q[j] > q[i] { ans += 1 } } } if shouldShow { print(ans) } else { print("Too chaotic") } } https://github.com/AnanthaKrish/example-ios-apps
Compare Array [Triplets]
I'm trying to complete this a = [5, 6, 7] b = [3, 6, 10] Now, let's compare each individual score: a[0] > b[0], so Alice receives point. a[1] == b[1], so nobody receives a point. a[2] < b[2] , so Bob receives point. Alice's comparison score is 1, and Bob's comparison score is 1. Thus, we have to print 1 1 (Alice's comparison score followed by Bob's comparison score) on a single line. I did it by this way: var a = [5, 6, 7] var b = [3, 6, 10] let pointsAlice = (a[0] > b[0] ? 1: 0) + (a[1] > b[1] ? 1: 0) + (a[2] > b[2] ? 1: 0) let pointsBob = (a[0] < b[0] ? 1: 0) + (a[1] < b[1] ? 1: 0) + (a[2] < b[2] ? 1: 0) print("\(pointsAlice) \(pointsBob)") But it is showing as an incorrect answer(Hackerrank). Is my way correct or is there any other way to solve this ?
Please try this ... let a = readLine()!.components(separatedBy: " ").flatMap { Int($0) } let b = readLine()!.components(separatedBy: " ").flatMap { Int($0) } let aResult = zip(a, b).filter(>).count let bResult = zip(b, a).filter(>).count //Final Result Status print(aResult, bResult)
I guess u concentrated on explanation more :p Try this: let arrA = readLine()!.components(separatedBy: " ").map{ Int($0)! } let arrB = readLine()!.components(separatedBy: " ").map{ Int($0)! } var aliceScore = 0 var bobScore = 0 for index in 0...2{ if (arrA[index] > arrB[index]){ aliceScore += 1 } if (arrA[index] < arrB[index]){ bobScore += 1 } } print("\(aliceScore)" + " " + "\(bobScore)")
This was my solution. Trying to learn how the reduce func works. func compareTriplets(a: [Int], b: [Int]) -> [Int] { var total = [0,0] for x in 0..<a.count { if a[x] > b[x] { total[0] += 1 } else if a[x] < b[x] { total[1] += 1 } } return aTotal }
Why not just use higher order functions like reduce? let a = [5, 6, 7] let b = [3, 6, 10] let pointsAlice = zip(a, b).reduce(0, combine: { $0 + ($1.0 > $1.1 ? 1 : 0) } ) let pointsBob = zip(b, a).reduce(0, combine: { $0 + ($1.0 > $1.1 ? 1 : 0) } ) print("\(pointsAlice) \(pointsBob)") // 1 1
Do not hard code like this (a[0] > b[0] ? 1: 0) + (a[1] > b[1] ? 1: 0) + (a[2] > b[2] ? 1: 0) Imagine if the array has 1,000 or more elements!!! I think the simplest solution is: var alicePoints = 0 var bobPoints = 0 for i in 0..<A.count { if A[i] > B[i] { alicePoints += 1 } else if A[i] < B[i] { bobPoints += 1 } } print("\(alicePoints) \(bobPoints)") And the more elegant solution is the one from #koropok above (I upated for Swift 3.x) let bobPoints = zip(B, A).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) } let alicePoints = zip(A, B).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) } print("\(alicePoints) \(bobPoints)")
Here is the updated Answer for comparing Triplets Let's say if we have var a = [5, 6, 7] var b = [3, 6, 10] Then // compareTriplets function func compareTriplets(a: [Int], b: [Int]) -> [Int] { let bobPoints = zip(b, a).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) } let alicePoints = zip(a, b).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) } print("Points earned by Alice and Bob : \(alicePoints) \(bobPoints)") return [alicePoints, bobPoints] }
Try this: return [zip(a, b).reduce(0) {$0 + ($1.0 > $1.1 ? 1 : 0)}, zip(b, a).reduce(0) {$0 + ($1.0 > $1.1 ? 1 : 0)}]
In python, without considering the constraints proposed(ab >= 1 & ab<=100): def compareTriplets(a, b): i = 0 score = [0,0] while i < 3: if a[i] > b[i]: score[0] = score[0] + 1 elif a[i] < b[i]: score[1] = score[1] + 1 i = i + 1 return score
import java.util.*.; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int klice=0; int bob=0; int a[]=new int[n]; int b[]=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=0;i<n;i++){ b[i]=sc.nextInt(); } for(int i=0;i<n;i++){ if(a[i]>b[i]){ klice++; } else if(b[i]>a[i]){ bob++; } } System.out.println(klice +" "+ bob); } }
How can I change the Double value in my dictionary outside the function?
var grades: [String : Double] grades = ["A": 0.0, "A-": 0.0, "B+": 0.0, "B": 0.0, "B-": 0.0, "C+": 0.0, "C": 0.0, "C-": 0.0, "D+": 0.0, "D": 0.0, "D-": 0.0, "F": 0.0] func calcGPA() { if let a = grades["A"], amin = grades["A-"], bplu = grades["B+"], b = grades["B"], bmin = grades["B-"], cplu = grades["C+"], c = grades["C"], cmin = grades["C-"], dplu = grades["D+"], d = grades["D"], dmin = grades["D-"], f = grades["F"] { // Divide by this let gradesAdded = a + amin + bplu + b + bmin + cplu + c + cmin + dplu + d + dmin + f //grades multiplied by their values and added ex. a * 4.0 + amin * 3.7 let gradesCalculated = a * 4.0 + amin * 3.7 + bplu * 3.3 + b * 3.0 + bmin * 2.7 + cplu * 2.3 + c * 2.0 + cmin * 1.7 + dplu * 1.3 + d * 1.0 + dmin * 0.7 // Dont do F because it would just add 0 var gpa = gradesCalculated / gradesAdded if gpa.isNaN { gpa = 0.0 } } } Is there a way to do something like grades["A"] += 1.0 so that it goes up by one and I can call calcGPA()? I can't figure out how to make this work. Any help on this would be awesome
You can increase a value in your dictionary by force unwrapping the lookup: grades["A"]! += 1.0 but this is dangerous because it will crash if the key isn't in the dictionary. So, you should check: if let count = grades["A"] { grades["A"] = count + 1 } Here is an updated version of your program: func calcGPA(_ termGrades: [String]) -> Double? { var grades: [String: Double] = ["A": 0.0, "A-": 0.0, "B+": 0.0, "B": 0.0, "B-": 0.0, "C+": 0.0, "C": 0.0, "C-": 0.0, "D+": 0.0, "D": 0.0, "D-": 0.0, "F": 0.0] var gpa: Double? for grade in termGrades { if let count = grades[grade] { grades[grade] = count + 1 } else { print("Hmmm, \(grade) is not a valid value for a grade") return nil } } if let a = grades["A"], amin = grades["A-"], bplu = grades["B+"], b = grades["B"], bmin = grades["B-"], cplu = grades["C+"], c = grades["C"], cmin = grades["C-"], dplu = grades["D+"], d = grades["D"], dmin = grades["D-"], f = grades["F"] { // Divide by this let gradesAdded = a + amin + bplu + b + bmin + cplu + c + cmin + dplu + d + dmin + f //grades multiplied by their values and added ex. a * 4.0 + amin * 3.7 let gradesCalculated = a * 4.0 + amin * 3.7 + bplu * 3.3 + b * 3.0 + bmin * 2.7 + cplu * 2.3 + c * 2.0 + cmin * 1.7 + dplu * 1.3 + d * 1.0 + dmin * 0.7 // Dont do F because it would just add 0 gpa = gradesAdded == 0 ? nil : gradesCalculated / gradesAdded } return gpa } // example calls calcGPA(["E"]) // nil "Hmmm, E is not a valid value for a grade" calcGPA(["A-"]) // 3.7 calcGPA(["A", "B"]) // 3.5 calcGPA(["B", "B+", "A-"]) // 3.333333333333333 calcGPA([]) // nil Notes: I changed your function to take an array of grades and return a Double? (Optional Double). nil is returned if anything goes wrong (input array is empty, input array contains invalid grade such as "E"). Divide by zero is avoided ahead of time by checking the divisor before the calculation. Alternate Solution: And now for something completely different... Inspired by #CodeBender's comment, here is an implementation that uses an enum with an associated value to represent the grades: enum Grade: Double { case A = 4.0 case Aminus = 3.7 case Bplus = 3.3 case B = 3.0 case Bminus = 2.7 case Cplus = 2.3 case C = 2.0 case Cminus = 1.7 case Dplus = 1.3 case D = 1.0 case Dminus = 0.7 case F = 0 } func calcGPA(_ termGrades: [Grade]) -> Double? { if termGrades.count == 0 { return nil } else { let total = termGrades.reduce(0.0) { (total, grade) in total + grade.rawValue } return total / Double(termGrades.count) } } // example calls calcGPA([.A, .B]) // 3.5 calcGPA([.B, .Bplus, .Aminus]) // 3.3333333333 calcGPA([.A, .A, .Bplus]) // 3.7666666666 calcGPA([.F, .F, .F]) // 0 calcGPA([]) // nil
AVAudioRecorder through accelerate FFT into frequency - EXECUTION
My main goal: find the frequency of the noises being pulled in through AVAudioRecorder. I have followed this: http://www.ehow.com/how_12224909_detect-blow-mic-xcode.html I have read up on many questions on SO asking how to detect frequency. The majority of those answers say, "Use FFT!" and then the question ask-ers say, "Oh, great!". My question is, how do you get from here: - (void)levelTimerCallback { [recorder updateMeters]; const double ALPHA = 0.05; double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0])); lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults; if (lowPassResults > sensitivitySlider.value) { NSLog(#"Sound detected"); //What goes here so I can spit out a frequency? } } Somehow magically use FFT... (I will use accelerate.h), And wind up with "The frequency = 450.3"? If somebody could show me the actual code that I would use to Plug the sound from the AVAudioRecorder into Accelerate and How to turn the result into a frequency... That would be greatly appreciated. Thanks in advance.
Nothing "goes there", as the AVRecorder API does not plug into the Accelerate framework. Instead, you have to use a completely different API, the Audio Queue or RemoteIO Audio Unit API, to capture audio input, a completely different code arrangement, such as waiting for callbacks to get your data, buffer size management to get data arrays of the appropriate size to feed an FFT, then know enough DSP to post-process the FFT results for the particular kind of frequency measure for which you are looking.
Well, it turns out that something CAN "go there". Instead of using Accelerate, I bought a book on Fourier Analysis on Amazon and used it to build my own FFT. Which spits out not a single frequency but the levels of each of many frequencies, which is basically what I wanted. Here's my FFT-computing class: class FFTComputer: NSObject { class func integerBitReverse(_ input:Int,binaryDigits:Int) -> Int { return integerForReversedBooleans(booleansForInt(input, binaryDigits: binaryDigits)) } class func integerForReversedBooleans(_ booleans:[Bool]) -> Int { var integer = 0 var digit = booleans.count - 1 while digit >= 0 { if booleans[digit] == true { integer += Int(pow(Double(2), Double(digit))) } digit -= 1 } return integer } class func Pnumber(_ k:Int,placesToMove:Int, gamma:Int) -> Int { var booleans = booleansForInt(k, binaryDigits: gamma) for _ in 0 ..< placesToMove { booleans.removeLast() booleans.insert(false, at: 0) } return integerForReversedBooleans(booleans) } class func booleansForInt(_ input:Int,binaryDigits:Int) -> [Bool] { var booleans = [Bool]() var remainingInput = input var digit = binaryDigits - 1 while digit >= 0 { let potential = Int(pow(Double(2), Double(digit))) if potential > remainingInput { booleans.append(false) } else { booleans.append(true) remainingInput -= potential } digit += -1 } return booleans } class func fftOfTwoRealFunctions(_ realX1:[CGFloat], realX2:[CGFloat], gamma:Int) -> (([CGFloat],[CGFloat]),([CGFloat],[CGFloat])) { let theFFT = fft(realX1, imaginaryXin: realX2, gamma: gamma) var R = theFFT.0 var I = theFFT.1 let N = Int(pow(2.0, Double(gamma))) var realOut1 = [CGFloat]() var imagOut1 = [CGFloat]() var realOut2 = [CGFloat]() var imagOut2 = [CGFloat]() for n in 0..<N { var Rback:CGFloat var Iback:CGFloat if n == 0 { Rback = R[0] Iback = I[0] } else { Rback = R[N-n] Iback = I[N-n] } realOut1.append(CGFloat(R[n]/2 + Rback/2)) realOut2.append(CGFloat(I[n]/2 + Iback/2)) imagOut1.append(CGFloat(I[n]/2 - Iback/2)) imagOut2.append(-CGFloat(R[n]/2 - Rback/2)) } return ((realOut1,imagOut1),(realOut2,imagOut2)) } class func fft(_ realXin:[CGFloat], imaginaryXin:[CGFloat], gamma:Int) -> ([CGFloat],[CGFloat]) { var realX = realXin var imaginaryX = imaginaryXin let N = Int(pow(2.0, Double(gamma))) var N2 = N/2 var NU1 = gamma - 1 // Always equals (gamma - l) var realWP:Double = 1 var imaginaryWP:Double = 0 var redoPCounter = 0 func redoP(_ k:Int, places:Int) { let P = Pnumber(k, placesToMove:places, gamma: gamma) let inside = (-2*Double.pi*Double(P))/Double(N) realWP = cos(inside) imaginaryWP = sin(inside) } var l = 1 while l <= gamma { var k = 0 var I = 1 while k < N - 1 { if redoPCounter == N2 { redoP(k,places: NU1) redoPCounter = 0 } redoPCounter += 1 // Swift.print(realX.count,imaginaryX.count,k+N2) let realT1 = (realWP*Double(realX[k + N2]))-(imaginaryWP*Double(imaginaryX[k + N2])) let imaginaryT1 = (realWP*Double(imaginaryX[k + N2]))+(imaginaryWP*Double(realX[k + N2])) realX[k+N2] = realX[k] - CGFloat(realT1) imaginaryX[k+N2] = imaginaryX[k] - CGFloat(imaginaryT1) realX[k] = realX[k] + CGFloat(realT1) imaginaryX[k] = imaginaryX[k] + CGFloat(imaginaryT1) k += 1 if I == N2 { k += N2 I = 1 } else { I += 1 } } N2 = N2/2 NU1 = NU1 - 1 redoPCounter = 0 realWP = 1 imaginaryWP = 0 l += 1 } for k in 0 ..< N - 1 { let i = integerBitReverse(k, binaryDigits:gamma) if i > k { let placeholderReal = realX[k] let placeholderImaginary = imaginaryX[k] realX[k] = realX[i] imaginaryX[k] = imaginaryX[i] realX[i] = placeholderReal imaginaryX[i] = placeholderImaginary } } return (realX,imaginaryX) } class func magnitudeAndPhasePresentations(_ realX:[CGFloat], imaginaryX:[CGFloat]) -> ([CGFloat],[CGFloat]) { var magnitudes = [CGFloat]() var phases = [CGFloat]() var lastMagnitude:CGFloat = 0 var lastPhase:CGFloat = 0 for n in 0 ..< realX.count { let real = realX[n] let imaginary = imaginaryX[n] if real != 0 { lastMagnitude = sqrt(pow(real, 2)+pow(imaginary, 2)) lastPhase = atan(imaginary/real) } magnitudes.append(lastMagnitude) phases.append(lastPhase) } return (magnitudes,phases) } class func magnitudePresentation(_ realX:[CGFloat], imaginaryX:[CGFloat]) -> [CGFloat] { var magnitudes = [CGFloat]() var lastMagnitude:CGFloat = 0 for n in 0 ..< realX.count { let real = realX[n] let imaginary = imaginaryX[n] if real != 0 { lastMagnitude = sqrt(pow(real, 2)+pow(imaginary, 2)) } magnitudes.append(lastMagnitude) } return magnitudes } } And to get the audio, I used Novocaine: https://github.com/alexbw/novocaine I would recommend reading a bit about the Fourier Transform, but it really doesn't have to be that difficult to plug data from Novocaine (the mic) into an FFTComputer and get back some frequencies. (2 to the gamma is the count of realXin. I could have just computed gamma, so if you want to change that, go ahead. Just turn the Novocaine data into an array of CGFloats, put that in realXin, put an empty array of the same size in imagXin, and enter the right gamma. Then, maybe graph the output to see the frequencies.)