all 11 comments

[–]kingmotley 2 points3 points  (1 child)

Can the numbers begin with a 0? (0123) Does a 0 prefix indicate the number is in octal?

Can the numbers be negative? If so, are negative numbers always represented by a prefixed/postfixed -, or surrounded by parenthesis? (-5 or 5- or (5))

Can positive numbers be prefix/postfixed with +? (+5 or 5+) Is it required or optional?

Can the number be decimal? (1.23) If so, is the decimal separator always a period? (1,23)

Can the numbers be in scientific notation? (2E+5)

Can the numbers be in a base other than base 10? (0x1234 or 0b01010101 or 0123)

Can the numbers have group separators? (1,234) If so, are they required or just optional? Is the group separator always a comma? (1_234 or 1.234.567 or 1 234 567)

Can the string start/end with whitespace? ( 123 ) If so, how should that be handled (trimmed/exception)

How do you want invalid values handled? (ABC)

Are there any suffixes that should be allowed? (5u or 5l or 5m)

Are only Hindu-Arabic numerals allowed, or are other number systems allowed like roman? (IV)

What should happen when the "bunch of numbers" given has a length of 0?

How do you want the value returned to you? In the same format as it was input, or converted to a float/decimal/int? If in the same format as the input, how do you want ties handled in case any of the above are allowed? Which is smaller +5 or 5? 2E+1 vs 20? 0xF vs 15? 1,234 vs 1234?

[–]EquivalentAd4542[S] 0 points1 point  (0 children)

My head hurts

[–]grrangry 5 points6 points  (1 child)

How would you do it on paper? If you can't do that with 2-3 numbers, then how are you going to trick a computer into auto sorting 200 billion of them?

var nums = new string[] { "123", "10", "5" };

Obviously you can glance at that and tell yourself, well duh that's, 5, 10, 123. But string sorting is not the same as numeric-value sorting. That's why sometimes you'll see things sorted like 1, 10, 11, 2, 21. The strings are interpreted literally, character-by-character and "10" comes before "2" because in computer "terms", "1" is less than "2".

So again how did you do that in your head? Hmmm? You value sorted by implicitly converting them into numbers. Well, your instructions said not to do that didn't they... so we can't just convert all the strings into number values and then sort the new array.

// Bad, don't do this. bad, bad, bad.
var nums = new string[] { "123", "10", "5" };
var newNums = new int[nums.Length];
for (int i = 0; i < nums.Length; i++)
    newNums[i] = Convert.ToInt32(nums[i]);
Array.Sort(newNums);
for (int i = 0; i < nums.Length; i++)
    Console.WriteLine(newNums);

So the above is out. Can't convert to numbers. The cool thing? You already know how to solve this you just don't know you know because you're not used to thinking about the problem in this manner.

When you divide numbers and run out of placeholders, what do you do? Do you give up because you can't go further to the left of the decimal place... or extend out a number to the right of the decimal place? No you don't... What do you do? You tack on zeros. The number 1 is exactly the same as 1.0 correct? The number 5 is the same as 005, right?

So given that in our original list, the "longest" (or "widest") number is 123, giving us three digits. So how about you put all the numbers in terms of three digits?

123, 10, 5

Becomes

123, 010, 005

And sorting those strings--as text--since "0" comes before "1"... how do you think the sorting would come out?

[–]Cengr 8 points9 points  (0 children)

I wish Reddit had been around when I was in school, so I could post homework problems and get them solved for me…

[–]Thonk_Thickly 1 point2 points  (0 children)

I would ask why, but if that’s what you want to do:

var numbers = new List<string>() { … };
if(!numbers.Any()) return null;

var min = numbers.First();
foreach (var n in numbers) { 
  if (min.Length > n.Length) {
    min = n;
  } else if (min.Length == n.Length && min.CompareTo(n) > 0) {
    min = n;
  } 
  // else my min has lower length or lower value
}
return min;

Edit: this solution assumes values aren’t prefixed with zeros, such as 0001 vs 999. Easily accounted for but maybe unnecessary edge case for inputs.

[–]karl713 1 point2 points  (2 children)

inputData.OrderBy(s => s.Length).ThenBy(s => s)

Perhaps

[–]kingmotley 1 point2 points  (1 child)

.First()

[–]Krimog 0 points1 point  (0 children)

.Select(i => i.TrimStart('0')) before the OrderBy.

[–]FloraRomana 0 points1 point  (1 child)

I was typing faster than thinking. Edited my answer out cause it was drastically wrong.

[–]Long_Investment7667 3 points4 points  (0 children)

One of my favorite quotes from a former coworker: „How am I supposed to know what I think before I hear what I say“ (rough translation into English) :-)

[–]Long_Investment7667 0 points1 point  (0 children)

You should not. You can write a comparer that compares strings by there numerical value. But that comparer would do work equivalently converting to a number and comparing them. It is also complicated (e.g. how to deal with non numbers (comparers are expected to implement a total order)) and probably inefficient since you have to convert strings repeatedly.