Swift Code Challenges
Approaching a Code Challenge
Before you hop into a repl.it file and start writing code to solve any of these challenges, make sure to take the proper steps to pseudo-code and plan!
Link psuedo-coding materials here
Mild Challenges
Sum of Two
Declare a function that prints the sum of the two Integers that are passed in as arguments. For example, when 14
and 9
, the output should be 25
.
Check Word Length
Declare a function that takes one argument, a String. If the string is 8 characters or longer, print out Valid password length!
. If it is shorter, print out Try again, must be at least 8 characters
.
Medium Challenges
Even or Odd?
Write a function that takes an Integer as an argument and returns "even" for even numbers or "odd" for odd numbers.
Sum of Two
Declare a function that prints the sum of the two Integers that are passed in as arguments, but only if both Integers are even. If they are not both even, print out Oops, try again
. For example, when 14
and 9
are passed in, the output should be Oops, try again
. When 10
and 8
are passed in, the output should be 18
.
Need a Hint? You can read about the Logical AND Operator here.
Sound It Out
Declare a function that takes in a String as an argument. Return a String with dash’-’marks in between each letter in the String. For example soundItOut("Hello") -> "H-e-l-l-o-"
.
Once you've done that, level up! Modify your code so that the last letter does not have a dash trailing after it. For example soundItOut("Hello") -> "H-e-l-l-o"
.
Fizz Buzz
Write a short program that takes in an Array of Integers. For each multiple of 3, print "Fizz". For each multiple of 5, print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". If a number is not a multiple of 3 or 5, print the number itself.
Spicy Challenges
Super Fizz Buzz
Write a short program that prints each number from 1 to 100 on a new line. For each multiple of 3, print "Fizz" instead of the number. For each multiple of 5, print "Buzz" instead of the number. For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.
Add Up
Write a function that takes a number as an argument. Add up all the numbers from 1 to the number that was passed in, and return the sum. For example, if your argument is 4, your function should return 10 because 1 + 2 + 3 + 4 = 10
.
Dashitize
Declare a function that takes in an Integer as an argument. Return a String with dash’-’marks before and after each odd Integer, but do not begin or end the string with a dash mark. Example: dashitize(274) -> 2-7-4
dashitize(6815) -> 68-1-5
Sum of Two
Declare a function that returns the sum of the two lowest positive numbers given an array of minimum 4 integers. No empty arrays will be passed in. For example, when an array is passed like [19, 5, 42, 2, 77]
, the output should be 7
.
Scrabble
Have you ever played Scrabble or Words with Friends? Each player gets a set of tiles, with each tile having one letter printed on it. Using the tiles you have, your task is build words. Each letter has a point value (common letters are lower in value, less commonly used letter are worth a lot!) The player who has the most points wins.
Your task today is to write a program that will calculate the score of any given word. Your function should take one argument (a word) and return an integer (the score of that word).
Use this dictionary to access the scores for each letter:
let letterScores = [ "A": 1, "B": 3, "C": 3, "D": 2, "E": 1, "F": 4, "G": 2, "H": 4, "I": 1, "J": 8, "K": 5, "L": 1, "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, "Y": 4, "Z": 10 ]
Trolls
Trolls are attacking your comment section! A common way to deal with this situation is to remove all of the vowels from the trolls’ comments, neutralizing the threat. Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string “This website is for losers LOL!” would become “Ths wbst s fr lsrs LL!“. Note: for this exercise, y isn’t considered a vowel.