|
Assuming this is not really your homework, then: how is it you so clearly remember the details of this question, and why are you concerned with coding it up now, rather than concerned with how you can increase your understanding of how to think about algorithms, and how to render them into a series of discrete operations that can be expressed in code ?
Note: I do not mean to imply you will not learn something "algorithmic" from coding up this specific task ! The question is: how to approach the coding in the way that will result in your extracting the maximum "algorithmic" learning from the task.
If you get started, and share some code, and ask specific questions accompanied by relevant code quotes, and error messages ... I, and, I am sure, many others here, will be happy to help.
Keep the phrase "divide and conquer" in mind !
Here's a "seed" idea to get you started:
1. since only every other digit starting from the final digit will need to be manipulated: step one is to isolate, and process as a group, the set of alternate digits; you can decompose that into:
a. how take your instance of an integer datatype, and transform it into a list of usable digits.
b. how to sort those digits into two groups, one of which contains every other digit starting from the end, the other which contains the other alternating digits.
c. how to take the alternating starts-at-end group of digits and call a function that transforms the values as required
d. how to create a new integer datatype instance that "integrates" the two lists.
Examining the requirements, it's clear you need a function that takes an integer and transforms it into the sum of its individual digits. You need to use that function repeatedly in your transform function, and, again, at the end.
Finally, you will need to make some strategic decisions about how you will implement the steps based on the context, and requirements. Decisions like:
2. in C#, based on estimated magnitude of values to be dealt with: choose from Int32, or Long, or even BigInteger data types ?
3. if high-performance, high volume, processing is required:
a. you are going to need to write code that decomposes your integer datatype into bytes, and parses those bytes to get at the digits they store. You are going to need to avoid expensive transformations like casting into Type 'String.
4. if high performance is not required:
a. you can "get away" with converting your integer datatype to strings, and then dealing with strings as arrays of type Char which you can easily decompose into arrays of integer digits.
By careful initial design, you can prototype using the less "expensive" techniques ... but, techniques that allow you to work more rapidly, and, then, later, as you refine more powerful, faster, techniques: simply "plug those in," replacing the slower ones.
Think of it as like prototyping a car by using a "socket" where the motor will, eventually, go. You can learn a lot about the design using a simple motor that uses a lot of gasoline, and is under-powered, but allows you to fully test. Later, when the powerful final motor is ready, and tested, you can just plug it in.
So, on a practical level this means I might write/organize code where certain functions were separated out into different classes with very specific methods:
public static class TransformationUtilities
{
public static long GetCheckInteger(long longValue)
{
}
public static long TransformDigit(int digit)
{
if(digit < 5 ) return digit;
return SumOfDigits(digit * 2);
}
public static long SumOfDigits(long value)
{
return (value.ToString()).Sum(ch => ch - 48);
}
} Keep in mind the above code is just a "sketch."
«The truth is a snare: you cannot have it, without being caught. You cannot have the truth in such a way that you catch it, but only in such a way that it catches you.» Soren Kierkegaard
modified 13-Apr-16 8:06am.
|
|
|
|
|
My guess is that it's a "pre-interview weeding out" task - and he can't do it so he won't get the interview.
And to be honest, if he can't do it given that precise a question...
You get an upvote for the excellent advice though!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
An excellent tutorial.
A very minor point, I always think the following looks clearer: '0' rather than 48 or 0x30.
return (value.ToString()).Sum(ch => ch - '0');
|
|
|
|
|
Yes, I think that does look better, and read better. I remember (dimly) once using a 'const named 'DigitBase for this.
But, if I were doing code requiring decomposing ints to lists of digits, I'd be using something like this ... from code written several years ago:
public const long TenBase = 10L;
public IEnumerable<long> IntToDigitList(long longint)
{
while (longint > 0)
{
yield return (longint % TenBase);
longint /= TenBase;
}
} To avoid mucking about with strings/chars. Code I probably got the idea from, if not the basis for this code, from StackOverFlow
«The truth is a snare: you cannot have it, without being caught. You cannot have the truth in such a way that you catch it, but only in such a way that it catches you.» Soren Kierkegaard
|
|
|
|
|
Tonight I banged my head against the wall while sleeping and I'm not feeling like reading a lot today but it looks like another excellent example of guiding rather than spoon feeding, so you get my upvote, Bill!
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Sascha Lefèvre wrote: Tonight I banged my head against the wall while sleeping Hi Sascha,
Ouch. Usually it is the walls who bang themselves against my head while I am trying to sleep; I am never sure if it is personal. Banging back at them is an idea I hadn't thought of.
I'd accept an up-vote from you, even if you were comatose.
cheers, Bill
«The truth is a snare: you cannot have it, without being caught. You cannot have the truth in such a way that you catch it, but only in such a way that it catches you.» Soren Kierkegaard
|
|
|
|
|
Looks pretty trivial.
What is your problem with this ?
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Hi,
I was given following question as an assessment but unfortunately I was unable to answer. Can someone help ?
The question is ;
The function given below is supposed to retrieve the substring between and including the ith and jth characters of the input string s. When i and j are out of range, ie. i,j< 0 ori,j >= s.length(), the function returns the string "<error>". The following table shows the expected and actual return values of the function for various inputs.
Test case s i j Expected return value Actual return value
1 "quick" 0 2 "qui" "qui"
2 "brown" 2 1 "ro" ""
3 "fox" 1 9 "<error>" "<error>"
4 "jumped" -1 4 "<error>" "<error>"
5 "lazy" 3 0 "lazy" "<error>"
6 "dog" 1 1 "o" "o"
Unfortunately, the function returns the wrong value for the 2nd and 5th test cases.
C# Version:
string SubString(string s, int i, int j)
{
int k;
string result;
try
{
k = s.Length;
if (i < 0 || j < 0 || i >= k || j >= k)
throw new Exception();
result = s.Substring(i, j + 1 - i);
}
catch (Exception e)
{
return "<error>";
}
return result;
}
a) Under what general conditions does the function return the wrong value?
b) Add some code to fix the function. Can you improve it? If so explain why your version is better.
|
|
|
|
|
A glance at those cases shows that when j is less than i it still sends a valid substring. This is not correct, the code should test for such a condition. Now it's up to you to fix the code.
|
|
|
|
|
Hello,
I have an application, Main, that is big enough to make debugging new components tedious. I developed another module(Sub) separately which I intended to pull into the Main when it was up and running. It has its own namespace but uses a lot of things from Main by way of linked files. When things were working I brought it into Main by adding an existing project. That all works fine inside the IDE.
I can compile and run Main.exe as a Release. It runs in the Bin\Release folder, which means, when I click the menu item the window for Sub opens as planned.
However when I move Main to Desktop or another folder and try to run it I get:
"System.IO.FileNotFoundException occurred in PresentationCore.dll"
Could not load file or assembly (Sub)'Treat04', Version =1.0.0.0, The system cannot find the file specified.
It looks like it is compiling two separate jobs. I want it all wrapped up as a single Release application.
Thanks for any suggestions,
Russ Fisher
|
|
|
|
|
You need to move Sub also. The system has a default list of directories to search and your project directory is not in the list, except when you run the application from within that directory.
|
|
|
|
|
Hello,
I have developed an application which has all the filter options of windows search in a single window. I want to add this application to Windows Explorer's menu.
How can I achieve this ?
Please help.
|
|
|
|
|
You are going to have to write a Browser Helper Object (BHO) that interacts with Explorer. Bear in mind that BHOs were originally introduced to extend IE but were extended to work with Explorer, so you are going to have to do a bit of work to make it work only in Explorer. Then you're going to have to do some pure COM interop work I'm afraid. You should get some idea on what you need to do here[^].
This space for rent
|
|
|
|
|
|
Why is it not working for you?
|
|
|
|
|
If you wanted help with a specific task, then it would make sense to state that in the original question. You might also want to explain "how" it is not working for you, otherwise you'll simply get alternative options. Did it compile? Did it throw an exception? If yes, what did the exception say? Did you follow the instructions on the page you linked? Up to where does it work?
AFAIK, adding a button would require a Shell Band Object[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Why I don't know. Since it deals with only registry It is not possible to trace the error. Also very less references are available for this work.
|
|
|
|
|
srikrishnathanthri wrote: Since it deals with only registry It is not possible to trace the error Did you change the permissions for the registry keys as suggested in the article? Sounds like that is the part most likely to go wrong. You might also want to try to contact the author by adding a comment there.
I cannot see whether this works on any other platform than Windows 7.
The deskband-solution would be my first choice, and MSDN[^] the starting point.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have changed the permissions. Without giving permission it is not possible to add the keys to that hives. Yes, It is working in only Windows 7.
|
|
|
|
|
Hi everyone i want to read rar file name and folder in this rar file but i dont know how i can do it
|
|
|
|
|
I would suggest a Google search for "how to read rar file" or similar.
|
|
|
|
|
|
There are many Unzip API available, that you can use to unzip your RAR file.
IonicZip.dll is one of the most used zip/unzip library you can use. Here is code snippet
ZipFile zipFile = new ZipFile("Path TO Your ZIP File");
zipFile.ExtractAll(Application.StartupPath);
see below link to download API
DotNetZip Library - Home[^]
SevenZipSharp - Home[^]
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
Hi,
1. I am developing an application in C# in which I want to disable all the mass storage USB devices except a few USB based hard disk (as white list hard disk).
2. Am trying to make logic in which I would like to make my my white-list based on internal serial number of hard disks (USB based ). This white-list I will try to populate and try to read as configuration file for my utility.
3. Thereafter, I will try like to compare all the incoming USB mass storage devices internal serial number with my white list serial numbers to filter them and extend the access permission based on positive matching else disable them if not matching with my white-list serial number of hard disk.
4. Now I know how to disable the USB Mass storage but am not able to get the USB mass storage serial number of all incoming USB devices in real time and also how to supply my white-list of UBS mass storage hard disk serial number during the run-time of my application.
Thanking you all in advance for your kind reply.
regards
Deepeshonly
|
|
|
|
|
So what is your actual question?
|
|
|
|
|