Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,72 @@

@interface CaesarCipher : NSObject

- (NSString *)encode:(NSString *)string offset:(int)offset;
- (NSString *)decode:(NSString *)string offset:(int)offset;
- (NSString *)encode:(NSString *)string offset:(int)offset;
- (BOOL)codeBreaker: (NSString *)string word:(NSString*)string2;

@end


@implementation CaesarCipher

- (NSString *)encode:(NSString *)string offset:(int)offset {
if (offset > 25) {
NSAssert(offset < 26, @"offset is out of range. 1 - 25");
}
NSString *str = [string lowercaseString];
unsigned long count = [string length];
unichar result[count];
unichar buffer[count];
[str getCharacters:buffer range:NSMakeRange(0, count)];
[string getCharacters:buffer range:NSMakeRange(0, count)];

char allchars[] = "abcdefghijklmnopqrstuvwxyz";

for (int i = 0; i < count; i++) {
if (buffer[i] == ' ' || ispunct(buffer[i])) {
result[i] = buffer[i];
continue;
}

char *e = strchr(allchars, buffer[i]);
int idx= (int)(e - allchars);
int new_idx = (idx + offset) % strlen(allchars);

result[i] = allchars[new_idx];
int low = islower(buffer[i]) ? 'a' : 'A';
result[i] = (buffer[i]%low + offset)%26 + low;
}

return [NSString stringWithCharacters:result length:count];
}

- (NSString *)decode:(NSString *)string offset:(int)offset {
return [self encode:string offset: (26 - offset)];
}

- (BOOL)codeBreaker: (NSString *)string word:(NSString*)string2{
//Method 1
// for (int i=1; i<=25; i++){
// for(int j=1;j<=25;j++){
// if([[self decode:string offset:i] isEqualToString:[self decode:string2 offset:j]]){
// return 1;
// }
// }
// }
//Method 2 thanks to V's suggestion
for(int i=1;i<25;i++){
if([string isEqualToString:[self encode:string2 offset:i]]){
return 1;
}
}
return 0;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend getting used to YES and NO as the two bool values. 1 and 0 is fine, but not the objective-c way.

@end


int main(int argc, const char * argv[]) {
@autoreleasepool {
CaesarCipher * ceasarCipher = [[CaesarCipher alloc]init];
NSString *string = @"mike";
NSString *string2 = @"mesfin";
NSString *result =[ceasarCipher encode:string offset:2];
NSString *result2 = [ceasarCipher encode:string2 offset:7];

NSLog(@"This is the first string, %@ and this is the second %@",result,result2);

BOOL result3 = [ceasarCipher codeBreaker:result word:result2];
NSLog(@"\n");

NSLog(@"%hhd",result3);
}
}
50 changes: 45 additions & 5 deletions Election/Election/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
// forward declarations
@class Contender;
@class Election;
@class VotingSimulator;

// Contender class
@interface Contender : NSObject

- (instancetype)initWithName:(NSString *)name;

- (void)addVote;
- (NSInteger)votesReceived;
- (NSString *)name;


@end

@implementation Contender {
Expand Down Expand Up @@ -87,9 +89,9 @@ - (instancetype)initWithElectionName:(NSString *)name {
}

- (void)addContender:(Contender *)contender {
if (_listOfContenders == nil) {
_listOfContenders = [[NSMutableArray alloc] init];
}
if (_listOfContenders == nil) {
_listOfContenders = [[NSMutableArray alloc] init];
}
[_listOfContenders addObject:contender];
}

Expand Down Expand Up @@ -146,7 +148,7 @@ - (void)vote {
printf("Contender does not exist...\n");
}
}

}

@end
Expand Down Expand Up @@ -201,10 +203,48 @@ - (BOOL)pollsOpen {

@end

// Voting Simulator Class
@interface VotingSimulator : NSObject

-(void) simulate;

@end

@implementation VotingSimulator

-(void) simulate{

Election* election = [[Election alloc]init];
Contender* c = [[Contender alloc]init];
Contender* c2 = [[Contender alloc]init];
Contender* c3 = [[Contender alloc]init];

[c initWithName:@"Carlos"];
[c2 initWithName:@"Carl"];
[c3 initWithName:@"Steven"];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

222 - 224 is reainitializing your 3 contenders. You should use this method on lines 218-220.


[election setElectionName:@"The Election"];
[election addContender:c];
[election addContender:c2];
[election addContender:c3];




ElectionManager *em = [[ElectionManager alloc]init];
[em manage:election];
[em initiatePolling];

[em displayResults];
}

@end

int main(int argc, const char * argv[]) {
@autoreleasepool {
VotingSimulator *vs = [[VotingSimulator alloc]init];
[vs simulate];

}
return 0;
Expand Down
45 changes: 43 additions & 2 deletions Person/Person/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ - (NSString *)city;
- (void)setPhoneNumber:(NSString *)phoneNumber;
- (NSString *)phoneNumber;

-(BOOL)checkSameCity:(Person *)person;

-(Person *)registerChild;
@end

@implementation Person {
Expand Down Expand Up @@ -51,13 +54,51 @@ - (NSString *)phoneNumber {
return _phoneNumber;
}

-(BOOL)checkSameCity:(Person *)person{
if([[self city] isEqualToString:[person city]]){
return YES;
}
return NO;
}

-(Person *)registerChild{
Person *child = [[Person alloc]init];
child.phoneNumber = self.phoneNumber;
child.city = self.city;

return child;
}
@end


int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Person* carl = [[Person alloc]init];

Person* lisa = [[Person alloc]init];

[lisa setCity:@"Gotham"];

[carl setName:@"Carl"];

[carl setCity:@"Gotham"];

[carl setPhoneNumber:@"234-843-2333"];


Person *child = [carl registerChild];

BOOL result = [carl checkSameCity:lisa];

NSLog(@"%hhd",result);

NSLog(@"%@",[carl name]);
NSLog(@"%@",[carl city]);
NSLog(@"%@",[carl phoneNumber]);

NSLog(@"%@",[child phoneNumber]);


}
return 0;
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ Write a program to simulate an election. Create a class called **VotingSimulator
4. Ask the ElectionManager to ***initiatePolling***
5. Follow the instructions on the console. After each round of polling you will be asked(within the console) whether you want to continue or not.
6. Ask the ElectionManager to ***displayResults***


something changed