Church Directory

You have been asked by a local church for help maintaining their directory and to build functions that will automate tasks for the church staff.

Challenge

For this challenge we will be dealing with building complex objects and manipulating them. This may involve reading and writing to a file and creating a user interface for entering data. Since the challenge is not about designing good UI your interface may be console based or a simple HTML page, but it will need to have all the functionality required for the level of challenge you choose.

Similar to last month you will be presented with what the end user wants to be able to do but the actual design and architecture is up to you to create.

Creating a Test Case

We’ll continue to use test driven development. No matter the level you choose write a test case before you start coding. Then write enough code to pass the test. Once you have a passing test write more tests and refactor you code to accommodate all possible tests.

Level 1

The first step in an active church directory is to be able to enter the information for each family in the church. Store this information to a file for future retrieval.

The secretary needs to be able to input an address with city and state along with a home phone number (if one exists) for each family. The family may only be a single person or a mother and father with children and everything in between. Each person will have a name (first, middle, last), a date of birth, and some will have a cell phone number.

A sample JSON object may be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
family: {
address: "123 Main St",
city: "Somewhere",
state: "TN",
adults: [
{
name: "Jane Smith",
birthDate: 1987-02-27,
cellPhone: "6155551234"
},
{
name: "James Smith",
birthDate: 1983-8-21,
cellPhone: "6155551235"
}
]
children: [
{
name: "Johnny Smith",
birthDate: 2010-05-21,
cellPhone: null
}
]
}

A Step Further

Make your directory indexable. The church has asked that they be able to look up families by last name so they can quickly update their information such as change of address or new child.

Level 2

Now that you have built them a directory the youth minister wants to automate sending out flier about an upcoming event to all of the families with children between the ages of 10 and 12. He normally handwrites these invitations but asked if you could personalize them automatically.

The text of the flier should read:



[LastName] Family
[Street Address]
[City] [State]

We are having a lock in for children ages 10 - 12 at the church on September 26, 2017. The youth staff wanted to make sure that [children’s first name between ages 10 - 12] know they are invited and need to bring a sleeping bag, tooth brush/paste, pajamas, and their favorite board game.

Doors will shut at 7:30 pm and open back up at 7:30 am.

Thanks,
Vinny Youts
Church Youth Minister


A Step Further

Since your flier for the youth minister was so successful the college/singles minister has asked you to send out an invitation to a singles retreat to all adults in their 20’s that are in a single adult family. If they have children she wants it to ask them to leave the kids at home.

The invitation should read:



[Full Name]
[Street Address]
[City] [State]

You are invited to a singles retreat for young professionals at church. It will be October 15 - 17, 2017 at [Choose a location].

Please bring a sleeping bag, tooth brush/paste, pajamas, and your favorite board game. All other necessities will be provided by the church.


If they have children include the following as well:



This is an adults only event so please make arrangements for [Children’s First Names] during the retreat.


Level 3

The church secretary now wants to import the old church directory into the new one so that all the existing families do not have to be reentered. Build a process for importing the JSON file with all of the families in the old directory.

Then go through the new directory and compare it with the information in the old to make sure that families are not duplicated with new ones that have been created. If they are remove the duplicates and/or update the record to show the most current information for each family.

A Step Further

They church has other directories that are not in JSON but in XML, CSV, etc. To be completely thorough build a process for each type they may have and import the data into the JSON file.

Birthday List

Will is not here this month because he is attending his daughter’s birthday party. In light of this we are going to help another little girl named Lisa to plan out her birthday.

Challenge

For this challenge we will be reading and writing to a file, organizing the information in the file, and presenting it to the user. This will involve creating a user interface which can be done in the console or as an html page or however you decide to build it.

This will also take some planning on your part as the developer in creating an architecture for your application. Each level presents you with what Lisa wants your app to do. As the developer you decide how to design the app to accomplish these tasks.

Creating a Test Case

Like in past challenges we’ll be using our tests to drive our development. No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Level 1

To make sure she gets the toys she wants and cloths that fit Lisa has asked you to create an app that lets her enter and store a birthday wish list. She wants to be able to enter the items she wants, their type (toy, clothes, supplies, etc.) and any relevant information like size or color.

A Step Further

Once the wish list is created she needs to be able to print it out for her friends and family to see what she wants. If you have time Lisa would like for the toys to be listed on top, but don’t tell her mom that.

Level 2

Lisa needs your help to organize who she is inviting to her party. She will need to enter their name, address to send the invitation, relationship (family or friend), and their RSVP.

Once the table is created she’ll need to be able to print it out so that her mom can mail the invitations. Lisa’s mom told her that family comes first but it would be easier to mail if they are organized by city and state.

A Step Further

At the party Lisa needs to be able to track the gifts she has received and information about those gifts such as type of gift (toy, cloths, etc.). She also wants to know who gave her which gifts.

Hint: Adding a unique index to these tables will help track which records are associated.

Level 3

Now that the party is over Lisa doesn’t would rather play with her toys than write the thank you cards. She asked you to help her out by creating an app that will write the thank you card for her and include the name of the person attending along with the gift they gave.

Since her mom said family comes first have the family members thank you cards display first then the friends that gave her toys. Don’t forget about the friends and family that came to the party but didn’t bring a gift.

Sample Thank You Text

1
2
3
4
5
6
7
8
Grandma Ester
123 Main Street
Somewhere, TN 38583
Thank you Grandma Ester for coming to my birthday party and for the pink socks you gave me. I really like them.
Love,
Lisa

Hint: Use the information about the gift such as color to add more personalization to the message. This works with some types but not others.

A Step Further

Lisa really liked the wish list you helped her create but wants to know who gave her items from her list so that she knows who to send the list to next year. Also now that her birthday is over she wants to save the list for next year but remove the items she’s already received.

Ticket to Ride

BJ is down in Florida this week with his family visiting Disney and the beach. He’s a big kid and really gets along with his nieces and nephew. They all have rides they want to go on while at Disney but don’t like standing in line. We’re going to help them choose which rides to get Fast Pass tickets on.

The Challenge

For this challenge the family will be at Disney for 2 days. Each person gets 4 Fast Passes to skip the lines and they want to all ride together as much as possible.

Each of the kids and the big kid (BJ) have listed out their favorite rides in the table below. They have prioritized the rides (1 being top priority).

Name Ride Priority Park
BJ Space Mountain 1 Magic Kingdom
BJ Mad Tea Party 3 Magic Kingdom
BJ Peter Pan 4 Magic Kingdom
BJ Kali River Rapids 2 Animal Kingdom
John Peter Pan 1 Magic Kingdom
John Triceritop Spin 2 Animal Kingdom
John Wildlife Express Train 3 Animal Kingdom
John Dumbo 4 Magic Kingdom
Addie Small World 3 Magic Kingdom
Addie Kali River Rapids 2 Animal Kingdom
Addie Finding Nemo 1 Animal Kingdom
Addie Splash Mountain 4 Magic Kingdom
Kinley Small World 1 Magic Kingdom
Kinley Kilimanjaro Safaris 2 Animal Kingdom
Kinley Dumbo 3 Magic Kingdom
Kinley Wildlife Express Train 4 Magic Kingdom

Creating a Test Case

Like in past challenges we’ll be using our tests to drive our development. No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Level 1

From the table of everyone’s favorite rides create a list of rides to get fast pass tickets so that each person gets to ride at least one from their list.

A Step Further

For each ride on the list of fast pass rides also print the names of the people wanting to go on that ride.

Level 2

Prioritize the rides based on how many people want to go on that ride. If more than one person wants to go on a ride it becomes a higher priority. Make sure everyone gets to go on at least one from their list.

A Step Further

If only one person wants to go on a ride they can go through the single rider line and not spend a fast pass ticket. Create a list of single rider rides and exclude these from the list of fast pass rides.

Level 3

The rides are spread between two parks. The family is spending two days at Disney going to one park each day. Create a list of 3 rides per park so that all members get to ride at least one on their list of rides.

Anagrams: Raga Mans

Anagrams are words or phrases that are created from the letters of another word or phrase. They are useful in games like Scrabble or Boggle as well as being a fun way to show your skill in coding.

The Challenge

This challenge was inspired by a code kata from Dave Thomas that can be found here. We’re taking his kata and making it our own. Though since he’s already created a thorough word list we’ll be using that as part of this challenge.

Dave Thomas Word List

In the challenge we’ll be looking at the characters in a word, counting them, and manipulating them. We’ll even turn it into a fun text based game. For each level you may use the command line for input and output or create a view to display on the web.

Creating a Test Case

Like in past challenges we’ll be using our tests to drive our development. No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Level 1

For level 1 we’ll build a useful tool for counting the number of characters in a word or phrase. Start out by taking in a word or phrase as input. Then ignoring the spaces return the total number of characters in that word or phrase. Write unit tests for both words and phrases. Next return only the letters. Finally return the number of punctuation marks (.:;,!?)

Input:

1
2
Input a word or phrase:
This is a test.

Output:

1
2
3
Characters: 11
Letters: 10
Punctuation: 1

A Step Further

Going a step further we’ll add some math into the mix. Calculate the possible number of combinations of the letters in the phrase.

Input:

1
2
Input a word or phrase:
Test!

Output:

1
2
3
4
Characters: 5
Letters: 4
Punctuation: 1
Combinations: 24

Hint: The formula for calculating the possible combinations is n! or n(n - 1)(n - 2)…

Level 2

Instead of looking for the number of possible combinations of letters from the input create a list of combinations and then check that on the word list provided to see if the combination is an anagram or just gibberish.

Input:

1
2
Input a word to find it's anagrams:
start

Output:

1
2
tarts
trats

A Step Further

Going a step further include in your results phrases or multiple words with the same set of characters.

Input:

1
2
Input a word or phrase to find it's anagrams:
start

Output:

1
2
3
4
5
tarts
trats
rat St
art St
tar St

Level 3

Now the fun begins. We are creating a game of anagrams. Instead of taking in a word or phrase have the player input the number of characters for the word with a minimum of three characters. Next randomly select a word from the word list with that number of characters and display it to the player.

Allow the player to input different anagrams for the word selected. Calculate points based on the following scheme:

Input Points
Correct +2
Incorrect -1

Display the player’s score after they input their anagrams.

The display should look something like this:

1
2
3
4
5
6
7
8
Enter the number of characters for the word:
5
Your word is 'start' enter anagrams separated by a comma:
tarts, ratts, trats, rat st
Score: 5
Total Possible: 10

A Step Further

Make the game more interesting by adding a timer to limit the amount of time the player has to enter anagrams. If you have time make the game multiplayer and track scores for each player in the round.

Word Wrapper

Will has been having lots of trouble with the word wrapper in his favorite text editor. He complains about it all the time. So to help him out we are going to be writing some custom word wrapping.

The Challenge

For this challenge we will be writing a custom word wrapper to display formatted text when entered into an input. First you will take in the text as one long string then adjust it with word wrapping before displaying it.

You can do this in any language you choose but if possible display it so that it can be used on a website. Use whatever html and css you choose to write. If you complete one level and move to the next you’ll be able to reuse your html and css.

Creating a Test Case

Unit testing for this challenge will be a bit different than the testing we’ve done in past challenges. You will also need different types of tests for each level. As part of the challenge find a way to test the code before you write it. Let the tests drive your development.

Level 1

Start by creating an input field to take in the text as a string. Then create a break at each sentence so that each one displays on a different line.

Input

1
This is a test of the word wrapping system. Had this been a real word wrap someone else would have written it.

Output

1
2
3
This is a test of the word wrapping system.
Had this been a real word wrap someone else would have written it.

A Step Further

In college Will dated a girl that wrote long emails with run-on sentences and no capitalizations. It really annoyed him so let’s help out by capitalizing only the first letter in a sentence when we break them up or an ‘I’ in the middle of a sentence.

Input

1
i really enjoyed the movie last night. it was great. do you want to get lunch before class tomorrow? dana said you and i could meet them at noon.

Output

1
2
3
4
5
6
7
I really enjoyed the movie last night.
It was great.
Do you want to get lunch before class tomorrow?
Dana said you and I could meet them at noon.

Level 2

Instead of breaking at each sentence we want our app to function more like a word wrapper in a text editor so we will break at the end of the screen with some padding.

You can do this by taking in the number of columns with your text input or by determining the size of the screen and how many characters will fit on it. Remember to leave some space at the ends for padding.

Make sure you break at the end of a word and not in the middle of a word as in Example A not in the middle of a word as in Example B. Though you may break in a hyphenated word on the hyphen as in Example C.

Example A

1
2
This is a very long sentence that just keeps
going and going and going.

Example B

1
2
This sentence is also a long one but it doesn't bre
ak at the right place so looks funny.

Example C

1
2
3
This sentence is a rather long sentence that is a run-
on sentence with the word sentence in it way too much
when will it ever end?

A Step Further

Going a step further make your word wrapper dynamic so that as the screen size changes so does your end point for wrapping.

Level 3

For some reason Will doesn’t want his html or xml elements to be wrapped but he wants the text within them to be wrapped. So for Level 3 wrap only the text that is not contained within <>.

Input

1
<html-element class="theClass anotherClass makingItLonger" name="longElement">This is a really long html element, well not really as I just say that because I want you to think of it as long.</html-element>

Output

1
2
3
4
<html-element class="theClass anotherClass makingItLonger" name="longElement">
This is a really long html element, well not really as
I just say that because I want you to think of it as long.
</html-element>

A Step Further

Instead of taking in a string receive a file as your input (html, xml, or txt) and read the information on the file only adding the wrapper to the txt files and not the html or xml.

Music Cart

Here in Nashville we have a lot of musicians trying to make it big and some bigger ones continuing or changing their careers. Many of them do so using social media and even have their own websites while they are trying to get picked up by a record label. As the experts in computers we are here to help them out.

The Challenge

For this challenge we are going to be working on a variant of the shopping cart code kata. In it we will apply business logic to determine discounts and pricing for the items in our shopping cart.

In this case we are working with Lindsey, a local musician who needs a shopping cart built for her website. Lindsey currently has 3 self published album she is selling for $10 each.

If a fan purchases two different albums they will receive a 10% discount on the second album and if they purchase all three albums they get a 15% discount on the third but only if they purchase each of the three albums not three of the same album.

Creating a Test Case

A Unit Test looks at and tests a specific unit of code to see if it is returning what is expected. No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Level 1

Start by creating a simple view allowing users to select the album or albums they want to purchase. Then write a function that calculates the total cost for the purchase. Assuming that all sales are made here in Nashville now apply a 9.25% sales tax to the purchase. Finally display for the user the total amount of the purchase, the sales tax, and the amount after tax.

A Step Further

Remember if a fan purchases a second album different from the first there is a 10% discount on that album and if they purchase all three albums there is a 15% discount on the third album. However if they purchase two or three copies of album 1 the discount does not apply.

Level 2

Lindsey has produced another album and is going on tour to promote her new album. As a thank you to her fans and to encourage more purchases she wants to give anyone that purchases the new album at full price 5% discount on any other albums they purchase on top of any other discounts that might apply.

A Step Further

As a special thanks to her fans who attend the concerts in her tour Lindsey is including with the ticket a discount code (LINDSEYROCKS!) for 4% off her fourth album. Add an input field for users to input the discount code if they have it. Then apply this discount only to album 4 along with the previous discounts.

Level 3

Lindsey also wants to include t-shirts at $20 each, sheet music at $15 each, and individual songs at $2 each. Add these to the shopping cart. Each album contains 8 songs, if a person buys all 8 songs on an album they should be charged the album price of $10 instead of the cost of all 8 individual songs. If a person purchases all of the individual songs of an album the same album rules apply so far as discounts are concerned. Including the 5% off with purchase of the fourth album at full price in Level 2.

A Step Further

Going all out for her superfans Lindsey is giving a discount to those that purchase only the songs from the set list of her tour concerts. If the fan purchases all of these songs individually each song is $1 instead of $2.

Lindsey’s Set List

Album Song
album 3 song 4
album 4 song 2
album 4 song 4
album 1 song 8
album 2 song 3
album 4 song 5
album 1 song 1
album 3 song 8
album 4 song 1
album 2 song 6

Date Time

Formatting data can be tricky especially when dealing with dates and times. Sometimes you’ll need to format the data to present just a month or year to the user other times you’ll need to format the user input to store in a database.

The Challenge

For this challenge we are working with dates and times. We’ll be formatting them to present to a user or display on a website. Many frameworks have tools to do this functionality for you but understanding the basics of how to format a date can help when your preferred framework doesn’t offer the functionality that you need.

The pattern for date time may vary depending on your language and framework. The universal sortable date time pattern is “yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’”. Where ‘yyyy’ is year, ‘MM’ is month, ‘dd’ is day, ‘HH’ is hours, ‘mm’ is minutes ‘ss’ is seconds, and ‘Z’ is the offset from UTC. For example 2017-02-25 08:30:00-6:00 is the time and day this meet up started February 25, 2017 at 2:30 pm CST.

Creating a Test Case

A Unit Test looks at and tests a specific unit of code be it a controller testing if it calls the right functions or a converter service. Writing Unit Tests can be broken down into three areas Arrange, Act, and Assert. The three A’s of unit testing. When you Arrange you set up your test cases for testing. This could be as simple as setting a variable to be passed in or complex like creating a large object or calling the database to get an existing item to update. When you Act you are doing the functionality that you are testing. This could be calling a method or checking that your database connections exist or your converter service is functioning. Finally you need to Assert that what you are doing is actually working.

No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Some languages and frameworks have testing suites available to help build test driven apps. Here is a simple example of a test for this challenge written in JavaScript:

1
2
3
4
5
6
7
8
9
10
11
12
function TestCase1(){
//Arrange
var dateTime = "2017-02-25T08:30:00-06:00"
//Act
var month = getMonth(dateTime);
//Assert
if (month === "Feb"){
return "Test Passed"
} else {
return "Test Failed"
}
}

Level 1

Given a specific date write a function to return the month as a string. If the month is ‘02’ then the function should return ‘Feb’ or ‘February’. Next write a function to return the day of the week. So if the input is ‘2017-02-25’ the function will return Saturday.

A Step Further

Going a step further write a function that returns the time of day using the following table.

Time Ranges Time of Day
6am - 12pm Morning
12pm - 5pm Afternoon
5pm - 7pm Evening
7pm - 12am Night
12am - 6am Early Morning

Level 2

Given a task and how long it will take to complete write a function that returns the time or day and time the task will be complete if started immediately. For example if given the task ‘clean the kitchen’ and time ‘25 minutes’ return the current time plus 25 minutes.

A Step Further

Going a step further add another function for the user to note the time they finished the task. Then return the difference in how long it took to actually do the task as opposed to the predicted time.

Level 3

Given user input as a string with date information write a function to standardize the date information and return it in a format of your choice. The date could be represented in different ways. For example:

  • 25-Feb-2017
  • Feb,25,2017
  • 25/Feb/17
  • February 25, 2017
  • FEB/25/2017
  • 02/25/2017
  • 2017-02-25

A Step Further

Going a step further add a function to standardize the time then combine it with the standardized date to return a date time in the format of your choice. The time could be represented in different ways. For example:

  • 12pm
  • 2:30pm
  • Seven in the Morning
  • 12 noon
  • 1430
  • 08:30

Numbers to Strings

A Unit Test looks at and tests a specific unit of code be it a controller testing if it calls the right functions or a converter service. Writing Unit Tests can be broken down into three areas Arrange, Act, and Assert. The three A’s of unit testing. When you Arrange you set up your test cases for testing. This could be as simple as setting a variable to be passed in or complex like creating a large object or calling the database to get an existing item to update. When you Act you are doing the functionality that you are testing. This could be calling a method or checking that your database connections exist or your converter service is functioning. Finally you need to Assert that what you are doing is actually working.

The Challenge

Last month we took numbers and converted them into roman numerals and then back. While testing along the way. Keep letting your tests drive your development as you work.

Data manipulation is a core part of programming. Many times this manipulation has to do with business logic such as converting one type or value to another. Some times this is because of what the customer wants such as seeing the words true and false in a database instead of 1 and 0. You might also need to convert a sum of money from a number into a string to automate writing checks.

This month’s challenge involves numbers and strings. We will be finding the numbers in strings converting them into text and then converting them back into numbers.

Creating a Test Case

No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Some languages and frameworks have testing suites available to help build test driven apps. Here is a simple example of a test for this challenge written in JavaScript:

1
2
3
4
5
6
7
8
9
10
11
12
function TestCase1(){
//Arrange
var testNum = 3;
//Act
var result = ConvertToString(testNum);
//Assert
if(result == "three"){
console.log("Test Passed");
} else {
console.log("Test Failed");
}
}

Level 1

Create a simple converter to convert the numbers 1-10 to the text for that number so that if the number 2 is passed into the converter it returns “two”.

Next add to your converter service to allow numbers up to 1,000. If you have time you can keep going as high as you like.

A Step Further

Going a step further reverse the functionality of level one. This time write a service to convert a string representation of a number into the actual number. If the string “two” is passed into your service is should output the number 2.

Level 2

So far we’ve been working with just integers in this level convert numbers with decimals. If pass 2.5 into the converter it should output “two point five” or “two dot five”.

A Step Further

Going a step further instead of stating the decimals as “point” or “dot” convert them to fractions so that 2.5 will output “two and five tenths”. For a fun challenge try reducing the fractions so that 2.5 instead outputs “two and one half”.

Level 3

This level deals with strings that contain numbers in them. Start by finding the numbers in a string then convert them to a text representation of the number. If your input is “We have 2 dogs and 1 cat.” the output should be “We have two dogs and one cat.”

Start with the integers then add decimals and fractions into the mix so that “I ate 1.5 cookies” will output “I ate one point five cookies” or even “I ate one and one half cookies”.

A Step Further

Just like in level 1 going a step further involves reversing the conversion so that if the string “I drank eight cups of water today.” is passed into the converter it will output “I drank 8 cups of water today.”

Roman Numerals

Test Driven Development (TDD) is the concept of first writing a failing test for your code then coding to pass then test and refactoring to meet all possible test cases.

The challenge used to practice TDD focuses on loops and iteration with some simple math and string manipulation. Included in each level is a basic solution, look to it if you get stuck.

The Challenge

This is a simple variant of the Roman Numerals Code Kata where we will be taking arabic numbers and converting them to roman numbers (I, V, X, L, C, D, M).

The Romans developed a numbering system using letters. They were clever about this with rules so that you will not see more than three of the same letter together. Therefore 3 in roman numerals is “III” and 5 is “V” and then added these together to get higher numbers like 7 which is “VII”. To get 4 they had to subtract one from 5. They did this by placing the “I” to the left of the “V” so 4 is “IV”.

1
2
3
4
5
6
7
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1,000

Creating a Test Case

No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Some languages and frameworks have testing suites available to help build test driven apps. Here is a simple example of a test for this challenge written in JavaScript:

1
2
3
4
5
6
7
8
9
function TestCase() {
var testNum = 3;
var result = ConvertToRoman(testNum);
if (result == "III"){
console.log("Test Passed");
} else {
console.log("Test Failed");
}
}

Your initial failing code would look as follows:

1
2
3
function ConvertToRoman(num){
return "";
}

In this case your initial passing code before refactoring would look as follows:

1
2
3
function ConvertToRoman(num){
return "III";
}

Why would we write such a useless function just to pass the test when we know we will have to refactor? In a way it tests the test to show that we are testing what we think we are testing. In this example it seems silly but with some of the testing available for advanced frameworks this is good because you may not be testing what you think and your passing code that will does not do what it is supposed to do.

Level 1

Create a simple tick counter so that if the number 3 is entered the output will be “///“. Then group the ticks into sets of five so that if 15 is entered the output will look like “///// ///// /////“.

A Step further

Going a step further replace the “/////“ with a simpler way of recognizing a unit of 5 ticks. For example if 12 is entered the output would be “V V //“. Don’t forget to adjust your test case to accommodate the change in output.

Solution

Level 2

Start out writing a simple function to pass your test case. So for the example test case the following code would pass just the test case:

1
2
3
function ConvertToRoman(num){
return "III";
}

Next refactor convert any number up to 3. Then add logic to convert 5 through 7. Next have your function determine if a number is between 5 and 3 and add the appropriate amount of “I” to the left of the “V” to subtract down. Continue refactoring to add 10’s, 50’s and 100’s. If you have time try to get any number up to 3,000.

A Step Further

Now that you’ve written your code to accept any number up to 3,000 go back and look at your code. You probably used a lot of if statements. Refactor your code to use less if statements and make it more legible to someone looking at it for the first time.

Solution

Level 3

In this level we are doing the reverse of the kata. Take an input of a roman numeral and convert it back to an arabic number.

Start with writing a test case. Then code to the test and refactor to accept any test case. A sample in JavaScript will be:

1
2
3
4
5
6
7
8
9
function TestCase(){
var testRom = "III";
var result = ConvertToArabic(testRom);
if (result == 3){
console.log("Test Passed");
} else {
console.log("Test Failed");
}
}

In this case the return value is an integer but you can have it return a string just set your test case up for the return type.

Hint

The only characters allowed are I, V, X, L, C, and M. Also romans did not have a concept of zero so you will not return that number.

A step further

Taking this a step further write your code so that it will return an error if the characters are not roman numerals or in the correct order of roman numerals but allow for both upper and lowercase on the characters allowed.

Solution

All Levels Bonus

If you have extra time build a user interface so that numbers can be entered byt he user then converted via your code then displayed in the new format.

Grocery List

These Challenges will help you learn some of the basic problems you might see on a white board in an interview. This challenge builds partly on some of the skills used last month but instead of manipulating arrays and strings we will manipulate objects and lists.

The Challenge

For this challenge we will begin with a grocery list. You will be manipulating this list to help buy groceries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"milk": "false",
"eggs": "false",
"batteries": "false",
"bread": "false",
"orange juice": "false",
"grape juice": "false",
"bananas": "false",
"pears": "false",
"apples": "false",
"American cheese": "false",
"Cheddar cheese": "false",
"Shredded cheese": "false"
}

Level 1

Each of the items in the grocery list has a boolean value of false. Write an app to help a shopper keep track of their items. Allow a shopper to change the value to true when they have selected the item. A value of false means they do not have the item and a value of true means it is in their cart.

Level 2

Adding to level 1 sort the items by the boolean value so that the items left (false) are at the top of the display and the items obtained (true) are at the bottom. Update the list as the shopper selects changes a value.

Level 3

Help the shopper further by grouping the items so they can find them easier in the store. Create a nested list or object with values for cheeses, fruits, etc. Then sort the items in the nested lists to only show the items not in the cart (false).

A step further

Add prices to the items and track the amount for the shopper. To really be impressive calculate the taxes as well.

String Manipulation

This CodeNewbie Challenge will help you learn some of the basic problems you might see on a white board in an interview. This challenge was based on some of the white board problems we’ve seen throughout interviews. The goal here is to familiarize yourself with the types of white board challenges you might face in an interview.

The Challenge

For this challenge we will begin with an array of strings:

1
["apple", "banana", "orange", "pear", "grape"]

In this challenge you will be manipulating this array and the strings within it.

Level 1

In the first level you will start by reversing the order of the strings in the array. Your output should look as follows:

1
2
3
4
5
grape
pear
orange
banana
apple

Level 2

The second level starts with reversals in mind but instead of reversing the list you are now to reverse the strings themselves. Your output will look as follows:

1
2
3
4
5
elppa
ananab
egnaro
raep
eparg

Next you will take a user input string and reverse it. The input should look like:

1
Input a string to reverse:

With user input:

1
Input a string to reverse: antidisestablishmentarianism

And your output will look as follows:

1
msinairatnemhsilbatsesiditna

Level 3

The third level takes a user input and reverses it to determine if it is a palindrome or a word or phrase that is the same forward as backward. Your input screen will look as follows:

1
2
Palindrome Check:
Enter a word or phrase:

If the user enters the word “aibohphobia” your output will be as follows:

1
aibohphobia is a palindrome

If the user enters the phrase “Are we not drawn onward to new era” your output will be as follows:

1
Are we not drawn onward to new era is a palindrome

If the user enters the word banana your output will be as follows:

1
banana is not a palindrome

Hint:

You’ll need to remove the punctuation on phrases.

A step further

Going a step further with string manipulation try taking two strings and checking to see if they are anagrams (words or phrases with the same characters but in a different order). Your output should state the two strings and if they are or are not anagrams.

1
2
3
Torchwood and Doctor Who are anagrams
bicycle and acyclic are not anagrams

Hint

An easy way to do this is to check character count for each string. If they match they are anagrams.

Happy Primes

Given the numbers 313, 331, 367 what comes next? Doctor Who fans will know.

This CodeNewbie Challenge is about finding Happy Primes. A prime number is one that is only divisible by itself and one. A happy number is a number that reduces to one when you take the sum of the squared digits and continue iterating it. An unhappy or sad number will continue iterating indefinitely.

For example 19 is a happy number:

12 + 92 = 82

82 + 22 = 68

62 + 82 = 100

12 + 02 + 02 = 1

A happy prime is a number that is both happy and prime.

The Challenge

The challenge is a math based coding and testing challenge. Your job is to find numbers that are happy primes.

Level 1

In the first level you will start by producing a list of the happy primes below 100 from a list of primes and a list of happy numbers. Then take a user input and determine if that number is a happy prime or not.

Prime Numbers Below 100:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Happy Numbers Below 100:

[1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100]

Level 2

The second level starts where the first leaves off. You now need to find the happy primes from 0 - 500. Your challenge is to write code that generates a list of happy prime numbers between 0 and 500.

List of happy primes from level 1 (0-100) as a starting point:

[7, 13, 19, 23, 31, 79, 97]

Continue this list out to have all the happy primes from 0-500.

Level 3

The third level puts components of levels one and two together to determine if a number entered is a happy prime and if not what the nearest happy prime is to that number.

Your challenge is to write code that will determine if a given number is a happy prime or not. If it is not give the nearest happy prime to the given number.

List of happy primes 0-500:

[7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487]

A step further

Write your code so that any number may be tested even above 500.