Skip to content

Instantly share code, notes, and snippets.

@raymak
Last active October 30, 2019 01:55
Show Gist options
  • Save raymak/598bc02a88c74df1ec31f2c3bef1c2d5 to your computer and use it in GitHub Desktop.
Save raymak/598bc02a88c74df1ec31f2c3bef1c2d5 to your computer and use it in GitHub Desktop.

Problem 1

Write a function that takes two Integer parameters as input, and determines if the first integer is divisible by the second integer. Name the function isDivisible and name the parameters a and b. The function returns a Bool value: it returns true if the first number is divisible by the second number and returns false otherwise. Hint: use the remainder operator (%) to determine if an integer is divisible by another integer.

Problem 2

Write a function that takes a String (call it word) as input and prints it if it is shorter than 5 characters. It should not print anything if the String is 5 or more characters. The function is called printShortString and has no output. Hint: you can get the length of a String by using .count (similar to an Array), for instance, "hello".count gives 5

Problem 3

Write a function that takes three names (as Strings, call them name1, name2, name3) and an Integer (call it number) between 1 and 3. It then prints the sentence "[name1] and [name2] and [name3] are friends" (replaced by the actual values of the names) if number is equal to 3. It prints "[name1] and [name2] are friends" if number is equal to 2. And prints "[name1] has no friends" if number is equal to 1. The function header is written for you below.

func friends(_ name1: String, _ name2: String, _ name3: String, _ number: Int) {
  // your code
}

Example usage of the function:

friends("Jason", "Sarah", "Bob", 2)
Output

Jason and Sarah are friends

Problem 4

This is not really a problem. You are just going to use a function that's been written by someone else (and you don't even know how it works) and use it in your program. The function implementation is below. Copy this into your playground.

import AVFoundation
import PlaygroundSupport
import UIKit

func playVideo(_ urlstring: String){
   
    let url: URL = URL(string: urlstring)!
    
    URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
    let width = 568
    let height = 320

    let container = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))

    PlaygroundPage.current.liveView = container

    PlaygroundPage.current.needsIndefiniteExecution = true

    
    let f=CGRect(x: 0, y: 0, width: width, height: height)
    let playerItem = AVPlayerItem(url: url)

    let player=AVPlayer(playerItem: playerItem)
    let playerLayer=AVPlayerLayer(player: player)

    playerLayer.frame=f
    container.layer.addSublayer(playerLayer)
    PlaygroundPage.current.liveView = container

    player.play()
}

When dealing with a new function, you have to answer the following questions. Answer them for the function that is given to you.
a. What is the name of the function?
b. What are the input parameters of the function? What types do they have?
c. Does the function have an output? If yes, what type is it?

Now you should use the function. This function gets a url to a video file and plays that video. Go to this page and get the link for one of the videos by right clicking its "Click" link and then selecting "Copy link location". You can then paste that link in your playground and call the playVideo function using with that url as the input. Then wait for a few seconds and watch what happens!

Problem 5

In this problem, we want to write and use a series of functions that work with prime numbers. Recall that a prime number is a positive integer that is divisible only by 1 and itself (e.g. 2, 7, 29).

a. The isPrime function returns true or false if its input number is prime or not respectively. It utilizes the isDivisible function that you wrote earlier, so make sure that isDivisible is defined in the same playground. You are welcome to read and understand how this function works, but you don't have to fully understand it: given the integer n, it counts all the numbers that n is divisible by. If the count is two, this means that the number is prime (because it's only divisible by 1 and n), if it is larger that two, it means that the number is not prime.

func isPrime(_ n: Int) -> Bool {
  var count = 0
  for i in 1...n {
    if isDivisible(n, i) {
      count = count + 1
     }
   }
   
   if count == 2 {
    return true
   }
   
   return false
}

Copy this function into your playground and use it to determine which of the numbers 29981, 12552, 1783 are prime.

b. Write a function that takes an Array of Integers as input and prints the elements that are prime. It should use the isPrime function defined above to determine whether a specific element is prime. Name the function printPrimes. The function does not have an output.

Example usage of printPrimes

printPrimes([2, 7, 8, 9, 13])

Output
2
7
13

c. The following program prints all the prime numbers from 2 to 1000. Notice that it uses the isPrime function.

for number in 2...1000 {
  if isPrime(number) {
    print("\(number) is prime")
   }
}

Write a function that prints all the prime numbers from 2 to n (n is the input of the function). The function header is written for you below.

func printPrimes2toN(_ n: Int) {
  // your code
}

Example usage of printPrimes2toN

printPrimes2toN(19)

Output
2
3
5
7
11
13
17
19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment