GithubHelp home page GithubHelp logo

hedgemannate / ios-afternoon-project-swift-generics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bloominstituteoftechnology/ios-afternoon-project-swift-generics

0.0 1.0 0.0 3 KB

Swift 100.00%

ios-afternoon-project-swift-generics's Introduction

Generics Challenge

Create a generic CountedSet struct that is constrained to Hashable elements. A counted set is an unordered collection of unique elements that may appear more than once in the collection. Use a private dictionary as your backing storage for set members and their counts.

Set up the project

Create a new playground or project to develop and test your type. It may be easier to develop a single type in a playground but Xcode is a bit unstable with playgrounds these days. Use whatever tool best works for you.

Goals

  • Add insertion and removal (insert and remove) of one element at a time.
  • Support subscripting to look up current values (by implementing subscript(_ member: Element) -> Int). Return 0 for any value that is not found.
  • Add count, returning the number of unique elements in the counted set and isEmpty for when count is zero.

Conform to ExpressibleByArrayLiteral

As demonstrated in class, conform your set to ExpressibleByArrayLiteral so you can initialize a counted set using an array of same-type items.

Your implementation should support the following interaction style:

enum Arrow { case iron, wooden, elven, dwarvish, magic, silver }
var aCountedSet = CountedSet<Arrow>()
aCountedSet[.iron] // 0
var myCountedSet: CountedSet<Arrow> = [.iron, .magic, .iron, .silver, .iron, .iron]
myCountedSet[.iron] // 4
myCountedSet.remove(.iron) // 3
myCountedSet.remove(.dwarvish) // 0
myCountedSet.remove(.magic) // 0

Test

  1. Run the project and make sure everything works. Create a good suite of tests that check for boundary conditions and many different types.
  2. If anything doesn't work the way the above example shows, go back and debug your issues.
  3. As always, if you need help, follow the 20-minute rule, then ask your PM.

Go Farther

Time allowing, consider adding the following enhancements to your CountedSet type:

  • Conform CountedSet to Sequence by creating a custom iterator (DictionaryIterator).
  • Implement contains to test whether your set contains at least one of a given item.
  • Implement union with another set, returning all members and their combined sums. For extra karma, implement a mutating version (change in place) and a non-mutating version (return a copy).
  • Implement intersection and/or subtraction using the same logic. Intersection returns all members and counts appearing in both sets. Subtraction removes all counts found in the second set from the first.
  • Implement isDisjoint, testing that there's no overlap between elements.
  • Conform to Equatable and implement == to test if two counted sets are the same.

References

ios-afternoon-project-swift-generics's People

Contributors

hedgemannate avatar jcgohlke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.