|
| 1 | +// |
| 2 | +// Dictionary.swift |
| 3 | +// Help |
| 4 | +// |
| 5 | +// Created by Sergey Lukaschuk on 25.06.2021. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | + |
| 11 | +// MARK: Dictionary Reduce |
| 12 | + |
| 13 | + |
| 14 | +let fruits = ["🍏", "🍓", "🍒", "🍌", "🍏", "🍒", "🍌", "🍌", "🍌", "🍒", "🍒", "🍌", "🍓", "🍓"] |
| 15 | + |
| 16 | +let fruitsCount = fruits.reduce(into: [:]) { counts, fruit in |
| 17 | + counts[fruit, default: 0] += 1 |
| 18 | +} |
| 19 | + |
| 20 | +// fruitsCount |
| 21 | +// ["🍒": 4, "🍏": 2, "🍓": 3, "🍌": 5] |
| 22 | +// [String : Int] |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +// MARK: Dictionary Search List |
| 27 | + |
| 28 | +struct Product: Hashable { |
| 29 | + let id: String; // unique identifier |
| 30 | + let name: String; |
| 31 | + let producer: String; |
| 32 | + |
| 33 | + static func ==(lhs: Product, rhs: Product) -> Bool { |
| 34 | + return lhs.id == rhs.id |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +protocol Library { |
| 39 | + /** |
| 40 | + Adds a new product object to the Library. |
| 41 | + - Parameter product: product to add to the Library |
| 42 | + - Returns: false if the product with same id already exists in the Library, true – otherwise. |
| 43 | + */ |
| 44 | + func addNewProduct(product: Product) -> Bool; |
| 45 | + |
| 46 | + /** |
| 47 | + Deletes the product with the specified id from the Library. |
| 48 | + - Returns: true if the product with same id existed in the Library, false – otherwise. |
| 49 | + */ |
| 50 | + func deleteProduct(id: String) -> Bool; |
| 51 | + |
| 52 | + /** |
| 53 | + - Returns: 10 product names containing the specified string. If there are several products with the same name, producer's name is appended to product's name. |
| 54 | + */ |
| 55 | + func listProductsByName(searchString: String) -> Set<String>; |
| 56 | + |
| 57 | + /** |
| 58 | + - Returns: 10 product names whose producer contains the specified string, ordered by producers. |
| 59 | + */ |
| 60 | + func listProductsByProducer(searchString: String) -> [String]; |
| 61 | +} |
| 62 | + |
| 63 | +class LibraryRepository: Library { |
| 64 | + private var products = [String: Product]() |
| 65 | + |
| 66 | + func addNewProduct(product: Product) -> Bool { |
| 67 | + return self.products.updateValue(product, forKey: product.id) == nil |
| 68 | + } |
| 69 | + |
| 70 | + func deleteProduct(id: String) -> Bool { |
| 71 | + return self.products.removeValue(forKey: id) != nil |
| 72 | + } |
| 73 | + |
| 74 | + func listProductsByName(searchString: String) -> Set<String> { |
| 75 | + return self.products |
| 76 | + .filter({ $1.name.contains(searchString) }) |
| 77 | + .prefix(10) |
| 78 | + .reduce(into: Set<String>()) { (productNames, product) in |
| 79 | + if products.filter({ $1.name == product.value.name }).count > 1 { |
| 80 | + productNames.insert("\(product.value.producer) - \(product.value.name)") |
| 81 | + return |
| 82 | + } |
| 83 | + productNames.insert(product.value.name) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func listProductsByProducer(searchString: String) -> [String] { |
| 88 | + return self.products |
| 89 | + .filter({ $1.producer.contains(searchString) }) |
| 90 | + .sorted(by: { $0.value.producer > $1.value.producer }) |
| 91 | + .prefix(10) |
| 92 | + .map { $0.value.name } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func test(lib: Library) { |
| 97 | + assert(!lib.deleteProduct(id: "1")) |
| 98 | + assert(lib.addNewProduct(product: Product(id: "1", name: "1", producer: "Lex"))) |
| 99 | + assert(!lib.addNewProduct(product: Product(id: "1", name: "any name because we check id only", producer: "any producer"))) |
| 100 | + assert(lib.deleteProduct(id: "1")) |
| 101 | + assert(lib.addNewProduct(product: Product(id: "3", name: "Some Product3", producer: "Some Producer2"))) |
| 102 | + assert(lib.addNewProduct(product: Product(id: "4", name: "Some Product1", producer: "Some Producer3"))) |
| 103 | + assert(lib.addNewProduct(product: Product(id: "2", name: "Some Product2", producer: "Some Producer2"))) |
| 104 | + assert(lib.addNewProduct(product: Product(id: "1", name: "Some Product1", producer: "Some Producer1"))) |
| 105 | + assert(lib.addNewProduct(product: Product(id: "5", name: "Other Product5", producer: "Other Producer4"))) |
| 106 | + assert(lib.addNewProduct(product: Product(id: "6", name: "Other Product6", producer: "Other Producer4"))) |
| 107 | + assert(lib.addNewProduct(product: Product(id: "7", name: "Other Product7", producer: "Other Producer4"))) |
| 108 | + assert(lib.addNewProduct(product: Product(id: "8", name: "Other Product8", producer: "Other Producer4"))) |
| 109 | + assert(lib.addNewProduct(product: Product(id: "9", name: "Other Product9", producer: "Other Producer4"))) |
| 110 | + assert(lib.addNewProduct(product: Product(id: "10", name: "Other Product10", producer: "Other Producer4"))) |
| 111 | + assert(lib.addNewProduct(product: Product(id: "11", name: "Other Product11", producer: "Other Producer4"))) |
| 112 | + |
| 113 | + var byNames: Set<String> = lib.listProductsByName(searchString: "Product") |
| 114 | + assert(byNames.count == 10) |
| 115 | + |
| 116 | + byNames = lib.listProductsByName(searchString: "Some Product") |
| 117 | + assert(byNames.count == 4) |
| 118 | + assert(byNames.contains("Some Producer3 - Some Product1")) |
| 119 | + assert(byNames.contains("Some Product2")) |
| 120 | + assert(byNames.contains("Some Product3")) |
| 121 | + assert(!byNames.contains("Some Product1")) |
| 122 | + assert(byNames.contains("Some Producer1 - Some Product1")) |
| 123 | + |
| 124 | + var byProducer: [String] = lib.listProductsByProducer(searchString: "Producer") |
| 125 | + assert(byProducer.count == 10) |
| 126 | + |
| 127 | + byProducer = lib.listProductsByProducer(searchString: "Some Producer") |
| 128 | + assert(byProducer.count == 4) |
| 129 | + assert(byProducer[0] == "Some Product1") |
| 130 | + assert(byProducer[1] == "Some Product2" || byProducer[1] == "Some Product3") |
| 131 | + assert(byProducer[2] == "Some Product2" || byProducer[2] == "Some Product3") |
| 132 | + assert(byProducer[3] == "Some Product1") |
| 133 | +} |
| 134 | + |
| 135 | +//test(lib: LibraryRepository()) |
0 commit comments