r/dailyprogrammer • u/fvandepitte 0 0 • Nov 15 '16
[2016-11-15] Challenge #292 [Easy] Increasing range parsing
Description:
We are given a list of numbers in a "short-hand" range notation where only the significant part of the next number is written because we know the numbers are always increasing (ex. "1,3,7,2,4,1" represents [1, 3, 7, 12, 14, 21]). Some people use different separators for their ranges (ex. "1-3,1-2", "1:3,1:2", "1..3,1..2" represent the same numbers [1, 2, 3, 11, 12]) and they sometimes specify a third digit for the range step (ex. "1:5:2" represents [1, 3, 5]).
NOTE: For this challenge range limits are always inclusive.
Our job is to return a list of the complete numbers.
The possible separators are: ["-", ":", ".."]
Input:
You'll be given strings in the "short-hand" range notation
"1,3,7,2,4,1"
"1-3,1-2"
"1:5:2"
"104-2"
"104..02"
"545,64:11"Output:
You should output a string of all the numbers separated by a space
"1 3 7 12 14 21"
"1 2 3 11 12"
"1 3 5"
"104 105 106 107 108 109 110 111 112"
"104 105 106...200 201 202" # truncated for simplicity
"545 564 565 566...609 610 611" # truncated for simplicityFinally
Have a good challenge idea, like /u/izxle did?
Consider submitting it to /r/dailyprogrammer_ideas
Update
As /u/SeverianLies pointed out, it is unclear if the - is a seperator or a sign.
For this challenge we work with only positive natural numbers.
12
u/[deleted] Nov 15 '16
Should it work for negative numbers? i.e. will '-' ever not be a separator?
3
u/Godspiral 3 3 Nov 15 '16
How would the list,
1 32be encoded?6
u/Daanvdk 1 0 Nov 15 '16
+/u/CompileBot Python 3
Input:
1
u/balkierode Nov 15 '16
Is step allowed only with ':' or any other seperator as well?
1-5-21
u/KoncealedCSGO Nov 15 '16
Can someone explain how to get the numbers on how they did in the output. I'm extremely confused.
3
u/Relayerduos Nov 18 '16
I tried to golf this problem as much as possible, I managed to get it down to 333 characters of Python 3!
+/u/CompileBot Python 3
Input:
2
u/skeeto -9 8 Nov 15 '16
C. It breaks the input into tokens split on commas, then resolves the individual ranges by a scanf() pattern match.
2
u/casualfrog Nov 15 '16
JavaScript
Try it online
2
u/[deleted] Nov 16 '16 edited Nov 16 '16
Doesn't seem easy at all, haskell:
Result
Edit: Fix logic errors & add test cases.
2
u/[deleted] Nov 16 '16
Mathematica
Output:
2
u/Vectorious Nov 16 '16 edited Sep 18 '25
teeny distinct dependent live cover truck fall wrench wide gold
This post was mass deleted and anonymized with Redact
2
u/val0528 Nov 17 '16
C++
Results
2
u/a_ctor Nov 20 '16
C# 7 - If you want to try it you will need VS 2017; the compilation symbols __DEMO__ and __DEMO_EXPERIMENTAL__; as well as the Nuget package System.ValueTuple (you only need this if you are targeting .NET 4.6.1 or lower).
Code:
Input:
Output:
2
u/[deleted] Nov 21 '16
Another Python 2 solution:
2
u/Gregorio2121 Nov 25 '16
Java https://gist.github.com/anonymous/869d514e61cb240a1f7d5d069c8907f6
2
u/draegtun Nov 29 '16 edited Nov 29 '16
Rebol
Example usage in Rebol console:
NB. Tested in Rebol 3
2
u/apentlander 0 1 Dec 06 '16
Python 3
1
u/Minolwa Nov 15 '16 edited Nov 15 '16
Scala
This isn't my favorite solution that I've ever made. But this challenge was a little more challenging than an [Easy] for me. Also, my first time using regexes, so forgive me for any mistakes.
I'm accumulating all of my solutions over on my Github! Go check it out!
1
u/[deleted] Nov 16 '16
Not too different from /u/Daanvdk's method, but I find the first value of the range in constant time, among other things.
+/u/CompileBot Python 3
Input:
1
u/kalgynirae Nov 16 '16 edited Nov 16 '16
+/u/CompileBot Python 3 (edit: sadly, ideone (and thus CompileBot) is still on Python 3.4, so it can't run this code with the Python 3.5
typingmodule included)Input:
1
u/davanger Nov 16 '16
C#:
1
u/GamingG Nov 16 '16
PHP
1
u/ASpueW Nov 16 '16 edited Nov 16 '16
Rust
1
u/free2use Nov 16 '16 edited Nov 17 '16
Not the most efficient one, but I'm happy even with that) Definitely not the easy day, but no complains though!)
+/u/CompileBot Clojure
Input:
1
u/gabyjunior 1 2 Nov 17 '16
C, reads input char by char and makes computation on strings. Handles base 2 to 36 (program argument).
Sample hexa input
Output
1
u/Tetsumi- 1 0 Nov 17 '16
Racket
1
u/narcodis Nov 17 '16
Go
Output:
1
u/voice-of-hermes Nov 18 '16
1
u/Fl114x Nov 18 '16
Ocaml, well, french ocaml but still ocaml
1
u/SoftwareAlchemist Nov 19 '16
Written in C
1
u/Stan-It Nov 19 '16
Python
1
u/_tpr_ Nov 19 '16
Dart Not the cleanest in the world...
1
u/PharmyOf1 Nov 20 '16
Moving from Python to Java . . . for some reason.
1
u/animejunkied Nov 20 '16
Python. Not the shortest but maybe easier to understand than the others.
1
u/Ge_O Nov 21 '16
C#
1
u/sickapps420 Nov 21 '16
AWK Any feedback is welcome.
1
u/Saytahri Nov 27 '16
+/u/CompileBot Haskell
Input:
1
u/tealfan Dec 04 '16
Java
Output
0
u/[deleted] Nov 15 '16
[deleted] — view deleted comment
0
u/[deleted] Nov 17 '16
[deleted] — view deleted comment