diff --git a/Codecademy.playground/Pages/Factorial.xcplaygroundpage/Contents.swift b/Codecademy.playground/Pages/Factorial.xcplaygroundpage/Contents.swift index 8fa6150..6e8be50 100644 --- a/Codecademy.playground/Pages/Factorial.xcplaygroundpage/Contents.swift +++ b/Codecademy.playground/Pages/Factorial.xcplaygroundpage/Contents.swift @@ -27,7 +27,7 @@ while num != 0 { } print(result) -// factorial using reppeat while +// factorial using repeat while num = 5 result = 1 diff --git a/Codecademy.playground/Pages/Polindrome.xcplaygroundpage/Contents.swift b/Codecademy.playground/Pages/Polindrome.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..9b73e42 --- /dev/null +++ b/Codecademy.playground/Pages/Polindrome.xcplaygroundpage/Contents.swift @@ -0,0 +1,56 @@ +var text = ["h", "e", "l", "l", "o"] +var reversed = [String]() +var counter = text.count - 1 + +while counter >= 0 { + reversed.append(text[counter]) + counter -= 1 + +} + if text == reversed { + print("We have a palindrome!") + } else { + print("We don’t have a palindrome") + } + +/* + //with for in usage + + var text = ["h", "e", "l", "l", "e", "h"] + var reversed = [String]() + var counter = text.count - 1 + + for x in 0 ..< text.count { + if x >= 0 { + reversed.append(text[counter]) + counter -= 1 + } + } + if text == reversed { + print("We have a palindrome!") + } else { + print("We don’t have a palindrome") + } + + + //with stride usage + + var text = ["h", "e", "l", "l", "e"] + var reversed = [String]() + var counter = text.count - 1 + + for x in stride (from: 0, to: text.count, by: 1){ + if x >= 0 { + reversed.append(text[counter]) + counter -= 1 + } + } + if text == reversed { + print("We have a palindrome!") + } else { + print("We don’t have a palindrome") + } + + + + */