iOS Swift Development: 6KYU SWift Codewars Question

iOS Swift Development: 6KYU SWift Codewars Question

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

6KYU - Which are in? https://www.codewars.com/kata/which-are-in/train/swift

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (5)

Section 1

(5 cards)

6KYU - Which are in? https://www.codewars.com/kata/which-are-in/train/swift

Front

func inArray(_ a1: [String], _ a2: [String]) -> [String] { var array: [String] = [] //see if word exists within word for string in a1 { for string2 in a2 { if string2.range(of:string) != nil { array.append(string) } } } //make array unique, remove duplicates let unique = Array(Set(array)) //sort in alphabetical order let sortedArray = unique.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending } //return answer return sortedArray } print(inArray(["tarp", "mice", "bull"], ["lively", "alive", "harp", "sharp", "armstrong"])) OR func inArray(_ a1: [String], _ a2: [String]) -> [String] { return Set(a1.filter { s1 in a2.contains { $0.contains(s1) } }).sorted() }

Back

Prime Number: Shuffle Method

Front

import UIKit func takeInPrime (prime: Int) -> Int { var aP: [String] = [] var listOfShuffledNumbers: [String] = [] var uniqueArray: [String] = [] var count = 0 let takeInPrime = String(prime) for value in takeInPrime { aP.append(String(value)) } while uniqueArray.count != 40320 { while count <= 50 { listOfShuffledNumbers.append(aP.shuffled().joined()) count += 1 } uniqueArray += Array(Set(listOfShuffledNumbers)) uniqueArray = Array(Set(uniqueArray)) count = 0 listOfShuffledNumbers.removeAll() } var lol = uniqueArray.filter{Int($0)! % 2 == 1} print(lol) return lol.count } print(takeInPrime(prime: 12345678))

Back

Check to see if a number is a prime, the long way

Front

func isPrime(_ number: Int) -> Bool { return number > 1 && !(2..<number).contains { number % $0 == 0 } }

Back

6KYU 1RM Calculator https://www.codewars.com/kata/1rm-calculator/train/swift

Front

func calculate1RM(weight: Int, reps: Int) -> Int? { if reps == 0 && weight == 0 { } var answer = 0 let weightDouble = Double(weight) let repsDouble = Double(reps) var equationAnswers = [Double]() if reps == 1 { answer = weight } if reps > 1 && weight > 0 { let equation1: Double = weightDouble*((repsDouble/30) + 1) let equation2: Double = (100 weightDouble) / (101.3 - (2.67123 repsDouble)) let equation3: Double = (weightDouble * pow(repsDouble, 0.1)) equationAnswers.append(equation1) equationAnswers.append(equation2) equationAnswers.append(equation3) let maxvalue = equationAnswers.max()! answer = Int(maxvalue) } return answer } let my1RepMax = calculate1RM(weight: 265, reps: 2) print(my1RepMax!)

Back

6KYU Are they the "same"? https://www.codewars.com/kata/are-they-the-same/train/swift

Front

func comp(_ a: [Int], _ b: [Int]) -> Bool { var answer: Bool var newB = [Int]() var aIntSet: Set<Int> = Set<Int>(a) //square root for number in b { newB.append(Int(pow(Double(number), 0.5))) } //convert to set var bIntSet: Set<Int> = Set<Int>(newB) print(aIntSet) print(bIntSet) //test answer = bIntSet.isSubset(of: aIntSet) return answer } OR USING REDUCE func comp(_ a: [Int], _ b: [Int]) -> Bool { //square root var newB = [Int]() for number in b { newB.append(Int(pow(Double(number), 0.5))) } //reduce //must set variable to a reduce, hence created answerA and answerB var answerA = a.reduce(0, {sum, number in sum + number}) print(answerA) var answerB = newB.reduce(0, {sum, number in sum + number}) print(answerB) //return return answerA == answerB ? true : false }

Back