Codegiz

Explore what Copilot can do for us.
Show us your discoveries on using the Copilot code assistant.
Swift: Merging of 2 integer arrays
By somaria
2022-11-26
import Cocoa /* Create an array of 2 negative integers */ var negativeIntegers = [-1, -2] /* print the array */ print(negativeIntegers) /* Create an array of 2 positive integers */ var positiveIntegers = [1, 2] /* print the array */ print(positiveIntegers) /* Merge the two arrays */ var allIntegers = negativeIntegers + positiveIntegers /* print the array */ print(allIntegers)

Output

[-1, -2]

[1, 2]

[-1, -2, 1, 2]

Swift: Add 1 to each element of an integer array
By somaria
2022-11-13
import Cocoa /* Create an array of 5 integers */ var myArray = [1, 2, 3, 4, 5] /* print the array */ print(myArray) /* using closure, add 1 to each element of the array */ myArray = myArray.map({$0 + 1}) /* print the array */ print(myArray)

Output

[1, 2, 3, 4, 5]

[2, 3, 4, 5, 6]

Swift: Add 2 numbers using function or closure
By somaria
2022-11-11
import Cocoa /* Swift: Add 2 numbers using function */ func addNumbers(num1: Int, num2: Int) -> Int { return num1 + num2 } print(addNumbers(num1: 1, num2: 2)) /* Swift: Add 2 numbers using closure */ let addNumbersClosure = { (num1: Int, num2: Int) -> Int in return num1 + num2 } print(addNumbersClosure(1, 2))

Output

3

3

Swift: Function vs Closure
By somaria
2022-11-08
import Cocoa /* Create a function that takes a string and returns a string. */ func sayHello(name: String) -> String { return "Hello \(name)!" } /* print the result of calling sayHello with "Cocoa" as the argument. */ print(sayHello(name: "Cocoa")) /* Create a closure that takes a string and returns a string. */ let sayHelloClosure = { (name: String) -> String in return "Hello \(name)!" } /* print the result of calling sayHelloClosure with "Cocoa" as the argument. */ print(sayHelloClosure("Closure"))

Output

Hello Cocoa!

Hello Closure!

Swift: Sum an array of integers using reduce
By somaria
2022-11-08
import Cocoa /* Create an array of 5 integers */ var myArray = [1, 2, 3, 4, 5] /* Using reduce, sum the values in the array */ var sum = myArray.reduce(0, +) /* Print the sum */ print(sum)

Output

15

Swift: How many friday the 13th in the year 2023?
By somaria
2022-11-03
import Cocoa /* How many friday the 13th in the year 2023? */ let year = 2023 let calendar = Calendar.current let dateComponents = DateComponents(year: year, month: 1, day: 1) let date = calendar.date(from: dateComponents)! let range = calendar.range(of: .month, in: .year, for: date)! let months = range.count var friday13th = 0 for month in 1...months { let dateComponents = DateComponents(year: year, month: month, day: 13) let date = calendar.date(from: dateComponents)! let weekday = calendar.component(.weekday, from: date) if weekday == 6 { friday13th += 1 } } print(friday13th)

Output

2

Swift: How many days are there in the month of December 2023?
By somaria
2022-11-02
import Cocoa /* How many days are there in the month of December 2023? */ let calendar = Calendar.current let dateComponents = DateComponents(calendar: calendar, year: 2023, month: 12) let date = calendar.date(from: dateComponents)! let range = calendar.range(of: .day, in: .month, for: date)! let days = range.count print("There are \(days) days in December 2023")

Output

There are 31 days in December 2023

Swift: Create a random list of 5 even numbers between 0 and 100
By somaria
2022-11-01
import Cocoa /* Create a random list of 5 even numbers between 0 and 100. */ var evenNumbers = [Int]() for _ in 1...5 { var randomNumber = Int(arc4random_uniform(50)) randomNumber *= 2 evenNumbers.append(randomNumber) } print(evenNumbers)

Output

[72, 82, 64, 84, 52]

Swift: Capitalize and de-capitalize a list
By somaria
2022-10-28
import Cocoa /* Create a 3 letter list of the first 6 months of the year */ let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] /* de-capitalize the list of months */ let lowerMonths = months.map { $0.lowercased() } /* print the list of months */ print(lowerMonths) /* Capitalize the first letter of the list of months */ let capitalizedMonths = months.map { $0.capitalized } /* print the list of months */ print(capitalizedMonths)

Output

["jan", "feb", "mar", "apr", "may", "jun"]

["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

Swift: Is 2023 a leap year?
By somaria
2022-10-28
import Cocoa //Is 2023 a leap year? let isLeapYear = 2023 % 4 == 0 && (2023 % 100 != 0 || 2023 % 400 == 0) if isLeapYear { print("2023 is a leap year") } else { print("2023 is not a leap year") }

Output

2023 is not a leap year

Challenges Suggested By Our Community
Scrape a tweet from Twitter
By somaria2022-10-10

Scrape a tweet from Twitter and put the data in Json format.

Get the exact day of 2023-01-01
By somaria2022-10-08

Get the exact day of 2023-01-01

For example, return Tuesday if it is Tuesday.