From f8c9f0b6f5a2c557cf5196e9559719ef6f207d88 Mon Sep 17 00:00:00 2001 From: Wenru Dong Date: Wed, 3 Oct 2018 22:19:11 +0100 Subject: [PATCH] add implementations of array in python and objective-c --- object-c/05_array/MyArray.h | 19 ++ object-c/05_array/MyArray.m | 70 +++++ .../algo/algo.xcodeproj/project.pbxproj | 277 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../UserInterfaceState.xcuserstate | Bin 0 -> 5181 bytes .../xcschemes/xcschememanagement.plist | 14 + object-c/05_array/algo/algo/MyArray.h | 13 + object-c/05_array/algo/algo/MyArray.m | 13 + object-c/05_array/algo/algo/main.m | 17 ++ python/05_array/myarray.py | 60 ++++ 11 files changed, 498 insertions(+) create mode 100644 object-c/05_array/MyArray.h create mode 100644 object-c/05_array/MyArray.m create mode 100644 object-c/05_array/algo/algo.xcodeproj/project.pbxproj create mode 100644 object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 object-c/05_array/algo/algo.xcodeproj/project.xcworkspace/xcuserdata/wenrudong.xcuserdatad/UserInterfaceState.xcuserstate create mode 100644 object-c/05_array/algo/algo.xcodeproj/xcuserdata/wenrudong.xcuserdatad/xcschemes/xcschememanagement.plist create mode 100644 object-c/05_array/algo/algo/MyArray.h create mode 100644 object-c/05_array/algo/algo/MyArray.m create mode 100644 object-c/05_array/algo/algo/main.m create mode 100644 python/05_array/myarray.py 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 0000000000000000000000000000000000000000..abd58833d57fd0c49dc7dc61a528159922b28efe GIT binary patch literal 5181 zcma)A3wRV&mOi&ay6f3hK?o2YX-Fflbm%0V7tb{51OiDD(@Dq+nod%LGM(;N-7&^Y zp%+mDQajAmGmEI4a-+1=k%{9d#5Ke2n_Bj<1<-X9h=ynOT>0Pj#i6^rGKq z@_nhQd+&MNbN=(+bJNim(xNe!>kJTJAPpu#I#|d*k^7q+*Q$C{i-emUk&dg?&RDd? z5!5@kYdGBCh(-D)1G#17XG#dHz`_+U1*XD$xDpn?LRbWgVF@gSVkm)XSPvVZ25P|v z8=(%af=$p2o1q)Fg9cYa4}>5BgK$0E06Sqf+zPkBKfpckV;F&dg`dC?I10z$m+%_A z4sXD(;7xc7PQht71LxpB;UoAhd<^H|3-~j9317iq;qUMb0YZqCh$NFtBXh|-awW+n z`D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`~;w$1W#n;7OiEoMTiJyso6wix)7QYg|7B7n5NJL7LL}`YUCC!v(ON%9^ zv{WjP+)}Z$TJlO&(mH9qv`N|`ZIwEtE~!WAl_JtLQdAn2Zjts%`=ke?f00I{L(;?2 zPozhrpGhxBuS&m>&f9p~eA@!sBHI$1(^h5MXlt@{*ml|OvhB6qW4q6GzwL3`leVL_ zr)Np%*eTNk=!P4m2Z)MEdNw~T7Fi3PCg;OAipU8LOvy*km+@{>E+@sv5+^>u&4=IO~hn1tsG36=c g8RbRg-<6k?ca#s5-^R}sU>NgB9x$JYzse{72j@-oLI3~& literal 0 HcmV?d00001 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