diff --git a/Assignment-1.md b/Assignment-1.md new file mode 100644 index 0000000..4c859f4 --- /dev/null +++ b/Assignment-1.md @@ -0,0 +1,106 @@ +# Assignment 1 + +In this assignment, you'll make a command-line app that does the following things: + +* Read a CSV file, which contains strings and numbers +* Divide the data into two groups +* Print a report based on these two groups to the console + +## Details: + +Given a text file containing this data: + + Shipment Type,Weight (t) + Neptune Fuel,6 + Neptune Fuel,5 + Neptune Fuel,6 + Neptune Fuel,4 + Mars Food,3 + Neptune Fuel,10 + Mars Food,3 + Neptune Fuel,6 + Mars Food,10 + Neptune Fuel,8 + Mars Food,7 + Mars Food,2 + Mars Food,9 + Neptune Fuel,5 + Neptune Fuel,7 + Mars Food,8 + Mars Food,7 + Mars Food,9 + +The program should produce the following output: + + Mars Food: 58 + Neptune Fuel: 57 + +### Notes + +* The first line of the data simply describes the content - it doesn't contain data to use. +* The application should error if it encounters any data besides "Mars Food" and "Neptune Fuel". +* The numbers in the data are always integers. + +## Some tools + +Here are some useful tips that can help you to build this program. + +### How to add a text file + +Open the File menu, and choose New -> New File. + +![Choosing File->New->New File](Figures/new-file-1.png) + +Scroll down to the OS X section, and choose Empty File. + +![Creating a new Empty File](Figures/new-file-2.png) + +Name the new file data.csv. + +### How to make sure the text file is in the same folder as your built product + +Select the Project at the top of the Project navigator. + +![Add File 1](Figures/add-file-1.png) + +Choose Build Phases at the top of the window. + +![Add File 2](Figures/add-file-2.png) + +Open the Copy File section, and drag the file into the list. + +![Add File 3](Figures/add-file-3.png) + +Turn off **Copy only when installing**, and set the **Destination** to *Products Directory*. + +![Add File 4](Figures/add-file-4.png) + +When you build and run the program, the file will be copied into the same directory as the compiled program. + +### How to open a file + +Use the `contentsOfFile` initialiser for `String`: + + String(contentsOfFile: "data.csv") + +Note that this returns an optional! + +### How to split a string + +Use the `componentsSeparatedByString` method on a string: + + data.componentsSeparatedByString("\n") + +Note that not all text files end their lines with \n! A better option is to use the `NSCharacterSet` class and the `componentsSeparatedByCharactersInSet`: + + let newlineCharacterSet = NSCharacterSet.newlineCharacterSet() + + data.componentsSeparatedByCharactersInSet(newlineCharacterSet) + +### How to convert a `String` to an `Int` + +Pass the string to the `Int`'s initialiser: + + let theInt = Int("42") + +Note that this returns an optional, because the string might not contain an int! diff --git a/CommandLine/CommandLine.xcodeproj/project.pbxproj b/CommandLine/CommandLine.xcodeproj/project.pbxproj new file mode 100644 index 0000000..a2bf692 --- /dev/null +++ b/CommandLine/CommandLine.xcodeproj/project.pbxproj @@ -0,0 +1,252 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 68B41FC41D3DEB5900AAEC62 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68B41FC31D3DEB5900AAEC62 /* main.swift */; }; + 68B41FCB1D3DFB3800AAEC62 /* data.csv in CopyFiles */ = {isa = PBXBuildFile; fileRef = 68B41FCA1D3DFB1E00AAEC62 /* data.csv */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 68B41FBE1D3DEB5900AAEC62 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 12; + dstPath = ""; + dstSubfolderSpec = 16; + files = ( + 68B41FCB1D3DFB3800AAEC62 /* data.csv in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 68B41FC01D3DEB5900AAEC62 /* CommandLine */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CommandLine; sourceTree = BUILT_PRODUCTS_DIR; }; + 68B41FC31D3DEB5900AAEC62 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + 68B41FCA1D3DFB1E00AAEC62 /* data.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = data.csv; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 68B41FBD1D3DEB5900AAEC62 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 68B41FB71D3DEB5900AAEC62 = { + isa = PBXGroup; + children = ( + 68B41FC21D3DEB5900AAEC62 /* CommandLine */, + 68B41FC11D3DEB5900AAEC62 /* Products */, + ); + sourceTree = ""; + }; + 68B41FC11D3DEB5900AAEC62 /* Products */ = { + isa = PBXGroup; + children = ( + 68B41FC01D3DEB5900AAEC62 /* CommandLine */, + ); + name = Products; + sourceTree = ""; + }; + 68B41FC21D3DEB5900AAEC62 /* CommandLine */ = { + isa = PBXGroup; + children = ( + 68B41FC31D3DEB5900AAEC62 /* main.swift */, + 68B41FCA1D3DFB1E00AAEC62 /* data.csv */, + ); + path = CommandLine; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 68B41FBF1D3DEB5900AAEC62 /* CommandLine */ = { + isa = PBXNativeTarget; + buildConfigurationList = 68B41FC71D3DEB5900AAEC62 /* Build configuration list for PBXNativeTarget "CommandLine" */; + buildPhases = ( + 68B41FBC1D3DEB5900AAEC62 /* Sources */, + 68B41FBD1D3DEB5900AAEC62 /* Frameworks */, + 68B41FBE1D3DEB5900AAEC62 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = CommandLine; + productName = CommandLine; + productReference = 68B41FC01D3DEB5900AAEC62 /* CommandLine */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 68B41FB81D3DEB5900AAEC62 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0730; + LastUpgradeCheck = 0730; + ORGANIZATIONNAME = "Secret Lab"; + TargetAttributes = { + 68B41FBF1D3DEB5900AAEC62 = { + CreatedOnToolsVersion = 7.3; + }; + }; + }; + buildConfigurationList = 68B41FBB1D3DEB5900AAEC62 /* Build configuration list for PBXProject "CommandLine" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 68B41FB71D3DEB5900AAEC62; + productRefGroup = 68B41FC11D3DEB5900AAEC62 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 68B41FBF1D3DEB5900AAEC62 /* CommandLine */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 68B41FBC1D3DEB5900AAEC62 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 68B41FC41D3DEB5900AAEC62 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 68B41FC51D3DEB5900AAEC62 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 68B41FC61D3DEB5900AAEC62 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 68B41FC81D3DEB5900AAEC62 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 68B41FC91D3DEB5900AAEC62 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 68B41FBB1D3DEB5900AAEC62 /* Build configuration list for PBXProject "CommandLine" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 68B41FC51D3DEB5900AAEC62 /* Debug */, + 68B41FC61D3DEB5900AAEC62 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 68B41FC71D3DEB5900AAEC62 /* Build configuration list for PBXNativeTarget "CommandLine" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 68B41FC81D3DEB5900AAEC62 /* Debug */, + 68B41FC91D3DEB5900AAEC62 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 68B41FB81D3DEB5900AAEC62 /* Project object */; +} diff --git a/CommandLine/CommandLine.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/CommandLine/CommandLine.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..75b816c --- /dev/null +++ b/CommandLine/CommandLine.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/CommandLine/CommandLine/data.csv b/CommandLine/CommandLine/data.csv new file mode 100644 index 0000000..4bc2a4c --- /dev/null +++ b/CommandLine/CommandLine/data.csv @@ -0,0 +1,19 @@ +Shipment Type,Weight (t) +Neptune Fuel,6 +Neptune Fuel,5 +Neptune Fuel,6 +Neptune Fuel,4 +Mars Food,3 +Neptune Fuel,10 +Mars Food,3 +Neptune Fuel,6 +Mars Food,10 +Neptune Fuel,8 +Mars Food,7 +Mars Food,2 +Mars Food,9 +Neptune Fuel,5 +Neptune Fuel,7 +Mars Food,8 +Mars Food,7 +Mars Food,9 \ No newline at end of file diff --git a/CommandLine/CommandLine/main.swift b/CommandLine/CommandLine/main.swift new file mode 100644 index 0000000..8ce87e2 --- /dev/null +++ b/CommandLine/CommandLine/main.swift @@ -0,0 +1,44 @@ +// +// main.swift +// CommandLine +// +// Created by Jon Manning on 19/07/2016. +// Copyright © 2016 Secret Lab. All rights reserved. +// + +import Foundation + +let data = try! String(contentsOfFile: "data.csv") + +let lines = data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) + +var foodTotal = 0 +var fuelTotal = 0 + + + +for lineNumber in 1..