Created
February 29, 2020 10:56
-
-
Save antnruban/2a970a94632a09c121e458fd0df6d173 to your computer and use it in GitHub Desktop.
swift structures HW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============ Structures HW ============ | |
//1. Создайте структуру студент. Добавьте свойства: имя, фамилия, год рождения, средний бал. Создайте несколько экземпляров этой структуры и заполните их данными. Положите их всех в массив (журнал). | |
// | |
//2. Напишите функцию, которая принимает массив студентов и выводит в консоль данные каждого. Перед выводом каждого студента добавляйте порядковый номер в “журнале”, начиная с 1. | |
// | |
//3. С помощью функции sorted отсортируйте массив по среднему баллу, по убыванию и распечатайте “журнал”. | |
// | |
//4. Отсортируйте теперь массив по фамилии (по возрастанию), причем если фамилии одинаковые, а вы сделайте так чтобы такое произошло, то сравниваются по имени. Распечатайте “журнал”. | |
// | |
//5. Создайте переменную и присвойте ей ваш существующий массив. Измените в нем данные всех студентов. Изменится ли первый массив? Распечатайте оба массива. | |
//6. Теперь проделайте все тоже самое, но не для структуры Студент, а для класса. Какой результат в 5м задании? Что изменилось и почему? | |
struct Student { | |
var name: String | |
var surname: String | |
var age: Int | |
var averMark: Int | |
} | |
// Redefined Student structure as a class. | |
//class Student { | |
// var name: String | |
// var surname: String | |
// var age: Int | |
// var averMark: Int | |
// | |
// init(name: String, surname: String, age: Int, averMark: Int) { | |
// self.name = name | |
// self.surname = surname | |
// self.age = age | |
// self.averMark = averMark | |
// } | |
//} | |
var journal = [ | |
Student.init(name: "c", surname: "c", age: 12, averMark: 11), | |
Student.init(name: "d", surname: "c", age: 11, averMark: 12), | |
Student.init(name: "a", surname: "a", age: 15, averMark: 14), | |
] | |
func sortStudentsAndPrintJournal( | |
journal: [Student], | |
printStudent: (Student) -> Void, | |
sortStudent: ([Student]) -> [Student] = { (list: [Student]) -> [Student] in return list } | |
) -> Void { | |
for (index, student) in sortStudent(journal).enumerated() { | |
print("Number: \(index)") | |
printStudent(student) | |
print("============") | |
} | |
} | |
func printStudent(_ student: Student) -> Void { | |
print( | |
""" | |
Name: \(student.name) | |
Surname: \(student.surname) | |
Age: \(String(student.age)) | |
Average Mark: \(String(student.averMark)) | |
""" | |
) | |
} | |
func sortStudentsByMark(_ listToSort: [Student]) -> [Student] { | |
print("=== By Mark ===") | |
var mutatedList = listToSort | |
mutatedList.sort { $0.averMark > $1.averMark } | |
return mutatedList | |
} | |
func sortStudentsByName(_ listToSort: [Student]) -> [Student] { | |
print("=== By Name ===") | |
var mutatedList = listToSort | |
mutatedList.sort { | |
guard $0.surname == $1.surname else { | |
return $0.name < $1.name | |
} | |
return $0.surname < $1.surname | |
} | |
return mutatedList | |
} | |
sortStudentsAndPrintJournal(journal: journal, printStudent: printStudent) | |
sortStudentsAndPrintJournal(journal: journal, printStudent: printStudent, sortStudent: sortStudentsByMark) | |
sortStudentsAndPrintJournal(journal: journal, printStudent: printStudent, sortStudent: sortStudentsByName) | |
var clonedJournal = journal | |
for var student in clonedJournal { | |
student.name = "asd" | |
student.surname = "asd" | |
} | |
print(journal[0].name) | |
print(clonedJournal[0].name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment