diff --git a/object-c/05_array/MyArray.h b/object-c/05_array/MyArray.h new file mode 100644 index 00000000..4d411bb6 --- /dev/null +++ b/object-c/05_array/MyArray.h @@ -0,0 +1,19 @@ +// +// MyArray.h +// algo +// +// Created by Wenru Dong on 2018/10/3. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import + +@interface MyArray : NSObject + +- (instancetype)initWithCapacity:(NSUInteger)capacity; +- (id)objectAtIndexedSubscript:(NSUInteger)index; +- (void)removeObjectAtIndex:(NSUInteger)index; +- (void)insertObject:(id)anObject atIndex:(NSUInteger)index; +- (void)addObject:(id)anObject; +- (void)printAll; +@end diff --git a/object-c/05_array/MyArray.m b/object-c/05_array/MyArray.m new file mode 100644 index 00000000..451f8a0d --- /dev/null +++ b/object-c/05_array/MyArray.m @@ -0,0 +1,70 @@ +// +// MyArray.m +// algo +// +// Created by Wenru Dong on 2018/10/3. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import "MyArray.h" + +@implementation MyArray +{ + @private + NSMutableArray *_data; + NSUInteger _capacity; + NSUInteger _count; +} + +- (instancetype)initWithCapacity:(NSUInteger)capacity { + self = [super init]; + if (self) { + _data = [NSMutableArray arrayWithCapacity:capacity]; + _capacity = capacity; + _count = 0; + } + return self; +} + +- (id)objectAtIndexedSubscript:(NSUInteger)index { + if (index >= _count) return nil; + return _data[index]; +} + +- (void)removeObjectAtIndex:(NSUInteger)index { + if (index >= _count) { + [NSException raise:NSRangeException format:@"Index out of range."]; + } + for (NSUInteger i = index + 1; i < _data.count; i++) { + _data[i-1] = _data[i]; + } + _count--; +} + +- (void)insertObject:(nonnull id)anObject atIndex:(NSUInteger)index { + if (index >= _count || _count == _capacity) { + [NSException raise:NSRangeException format:@"Index out of range."]; + } + for (NSUInteger i = _count - 1; i >= index; i--) { + _data[i+1] = _data[i]; + } + _data[index] = anObject; + _count++; +} + +// insertToTail +- (void)addObject:(nonnull id)anObject { + if (_count == _capacity) { + [NSException raise:NSRangeException format:@"Array is full."]; + } + [_data addObject:anObject]; + _count++; +} + +- (void)printAll { + for (id obj in _data) { + NSLog(@"%@", obj); + } +} + +@end diff --git a/object-c/05_array/algo/algo.xcodeproj/project.pbxproj b/object-c/05_array/algo/algo.xcodeproj/project.pbxproj new file mode 100644 index 00000000..949357d8 --- /dev/null +++ b/object-c/05_array/algo/algo.xcodeproj/project.pbxproj @@ -0,0 +1,277 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 80EF91E12165521D00600B1E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 80EF91E02165521D00600B1E /* main.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 80EF91DB2165521D00600B1E /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 80EF91DD2165521D00600B1E /* algo */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = algo; sourceTree = BUILT_PRODUCTS_DIR; }; + 80EF91E02165521D00600B1E /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 80EF91DA2165521D00600B1E /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 80EF91D42165521D00600B1E = { + isa = PBXGroup; + children = ( + 80EF91DF2165521D00600B1E /* algo */, + 80EF91DE2165521D00600B1E /* Products */, + ); + sourceTree = ""; + }; + 80EF91DE2165521D00600B1E /* Products */ = { + isa = PBXGroup; + children = ( + 80EF91DD2165521D00600B1E /* algo */, + ); + name = Products; + sourceTree = ""; + }; + 80EF91DF2165521D00600B1E /* algo */ = { + isa = PBXGroup; + children = ( + 80EF91E02165521D00600B1E /* main.m */, + ); + path = algo; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 80EF91DC2165521D00600B1E /* algo */ = { + isa = PBXNativeTarget; + buildConfigurationList = 80EF91E42165521D00600B1E /* Build configuration list for PBXNativeTarget "algo" */; + buildPhases = ( + 80EF91D92165521D00600B1E /* Sources */, + 80EF91DA2165521D00600B1E /* Frameworks */, + 80EF91DB2165521D00600B1E /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = algo; + productName = algo; + productReference = 80EF91DD2165521D00600B1E /* algo */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 80EF91D52165521D00600B1E /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0940; + ORGANIZATIONNAME = "Wenru Dong"; + TargetAttributes = { + 80EF91DC2165521D00600B1E = { + CreatedOnToolsVersion = 9.4.1; + }; + }; + }; + buildConfigurationList = 80EF91D82165521D00600B1E /* Build configuration list for PBXProject "algo" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 80EF91D42165521D00600B1E; + productRefGroup = 80EF91DE2165521D00600B1E /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 80EF91DC2165521D00600B1E /* algo */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 80EF91D92165521D00600B1E /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 80EF91E12165521D00600B1E /* main.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 80EF91E22165521D00600B1E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + 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 = gnu11; + 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.13; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 80EF91E32165521D00600B1E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + 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 = gnu11; + 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.13; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 80EF91E52165521D00600B1E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 80EF91E62165521D00600B1E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 80EF91D82165521D00600B1E /* Build configuration list for PBXProject "algo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 80EF91E22165521D00600B1E /* Debug */, + 80EF91E32165521D00600B1E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 80EF91E42165521D00600B1E /* Build configuration list for PBXNativeTarget "algo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 80EF91E52165521D00600B1E /* Debug */, + 80EF91E62165521D00600B1E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 80EF91D52165521D00600B1E /* Project object */; +} diff --git a/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..3b46c1a9 --- /dev/null +++ b/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcuserdata/wenrudong.xcuserdatad/UserInterfaceState.xcuserstate b/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcuserdata/wenrudong.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 00000000..abd58833 Binary files /dev/null and b/object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcuserdata/wenrudong.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/object-c/05_array/algo/algo.xcodeproj/xcuserdata/wenrudong.xcuserdatad/xcschemes/xcschememanagement.plist b/object-c/05_array/algo/algo.xcodeproj/xcuserdata/wenrudong.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 00000000..d45f8510 --- /dev/null +++ b/object-c/05_array/algo/algo.xcodeproj/xcuserdata/wenrudong.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + algo.xcscheme + + orderHint + 0 + + + + diff --git a/object-c/05_array/algo/algo/MyArray.h b/object-c/05_array/algo/algo/MyArray.h new file mode 100644 index 00000000..63be00e4 --- /dev/null +++ b/object-c/05_array/algo/algo/MyArray.h @@ -0,0 +1,13 @@ +// +// MyArray.h +// algo +// +// Created by Wenru Dong on 2018/10/3. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import + +@interface MyArray : NSObject + +@end diff --git a/object-c/05_array/algo/algo/MyArray.m b/object-c/05_array/algo/algo/MyArray.m new file mode 100644 index 00000000..972c76df --- /dev/null +++ b/object-c/05_array/algo/algo/MyArray.m @@ -0,0 +1,13 @@ +// +// MyArray.m +// algo +// +// Created by Wenru Dong on 2018/10/3. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import "MyArray.h" + +@implementation MyArray + +@end diff --git a/object-c/05_array/algo/algo/main.m b/object-c/05_array/algo/algo/main.m new file mode 100644 index 00000000..5bac8d32 --- /dev/null +++ b/object-c/05_array/algo/algo/main.m @@ -0,0 +1,17 @@ +// +// main.m +// algo +// +// Created by Wenru Dong on 2018/10/3. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import + +int main(int argc, const char * argv[]) { + @autoreleasepool { + // insert code here... + NSLog(@"Hello, World!"); + } + return 0; +} diff --git a/python/05_array/myarray.py b/python/05_array/myarray.py new file mode 100644 index 00000000..af554804 --- /dev/null +++ b/python/05_array/myarray.py @@ -0,0 +1,60 @@ +from typing import Optional +# +# 1) Insertion, deletion and random access of array +# 2) Assumes int for element type +# +# Author: Wenru +# +class MyArray: + """A simple wrapper around List. + You cannot have -1 in the array. + """ + def __init__(self, capacity: int): + self._data = [] + self._count = 0 + self._capacity = capacity + + def __getitem__(self, position: int) -> int: + """Support for subscript. + Perhaps better than the find() method below. + """ + return self._data[position] + + def find(self, index: int) -> Optional[int]: + if index >= self._count or index <= -self._count: return None + return self._data[index] + + def delete(self, index: int) -> bool: + if index >= self._count or index <= -self._count: return False + self._data[index:-1] = self._data[index+1:] + self._count -= 1 + return True + + def insert(self, index: int, value: int) -> bool: + if index >= self._count or index <= -self._count: return False + if self._capacity == self._count: return False + self._data.insert(index, value) + self._count += 1 + return True + + def insert_to_tail(self, value: int) -> bool: + if self._count == self._capacity: return False + self._data.append(value) + self._count += 1 + return True + + def __repr__(self) -> str: + return " ".join(str(num) for num in self._data[:self._count]) + + def print_all(self): + for num in self._data[:self._count]: + print(f"{num}", end=" ") + print("\n", flush=True) + +if __name__ == "__main__": + a = MyArray(6) + for i in range(6): + a.insert_to_tail(i) + + a.delete(2) + print(a) \ No newline at end of file