Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 2 HW - Henna #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
Binary file modified CaesarCipher/.DS_Store
Binary file not shown.
57 changes: 48 additions & 9 deletions CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,81 @@

@interface CaesarCipher : NSObject

- (NSString *)decode:(NSString *)string offset:(int)offset;
- (NSString *)encode:(NSString *)string offset:(int)offset;
- (NSString *)decode:(NSString *)string offset:(int)offset;

//Being amateur codebreakers, we want to know if two distinct looking ciphers correspond to the same input message. Write a method called codeBreaker, which accepts two cipher strings as paramaters and returns a boolean value which tells us whether they are actually the same input message encoded using two different offsets.

- (BOOL) codeBreaker:(NSString *) string1 with:(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];
[string getCharacters:buffer range:NSMakeRange(0, count)];

[str 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;
}

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

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

result[i] = allchars[new_idx];
}

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

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

- (BOOL) codeBreaker:(NSString *) string1 with:(NSString *) string2{

//Loop through offset on first encoded input and decode
for (int i = 1; i<25; i++) {
NSString * decoded = [self decode:string1 offset:i];
//Loop through offset on second encoded input and decode
for (int j = 1; j < 25; j++) {
NSString * decoded2 = [self decode:string2 offset:j];

if ([decoded isEqualToString:decoded2]) {
NSLog(@"%@ = '%@', offset: %d", string1, decoded, i);
NSLog(@"%@ = '%@', offset: %d", string2, decoded2, j);
return YES;
}

}

Copy link
Contributor

Choose a reason for hiding this comment

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

This code is technically correct, however, it can be optimized into a single loop:

    for (int i = 1; i<25; i++) {
            NSString * decoded2 = [self decode:string2 offset:i];
            if ([string1 isEqualToString:decoded2]) {
                return YES;
            }
    }

    return NO;

The difference is wost case 25 iterations vs. worst case 600+ iterations.

}

return NO;

}

@end


int main(int argc, const char * argv[]) {
@autoreleasepool {


CaesarCipher *decrypt = [[CaesarCipher alloc] init];
[decrypt codeBreaker:@"okmg" with:@"tprl"];

}
}
}
69 changes: 65 additions & 4 deletions Person/Person/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ - (NSString *)city;
- (void)setPhoneNumber:(NSString *)phoneNumber;
- (NSString *)phoneNumber;

- (void)changePersonsName:(Person *) aPerson
toName:(NSString *) newName;

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

- (Person*)registerChild;

@end

@implementation Person {
NSString *_name;
NSString *_phoneNumber;
NSString *_name; //can be access from anywhere in your class -- might have to be selective later
NSString *_phoneNumber; //can't be accessed from instance variable?
NSString *_city;
}

Expand Down Expand Up @@ -51,13 +58,67 @@ - (NSString *)phoneNumber {
return _phoneNumber;
}

- (void)changePersonsName:(Person *) aPerson toName:(NSString *) newName{

[aPerson setName:newName];

}

- (BOOL)checkSameCity:(Person *) name{

if ([[name city] isEqualToString:[self city]]) {
return YES;
}
else{
return NO;
}

}

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

return child;

}

@end


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

Person *Henna = [[Person alloc]init]; //allocates memory and initializes a Person object
[Henna setName: @"Henna"];
[Henna setCity: @"New York"];
[Henna setPhoneNumber: @"7187777777"];

Person *Bob = [[Person alloc]init];
[Bob setName: @"Bob"];
[Bob setCity: @"New Jersey"];
[Bob setPhoneNumber: @"7187777778"];

Person *Clara = [[Person alloc]init];
[Clara setName: @"Clara"];
[Clara setCity: @"New York"];
[Clara setPhoneNumber: @"7187774778"];

BOOL hennaAndClara = [Henna checkSameCity:Clara];
BOOL claraAndBob = [Clara checkSameCity:Bob];

NSLog(@"%d %d", hennaAndClara, claraAndBob);


//A Person has recently had a child, whose name is 'Abc'. Write a method called registerChild which takes 0 parameters and returns a new Person * instance represeting the child, which has the same phoneNumber and city as the parent.

Person *Abc = [Henna registerChild];
[Abc setName:@"Abc"];
NSLog(@"%@'s baby, %@, lives in %@ and it's phone number is %@", [Henna name], [Abc name], [Abc city], [Abc phoneNumber]);



}
return 0;
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,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***


comment for branch