Given an arbitrary array of integers, print all positive elements of the array.
var numbers: [Int] = [2, -4, 5, 0, 8]
Write a program that given two arbitrary arrays of String
s (that have the same size) counts the number of items that are in the same position and equal in both arrays. See the example input/output pairs below.
var arr1 = ["apple", "pear", "peach", "cucumber"]
var arr2 = ["banana", "pear", "lettuce", "cucumber"]
Write a program that takes an array of String
s and removes the empty strings. To remove an element from an array using the index of the element you want to remove, you can use the following method:
var arr = ["good morning", "good afternoon", "good night"]
print(arr) // ["good morning", "good afternoon", "good night"]
arr.remove(at: 1) // removes the element with the index 1, which is "good afternoon" in this case
print(arr) // ["good morning", "good night"]
The following code defines two arrays of laptop models and their respective costs. Assume that the array is arbitrary and can be different each time we run the program (your program should work for any array).
var models: [String] = ["acer-aspire-5", "lenovo-ideapad", "acer-chromebook", "asus", "hp-touchscreen"]
var costs: [Double] = [499.98, 599.99, 269.99, 369.99, 799.99]
a. Print the length of the array and print the array is empty if the length is 0. b. Print the first and the last models in the array with their costs
# ggplot2 | |
# based on grammar of graphics | |
# layers | |
# data -> aesthetics (scales we should map our data onto) -> geometries -> (the remaining are optional) facets -> statistics (e.g. linear model) -> coordinates (dimensions of the plot) -> theme (data-independent) | |
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() | |
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() + geom_smooth() | |
+ geom_smooth(se = FALSE) | |
+ geom_point(alpha = 0.2) |
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.
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
Write a function that takes three names (as String
s, call them name1
, name2
, name3
) and
// Language Guide: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html | |
// :)))) Swift Basics | |
// :)) Basic Characteristics | |
// type-safe: helps to make it clear what types of values the code is working with | |
// has advanced types like tuples that are not available in Objective-C | |
// has optionals | |
// compiled language | |
// "It’s an industrial-quality programming language that’s as expressive and enjoyable as a scripting language." |
// example 1 | |
var a: Int = 5 | |
{ | |
a = 10 | |
} | |
print("first: \(a)") |
# this is a comment | |
# emacs reference card: https://web.stanford.edu/class/archive/cs/cs107/cs107.1196/resources/emacs_refcard.pdf | |
# emacs config from CS107: http://web.stanford.edu/class/cs107/resources/sample_emacs | |
# (INFO) Emacs Basics | |
# meta (M)=> opt on Mac and alt on Windows and Linux | |
# emacs f1 f2 opens f1 and f2 side-by-side and lets you copy between them |
Write a program that converts degrees of Farenheit to degrees of Celsius. Assume that the degrees of Farenheit are stored in the Double
variable farenheit
and we want the output in Celcius to be stored in the Double
variable celcius
. You can find the formula to convert Farenheit to Celcius with a Google search.
a. Write a program that takes the age of a person in a Double
variable named age
, as well as the name of a country in a String
variable named country
, and tells if the person can legally drink in that country (if the age is equal to or greater than the legal drinking age). Refer to the following table to get the legal age for drinking in each country. If the country is not one of the five mentioned in this table, the program should tell the user that there is no data available for the specified country.
Country | Age |
---|