The current project I am working on is to work my way through Cracking the Code Interview creating a Swift implementation of each algorithm and data structure problem listed.
I decided to tackle the Linked List structure first because it was really annoying me how many people told me this was something I MUST learn how to do.
Fortunately the Swift Algorithm Club had an implementation I could look over. I really didn’t see the point in trying to reinvent the wheel, so I figured I could just drag and drop their implementation into a Playground and create a linked list so I could just focus on completing the questions.
(I strongly recommend reading through the tutorial they published recently explaining how to implement this yourself. It’s a great read!)
One issue I encountered was when I attempted to drag and drop the file into a playground. I recently discovered you can have multiple files in Playgrounds and was super excited about it.
I got a compiler error. I looked up the issue and found I needed to import the sources file. No worries. I added it then I got an additional error:
Playground execution failed: error: RemoveDuplicates.playground:8:12: error: 'LinkedList' initializer is inaccessible due to 'internal' protection level let list = LinkedList ()
Huh. That was weird.
I looked over the class and I didn’t see anything that was private. The class and most of the methods were listed as public.
I reached out to the Algorithm Club and they indicated that it was missing a public initializer.
I forked the repository and added a public initializer:
public init() {}
So cool. I should be able to access things now. Huzzah.
I went to go and figure out how to start working on implementing my code questions. The first one talks about iterating through a linked list to remove duplicate values.
I went to start working on this. I realized that there wasn’t a way to simply pass in an already existing data structure to turn into a linked list. I would have to add each node to the linked list one by one.
This seemed like an inconvenience, so I reached out to them again to ask if there was an easier way to do this. They helpfully suggested that I add an extension to the linked list class to let me do this.
Because they did such a great job setting up the class, creating an extension to do this was pretty easy:
extension LinkedList { public convenience init(array: Array) { self.init() for element in array { self.append(element) } } }
I wanted to make sure this actually worked and would be this easy, so I created a test case in their Playground for the Linked List:
let fibonacci = [1, 1, 2, 3, 5, 8,] let arrayList = LinkedList(array: fibonacci) print(arrayList)
I forked the repository and submitted this as a pull request.
Conclusion
I really wanted to do an algorithms post every day. I am glad I was able to do one today. It was frustrating to encounter some road blocks, but it was incredibly awesome that the Algorithm Club was around to answer my questions and that I am able to contribute some enhancements to this to make it more useful.
It’s also nice that I was able to use this in a project and find things that were not considered by the original creators and to be able to make it more useful.
Even though I didn’t get to work on my problem set for the evening, it was nice to have a team to work with to solve a problem with code. That’s what I like about programming and I don’t get to do this as much as I would like.