Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Nice. Thank you for your hint! Didnt know that :)
This approach is more elegant but can be less efficient due to the overhead of recursive calls, especially if the list is very long.
Iterative approach is generally preferred for linked list operations in practice due to better performance and less memory overhead.
The recursive approach can lead to a stack overflow if the list is too long, as each recursive call adds a layer to the call stack.
When I copy & past the code, I get an error "Use of unresoled identifier
push
. Where is defined thepush
function?I got a pretty strange error message, when clicking attempt. Sample test are running fine. Does anyone know, what this means?
0 swift 0x00000000034bf568 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1 swift 0x00000000034c0146
2 libpthread.so.0 0x00007f6d3ee61330
3 libpthread.so.0 0x00007f6d3f1d67c5
4 libpthread.so.0 0x00007f6d3f1d6ea1
5 libpthread.so.0 0x00007f6d3f1d82e8
6 libpthread.so.0 0x00007f6d3f1d6efb
7 libpthread.so.0 0x00007f6d3f1d8395
8 libXCTest.so 0x00007f6d38989641
9 libXCTest.so 0x00007f6d389a8460
10 libXCTest.so 0x00007f6d389a55c5
11 libXCTest.so 0x00007f6d389a7806
12 libXCTest.so 0x00007f6d3898f9e0
13 libXCTest.so 0x00007f6d389895c5 TF6XCTest14XCTAssertEqualuRxs9EquatablerFTKzT_xKzT_xKT_SS4fileVs12StaticString4lineSu_T + 101
14 libXCTest.so 0x00007f6d3f1d6deb TF6XCTest14XCTAssertEqualuRxs9EquatablerFTKzT_xKzT_xKT_SS4fileVs12StaticString4lineSu_T + 109369483
15 libXCTest.so 0x00007f6d3f1d7559 TF6XCTest14XCTAssertEqualuRxs9EquatablerFTKzT_xKzT_xKT_SS4fileVs12StaticString4lineSu_T + 109371385
16 libXCTest.so 0x00007f6d3f1d8880 TF6XCTest14XCTAssertEqualuRxs9EquatablerFTKzT_xKzT_xKT_SS4fileVs12StaticString4lineSu_T + 109376288
17 libXCTest.so 0x00007f6d3f1d6b5e TF6XCTest14XCTAssertEqualuRxs9EquatablerFTKzT_xKzT_xKT_SS4fileVs12StaticString4lineSu_T + 109368830
18 libXCTest.so 0x00007f6d3f1d8815 TF6XCTest14XCTAssertEqualuRxs9EquatablerFTKzT_xKzT_xKT_SS4fileVs12StaticString4lineSu_T + 109376181
19 libXCTest.so 0x00007f6d389bc6ab
20 libXCTest.so 0x00007f6d389931a2
21 libXCTest.so 0x00007f6d389bc660
22 libXCTest.so 0x00007f6d389bc3e0
23 libXCTest.so 0x00007f6d38992280 TFC6XCTest10XCTestCase10invokeTestfT_T + 112
24 libXCTest.so 0x00007f6d389aa219 TTSf4g_n___TFC6XCTest10XCTestCase7performfCS_9XCTestRunT + 313
25 libXCTest.so 0x00007f6d3899214e TFC6XCTest10XCTestCase7performfCS_9XCTestRunT + 14
26 libXCTest.so 0x00007f6d38991f45 TFC6XCTest6XCTest3runfT_T + 629
27 libXCTest.so 0x00007f6d389aaee5 TTSf4g_n___TFC6XCTest11XCTestSuite7performfCS_9XCTestRunT + 789
28 libXCTest.so 0x00007f6d389ab3b3 TTSf4g_n___TFC6XCTest11XCTestSuite7performfCS_9XCTestRunT + 2019
29 libXCTest.so 0x00007f6d389ab3b3 TTSf4g_n___TFC6XCTest11XCTestSuite7performfCS_9XCTestRunT + 2019
30 libXCTest.so 0x00007f6d389976db _TF6XCTest7XCTMainFGSaT13testCaseClassMCS_10XCTestCase8allTestsGSaTSSFzS0_T_____Os5Never + 4283
31 libXCTest.so 0x00007f6d3f1d60e9 _TF6XCTest7XCTMainFGSaT13testCaseClassMCS_10XCTestCase8allTestsGSaTSSFzS0_T_____Os5Never + 109312713
32 swift 0x0000000000c795f2 llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRefllvm::GenericValue) + 1042
33 swift 0x0000000000c7d0ff llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::vector > const&, char const* const*) + 1215
34 swift 0x000000000083ca6f swift::RunImmediately(swift::CompilerInstance&, std::vector > const&, swift::IRGenOptions&, swift::SILOptions const&) + 2303
35 swift 0x00000000008364f8
36 swift 0x0000000000831377 swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 2551
37 swift 0x00000000007f8788 main + 3016
38 libc.so.6 0x00007f6d3d60af45 __libc_start_main + 245
39 swift 0x00000000007f5e56
Stack dump:
0. Program arguments: /swift-vol/swift-gen/usr/bin/swift -frontend -interpret /tmp/swift117613-18-kkw4k5.pcfkxfxbt9/solution.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -module-name solution
Nice hint! Could use this in many other projects. Thanks!
func testSortedIntersectNoIntersectOne() {
let first:Node? = buildListFromArray([2, 2, 3, 3, 4, 4, 4, 5])
let second:Node? = buildListFromArray([8, 8, 8, 8, 8, 8, 8, 8])
let result:Node? = SortedIntersect(first: first, second: second)
printList(result)
XCTAssertTrue(linkedListsEqual(first: result, second: buildListFromArray([2, 4, 5])))
}
}
func testSortedIntersectNoIntersectTwo() {
let first:Node? = buildListFromArray([2, 2, 3, 3, 4, 4, 4, 5])
let second:Node? = buildListFromArray([1, 1, 1, 1, 1, 1, 1, 1])
let result:Node? = SortedIntersect(first: first, second: second)
printList(result)
XCTAssertTrue(linkedListsEqual(first: result, second: buildListFromArray([2, 4, 5])))
}
}
Could be added, since i recognized that my accepted solution would not pass these tests ;)
You're absolutely right! I honestly have no idea why I did that, I was probably trying to avoid adding an extra line of code, but I should've been consistent about either modifying the existing structure or leaving it intact and creating all new objects. I can't really think of a way to accomplish the latter that's as concise as your suggestion though, as the guard body would need to be much more complex.
Thanks for the feedback!
Actually sortedMerge() here is not doing what it is doing in the "Linked Lists - sorted Merge" exercise. There two lists are merged, sorted and the head of the new list is returned. Here sortedMerge() sometimes appends the first to the second and sometimes appends the second to the first list. Strange Behaviour :D
Amazing and clever solution! :) but wouldn't it make sense to split last line to:
a.next = sortedMerge(first: a.next, second: b)
return a
Otherwise you are creating new instances of nodes as long as "first" and "second" aren't nil and as soon as one of them is nil you are recyling the old nodes.
Anyway, best solution for this kata!
first time, i like my solution more than yours, tim ;) #imwatchingyou
Hey kazk, may you explain, how this exactly works?