-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_practice.swift
178 lines (137 loc) · 7.42 KB
/
functions_practice.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// functions_practice.swift
//
//
// Created by Emily Bailey on 5/24/17.
//
//
// This practice worksheet builds up your knowledge of writing functions.
// You can use https://iswift.org/playground to run the code you write to check that it works. Remember to run ALL of the lines of code that come before your function, in order - you must run the import lind, load your variables, and everything in order!
// Google any questions you have as you work on this! stackoverflow.com is a particularly great site.
// There are some hints at the bottom of this document
// Ask each other for help too, write stuff on the board, talk through where you get stuck.
// Feel free to email me if you get really stuck on anything! emilybailey12@gmail.com or emilyb@uber.com
// And yes - these problems are pretty hard! But I know you guys can do them!
// Overview
// In this workflow, we're thinking about a school and trying to figure out when students go to their classes.
// IMPORTANT: This is a school with 1-on-1 teaching only, so students go to class alone! Only one student per class per period.
// The classArray given here is for period 1. So in period 1, Ashley is in Physics. In period 2, each student goes to the next class - so Ashley is in Math. The last student on the list cycles back up. So in period 1, Hal is in History. In period 2, Hal is in Physics.
// Import necessary module
import Foundation
// Create variables that store data
let classArray = ["Physics", "Math", "English", "CompSci", "Government", "Statistics", "Chemistry", "History"]
let studentArray = ["Ashley", "Bob", "Cat", "Dan", "Eva", "Frank", "George", "Hal"]
let nPeriods: Int! = classArray.count
let nStudents: Int! = studentArray.count
// Check that we have the right number of students and classes (clean data is key!)
nPeriods==nStudents
// 1. Write a function to tell me what class any given student is in, assuming that the ordering of students and classes in the original arrays is current (we did this in class last week)
func findClassForStudent(studentToFind: String) -> String {
var classStudentIsIn: String = ""
for i in 1...studentArray.count {
if studentArray[i-1] == studentToFind {
classStudentIsIn = classArray[i-1]
}
}
return classStudentIsIn
}
// Checkpoint: call function
findClassForStudent(studentToFind: "Ashley")
/* Expected result:
[String] = "Physics"
*/
// You can call the function for as many students as you want to check that it works
// 2. Write a function that switches us to another period, i.e. shifts the array from period 1 to period 1+x (so if you enter shiftPeriods: 1, you are going from period 1 to period 2; if you enter shiftPeriods: 7, you are going from period 1 to period 8)
func shiftClasses(shiftPeriods: Int) -> [String] {
// Write your code in here!
}
// Checkpoint: call function
shiftClasses(shiftPeriods: 1)
/* Expected result:
[String] = 8 values {
[0] = "Math"
[1] = "English"
[2] = "CompSci"
[3] = "Government"
[4] = "Statistics"
[5] = "Chemistry"
[6] = "History"
[7] = "Physics"
}
*/
// 3. Write a function that tells you what class each student is in for a given period (inputs can be 1-8). The output should be a new array of strings saying "Ashley will be in classX", e.g. You will want to use the function you wrote in #2 inside of this function to help you out.
func studentsInClassesForPeriod(period: Int) -> [String] {
// Write your code in here!
}
// Checkpoint: call function
studentsInClassesForPeriod(period: 1)
/* Expected result:
[String] = 8 values {
[0] = "Ashley is going to Physics"
[1] = "Bob is going to Math"
[2] = "Cat is going to English"
[3] = "Dan is going to CompSci"
[4] = "Eva is going to Government"
[5] = "Frank is going to Statistics"
[6] = "George is going to Chemistry"
[7] = "Hal is going to History"
}
*/
// 4(A). Sometimes one student is excepted from a certain class. For instance, maybe Bob just hates Math and has somehow found a way to get out of it. How will the schedule be printed if Bob has a free period instead of going to Math?
func nextPeriodWithException(period: Int, exceptionStudent: String, exceptionClass: String) -> [String] {
// Write your code in here!
}
// Checkpoint: call function
nextPeriodWithException(period: 1, exceptionStudent: "Bob", exceptionClass: "Math")
/* Expected result:
[String] = 8 values {
[0] = "Ashley is going to Physics"
[1] = "Bob has a free period"
[2] = "Cat is going to English"
[3] = "Dan is going to CompSci"
[4] = "Eva is going to Government"
[5] = "Frank is going to Statistics"
[6] = "George is going to Chemistry"
[7] = "Hal is going to History"
}
*/
// 4(B). If Eva doesn't take Physics, when does she have a free period?
// 4(C).I wrote a for loop to answer that question, but it has a bug. Debug this to find the right answer
for i in 1...nPeriods {
var period: String = String(i)
var nextPeriodWithExceptionArray = nextPeriodWithException(period: i-1, exceptionStudent: "Eva", exceptionClass: "Physics")
print("In period " + period + " " + nextPeriodWithExceptionArray[i])
}
// Checkpoint: run for loop
/* Expected result:
In period 1 Please input a period between 1 and 8
In period 2 Bob is going to Math
In period 3 Cat is going to CompSci
In period 4 Dan is going to Statistics
In period 5 Eva is going to History
In period 6 Frank is going to Math
In period 7 George is going to CompSci
In period 8 Hal is going to Statistics
*/
// HINTS
// 2.
// Break this down into steps. First, you know you need to write a loop, because you are using each component of the array.
// Also, remember that your array index starts at 0. So when you write your for loop, it can't just go from 1-8, it needs to somehow give you 0-7:
// you could create a variable called "nPeriodsMinusOne" and set it equal to "nPeriods-1," then your loop could be "for i in 0...nPeriodsMinusOne"
// or your loop can be "for i 1...nPeriods", and then inside the loop you can say i = i-1 or you can just always use i-1 to access array elements instead of just i (classArray[i-1] instead of classArray[i], e.g.)
// Don't forget to use the input (shiftPeriods) - it's there for a reason!
// You will be returning an array, so make sure you create an array that you can return. You can start by creating an empty array like this:
// var myArray: [String] = []
// Then you can add new items to the array using .append("whatever you want to add"). You can read about this more online. It will look like:
// myArray.append("some string") or myArray.append(somevariable)
// Don't forget that if you shift forward by 1, the last class has nowhere to go! So the 8th class has to become the 1st class. How can you use your input variables plus a little bit of math to do this? We did something similar in class last week.
// 3.
// Use your shiftClasses function that you wrote in #2 to figure out who was what class in each period.
// Remember there are only 8 periods, so you can only use 1-8 as period inputs
// The hints from #2 will probably be helpful here too
// 4(A).
// Remember that if a student has an exception for one class, that exception will only show up 1 out of 8 times (i.e. if Bob has a free period instead of Math, he gets a free period during period 1, but he has all of his regular classes for every other period.
// 4(B).
// Call the function you wrote in part A. Does this answer your question? Did you input the right parameters?
// 4(C).
// Is i the correct index?