Skip to content

Commit

Permalink
Add first assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Jul 19, 2016
1 parent 30d3373 commit 1166e0d
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Assignment-1.md
Original file line number Diff line number Diff line change
@@ -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!
252 changes: 252 additions & 0 deletions CommandLine/CommandLine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -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 = "<group>"; };
68B41FCA1D3DFB1E00AAEC62 /* data.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = data.csv; sourceTree = "<group>"; };
/* 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 = "<group>";
};
68B41FC11D3DEB5900AAEC62 /* Products */ = {
isa = PBXGroup;
children = (
68B41FC01D3DEB5900AAEC62 /* CommandLine */,
);
name = Products;
sourceTree = "<group>";
};
68B41FC21D3DEB5900AAEC62 /* CommandLine */ = {
isa = PBXGroup;
children = (
68B41FC31D3DEB5900AAEC62 /* main.swift */,
68B41FCA1D3DFB1E00AAEC62 /* data.csv */,
);
path = CommandLine;
sourceTree = "<group>";
};
/* 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 */;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions CommandLine/CommandLine/data.csv
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 1166e0d

Please sign in to comment.