├── .gitignore ├── DollarBreak.xcodeproj └── project.pbxproj ├── DollarBreak ├── algorithm_overview.txt ├── coin.swift ├── functional.swift ├── imperative.swift └── main.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | *.pbxuser 4 | !default.pbxuser 5 | *.mode1v3 6 | !default.mode1v3 7 | *.mode2v3 8 | !default.mode2v3 9 | *.perspectivev3 10 | !default.perspectivev3 11 | *.xcworkspace 12 | !default.xcworkspace 13 | xcuserdata 14 | profile 15 | *.moved-aside 16 | DerivedData 17 | .idea/ -------------------------------------------------------------------------------- /DollarBreak.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 41300A231A2A5A8800DA04B5 /* functional.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41300A221A2A5A8800DA04B5 /* functional.swift */; }; 11 | 41300A251A2A5B2800DA04B5 /* imperative.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41300A241A2A5B2800DA04B5 /* imperative.swift */; }; 12 | 41300A271A2A5D7700DA04B5 /* coin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41300A261A2A5D7700DA04B5 /* coin.swift */; }; 13 | 41C0651A1A27E957002B860F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41C065191A27E957002B860F /* main.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | 41C065141A27E957002B860F /* CopyFiles */ = { 18 | isa = PBXCopyFilesBuildPhase; 19 | buildActionMask = 2147483647; 20 | dstPath = /usr/share/man/man1/; 21 | dstSubfolderSpec = 0; 22 | files = ( 23 | ); 24 | runOnlyForDeploymentPostprocessing = 1; 25 | }; 26 | /* End PBXCopyFilesBuildPhase section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | 41300A221A2A5A8800DA04B5 /* functional.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = functional.swift; sourceTree = ""; }; 30 | 41300A241A2A5B2800DA04B5 /* imperative.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = imperative.swift; sourceTree = ""; }; 31 | 41300A261A2A5D7700DA04B5 /* coin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = coin.swift; sourceTree = ""; }; 32 | 41300A281A2B989A00DA04B5 /* algorithm_overview.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = algorithm_overview.txt; sourceTree = ""; }; 33 | 41C065161A27E957002B860F /* DollarBreak */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = DollarBreak; sourceTree = BUILT_PRODUCTS_DIR; }; 34 | 41C065191A27E957002B860F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | 41C065131A27E957002B860F /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | 41C0650D1A27E957002B860F = { 49 | isa = PBXGroup; 50 | children = ( 51 | 41C065181A27E957002B860F /* DollarBreak */, 52 | 41C065171A27E957002B860F /* Products */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | 41C065171A27E957002B860F /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 41C065161A27E957002B860F /* DollarBreak */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | 41C065181A27E957002B860F /* DollarBreak */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | 41300A281A2B989A00DA04B5 /* algorithm_overview.txt */, 68 | 41300A261A2A5D7700DA04B5 /* coin.swift */, 69 | 41300A221A2A5A8800DA04B5 /* functional.swift */, 70 | 41300A241A2A5B2800DA04B5 /* imperative.swift */, 71 | 41C065191A27E957002B860F /* main.swift */, 72 | ); 73 | path = DollarBreak; 74 | sourceTree = ""; 75 | }; 76 | /* End PBXGroup section */ 77 | 78 | /* Begin PBXNativeTarget section */ 79 | 41C065151A27E957002B860F /* DollarBreak */ = { 80 | isa = PBXNativeTarget; 81 | buildConfigurationList = 41C0651D1A27E957002B860F /* Build configuration list for PBXNativeTarget "DollarBreak" */; 82 | buildPhases = ( 83 | 41C065121A27E957002B860F /* Sources */, 84 | 41C065131A27E957002B860F /* Frameworks */, 85 | 41C065141A27E957002B860F /* CopyFiles */, 86 | ); 87 | buildRules = ( 88 | ); 89 | dependencies = ( 90 | ); 91 | name = DollarBreak; 92 | productName = DollarBreak; 93 | productReference = 41C065161A27E957002B860F /* DollarBreak */; 94 | productType = "com.apple.product-type.tool"; 95 | }; 96 | /* End PBXNativeTarget section */ 97 | 98 | /* Begin PBXProject section */ 99 | 41C0650E1A27E957002B860F /* Project object */ = { 100 | isa = PBXProject; 101 | attributes = { 102 | LastUpgradeCheck = 0820; 103 | ORGANIZATIONNAME = iJoshSmith; 104 | TargetAttributes = { 105 | 41C065151A27E957002B860F = { 106 | CreatedOnToolsVersion = 6.1; 107 | LastSwiftMigration = 0820; 108 | }; 109 | }; 110 | }; 111 | buildConfigurationList = 41C065111A27E957002B860F /* Build configuration list for PBXProject "DollarBreak" */; 112 | compatibilityVersion = "Xcode 3.2"; 113 | developmentRegion = English; 114 | hasScannedForEncodings = 0; 115 | knownRegions = ( 116 | en, 117 | ); 118 | mainGroup = 41C0650D1A27E957002B860F; 119 | productRefGroup = 41C065171A27E957002B860F /* Products */; 120 | projectDirPath = ""; 121 | projectRoot = ""; 122 | targets = ( 123 | 41C065151A27E957002B860F /* DollarBreak */, 124 | ); 125 | }; 126 | /* End PBXProject section */ 127 | 128 | /* Begin PBXSourcesBuildPhase section */ 129 | 41C065121A27E957002B860F /* Sources */ = { 130 | isa = PBXSourcesBuildPhase; 131 | buildActionMask = 2147483647; 132 | files = ( 133 | 41300A231A2A5A8800DA04B5 /* functional.swift in Sources */, 134 | 41300A251A2A5B2800DA04B5 /* imperative.swift in Sources */, 135 | 41300A271A2A5D7700DA04B5 /* coin.swift in Sources */, 136 | 41C0651A1A27E957002B860F /* main.swift in Sources */, 137 | ); 138 | runOnlyForDeploymentPostprocessing = 0; 139 | }; 140 | /* End PBXSourcesBuildPhase section */ 141 | 142 | /* Begin XCBuildConfiguration section */ 143 | 41C0651B1A27E957002B860F /* Debug */ = { 144 | isa = XCBuildConfiguration; 145 | buildSettings = { 146 | ALWAYS_SEARCH_USER_PATHS = NO; 147 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 148 | CLANG_CXX_LIBRARY = "libc++"; 149 | CLANG_ENABLE_MODULES = YES; 150 | CLANG_ENABLE_OBJC_ARC = YES; 151 | CLANG_WARN_BOOL_CONVERSION = YES; 152 | CLANG_WARN_CONSTANT_CONVERSION = YES; 153 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 154 | CLANG_WARN_EMPTY_BODY = YES; 155 | CLANG_WARN_ENUM_CONVERSION = YES; 156 | CLANG_WARN_INFINITE_RECURSION = YES; 157 | CLANG_WARN_INT_CONVERSION = YES; 158 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 159 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 160 | CLANG_WARN_UNREACHABLE_CODE = YES; 161 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 162 | COPY_PHASE_STRIP = NO; 163 | ENABLE_STRICT_OBJC_MSGSEND = YES; 164 | ENABLE_TESTABILITY = YES; 165 | GCC_C_LANGUAGE_STANDARD = gnu99; 166 | GCC_DYNAMIC_NO_PIC = NO; 167 | GCC_NO_COMMON_BLOCKS = YES; 168 | GCC_OPTIMIZATION_LEVEL = 0; 169 | GCC_PREPROCESSOR_DEFINITIONS = ( 170 | "DEBUG=1", 171 | "$(inherited)", 172 | ); 173 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 174 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 175 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 176 | GCC_WARN_UNDECLARED_SELECTOR = YES; 177 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 178 | GCC_WARN_UNUSED_FUNCTION = YES; 179 | GCC_WARN_UNUSED_VARIABLE = YES; 180 | MACOSX_DEPLOYMENT_TARGET = 10.10; 181 | MTL_ENABLE_DEBUG_INFO = YES; 182 | ONLY_ACTIVE_ARCH = YES; 183 | SDKROOT = macosx; 184 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 185 | }; 186 | name = Debug; 187 | }; 188 | 41C0651C1A27E957002B860F /* Release */ = { 189 | isa = XCBuildConfiguration; 190 | buildSettings = { 191 | ALWAYS_SEARCH_USER_PATHS = NO; 192 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 193 | CLANG_CXX_LIBRARY = "libc++"; 194 | CLANG_ENABLE_MODULES = YES; 195 | CLANG_ENABLE_OBJC_ARC = YES; 196 | CLANG_WARN_BOOL_CONVERSION = YES; 197 | CLANG_WARN_CONSTANT_CONVERSION = YES; 198 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 199 | CLANG_WARN_EMPTY_BODY = YES; 200 | CLANG_WARN_ENUM_CONVERSION = YES; 201 | CLANG_WARN_INFINITE_RECURSION = YES; 202 | CLANG_WARN_INT_CONVERSION = YES; 203 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 204 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 205 | CLANG_WARN_UNREACHABLE_CODE = YES; 206 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 207 | COPY_PHASE_STRIP = YES; 208 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 209 | ENABLE_NS_ASSERTIONS = NO; 210 | ENABLE_STRICT_OBJC_MSGSEND = YES; 211 | GCC_C_LANGUAGE_STANDARD = gnu99; 212 | GCC_NO_COMMON_BLOCKS = YES; 213 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 214 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 215 | GCC_WARN_UNDECLARED_SELECTOR = YES; 216 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 217 | GCC_WARN_UNUSED_FUNCTION = YES; 218 | GCC_WARN_UNUSED_VARIABLE = YES; 219 | MACOSX_DEPLOYMENT_TARGET = 10.10; 220 | MTL_ENABLE_DEBUG_INFO = NO; 221 | SDKROOT = macosx; 222 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 223 | }; 224 | name = Release; 225 | }; 226 | 41C0651E1A27E957002B860F /* Debug */ = { 227 | isa = XCBuildConfiguration; 228 | buildSettings = { 229 | PRODUCT_NAME = "$(TARGET_NAME)"; 230 | SWIFT_VERSION = 3.0; 231 | }; 232 | name = Debug; 233 | }; 234 | 41C0651F1A27E957002B860F /* Release */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | PRODUCT_NAME = "$(TARGET_NAME)"; 238 | SWIFT_VERSION = 3.0; 239 | }; 240 | name = Release; 241 | }; 242 | /* End XCBuildConfiguration section */ 243 | 244 | /* Begin XCConfigurationList section */ 245 | 41C065111A27E957002B860F /* Build configuration list for PBXProject "DollarBreak" */ = { 246 | isa = XCConfigurationList; 247 | buildConfigurations = ( 248 | 41C0651B1A27E957002B860F /* Debug */, 249 | 41C0651C1A27E957002B860F /* Release */, 250 | ); 251 | defaultConfigurationIsVisible = 0; 252 | defaultConfigurationName = Release; 253 | }; 254 | 41C0651D1A27E957002B860F /* Build configuration list for PBXNativeTarget "DollarBreak" */ = { 255 | isa = XCConfigurationList; 256 | buildConfigurations = ( 257 | 41C0651E1A27E957002B860F /* Debug */, 258 | 41C0651F1A27E957002B860F /* Release */, 259 | ); 260 | defaultConfigurationIsVisible = 0; 261 | defaultConfigurationName = Release; 262 | }; 263 | /* End XCConfigurationList section */ 264 | }; 265 | rootObject = 41C0650E1A27E957002B860F /* Project object */; 266 | } 267 | -------------------------------------------------------------------------------- /DollarBreak/algorithm_overview.txt: -------------------------------------------------------------------------------- 1 | Algorithm Overview 2 | 3 | Here is the logic for figuring out how many ways there are to create 25 cents using 4 | only quarters, dimes, nickels, and pennies. The logic to solve this is the same logic 5 | needed to figure out how many ways there are to break a dollar. 6 | 7 | Here is a quick reference for people unfamiliar with American coins. 8 | Q = Quarter (25 cents) 9 | D = Dime (10 cents) 10 | N = Nickel ( 5 cents) 11 | P = Penny ( 1 cent) 12 | 13 | This table lists every way to create 25 cents. 14 | Q D N P 15 | -------------- 16 | 0 0 0 25 17 | 0 0 1 20 18 | 0 0 2 15 19 | 0 0 3 10 20 | 0 0 4 05 21 | 0 0 5 00 22 | 0 1 0 15 23 | 0 1 1 10 24 | 0 1 2 05 25 | 0 1 3 00 26 | 0 2 0 05 27 | 0 2 1 00 28 | 1 0 0 00 29 | 30 | This pseudo-code outlines the algorithm. 31 | fullAmount = 25 32 | loop Q from 0 to fullAmount/25: 33 | 34 | remainingAmount = fullAmount - (25 * Q) 35 | loop D from 0 to remainingAmount/10: 36 | 37 | remainingAmount = remainingAmount - (10 * D) 38 | loop N from 0 to remainingAmount/5: 39 | 40 | remainingAmount = remainingAmount - (5 * N) 41 | // there's only one way to create a value using pennies 42 | -------------------------------------------------------------------------------- /DollarBreak/coin.swift: -------------------------------------------------------------------------------- 1 | // 2 | // coin.swift 3 | // DollarBreak 4 | // 5 | // Created by Josh Smith on 11/29/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | enum Coin: Int 12 | { 13 | case silverDollar = 100 14 | case halfDollar = 50 15 | case quarter = 25 16 | case dime = 10 17 | case nickel = 5 18 | case penny = 1 19 | 20 | static func coinsInDescendingOrder() -> [Coin] 21 | { 22 | return [ 23 | Coin.silverDollar, 24 | Coin.halfDollar, 25 | Coin.quarter, 26 | Coin.dime, 27 | Coin.nickel, 28 | Coin.penny 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DollarBreak/functional.swift: -------------------------------------------------------------------------------- 1 | // 2 | // functional.swift 3 | // DollarBreak 4 | // 5 | // Created by Josh Smith on 11/29/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class Functional { 12 | static func countWaysToBreakAmount(_ amount: Int, usingCoins coins: [Coin]) -> Int { 13 | let (coin, smallerCoins) = (coins[0], coins[1.. Int { 13 | let coin = coins[0] 14 | if coin == .penny { 15 | return 1 16 | } 17 | 18 | var smallerCoins = [Coin]() 19 | for index in 1..