-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HomeWork 4 #5
Open
abercromb
wants to merge
4
commits into
master
Choose a base branch
from
lesson4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HomeWork 4 #5
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// main.swift | ||
// lesson2 | ||
// | ||
// Created by Vladislav Elkin on 15.07.2020. | ||
// Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
//1.Написать функцию, которая определяет, четное число или нет. | ||
|
||
func isEven(_ number: Int) -> Bool { | ||
return number % 2 == 0 ? true : false; | ||
} | ||
|
||
print(isEven(10)); | ||
print(isEven(5)); | ||
print("\r\n===============================\r\n") | ||
|
||
//2. Написать функцию, которая определяет, делится ли число без остатка на 3. | ||
|
||
func function2(_ number: Int) -> Bool{ | ||
return number % 3 == 0 ? true : false; | ||
} | ||
|
||
print(function2(9)); | ||
print(function2(7)); | ||
print("\r\n===============================\r\n") | ||
|
||
//3. Создать возрастающий массив из 100 чисел. | ||
|
||
var tArray: [Int] = []; | ||
for i in 0...100 { | ||
tArray.append(i) | ||
} | ||
|
||
print(tArray) | ||
print("\r\n===============================\r\n") | ||
|
||
//4. Удалить из этого массива все четные числа и все числа, которые не делятся на 3. | ||
|
||
for(index, value) in tArray.enumerated(){ | ||
if isEven(value) || !function2(value) { | ||
tArray.remove(at: tArray.firstIndex(of: value)!); | ||
} | ||
} | ||
|
||
print(tArray) | ||
|
||
print("\r\n===============================\r\n") | ||
|
||
//5. * Написать функцию, которая добавляет в массив новое число Фибоначчи, и добавить при помощи нее 100 элементов. | ||
|
||
func fibonacci(_ n: Int64) -> Int64{ | ||
if n == 1 || n == 2 { | ||
return 1; | ||
} | ||
|
||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
} | ||
|
||
var fibonacciArray: [Int64] = []; | ||
|
||
for i in 1...101{ | ||
fibonacciArray.append(fibonacci(Int64(i))); | ||
} | ||
|
||
print(fibonacciArray); | ||
|
||
print("\r\n===============================\r\n") | ||
|
||
/* | ||
6. * Заполнить массив из 100 элементов различными простыми числами. Натуральное число, большее единицы, называется простым, если оно делится только на себя и на единицу. Для нахождения всех простых чисел не больше заданного числа n, следуя методу Эратосфена, нужно выполнить следующие шаги: | ||
*/ | ||
|
||
func isPrime (_ number: Int64) -> Bool { | ||
if number < 2 { | ||
return false; | ||
} | ||
for i in 2..<number { | ||
if number % i == 0 { | ||
return false; | ||
} | ||
} | ||
|
||
return true | ||
} | ||
func getPrimeArray (_ count: Int64) -> [Int64] { | ||
var resultArray: [Int64] = []; | ||
var i = 2 | ||
while resultArray.count < count { | ||
if isPrime(Int64(i)) { | ||
resultArray.append(Int64(i)); | ||
} | ||
i += 1; | ||
} | ||
|
||
return resultArray; | ||
} | ||
print (getPrimeArray(100)); | ||
|
||
print("\r\n===============================\r\n") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// | ||
// main.swift | ||
// lesson3 | ||
// | ||
// Created by Vladislav Elkin on 21.07.2020. | ||
// Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum DoorState{ | ||
case open, close; | ||
} | ||
|
||
enum EngineState{ | ||
case start, stop; | ||
} | ||
|
||
enum VolumeState{ | ||
case full, empty; | ||
} | ||
|
||
enum VolumeAction { | ||
case add, get; | ||
} | ||
|
||
struct sportCar { | ||
let brand: String; | ||
let year: Int; | ||
let trunkVolume: Double; | ||
var engineState: EngineState; | ||
var doorState: DoorState; | ||
var volumeState: VolumeState; | ||
|
||
mutating func changeDoorState (state: DoorState){ | ||
self.doorState = state; | ||
} | ||
|
||
mutating func changeEngineState(state: EngineState){ | ||
self.engineState = state; | ||
} | ||
|
||
mutating func changeVolume(action: VolumeAction, count: Double){ | ||
switch action { | ||
case .add: | ||
|
||
if volumeState == .full { | ||
print("Trunk is full"); | ||
} else { | ||
if count >= trunkVolume { | ||
volumeState = .full; | ||
print("All added, Trunk is full"); | ||
}else { | ||
print("Added: \(count)"); | ||
} | ||
} | ||
|
||
break; | ||
|
||
case .get: | ||
|
||
if volumeState == .empty { | ||
print("Trunk is empty"); | ||
} else { | ||
|
||
if count >= trunkVolume { | ||
volumeState = .empty; | ||
print("Trunk is empty"); | ||
}else { | ||
print("Get: \(count)"); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
struct truckCar { | ||
let brand: String; | ||
let year: Int; | ||
let trunkVolume: Double; | ||
var engineState: EngineState; | ||
var doorState: DoorState; | ||
var volumeState: VolumeState; | ||
|
||
mutating func changeDoorState (state: DoorState){ | ||
self.doorState = state; | ||
} | ||
|
||
mutating func changeEngineState(state: EngineState){ | ||
self.engineState = state; | ||
} | ||
|
||
mutating func changeVolume(action: VolumeAction, count: Double){ | ||
switch action { | ||
case .add: | ||
|
||
if volumeState == .full { | ||
print("Trunk is full"); | ||
} else { | ||
if count >= trunkVolume { | ||
volumeState = .full; | ||
print("All added, Trunk is full"); | ||
}else { | ||
print("Added: \(count)"); | ||
} | ||
} | ||
|
||
break; | ||
|
||
case .get: | ||
|
||
if volumeState == .empty { | ||
print("Trunk is empty"); | ||
} else { | ||
|
||
if count >= trunkVolume { | ||
volumeState = .empty; | ||
print("Trunk is empty"); | ||
}else { | ||
print("Get: \(count)"); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
var mersedes = sportCar(brand: "mersedes", year: 2010, trunkVolume: 200, engineState: .stop, doorState: .close, volumeState: .empty); | ||
|
||
mersedes.changeDoorState(state: .open); | ||
mersedes.changeVolume(action: .add, count: 200); | ||
|
||
print(mersedes); | ||
|
||
|
||
var scania = truckCar(brand: "Scania", year: 2009, trunkVolume: 1000, engineState: .stop, doorState: .open, volumeState: .full); | ||
|
||
scania.changeDoorState(state: .close); | ||
scania.changeEngineState(state: .start); | ||
|
||
print(scania); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// | ||
// main.swift | ||
// lesson4 | ||
// | ||
// Created by Vladislav Elkin on 23.07.2020. | ||
// Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum DoorState{ | ||
case open, close; | ||
} | ||
|
||
enum EngineState{ | ||
case start, stop; | ||
} | ||
|
||
enum VolumeState{ | ||
case full, empty; | ||
} | ||
|
||
enum VolumeAction { | ||
case add, get; | ||
} | ||
|
||
class AbstractCar { | ||
let brand: String; | ||
let year: Int; | ||
let trunkVolume: Double; | ||
var engineState: EngineState; | ||
var doorState: DoorState; | ||
var volumeState: VolumeState; | ||
|
||
init(brand: String, year: Int, trunkVolume: Double, engineState: EngineState, doorState: DoorState, volumeState: VolumeState) { | ||
self.brand = brand; | ||
self.year = year; | ||
self.trunkVolume = trunkVolume; | ||
self.engineState = engineState; | ||
self.doorState = doorState; | ||
self.volumeState = volumeState; | ||
} | ||
|
||
func changeDoorState (state: DoorState){ | ||
self.doorState = state; | ||
} | ||
|
||
func changeEngineState(state: EngineState){ | ||
self.engineState = state; | ||
} | ||
|
||
func changeVolume(action: VolumeAction, count: Double){ | ||
switch action { | ||
case .add: | ||
|
||
if volumeState == .full { | ||
print("Trunk is full"); | ||
} else { | ||
if count >= trunkVolume { | ||
volumeState = .full; | ||
print("All added, Trunk is full"); | ||
}else { | ||
print("Added: \(count)"); | ||
} | ||
} | ||
|
||
|
||
case .get: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. называешь фукнцию |
||
|
||
if volumeState == .empty { | ||
print("Trunk is empty"); | ||
} else { | ||
|
||
if count >= trunkVolume { | ||
volumeState = .empty; | ||
print("Trunk is empty"); | ||
}else { | ||
print("Get: \(count)"); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
func toString(){ | ||
print("Brand: \(brand) Year: \(year) TrunkVolume: \(trunkVolume) EngineState: \(engineState) DoorState: \(doorState) VolumeState: \(volumeState)") | ||
} | ||
} | ||
|
||
|
||
class TrunkCar: AbstractCar { | ||
var destination: String; | ||
|
||
init(brand: String, year: Int, trunkVolume: Double, engineState: EngineState, doorState: DoorState, volumeState: VolumeState, destination: String) { | ||
self.destination = destination; | ||
super.init(brand: brand, year: year, trunkVolume: trunkVolume, engineState: engineState, doorState: doorState, volumeState: volumeState) | ||
} | ||
|
||
override func toString() { | ||
super.toString() | ||
print("Destination: \(destination)") | ||
} | ||
} | ||
|
||
class SportCar: AbstractCar { | ||
var maxSpeed: Int; | ||
var power: Int; | ||
|
||
|
||
init(brand: String, year: Int, trunkVolume: Double, engineState: EngineState, doorState: DoorState, volumeState: VolumeState, maxSpeed: Int, power: Int) { | ||
self.maxSpeed = maxSpeed; | ||
self.power = power; | ||
super.init(brand: brand, year: year, trunkVolume: trunkVolume, engineState: engineState, doorState: doorState, volumeState: volumeState) | ||
|
||
} | ||
|
||
override func toString() { | ||
super.toString() | ||
print("MaxSpeed: \(maxSpeed) Power: \(power)") | ||
} | ||
|
||
} | ||
|
||
var mers = SportCar(brand: "mersede", year: 2019, trunkVolume: 200, engineState: .stop, doorState: .close, volumeState: .full, maxSpeed: 320, power: 612) | ||
|
||
mers.changeDoorState(state: .open); | ||
mers.changeEngineState(state: .start); | ||
mers.toString() | ||
|
||
var scania = TrunkCar(brand: "Scania", year: 2011, trunkVolume: 1000, engineState: .start, doorState: .open, volumeState: .empty, destination: "Moscow") | ||
|
||
scania.changeDoorState(state: .close); | ||
scania.toString() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
старайся не делать if внутри switch (это плохо читается). Все можно сделать switc-ом