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

#import <Foundation/Foundation.h>

//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

//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)codeBreaker: (NSString *)string with:(NSString *)otherString;

@end


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


-(BOOL)codeBreaker: (NSString *)string with:(NSString *)otherString {

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

NSString *decodedString = [self decode: string offset: i];
//NSLog(@"string:%@, offset:%d", decodedString, i);

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

NSString *decodedOtherString = [self decode: otherString offset: j];
//NSLog(@"string:%@, offset:%d", decodedOtherString, j);

if ([decodedString isEqualToString: decodedOtherString]){
NSLog(@"Same input.");
return 1;
}
}
}
NSLog(@"Input does not match.");
return 0;
}

@end


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


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


//encoding examples

NSString *example1 = [cipher encode:@"mike" offset: 2];
NSLog(@"%@", example1);

NSString *example2 = [cipher encode:@"mike" offset: 7];
NSLog(@"%@", example2);

NSString *example3 = [cipher encode:@"seat" offset: 7];
NSLog(@"%@", example3);

//decoding examples

NSString *decodedExample1 = [cipher decode:@"okmg" offset: 2];
NSLog(@"%@", decodedExample1);

NSString *decodedExample2 = [cipher decode: @"tprl" offset:7];
NSLog(@"%@",decodedExample2);

NSString *decodedExample3 = [cipher decode:@"zlha" offset: 7];
NSLog(@"%@", decodedExample3);


[cipher codeBreaker:@"okmg" with:@"tprl"];

[cipher codeBreaker:@"tprl" with:@"zlha"];



}

}
100 changes: 99 additions & 1 deletion Election/Election/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,40 @@

#import <Foundation/Foundation.h>

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


// forward declarations
@class Contender;
@class Election;
@class VotingSimulator;

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// Contender class

@interface Contender : NSObject

- (instancetype)initWithName:(NSString *)name;
Expand All @@ -23,6 +52,8 @@ - (NSString *)name;

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@implementation Contender {
NSInteger _votesReceived;
NSString *_name;
Expand Down Expand Up @@ -55,6 +86,10 @@ - (NSString *)description {

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// Election class

@interface Election : NSObject

Expand All @@ -71,8 +106,8 @@ - (void)displayResults;

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// Election class
@implementation Election {
NSString *_electionName;
NSMutableArray *_listOfContenders;
Expand Down Expand Up @@ -151,6 +186,9 @@ - (void)vote {

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


// ElectionManager class
@interface ElectionManager : NSObject
Expand All @@ -162,6 +200,8 @@ - (BOOL)pollsOpen;

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@implementation ElectionManager {
NSMutableArray *_races;
}
Expand Down Expand Up @@ -201,11 +241,69 @@ - (BOOL)pollsOpen {

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//Voting Simulator Class

@interface VotingSimulator : NSObject

-(void)simulation: (Election *)e with: (ElectionManager *)eMgr ;

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@implementation VotingSimulator

-(void)simulation: (Election *)e with: (ElectionManager *)eMgr {
[eMgr manage:e];
[e displayCandidates];
[eMgr initiatePolling];
[eMgr displayResults];
}

@end

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


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

Election *bestArtist2016 = [[Election alloc] init];
[bestArtist2016 initWithElectionName:@"Best Artist 2016"];

ElectionManager *justine = [[ElectionManager alloc] init];

Contender *cindySherman = [[Contender alloc] init];
Contender *nanGoldin = [[Contender alloc] init];
Contender *vanessaBeecroft = [[Contender alloc] init];

[cindySherman initWithName:@"Cindy Sherman"];
[nanGoldin initWithName:@"Nan Goldin"];
[vanessaBeecroft initWithName:@"Vanessa Beecroft"];

[bestArtist2016 addContender:cindySherman];
[bestArtist2016 addContender:nanGoldin];
[bestArtist2016 addContender:vanessaBeecroft];

// [bestArtist2016 displayCandidates];
//
// [justine manage:bestArtist2016];
//
// [justine initiatePolling];
//
// [justine displayResults];

//Replaced the commented-out messages above with the voting simulator instance below:

VotingSimulator *test = [[VotingSimulator alloc] init];

[test simulation:bestArtist2016 with:justine];

}
return 0;
}
56 changes: 53 additions & 3 deletions Person/Person/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#import <Foundation/Foundation.h>

//Write a method called checkSameCity which accepts one parameter of type Person * and checks if they live in the same city. The method should return a boolean value.
//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.


@interface Person: NSObject

- (void)setName:(NSString *)name;
Expand All @@ -19,6 +23,10 @@ - (NSString *)city;
- (void)setPhoneNumber:(NSString *)phoneNumber;
- (NSString *)phoneNumber;

- (BOOL)checkSameCity:(NSString *)city;

- (Person *) haveChild;

@end

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

@end
- (BOOL)checkSameCity:(NSString *)city {
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.

This is 100% correct. You could make it a bit more concise by just return the result of the isEqualToString method call:

- (BOOL)checkSameCity:(NSString *)city {
  return [_city isEqualToString:city];
}

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.

Ok, yes. Thanks!

I originally had it that way, then I changed it in order to NSLog the
answer Yes or No as a way to check myself. Otherwise it only returned a 1
or 0.

Thanks again!

Justine
On Jun 29, 2015 11:47, "Mike Kavouras" notifications@github.com wrote:

In Person/Person/main.m
#31 (comment)
:

@@ -51,13 +59,55 @@ - (NSString *)phoneNumber {
return _phoneNumber;
}

-@EnD
+- (BOOL)checkSameCity:(NSString *)city {

This is 100% correct. You could make it a bit more concise by just return
the result of the isEqualToString method call:

  • (BOOL)checkSameCity:(NSString *)city {
    return [_city isEqualToString:city];
    }


Reply to this email directly or view it on GitHub
https://github.com/accesscode-2-2/unit-0-hw-week-2/pull/31/files#r33478010
.

if ([_city isEqualToString:city]){
NSLog(@"Yes");
return YES;
}else {
NSLog(@"No");
return NO;
}
}

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


@end
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

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

[julia setName:@"Julia"];
[julia setCity:@"Cheyenne"];
[julia setPhoneNumber:@"617-890-4563"];

NSLog(@"%@ lives in %@. Her phone number is %@.", [julia name], [julia city], [julia phoneNumber]);

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

[igor setName:@"Igor"];
[igor setCity:@"Brooklyn"];
[igor setPhoneNumber:@"347-517-2345"];

NSLog(@"%@ lives in %@. His phone number is %@.", [igor name], [igor city], [igor phoneNumber]);

NSLog(@"Do %@ and %@ live in the same city?", [julia name], [igor name]);
[julia checkSameCity:[igor city]];

Person *juliasChild = [julia haveChild];
[juliasChild setName: @"ABC"];

NSLog(@"%@ just had a child and named it %@. Can you believe that??", [julia name], [juliasChild name]);


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