Skip to content
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

[Swift 6]: Update Exercises batch 21 #808

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#if os(Linux)
import Glibc
import Glibc
#elseif os(OSX)
import Darwin
let random = arc4random
import Darwin
let random = arc4random
#endif

struct Robot {
var name: String
var name: String

init() {
let numberPart = (Int(random()) % 899) + 100
name = generateRandomLetter() + generateRandomLetter() + "\(numberPart)"
}
init() {
let numberPart = (Int(random()) % 899) + 100
name = generateRandomLetter() + generateRandomLetter() + "\(numberPart)"
}

mutating func resetName() {
let numberPart = (Int(random()) % 899) + 100
name = generateRandomLetter() + generateRandomLetter() + "\(numberPart)"
}
mutating func resetName() {
let numberPart = (Int(random()) % 899) + 100
name = generateRandomLetter() + generateRandomLetter() + "\(numberPart)"
}
}

private func convertStringToStringArray(_ input: String) -> [String] {
var characterArray: [String] = []
for character in input {
characterArray.append("\(character)")
}
return characterArray
var characterArray: [String] = []
for character in input {
characterArray.append("\(character)")
}
return characterArray
}

private func generateRandomLetter() -> String {
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let letters = convertStringToStringArray(alphabet)
let randomIndex = Int(random()) % letters.count
return letters[randomIndex]
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let letters = convertStringToStringArray(alphabet)
let randomIndex = Int(random()) % letters.count
return letters[randomIndex]
}
32 changes: 16 additions & 16 deletions exercises/practice/robot-name/Package.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "RobotName",
products: [
.library(
name: "RobotName",
targets: ["RobotName"]),
],
dependencies: [],
targets: [
.target(
name: "RobotName",
dependencies: []),
.testTarget(
name: "RobotNameTests",
dependencies: ["RobotName"]),
]
name: "RobotName",
products: [
.library(
name: "RobotName",
targets: ["RobotName"])
],
dependencies: [],
targets: [
.target(
name: "RobotName",
dependencies: []),
.testTarget(
name: "RobotNameTests",
dependencies: ["RobotName"]),
]
)
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
import XCTest
import Foundation
import Testing

@testable import RobotName

#if os(Linux)
#if swift(>=3.1)
typealias Regex = NSRegularExpression
#else
typealias Regex = RegularExpression
#endif
#else
typealias Regex = NSRegularExpression
#endif

class RobotNameTests: XCTestCase {
func robotNameIsCorrectlyFormatted(_ name: String) -> Bool {
let robotNameRegex = try? Regex(pattern: "\\A[A-Z]{2}\\d{3}\\z")
guard let matches = robotNameRegex?.matches(in: name, options: .withoutAnchoringBounds, range: NSRange(0..<name.utf16.count)) else { return false }

return matches.count > 0
}

func testHasName() {
let robot = Robot()
XCTAssert(robotNameIsCorrectlyFormatted(robot.name))
}

func testNameSticks() {
let robot = Robot()
let name = robot.name
XCTAssertEqual(name, robot.name)
}

func testDifferentRobotsHaveDifferentNames() {
let firstRobot = Robot()
let secondRobot = Robot()
XCTAssertNotEqual(firstRobot.name, secondRobot.name)
}

func testResetName() {
var robot = Robot()
let firstName = robot.name
robot.resetName()
let secondName = robot.name
XCTAssertNotEqual(firstName, secondName)
}
typealias Regex = NSRegularExpression

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct RobotNameTests {
func robotNameIsCorrectlyFormatted(_ name: String) -> Bool {
let robotNameRegex = try? Regex(pattern: "\\A[A-Z]{2}\\d{3}\\z")
guard
let matches = robotNameRegex?.matches(
in: name, options: .withoutAnchoringBounds, range: NSRange(0..<name.utf16.count))
else { return false }

return matches.count > 0
}

@Test("Test if the robot name is correctly formatted")
func testHasName() {
let robot = Robot()
#expect(robotNameIsCorrectlyFormatted(robot.name))
}

@Test("Test if the robot name is the same after multiple calls", .enabled(if: RUNALL))
func testNameSticks() {
let robot = Robot()
let name = robot.name
#expect(name == robot.name)
}

@Test("Test if different robots have different names", .enabled(if: RUNALL))
func testDifferentRobotsHaveDifferentNames() {
let firstRobot = Robot()
let secondRobot = Robot()
#expect(firstRobot.name != secondRobot.name)
}

@Test("Test if the robot name can be reset", .enabled(if: RUNALL))
func testResetName() {
var robot = Robot()
let firstName = robot.name
robot.resetName()
let secondName = robot.name
#expect(firstName != secondName)
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
private extension Array {
extension Array {

func reversedCustom() -> Array {
return reversed()
fileprivate func reversedCustom() -> Array {
return reversed()

}
}
}

class Element<T> {
var value: T?
var next: Element?
var value: T?
var next: Element?

init() { }
init() {}

init(_ value: T, _ next: Element?) {
self.value = value
self.next = next
}
func toArray () -> [T] {
return toA(self)
}
init(_ value: T, _ next: Element?) {
self.value = value
self.next = next
}
func toArray() -> [T] {
return toA(self)
}

private var countArray: [T] = []

@discardableResult
private func toA(_ input: Element, _ tempArray: [T] = []) -> [T] {
if tempArray.isEmpty && input.value != nil {
countArray.append(input.value!)
}
if input.next != nil && input.next!.value != nil {
countArray.append(input.next!.value!)
toA(input.next!, countArray)
}
return countArray
}
private var countArray: [T] = []

class func fromArray(_ input: [T]) -> Element {
var tempElement = Element<T>()
for each in Array(input.reversedCustom()) {
tempElement = Element(each, tempElement)
}
return tempElement
@discardableResult
private func toA(_ input: Element, _ tempArray: [T] = []) -> [T] {
if tempArray.isEmpty && input.value != nil {
countArray.append(input.value!)
}
if input.next != nil && input.next!.value != nil {
countArray.append(input.next!.value!)
toA(input.next!, countArray)
}
return countArray
}

func reverseElements() -> Element {
return Element.fromArray(Array(toArray().reversedCustom()))
class func fromArray(_ input: [T]) -> Element {
var tempElement = Element<T>()
for each in Array(input.reversedCustom()) {
tempElement = Element(each, tempElement)
}
return tempElement
}

func reverseElements() -> Element {
return Element.fromArray(Array(toArray().reversedCustom()))
}
}
32 changes: 16 additions & 16 deletions exercises/practice/simple-linked-list/Package.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "SimpleLinkedList",
products: [
.library(
name: "SimpleLinkedList",
targets: ["SimpleLinkedList"]),
],
dependencies: [],
targets: [
.target(
name: "SimpleLinkedList",
dependencies: []),
.testTarget(
name: "SimpleLinkedListTests",
dependencies: ["SimpleLinkedList"]),
]
name: "SimpleLinkedList",
products: [
.library(
name: "SimpleLinkedList",
targets: ["SimpleLinkedList"])
],
dependencies: [],
targets: [
.target(
name: "SimpleLinkedList",
dependencies: []),
.testTarget(
name: "SimpleLinkedListTests",
dependencies: ["SimpleLinkedList"]),
]
)
Loading