×

Simple Share Buttons

SeanMcCammon C# .Net core 3 Software Developer

I recently created a beginners guide to a simple Spring Boot project on my YouTube channel.

Here is the first video in that series - Add dependency and connection to PostgreSQL DB

I recently created a beginners guide to a simple Spring Boot project on my YouTube channel.

Here is the first video in that series - Create our project structure

I recently created a beginners guide to a simple Spring Boot project on my YouTube channel.

Here is the first video in that series - Setup The SQL Database and Tables

I recently created a beginners guide to a simple Spring Boot project on my YouTube channel.

Here is the first video in that series - Create Spring Boot Project Using Inltellij

I recently created a beginners guide to a simple Spring Boot project on my YouTube channel.

Here is the first video in that series - Create A Basic Beginners Spring Boot Project

From the above title you may be wondering what this one is all about. Well, let me explain a little.

I recently did an interview where part of it was peer programming. Well, that is not entirely true. There was a programming exercise where the interviewers watched while I attempted the question.

Now. little caveat is that my Java skills are very rusty. It has been a few years since I've had to knock out any Java code. Currently, I'm working in C# and ASP.NET.

That out of the way... here is the question I was asked.

The question

The question went something along the lines of:

Mary had some sweets which are represented as numbers in an array. She is going to share these sweets with her friend (let's call him George as I can't remember the name used).

The number of sweets, for this case, are always even.

Mary wants her sweets to be the maximum number of different sweets from what she has. She wants to know how many different types of sweets she will get.

So, image this array of numbers (sweets) {1, 2, 2, 3, 5, 5, 6, 6}. The size of the array is 8. The maximum number of different sweets Mary could expect is 4. It's 4 because she can have only half the number - and there are at least 4 different numbers she would have {1, 2, 5, 6) or {2, 3, 5, 6} and so on.

Now image if the array (sweets) looked like this {1, 80, 80 ,20957383,5 ,30,80, 80, 80, 80, 5,1,94,1090075, 80, 80}.

From this, you would work out that, although the array is 16 values in size, the number of different values is just 7. So we would return 7 for this array.

So, who did I work it out?

When I was doing the code in the 20-30 minutes I had, with my Java rusty I did not come up with the best solution. Too many loops and statements.

So, out of interest, I decided to rewrite this code (with the aid of Google to get the syntax of a few commands).

My idea was simple. Loop through the array and add any different values into a list. Then get the size of the list and if it was smaller than the array divided by 2 - that was your return value.

Otherwise, just return the size of the array divided by 2.

So, what does my code look like - what is the answer to this question. Let's take a look:

import java.util.*;

class DifferentValues {
	public int CalculateDifferentValues(int[] T) {
		int arrSize = T.length;
		List<Integer> foundValues = new ArrayList<Integer>();

		// Loop through the array
		for (int count = 0; count < arrSize; count++)
		{
			// Check if the current value has already been found
			if (!foundValues.contains(T[count]))
			{
				// If not in the ArrayList then add it
				foundValues.add(T[count]);
			}
		}

		// How many different found values where there (take size of ArrayList)
		int foundSize = foundValues.size();

		// If number different is smaller than half of array size - then 
		// return the different found size
		if (foundSize < (arrSize/2))
			return foundSize;

		// If foundSize is greater than half array size, then return 
		// the array size divided by 2
		return (arrSize/2);
	}

	public static void main(String[] args)
	{
		// Initialise my array with lots of numbers
		int theArray[] = {1, 80, 80 ,20957383,5 ,30,80, 80, 80, 80, 5,1,94,1090075, 80, 80};
		
		// Create my object and call the calculate different method
		DifferentValues differentValues = new DifferentValues();
		int totalDifferent = differentValues.CalculateDifferentValues(theArray);

		System.out.println("Total number of different items is: " + totalDifferent);
	}
}

Hope this helps

So, if you ever get asked a question similar to this one, I hope this example helps. It's basically can take an array of T[N] (so any size array) and return you the maximum different values that one person can expect.

The Task

I recently got asked to complete a test; in this test there was a requirement to create a dynamic HTML table. The requirement was that when data was entered into some fields; this would be used to populate the table.

There were a few other requirements; such as splitting on space; which I won’t cover in this post. We’re just looking at the dynamic tables.

How do we do this?

To do this we will be using HTML and and some Javascript. We need the Javascript to add the rows as we need them. I will keep this to the minimum; Javascript; as we don’t need that much. But it is required so..

The solution

Ok, lets get into this code.. First thing is we need to create a table.. So lets put that code together first

First, lets look at the HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>UI Demo</title>

<link href="Styles.css" rel="stylesheet" type="text/css"> <script src="Scripts.js">addName.counterRows = 0;</script>

</head> <body> <div id="container"> <div id="uiContainer"> Full Name : <input id="Name" type="text" name="Name"> Age : <input id="Age" type="number" name="Age">

<button onclick="addName(document.getElementById('Name').value, document.getElementById('Age').value,document.getElementById('counter').value)">Add</button>

<table id="namesTable"> <tr> <th>First Name</th> <th>Last Name</th> <th>Age</th> </tr> <tr>

</tr> </table> </div>

<div id="counterDiv"> Total Records : <input id="counter" type="number" value="0" readonly>

</div>

</div>

<;/body> </html>;

 This gives us a basic page that allows us to input some text value and number value and displays the table on the screen.. Quite simple so far.

The main part you want to look at is the button onclick function. This sets up the Javascript function and passes in the values so we can update them. The getElementById.<element>.value passes in the current value of that element; so for name its the value in the name text box.

Next, lets take a look at the CSS for this

#container { border: 4px solid darkblue; border-radius: 25px; padding: 30px;
/*center the container*/ width: 960px; margin: 0 auto; }
#Name { /* For Later Use */ }
#Age { /* For Later Use */ }
#namesTable tr:nth-child(even) { background: dimgray;}
#namesTable tr:nth-child(odd) { background: lightgray;}
#namesTable th { margin-top: 50px; background: royalblue; }
#namesTable { width: 100%; border-collapse: separate; empty-cells: show; }
#namesTable tr { /*display:block;*/ min-height:20px; }
#counterDiv { float: right; padding-bottom: 10px; }
#counter { float: right; border: none; width: 30px; }

This nicely sets up the formatting of the page; again nothing too complex.

So, finally. Next we can take a look at the simple Javascript function.

function addName(name, age, counter) { var table = document.getElementById("namesTable"); var row = table.insertRow(); var splitName = name.split(" "); /*Split name by space*/ var countRows = counter;

var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var cell3 = row.insertCell(2);

cell1.innerHTML = splitName[0]; cell2.innerHTML = splitName[1]; cell3.innerHTML = age;

countRows++; document.getElementById("counter").value = countRows; }

This function takes in the values for name, age and counter. We then create some variables linking one to the table and then creating a new row in that table.

Next, using the split function we split the text where there is a space and create an array called splitName. This will contain the two parts of our name (first and last).

Next, we create some new cell elements for the new row. Finally we set the HTML (value) of these cells to the elements in the array and age. innerHTML does this.

Doing this will create a new row in the table; making it a nice dynamic table.. You can also delete rows but I will leave this for another post.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram