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

Person & Caeser Cipher #35

Open
wants to merge 3 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
40 changes: 39 additions & 1 deletion CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ @interface CaesarCipher : NSObject

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

- (BOOL)codeBreaker:(NSString *)string1 withStrings:(NSString *)string2;
@end


Expand Down Expand Up @@ -50,11 +50,49 @@ - (NSString *)decode:(NSString *)string offset:(int)offset {
return [self encode:string offset: (26 - offset)];
}

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

BOOL result = NO;

for (int i = 1; i <= 25; i++) {

NSString * decodedString = [self decode:string1 offset:i];

for (int j = 1; j <= 25; j++) {

NSString * decodedString2 = [self decode:string2 offset:j];

if ([decodedString isEqualToString:decodedString2]) {

result = YES;

NSLog(@"%@ decoded is %@ using offset %d", string1, decodedString, i);

NSLog(@"%@ decoded is %@ using offset %d", string2, decodedString2, j);

return result;
}
}
}

return result;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

One suggestion. This method would read a bit better without the result variable:

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

    for (int i = 1; i <= 25; i++) {        
        NSString * decodedString = [self decode:string1 offset:i];

        for (int j = 1; j <= 25; j++) {
            NSString * decodedString2 = [self decode:string2 offset:j];

            if ([decodedString isEqualToString:decodedString2]) {
                NSLog(@"%@ decoded is %@ using offset %d", string1, decodedString, i);
                NSLog(@"%@ decoded is %@ using offset %d", string2, decodedString2, j);

                return YES;
            }
        }
    }

    return NO;
}

@end


int main(int argc, const char * argv[]) {
@autoreleasepool {
CaesarCipher *cipherInstance = [[CaesarCipher alloc]init];

NSString *mike = @"mike";
NSString *eric = @"eric";
NSString *encodedMike1 = [cipherInstance encode:mike offset:2];
NSString *encodedMike2 = [cipherInstance encode:eric offset:3];

BOOL isSameMessage = [cipherInstance codeBreaker:encodedMike1 withStrings:encodedMike2];

NSLog(@"Are these two ciphers the same message? %s", isSameMessage ? "YES":"NO");

}
}
79 changes: 77 additions & 2 deletions Person/Person/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ - (NSString *)city;
- (void)setPhoneNumber:(NSString *)phoneNumber;
- (NSString *)phoneNumber;

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

- (Person *)registerChild;

@end

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

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

if ([[person city] isEqualToString:[self city]]) {

return YES;

} else {

return NO;
}
}

- (Person *)registerChild {

Person *child;

child = [[Person alloc] init];

[child setPhoneNumber:[self phoneNumber]];

[child setCity:[self city]];

return child;

}

- (void)printName {
NSString *personName = self.name;
NSLog(@"Name: %@", personName);
}

@end


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

NSLog(@"This is Robert.");

Person *person = [[Person alloc] init];
[person setName:@"Robert"];

[person printName];

NSLog(@"He lives in New York City and has his own phone number.");
[person setCity:@"New York City"];
[person setPhoneNumber:@"718-123-4567"];

NSLog(@"Robert met Jane.");

Person *person2 = [[Person alloc] init];
[person2 setName:@"Jane"];
[person2 setCity:@"Chicago"];

BOOL result = [person checkSameCity:person2];

NSLog(@"Does she live in the same city as him? %s", result ? "Yes!":"No...");

NSLog(@"Then Robert met Vanessa. She has a phone number too. Do they live in the same city?");

Person *person3 = [[Person alloc] init];
[person3 setName:@"Vanessa"];
[person3 setPhoneNumber:@"718-123-4567"];
[person3 setCity:@"New York City"];

BOOL result2 =[person checkSameCity:person3];

NSLog(@"%s", result2 ? "Yes!":"No...");

NSLog(@"Robert and Vanessa have a child named Bunny who has the same phone number and city as they do.");

Person *child = [person registerChild];

[child setName:@"Bunny"];
BOOL result3 = [child checkSameCity:person];

NSLog(@"%s", result3 ? "Yes!":"No...");

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

Changed the ReadMe breh