Skip to content

Commit 2d1d223

Browse files
committed
[Swift 4.2] added tests
1 parent ab20d20 commit 2d1d223

File tree

7 files changed

+434
-5
lines changed

7 files changed

+434
-5
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ script:
4848
- xcodebuild test -project ./Treap/Treap/Treap.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
4949
- xcodebuild test -project ./Palindromes/Test/Test.xcodeproj -scheme Test | xcpretty -f `xcpretty-travis-formatter`
5050
- xcodebuild test -project ./Ternary\ Search\ Tree/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
51+
- xcodebuild test -project ./Karatsuba\ Multiplication/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
5152

5253
after_success:
5354

Karatsuba Multiplication/KaratsubaMultiplication.swift

+26-5
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,48 @@ func ^^ (radix: Int, power: Int) -> Int {
1919
return Int(pow(Double(radix), Double(power)))
2020
}
2121

22+
// Long Multiplication - O(n^2)
23+
func multiply(_ num1: Int, by num2: Int, base: Int = 10) -> Int {
24+
let num1Array = String(num1).reversed().map { Int(String($0))! }
25+
let num2Array = String(num2).reversed().map { Int(String($0))! }
26+
27+
var product = Array(repeating: 0, count: num1Array.count + num2Array.count)
28+
29+
for i in num1Array.indices {
30+
var carry = 0
31+
for j in num2Array.indices {
32+
product[i + j] += carry + num1Array[i] * num2Array[j]
33+
carry = product[i + j] / base
34+
product[i + j] %= base
35+
}
36+
product[i + num2Array.count] += carry
37+
}
38+
39+
return Int(product.reversed().map { String($0) }.reduce("", +))!
40+
}
41+
42+
// Karatsuba Multiplication - O(n^log2(3))
2243
func karatsuba(_ num1: Int, by num2: Int) -> Int {
2344
let num1String = String(num1)
2445
let num2String = String(num2)
25-
46+
2647
guard num1String.count > 1 && num2String.count > 1 else {
2748
return multiply(num1, by: num2)
2849
}
2950

3051
let n = max(num1String.count, num2String.count)
3152
let nBy2 = n / 2
32-
53+
3354
let a = num1 / 10^^nBy2
3455
let b = num1 % 10^^nBy2
3556
let c = num2 / 10^^nBy2
3657
let d = num2 % 10^^nBy2
37-
58+
3859
let ac = karatsuba(a, by: c)
3960
let bd = karatsuba(b, by: d)
4061
let adPlusbc = karatsuba(a+b, by: c+d) - ac - bd
41-
62+
4263
let product = ac * 10^^(2 * nBy2) + adPlusbc * 10^^nBy2 + bd
43-
64+
4465
return product
4566
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// KaratsubaMultiplicationTests.swift
3+
// Tests
4+
//
5+
// Created by Afonso Graça on 8/10/18.
6+
//
7+
8+
import XCTest
9+
10+
final class KaratsubaMultiplicationTests: XCTestCase {
11+
12+
func testReadmeExample() {
13+
let subject = karatsuba(1234, by: 5678)
14+
15+
XCTAssertEqual(subject, 7006652)
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 50;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
6AF099AC216B54E200F69B16 /* KaratsubaMultiplication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AF099AB216B54E200F69B16 /* KaratsubaMultiplication.swift */; };
11+
6AF099AE216B55A100F69B16 /* KaratsubaMultiplicationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AF099AD216B55A100F69B16 /* KaratsubaMultiplicationTests.swift */; };
12+
/* End PBXBuildFile section */
13+
14+
/* Begin PBXFileReference section */
15+
6AF099A2216B54D500F69B16 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
16+
6AF099A7216B54D500F69B16 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Tests/Info.plist; sourceTree = SOURCE_ROOT; };
17+
6AF099AB216B54E200F69B16 /* KaratsubaMultiplication.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KaratsubaMultiplication.swift; path = ../KaratsubaMultiplication.swift; sourceTree = "<group>"; };
18+
6AF099AD216B55A100F69B16 /* KaratsubaMultiplicationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KaratsubaMultiplicationTests.swift; sourceTree = "<group>"; };
19+
/* End PBXFileReference section */
20+
21+
/* Begin PBXFrameworksBuildPhase section */
22+
6AF0999F216B54D500F69B16 /* Frameworks */ = {
23+
isa = PBXFrameworksBuildPhase;
24+
buildActionMask = 2147483647;
25+
files = (
26+
);
27+
runOnlyForDeploymentPostprocessing = 0;
28+
};
29+
/* End PBXFrameworksBuildPhase section */
30+
31+
/* Begin PBXGroup section */
32+
6AF09997216B545D00F69B16 = {
33+
isa = PBXGroup;
34+
children = (
35+
6AF099AB216B54E200F69B16 /* KaratsubaMultiplication.swift */,
36+
6AF099A4216B54D500F69B16 /* Tests */,
37+
6AF099A3216B54D500F69B16 /* Products */,
38+
);
39+
sourceTree = "<group>";
40+
};
41+
6AF099A3216B54D500F69B16 /* Products */ = {
42+
isa = PBXGroup;
43+
children = (
44+
6AF099A2216B54D500F69B16 /* Tests.xctest */,
45+
);
46+
name = Products;
47+
sourceTree = "<group>";
48+
};
49+
6AF099A4216B54D500F69B16 /* Tests */ = {
50+
isa = PBXGroup;
51+
children = (
52+
6AF099AD216B55A100F69B16 /* KaratsubaMultiplicationTests.swift */,
53+
6AF099A7216B54D500F69B16 /* Info.plist */,
54+
);
55+
name = Tests;
56+
sourceTree = SOURCE_ROOT;
57+
};
58+
/* End PBXGroup section */
59+
60+
/* Begin PBXNativeTarget section */
61+
6AF099A1216B54D500F69B16 /* Tests */ = {
62+
isa = PBXNativeTarget;
63+
buildConfigurationList = 6AF099A8216B54D500F69B16 /* Build configuration list for PBXNativeTarget "Tests" */;
64+
buildPhases = (
65+
6AF0999E216B54D500F69B16 /* Sources */,
66+
6AF0999F216B54D500F69B16 /* Frameworks */,
67+
6AF099A0216B54D500F69B16 /* Resources */,
68+
);
69+
buildRules = (
70+
);
71+
dependencies = (
72+
);
73+
name = Tests;
74+
productName = Tests;
75+
productReference = 6AF099A2216B54D500F69B16 /* Tests.xctest */;
76+
productType = "com.apple.product-type.bundle.unit-test";
77+
};
78+
/* End PBXNativeTarget section */
79+
80+
/* Begin PBXProject section */
81+
6AF09998216B545D00F69B16 /* Project object */ = {
82+
isa = PBXProject;
83+
attributes = {
84+
LastSwiftUpdateCheck = 1000;
85+
LastUpgradeCheck = 1000;
86+
TargetAttributes = {
87+
6AF099A1216B54D500F69B16 = {
88+
CreatedOnToolsVersion = 10.0;
89+
};
90+
};
91+
};
92+
buildConfigurationList = 6AF0999B216B545D00F69B16 /* Build configuration list for PBXProject "Tests" */;
93+
compatibilityVersion = "Xcode 9.3";
94+
developmentRegion = en;
95+
hasScannedForEncodings = 0;
96+
knownRegions = (
97+
en,
98+
);
99+
mainGroup = 6AF09997216B545D00F69B16;
100+
productRefGroup = 6AF099A3216B54D500F69B16 /* Products */;
101+
projectDirPath = "";
102+
projectRoot = "";
103+
targets = (
104+
6AF099A1216B54D500F69B16 /* Tests */,
105+
);
106+
};
107+
/* End PBXProject section */
108+
109+
/* Begin PBXResourcesBuildPhase section */
110+
6AF099A0216B54D500F69B16 /* Resources */ = {
111+
isa = PBXResourcesBuildPhase;
112+
buildActionMask = 2147483647;
113+
files = (
114+
);
115+
runOnlyForDeploymentPostprocessing = 0;
116+
};
117+
/* End PBXResourcesBuildPhase section */
118+
119+
/* Begin PBXSourcesBuildPhase section */
120+
6AF0999E216B54D500F69B16 /* Sources */ = {
121+
isa = PBXSourcesBuildPhase;
122+
buildActionMask = 2147483647;
123+
files = (
124+
6AF099AE216B55A100F69B16 /* KaratsubaMultiplicationTests.swift in Sources */,
125+
6AF099AC216B54E200F69B16 /* KaratsubaMultiplication.swift in Sources */,
126+
);
127+
runOnlyForDeploymentPostprocessing = 0;
128+
};
129+
/* End PBXSourcesBuildPhase section */
130+
131+
/* Begin XCBuildConfiguration section */
132+
6AF0999C216B545D00F69B16 /* Debug */ = {
133+
isa = XCBuildConfiguration;
134+
buildSettings = {
135+
};
136+
name = Debug;
137+
};
138+
6AF0999D216B545D00F69B16 /* Release */ = {
139+
isa = XCBuildConfiguration;
140+
buildSettings = {
141+
};
142+
name = Release;
143+
};
144+
6AF099A9216B54D500F69B16 /* Debug */ = {
145+
isa = XCBuildConfiguration;
146+
buildSettings = {
147+
ALWAYS_SEARCH_USER_PATHS = NO;
148+
CLANG_ANALYZER_NONNULL = YES;
149+
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
150+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
151+
CLANG_CXX_LIBRARY = "libc++";
152+
CLANG_ENABLE_MODULES = YES;
153+
CLANG_ENABLE_OBJC_ARC = YES;
154+
CLANG_ENABLE_OBJC_WEAK = YES;
155+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
156+
CLANG_WARN_BOOL_CONVERSION = YES;
157+
CLANG_WARN_COMMA = YES;
158+
CLANG_WARN_CONSTANT_CONVERSION = YES;
159+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
160+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
161+
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
162+
CLANG_WARN_EMPTY_BODY = YES;
163+
CLANG_WARN_ENUM_CONVERSION = YES;
164+
CLANG_WARN_INFINITE_RECURSION = YES;
165+
CLANG_WARN_INT_CONVERSION = YES;
166+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
167+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
168+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
169+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
170+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
171+
CLANG_WARN_STRICT_PROTOTYPES = YES;
172+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
173+
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
174+
CLANG_WARN_UNREACHABLE_CODE = YES;
175+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
176+
CODE_SIGN_IDENTITY = "-";
177+
CODE_SIGN_STYLE = Automatic;
178+
COMBINE_HIDPI_IMAGES = YES;
179+
COPY_PHASE_STRIP = NO;
180+
DEBUG_INFORMATION_FORMAT = dwarf;
181+
ENABLE_STRICT_OBJC_MSGSEND = YES;
182+
ENABLE_TESTABILITY = YES;
183+
GCC_C_LANGUAGE_STANDARD = gnu11;
184+
GCC_DYNAMIC_NO_PIC = NO;
185+
GCC_NO_COMMON_BLOCKS = YES;
186+
GCC_OPTIMIZATION_LEVEL = 0;
187+
GCC_PREPROCESSOR_DEFINITIONS = (
188+
"DEBUG=1",
189+
"$(inherited)",
190+
);
191+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
192+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
193+
GCC_WARN_UNDECLARED_SELECTOR = YES;
194+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
195+
GCC_WARN_UNUSED_FUNCTION = YES;
196+
GCC_WARN_UNUSED_VARIABLE = YES;
197+
INFOPLIST_FILE = Tests/Info.plist;
198+
LD_RUNPATH_SEARCH_PATHS = (
199+
"$(inherited)",
200+
"@executable_path/../Frameworks",
201+
"@loader_path/../Frameworks",
202+
);
203+
MACOSX_DEPLOYMENT_TARGET = 10.14;
204+
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
205+
MTL_FAST_MATH = YES;
206+
ONLY_ACTIVE_ARCH = YES;
207+
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
208+
PRODUCT_NAME = "$(TARGET_NAME)";
209+
SDKROOT = macosx;
210+
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
211+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
212+
SWIFT_VERSION = 4.2;
213+
};
214+
name = Debug;
215+
};
216+
6AF099AA216B54D500F69B16 /* Release */ = {
217+
isa = XCBuildConfiguration;
218+
buildSettings = {
219+
ALWAYS_SEARCH_USER_PATHS = NO;
220+
CLANG_ANALYZER_NONNULL = YES;
221+
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
222+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
223+
CLANG_CXX_LIBRARY = "libc++";
224+
CLANG_ENABLE_MODULES = YES;
225+
CLANG_ENABLE_OBJC_ARC = YES;
226+
CLANG_ENABLE_OBJC_WEAK = YES;
227+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
228+
CLANG_WARN_BOOL_CONVERSION = YES;
229+
CLANG_WARN_COMMA = YES;
230+
CLANG_WARN_CONSTANT_CONVERSION = YES;
231+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
232+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
233+
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
234+
CLANG_WARN_EMPTY_BODY = YES;
235+
CLANG_WARN_ENUM_CONVERSION = YES;
236+
CLANG_WARN_INFINITE_RECURSION = YES;
237+
CLANG_WARN_INT_CONVERSION = YES;
238+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
239+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
240+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
241+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
242+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
243+
CLANG_WARN_STRICT_PROTOTYPES = YES;
244+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
245+
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
246+
CLANG_WARN_UNREACHABLE_CODE = YES;
247+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
248+
CODE_SIGN_IDENTITY = "-";
249+
CODE_SIGN_STYLE = Automatic;
250+
COMBINE_HIDPI_IMAGES = YES;
251+
COPY_PHASE_STRIP = NO;
252+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
253+
ENABLE_NS_ASSERTIONS = NO;
254+
ENABLE_STRICT_OBJC_MSGSEND = YES;
255+
GCC_C_LANGUAGE_STANDARD = gnu11;
256+
GCC_NO_COMMON_BLOCKS = YES;
257+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
258+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
259+
GCC_WARN_UNDECLARED_SELECTOR = YES;
260+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
261+
GCC_WARN_UNUSED_FUNCTION = YES;
262+
GCC_WARN_UNUSED_VARIABLE = YES;
263+
INFOPLIST_FILE = Tests/Info.plist;
264+
LD_RUNPATH_SEARCH_PATHS = (
265+
"$(inherited)",
266+
"@executable_path/../Frameworks",
267+
"@loader_path/../Frameworks",
268+
);
269+
MACOSX_DEPLOYMENT_TARGET = 10.14;
270+
MTL_ENABLE_DEBUG_INFO = NO;
271+
MTL_FAST_MATH = YES;
272+
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
273+
PRODUCT_NAME = "$(TARGET_NAME)";
274+
SDKROOT = macosx;
275+
SWIFT_COMPILATION_MODE = wholemodule;
276+
SWIFT_OPTIMIZATION_LEVEL = "-O";
277+
SWIFT_VERSION = 4.2;
278+
};
279+
name = Release;
280+
};
281+
/* End XCBuildConfiguration section */
282+
283+
/* Begin XCConfigurationList section */
284+
6AF0999B216B545D00F69B16 /* Build configuration list for PBXProject "Tests" */ = {
285+
isa = XCConfigurationList;
286+
buildConfigurations = (
287+
6AF0999C216B545D00F69B16 /* Debug */,
288+
6AF0999D216B545D00F69B16 /* Release */,
289+
);
290+
defaultConfigurationIsVisible = 0;
291+
defaultConfigurationName = Release;
292+
};
293+
6AF099A8216B54D500F69B16 /* Build configuration list for PBXNativeTarget "Tests" */ = {
294+
isa = XCConfigurationList;
295+
buildConfigurations = (
296+
6AF099A9216B54D500F69B16 /* Debug */,
297+
6AF099AA216B54D500F69B16 /* Release */,
298+
);
299+
defaultConfigurationIsVisible = 0;
300+
defaultConfigurationName = Release;
301+
};
302+
/* End XCConfigurationList section */
303+
};
304+
rootObject = 6AF09998216B545D00F69B16 /* Project object */;
305+
}

0 commit comments

Comments
 (0)