| pixelninja |
|
Administration
|
| Administration |
| Eric Burcham |
| Richardson, TX |
| Software / Database / Web / Graphics Designer |
| Swing & Salsa, Rock Climbing, Acoustic Guitar |
| Male |
|
| Sunday, July 24, 2005 |
| Thursday, June 25, 2009 9:11:48 AM |
14 [0.01% of all post / 0.01 posts per day] |
|
You may download the complete source for this example here.
As a working college student, I have a consistant problem having to change my network settings. For instance, I like to download files at home, so I need certain ports open on my wireless router. This dictates that I have to use a static IP address in order to not be re-mapping ports all the time.
At school, the university requires that I use a dynamic IP address, or I cannot even connect to the network.
At work, I need to use a certain DNS server, but have a dynamic IP address. Go figure.
This means that I am constantly changing my IP configuration, and that I have three different sets of information to remember. The solution... a batch file that handles the whole shebang. This has only been tested on WinXP, by the way.
In order to have three sets of settings, I just created four files. One .bat and three .txt files so that I can just add settings for each location in the .txt files.
The ConfigIP.bat file looks like this.
Code:@echo off title Eric Burcham's IP Configuration Batch File - 2005 setlocal set OK=N
:again set /p choice=Please enter 1[Home], 2[School] or END ?? if /i [%choice%]==[END] endlocal&goto end if [%choice%]==[] goto again if [%choice%]==[1] goto 1 if [%choice%]==[2] goto 2 set /p xxx=wrong entry, press any key to exit. endlocal goto end
:1 echo NIC setting for Home being configured..... @netsh exec home.txt set /p see=IP changed successfully to Home Settings [9]see new setting [Enter]exit... if [%see%]==[9] goto show echo Ending IP Configuration goto end
:2 echo NIC setting for School being configured..... @netsh exec school.txt set /p see=IP changed successfully to School Settings [9]see new setting [Enter]exit... if [%see%]==[9] goto show echo Ending IP Configuration goto end
:show @netsh int ip show config
:end @echo on cls
This files presents the user with a selection; 1 or 2. Based on which selection they make, the IP settings are loaded from the following files.
home.txt
Code:# ---------------------------------- # Interface IP Configuration # ---------------------------------- pushd interface ip
# Interface IP Configuration for "Wireless Network Connection" set address name="Wireless Network Connection" source=static addr=192.168.2.50 mask=255.255.255.0 set address name="Wireless Network Connection" gateway=192.168.2.1 gwmetric=0 set dns name="Wireless Network Connection" source=static addr=192.168.2.1 register=primary
popd # End of interface IP configuration
school.txt
Code:# ---------------------------------- # Interface IP Configuration # ---------------------------------- pushd interface ip
# Interface IP Configuration for "Wireless Network Connection" set address name="Wireless Network Connection" source=dhcp set dns name="Wireless Network Connection" source=dhcp register=primary set wins name="Wireless Network Connection" source=dhcp
popd # End of interface IP configuration
You can simply add a new .txt file for each set of settings you need, and a section in the .bat file to load them. Voila! 2 second IP configuration.
You may download the complete source for this example here.
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
This is my favorite to see someone do really well. The trick is to set two variables, one as a working variable and one that contains the highest sum found so far. You can iterate the array once, and reset the working variable any time it dips below zero, since the lowest number we will allow as a return value is zero. Then you just reset the highest value when the working one goes above it.
Code:/// <summary> /// This method should return the greatest possible sum of consecutive integers within the array "numbers" /// /// Please note that the solution should be as efficient as possible ans not be breakable. /// /// If all numbers in the array are negative, return 0 /// </summary> /// <param name="numbers">Integer array</param> /// <returns></returns> public static int GreatestConsecutiveSum(int[] numbers) { // Examples //-1, -2, -3 returns 0 // 0, 1, 2, 3, -1 returns 6 // -10, 1, 2, 3 returns 6 // 1, 2, 3, -10, 20 returns 20 // 1, 2, 3, -5, 20 returns 21
int sum = 0; // Variable to hold greatest sum int temp = 0; // Working variable
for (int i = 0; i < numbers.Length; i++) { temp += numbers[i];
if (temp < 0) { temp = 0; }
if (temp > sum) { sum = temp; } }
// Return answer return sum; }
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
This one only has a few places that people hiccup. Obviously people with a strong math background have an advantage here, because they might know Fermat's Little Theorem is a fast heuristic.
The main catching point is on how many possible factors to test before deciding the number is prime. A person might decide to divide 100 by every number from 1-99 and look for a remainder of 0, but they only need to go to ten. Any higher than a number's square root and any higher factors would have already divided evenly.
Code:/// <summary> /// This method should return whether or not an integer is a prime number /// /// Please note that the solution should be as efficient as possible and not be breakable. /// </summary> /// <param name="number">Integer to be tested</param> /// <returns>true or false</returns> public static bool IsPrime(int number) { // Throw out impossibles if (number < 2) { return false; }
// Don't need to test above the square root of a number for (int i = 2; i < (number); i++) { // If remainder is 0, number is not prime if (number % i == 0) { // return false return false; } }
// If all conditions are met, return true return true; }
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
Here is the best solution that I came up with. The obvious solution is to loop on one variable and add another. There are two things to watch out for when candidates answer this question.
1) Do they make sure to loop on the smaller integer? 2) Do they handle negatives?
Almost anyone can nail the basic problem. I am looking for someone who solves it intelligently, and covers all their bases. The best candidates will talk through everything carefully and make sure that they have thought of everything before they actually start writing on the whiteboard.
Code:/// <summary> /// This method should return the product of the two given integers. /// /// Do not use built in operatros such as "*" /// /// Please note that the solution should be as efficient as possible and not be breakable. /// /// Do not worry about results that are out of the normal integer range. /// </summary> /// <param name="x">Integer to be multiplied</param> /// <param name="y">Integer to be multiplied</param> /// <returns>The product of the two integers</returns> public static int Multiply(int x, int y) { int product = 0;
bool isNegative = false; // Variable to store whether or not the product should be negative
if ((x > 0 && y < 0) || (x < 0 && y > 0)) { isNegative = true; }
// Take absolute values to loop x = Math.Abs(x); y = Math.Abs(y);
// We'll loop on x, so make sure it is the smaller integer if (y > x) { int temp = x; x = y; y = temp; }
// Calculate the product for (int i = 0; i < x; i++) { product += y; }
// Recheck negative condition if (isNegative == true) { product = -product; }
// Return product return product; }
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
This one is a little trickier. Most people will try to brute force it... that is to say they will simply add every possible combination of integers and see which on is the largest. This works fine for small arrays, but what about an array with a million integers? I think it might run for a little while. You are looking for candidates that come up with an elegant solution. It is possible to iterate the array just once and come up with the correct answer.
Code:/// <summary> /// This method should return the greatest possible sum of consecutive integers within the array "numbers" /// /// Please note that the solution should be as efficient as possible ans not be breakable. /// /// If all numbers in the array are negative, return 0 /// </summary> /// <param name="numbers">Integer array</param> /// <returns></returns> public static int GreatestConsecutiveSum(int[] numbers) { // Examples //-1, -2, -3 returns 0 // 0, 1, 2, 3, -1 returns 6 // -10, 1, 2, 3 returns 6 // 1, 2, 3, -10, 20 returns 20 // 1, 2, 3, -5, 20 returns 21 return 0; }
Please post any solutions. I will post mine in a few weeks.
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
Anothe simple one for the entry level or internship candidates. The idea here is to watch them think through it and make sure they don't make any obvious mistakes.
Code:/// <summary> /// This method should return the product of the two given integers. /// /// Do not use built in operators such as "*" /// /// Please note that the solution should be as efficient as possible and not be breakable. /// /// Do not worry about results that are out of the normal integer range. /// </summary> /// <param name="x">Integer to be multiplied</param> /// <param name="y">Integer to be multiplied</param> /// <returns>The product of the two integers</returns> public static int Multiply(int x, int y) { return 0; }
Anybody see any obvious issues that might come up? Just curious as to what you think. I will post my solution in a few weeks.
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
This is a simple interview question I often use for potential interns or entry level candidates. Write a function (language is arbitray; I always use C#) that takes an integer as a parameter a boolean indicating whether or not the numnber is prime.
Code:/// <summary> /// This method should return whether or not an integer is a prime number /// /// Please note that the solution should be as efficient as possible and not be breakable. /// </summary> /// <param name="number">Integer to be tested</param> /// <returns>true or false</returns> public static bool IsPrime(int number) { return true; }
Please post any solutions that you may want to try... I will post mine in a few weeks.
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
C3 C6 C5 C1 C2 C4
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
After careful thought and consideration, I have come up with the following detailed list of things I like.
1) Everything
If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
This one is pretty sweeeeeet. C3's are the bomb.

If I have seen further than others, it is by standing upon the shoulders of giants. ~Sir Isaac Newton
If I have not seen as far as others, it is because giants were standing on my shoulders. ~Hal Abelson
|
|