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
87 changes: 84 additions & 3 deletions CaesarCipher/CaesarCipher/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@

#import <Foundation/Foundation.h>

/*The Roman General Julius Caesar used to correspond with his generals using a secret code. He devised a way of encrypting his messages using a simple encryption scheme now known as Caesar Cipher or Shift Cipher. You can read more about it here and watch a video here

You are given a class called CaesarCipher with methods encode and decode
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. hint: the maximum offset is 25
There are multiple ways to do this. Try to come up with as many solutions as you can.
Example:
okmg = "mike", offset 2
tprl = "mike", offset 7

Both are the same input message, but different offset. Your method would return YES in this case
*/

@interface CaesarCipher : NSObject

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

- (BOOL)codeBreakerCipher:(NSString *)cipher1 AndOtherCipher:(NSString *)cipher2;

@end


Expand All @@ -29,7 +43,7 @@ - (NSString *)encode:(NSString *)string offset:(int)offset {
[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];
Expand All @@ -39,22 +53,89 @@ - (NSString *)encode:(NSString *)string offset:(int)offset {
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)codeBreakerCipher:(NSString *)cipher1string AndOtherCipher:(NSString *)cipher2string{

CaesarCipher *cipher = [[CaesarCipher alloc] init];

//create an array of all the 26 possible decoded ciphers for cipher 1
NSMutableSet *decodedCipher1array = [[NSMutableSet alloc] init]; //sets used to allow for intersectsSetFunction
for (int i = 1; i < 26; i++) {
[decodedCipher1array addObject:[cipher decode:cipher1string offset:i]];
}

//create an array of all the 26 possible decoded ciphers for cipher 2
NSMutableSet *decodedCipher2array = [[NSMutableSet alloc] init];
for (int i = 1; i < 26; i++) {
[decodedCipher2array addObject:[cipher decode:cipher2string offset:i]];
}

//check if the two decoded arrays have a common element
if ([decodedCipher1array intersectsSet:decodedCipher2array]){
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.

Nice approach! Nice use of a new class. Keep in mind that NSSet is not the same as NSArray. Main difference is that each entry in NSSet is guarenteed to be unique and NSSet is unordered. NSArray can have multiple instances of the same object and is ordered.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the tip on NSSet vs. NSArray!

return YES;
}
else {
return NO;
}
}

@end


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

NSString *word1 = @"yellow";
NSString *word2 = @"yellow";

//encoding cipher 1
CaesarCipher *cipher1 = [[CaesarCipher alloc] init];
NSLog(@"Word 1: %@",word1);
NSString *encodedCipher1 = [cipher1 encode:word1 offset:4];
NSLog(@"Cipher 1 encoded: %@",encodedCipher1);

//encoding cipher 2
CaesarCipher *cipher2 = [[CaesarCipher alloc] init];
NSLog(@"Word 2: %@",word2);
NSString *encodedCipher2 = [cipher2 encode:word2 offset:17];
NSLog(@"Cipher 2 encoded: %@",encodedCipher2);

BOOL C4Q = [cipher1 codeBreakerCipher:encodedCipher1 AndOtherCipher:encodedCipher2];
if (C4Q) {
NSLog(@"The input messages are the same!");
}
else {
NSLog(@"The input messages are NOT THE SAME.");
}

// CaesarCipher *myCipher = [[CaesarCipher alloc] init];
//
// //encodes cipher
// NSString *encodedCipher = [myCipher encode:@"Yes" offset:3];
// NSLog(@"%@",encodedCipher);
//
// //decodes cipher
// NSString *decodedCipher = [myCipher decode:encodedCipher offset:3];
// NSLog(@"%@",decodedCipher);
//
//
// //decoding cipher 1
// NSString *decodedCipher1 = [cipher1 decode:encodedCipher1 offset:5];
// NSLog(@"Cipher 1 decoded: %@",decodedCipher1);
//
// //decoding cipher 2
// NSString *decodedCipher2 = [cipher2 decode:encodedCipher2 offset:5];
// NSLog(@"Cipher 2 decoded: %@",decodedCipher2);

}
}
71 changes: 68 additions & 3 deletions Election/Election/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@

#import <Foundation/Foundation.h>


/*3. BONUS : Voting System

You are given 3 classes

ElectionManager
Conducts an election.
Controls start of voting

Election
Maintains a list of contenders

Contender
Represents details about a contender
What is the contender's name?
How many votes has he received?

Write a program to simulate an election. Create a class called VotingSimulator. In the main , you are required to do the following:

Create an Election object, and given the Election a name
Create a few Contender objects. Add these to the Election object. Make sure that the contender names are distinct!
Create a ElectionManager object. Ask it to manage the Election object created above.
Ask the ElectionManager to initiatePolling
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.
Ask the ElectionManager to displayResults
*/

// forward declarations
@class Contender;
@class Election;
Expand All @@ -17,9 +44,9 @@ @interface Contender : NSObject

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

- (void)addVote;
- (NSInteger)votesReceived;
- (NSString *)name;
- (void)addVote; //setter
- (NSInteger)votesReceived; //getter
- (NSString *)name; //getter

@end

Expand Down Expand Up @@ -201,11 +228,49 @@ - (BOOL)pollsOpen {

@end

// VotingSimulator class

//@interface VotingSimulator
//
//@end
//
//@implementation VotingSimulator
//
//<#methods#>
//
//@end

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

// 1. Create an Election object, and give the Election a name.
Election *knittingClubElection2015 = [[Election alloc] initWithElectionName:@"Knitting Club Election 2015"];

// 2. Create a few Contender objects. Add these to the Election object. Make sure that the contender names are distinct!
Contender *mariah = [[Contender alloc] initWithName:@"Mariah"];
[knittingClubElection2015 addContender:mariah];

Contender *lauren = [[Contender alloc] initWithName:@"Lauren"];
[knittingClubElection2015 addContender:lauren];

Contender *anna = [[Contender alloc] initWithName:@"Anna"];
[knittingClubElection2015 addContender:anna];

[knittingClubElection2015 displayCandidates];

// 3. Create a ElectionManager object. Ask it to manage the Election object created above.
ElectionManager *knittingClubManager = [[ElectionManager alloc] init];
[knittingClubManager manage:knittingClubElection2015];

// 4. Ask the ElectionManager to initiatePolling
[knittingClubManager 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
[knittingClubManager displayResults];


}
return 0;
}
Loading