Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

Commonmark migration
Source Link

Challenge

##Challenge CompactifyCompactify a long list of numbers by replacing consecutive runs with ranges.

Example

Input

##Example ###Input 1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
The input is guaranteed to be in ascending order and will not contain duplicates.

Output

###Output 1 - 4, 7, 8, 10, 12 - 15
Note that ranges of two numbers should be left as is. (7, 8; not 7 - 8)

Rules

##Rules YouYou can accept a sorted list of integers (or equivalent datatype) as a method parameter, from the commandline, or from standard in. (pick whichever option results in shorter code)
You can output a list of strings by printing them, or by returning either a single string or set of strings.

Reference Implementation

##Reference Implementation (C#)

IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}

##Challenge Compactify a long list of numbers by replacing consecutive runs with ranges.

##Example ###Input 1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
The input is guaranteed to be in ascending order and will not contain duplicates.

###Output 1 - 4, 7, 8, 10, 12 - 15
Note that ranges of two numbers should be left as is. (7, 8; not 7 - 8)

##Rules You can accept a sorted list of integers (or equivalent datatype) as a method parameter, from the commandline, or from standard in. (pick whichever option results in shorter code)
You can output a list of strings by printing them, or by returning either a single string or set of strings.

##Reference Implementation (C#)

IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}

Challenge

Compactify a long list of numbers by replacing consecutive runs with ranges.

Example

Input

1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
The input is guaranteed to be in ascending order and will not contain duplicates.

Output

1 - 4, 7, 8, 10, 12 - 15
Note that ranges of two numbers should be left as is. (7, 8; not 7 - 8)

Rules

You can accept a sorted list of integers (or equivalent datatype) as a method parameter, from the commandline, or from standard in. (pick whichever option results in shorter code)
You can output a list of strings by printing them, or by returning either a single string or set of strings.

Reference Implementation

(C#)

IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}
Post Locked by Shog9
Notice added Historical significance by Shog9
Post Reopened by Shog9
Post Undeleted by Shog9
Post Deleted by animuson
Post Closed as "Needs more focus" by animuson
added 19 characters in body; added 4 characters in body; added 2 characters in body
Source Link
SLaks
  • 891.5k
  • 182
  • 1.9k
  • 2k
IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}
IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}
IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}
IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}
added 4 characters in body
Source Link
Nakilon
  • 35.2k
  • 16
  • 112
  • 149

##Challenge Compactify a long list of numbers by replacing consecutive runs with ranges.

##Example ###Input 1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 151, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
The input is guaranteed to be in ascending order and will not contain duplicates.

###Output 1 - 4, 7, 8, 10, 12 - 151 - 4, 7, 8, 10, 12 - 15
Note that ranges of two numbers should be left as is. (7, 8; not 7 - 8)

##Rules You can accept a sorted list of integers (or equivalent datatype) as a method parameter, from the commandline, or from standard in. (pick whichever option results in shorter code)
You can output a list of strings by printing them, or by returning either a single string or set of strings.

##Reference Implementation (C#)

IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}

##Challenge Compactify a long list of numbers by replacing consecutive runs with ranges.

##Example ###Input 1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
The input is guaranteed to be in ascending order and will not contain duplicates.

###Output 1 - 4, 7, 8, 10, 12 - 15
Note that ranges of two numbers should be left as is. (7, 8; not 7 - 8)

##Rules You can accept a sorted list of integers (or equivalent datatype) as a method parameter, from the commandline, or from standard in. (pick whichever option results in shorter code)
You can output a list of strings by printing them, or by returning either a single string or set of strings.

##Reference Implementation (C#)

IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}

##Challenge Compactify a long list of numbers by replacing consecutive runs with ranges.

##Example ###Input 1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
The input is guaranteed to be in ascending order and will not contain duplicates.

###Output 1 - 4, 7, 8, 10, 12 - 15
Note that ranges of two numbers should be left as is. (7, 8; not 7 - 8)

##Rules You can accept a sorted list of integers (or equivalent datatype) as a method parameter, from the commandline, or from standard in. (pick whichever option results in shorter code)
You can output a list of strings by printing them, or by returning either a single string or set of strings.

##Reference Implementation (C#)

IEnumerable<string> Sample(IList<int> input) {
 for (int i = 0; i < input.Count; ) {
 var start = input[i];
 int size = 1;
 while (++i < input.Count && input[i] == start + size)
 size++;
 if (size == 1)
 yield return start.ToString();
 else if (size == 2) {
 yield return start.ToString();
 yield return (start + 1).ToString();
 } else if (size > 2)
 yield return start + " - " + (start + size - 1);
 }
}
deleted 52 characters in body
Source Link
SLaks
  • 891.5k
  • 182
  • 1.9k
  • 2k
Loading
Post Made Community Wiki by Bill the Lizard
added 33 characters in body
Source Link
SLaks
  • 891.5k
  • 182
  • 1.9k
  • 2k
Loading
added 59 characters in body
Source Link
SLaks
  • 891.5k
  • 182
  • 1.9k
  • 2k
Loading
deleted 1 characters in body
Source Link
SLaks
  • 891.5k
  • 182
  • 1.9k
  • 2k
Loading
Source Link
SLaks
  • 891.5k
  • 182
  • 1.9k
  • 2k
Loading

AltStyle によって変換されたページ (->オリジナル) /