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 20 #804

Draft
wants to merge 3 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,84 +1,108 @@
class Node<T> {
var value: T?
var next: Node?
var prev: Node?
class Node<T: Equatable>: Equatable {
var value: T?
var next: Node?
var prev: Node?

init() {
}
init() {
}

init(value: T) {
self.value = value
}
init(value: T) {
self.value = value
}

static func == (lhs: Node<T>, rhs: Node<T>) -> Bool {
return lhs.value == rhs.value
}
}

class Deque<T> {
var count: Int = 0
var head: Node<T>
var tail: Node<T>
class Deque<T: Equatable> {
var count: Int = 0
var head: Node<T>
var tail: Node<T>

init() {
self.head = Node<T>()
self.tail = head
init() {
self.head = Node<T>()
self.tail = head

}
}

func isEmpty() -> Bool {
return self.count == 0
}
func isEmpty() -> Bool {
return self.count == 0
}

func push(_ value: T) {
let node = Node<T>(value: value)
if self.isEmpty() {
self.head = node
self.tail = node
func delete(_ value: T) {
var current: Node<T>? = self.head
while current != nil {
if current?.value == value {
if current == self.head {
self.head = self.head.next ?? Node<T>()
self.head.prev = nil
} else if current == self.tail {
self.tail = self.tail.prev ?? Node<T>()
self.tail.next = nil
} else {
node.next = self.head
self.head.prev = node
self.head = node
current?.prev?.next = current?.next
current?.next?.prev = current?.prev
}
self.count += 1
self.count -= 1
break
}
current = current?.next
}
}

func unshift(_ value: T) {
let node = Node<T>(value: value)
if self.isEmpty() {
self.head = node
self.tail = node
} else {
node.prev = self.tail
self.tail.next = node
self.tail = node
}
self.count += 1
func push(_ value: T) {
let node = Node<T>(value: value)
if self.isEmpty() {
self.head = node
self.tail = node
} else {
node.next = self.head
self.head.prev = node
self.head = node
}
self.count += 1
}

func pop() -> T? {
if self.isEmpty() {
return nil
} else if self.count == 1 {
let temp: Node<T> = self.head
self.count -= 1
return temp.value
} else {
let temp: Node<T> = self.head
self.head = self.head.next!
self.count -= 1
return temp.value
}
func unshift(_ value: T) {
let node = Node<T>(value: value)
if self.isEmpty() {
self.head = node
self.tail = node
} else {
node.prev = self.tail
self.tail.next = node
self.tail = node
}
func shift() -> T? {
if self.isEmpty() {
return nil
} else if self.count == 1 {
let temp: Node<T> = self.tail
self.count -= 1
return temp.value
} else {
let temp: Node<T> = self.tail
self.tail = self.tail.prev!
self.count -= 1
return temp.value
}
self.count += 1
}

func pop() -> T? {
if self.isEmpty() {
return nil
} else if self.count == 1 {
let temp: Node<T> = self.head
self.count -= 1
return temp.value
} else {
let temp: Node<T> = self.head
self.head = self.head.next!
self.count -= 1
return temp.value
}
}
func shift() -> T? {
if self.isEmpty() {
return nil
} else if self.count == 1 {
let temp: Node<T> = self.tail
self.count -= 1
return temp.value
} else {
let temp: Node<T> = self.tail
self.tail = self.tail.prev!
self.count -= 1
return temp.value
}
}
}
27 changes: 27 additions & 0 deletions exercises/practice/linked-list/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Testing
import Foundation
@testable import {{exercise|camelCase}}

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

@Suite struct {{exercise|camelCase}}Tests {
{% for case in cases %}
{% if forloop.first -%}
@Test("{{case.description}}")
{% else -%}
@Test("{{case.description}}", .enabled(if: RUNALL))
{% endif -%}
func test{{case.description |camelCase }}() {
let deque = Deque<Int>()
{%- for operation in case.input.operations -%}
{%- if operation.operation == "count" -%}
#expect(deque.{{operation.operation}} == {{operation.expected}})
{%- elif operation.expected -%}
#expect(deque.{{operation.operation}}() == {{operation.expected}})
{%- else -%}
deque.{{operation.operation}}({{operation.value}})
{%- endif -%}
{%- endfor -%}
}
{% endfor -%}
}
32 changes: 16 additions & 16 deletions exercises/practice/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: "LinkedList",
products: [
.library(
name: "LinkedList",
targets: ["LinkedList"]),
],
dependencies: [],
targets: [
.target(
name: "LinkedList",
dependencies: []),
.testTarget(
name: "LinkedListTests",
dependencies: ["LinkedList"]),
]
name: "LinkedList",
products: [
.library(
name: "LinkedList",
targets: ["LinkedList"])
],
dependencies: [],
targets: [
.target(
name: "LinkedList",
dependencies: []),
.testTarget(
name: "LinkedListTests",
dependencies: ["LinkedList"]),
]
)
Loading