From 6f4f273ed83276d7c47ef631d320f9b4a2ddad86 Mon Sep 17 00:00:00 2001 From: arthurwrls Date: Tue, 11 Oct 2022 21:55:24 +0100 Subject: [PATCH] Some changes --- .../Factorial.xcplaygroundpage/Contents.swift | 2 +- .../Contents.swift | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Codecademy.playground/Pages/Polindrome.xcplaygroundpage/Contents.swift 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") + } + + + + */