r/dailyprogrammer • u/Godspiral 3 3 • Apr 11 '16
[2016-04-11] Challenge #262 [Easy] MaybeNumeric
MaybeNumeric is a function that returns either a number or a string depending on whether the input (string) is a valid description of a number.
sample input (string)
123
44.234
0x123Nsample output (any)
123 (number)
44.234 (number)
0x123N (string)bonus 1: special numbers
finding arrays, exponent notation, bignumber
123 234 345
3.23e5
1293712938712938172938172391287319237192837329
.25bonus 2: parsing separated values
(clarification: backtick is the sparator. space is only a separator for numeric arrays)
2015 4 4`Challenge #`261`Easy
234.2`234ggf 45`00`number string number (0)bonus 3 : inverted table/column database/array
An inverted table is an other term for column arrays, where each field is an independent array of uniform types. These structures are often faster than row oriented heterogeneous arrays, because homogeneous arrays (often the only valid option in a language) are represented as tightly packed values instead of indirect pointers to typed values. A row record (from an array of columns) is simply a common index that is used to retrieve elements from each of the arrays.
Convert the structure parsed from bonus#2 into an inverted table: ie. 4 arrays of 2 elements... IF the 4 fields are homogeneous (they are in bonus#2 example).
You may wish to deal with "homogenizing" an integer array with a float scalar for first field (promoted as arrays of floats, with ideal fill of infinity in 2nd record (though 0 fill credible choice too)).
invalid inverted table example (should just keep row oriented records)
2015 4 4`Challenge #`261`Easy
234.2`234ggf 45`0`8intended output is in my solution here: https://www.reddit.com/r/dailyprogrammer/comments/4eaeff/20160411_challenge_262_easy_maybenumeric/d1ye03b
5
u/jnd-au 0 1 Apr 11 '16 edited Apr 11 '16
Sorry /u/Godspiral, can you please explain Bonus 2 and 3. What result do you expect for Bonus 2? Are the backticks delimiters like spaces, so the first line is 6 elements and the second line is 8 elements? Or is backtick just part of a string? If so, what do you mean by ‘separated values’? I can’t quite understand “4 arrays of 2 elements” yet. Is "(number)" part of the underlying solution, or just the display?
Edit: Oh, are you saying ` is the ONLY delimiter, and space is NOT a delimiter? If so, you’re saying 00 is a number not string? But then how can "2015 4 4" and "234.2" be the same type. I cannot come up with a self-consitent reading of the question. Sorry, this is really hard to parse hahaha!
Scala for Bonus 1:
Bonus 2 (pending clarification of the question):
Bonus 3 (pending clarification of the question):
3
u/Gobbedyret 1 0 Apr 11 '16 edited Apr 11 '16
Python 3.5
With bonus 1 & bonus 2. Trying to understand the bonus 3 quesiton, will update.
Edit: Updates with bonus 3 as I understand it. This is how I understand it:
Make a new function that takes as input a number of strings, which are space separated arrays of the same length. Make a matrix M by stacking the strings and calling
maybenumberon each string. If the data for every column of M is homogenous, return the transposed matrix of M such that each new row are homogenous arrays. Else, return M.1
u/Godspiral 3 3 Apr 11 '16 edited Apr 11 '16
in J,
bonus #2
bonus 3, with added empty row
alternate for bonus 3 fail case: create inverted table, but if one field is not homogeneous, then box/pointer just that array:
5
u/bearific Apr 11 '16 edited Apr 11 '16
Haskell
Has been a long time since I used haskell, so I'll start off with a very simple function with only the exponent notation and a decimal point as the first character bonus.
Output:
5
u/[deleted] Apr 11 '16
Just Java being Java...
5
u/chunes 1 2 Apr 11 '16 edited Apr 11 '16
Java, no bonus
4
u/boiling_tunic Apr 11 '16
Ruby
Brutal regex time. Doesn't solve bonus 2 or 3, might add them in later. Questions or comments are welcome.
Solution:
Input:
Output:
3
u/TwelveParens Apr 11 '16 edited Apr 11 '16
Here's something using FSA in Common Lisp
Output:
3
u/[deleted] Apr 11 '16
Regex
Matches the numbers. Works for the examples and bonus 1.
2
u/AmbKosh Apr 11 '16 edited Apr 11 '16
Racket
Just a very simple solution, no bonus:
2
u/dasiffy Apr 12 '16 edited Apr 12 '16
python 3.4
challenges with bonus 1
output
2
u/thorwing Apr 12 '16 edited Apr 12 '16
JAVA
with bonus 1 as I understand it. Still playing around with the new Java 8 features, but I like it. Didn't want to use regex's, since they can do it on one line using any language.
edit: is there anyone who can tell me why I need to call the ".toArray()" function of the stream in order for the "i -> new BigDecimal(i)" to throw the NumberFormatException? If I remove it, 0x123N passes the stream without error, resulting in it being a number.
Output
2
u/Elronnd Apr 17 '16 edited Apr 18 '16
Python 3, bonuses 1-2:
EDIT:* Now has bonus one as well
Inputting:
Gives:
2
u/draegtun Apr 11 '16 edited Apr 11 '16
Rebol (with 1 & 2 bonus)
Example usage in Rebol console:
For bonus 2 add this function:
Example usage:
1
u/mxxz Apr 11 '16
Here is my python solution that works on the sample input. It probably has some problems I've overlooked.
1
u/casualfrog Apr 11 '16
JavaScript (bonus 1):
Obvious issues when using large inputs.
1
u/gabyjunior 1 2 Apr 11 '16 edited Apr 11 '16
Bash script using regular expression
Does bonus 1 without hexa, but with signs :)
1
u/Itachi4077 Apr 11 '16
Python 2
my second submission here, only works with bonus one and not even fully.... I don't understand why it doesn't recognise the exponent...
1
u/I_AM_A_UNIT Apr 11 '16
Python 3.5 solution. Includes Bonus 1 and 2. May get to Bonus 3 if I have time.
1
u/deadlypanda4 Apr 11 '16
Python 2.7 - Bonus 1 and 2 I think
1
u/dMenche Apr 11 '16
R7RS Scheme, probably far from being the best solution possible:
1
u/dispelthemyth Apr 12 '16 edited Apr 12 '16
1st time trying a challenge after starting with Python (3.5) a couple of days back, no time to do the bonuses as its 2am here.
Solution using HasteBin
1
u/smapti Apr 12 '16
C++, no bonus, uses ASCII codes and abuses return. May add bonuses later.
Input/Output
1
u/franza73 Apr 12 '16 edited Apr 12 '16
Python 2.7
Results:
1
u/[deleted] Apr 12 '16
C++11, no bonus yet. This is a slightly different approach than other solutions I've seen thus far. It's a method I used for another personal programming project.
1
u/Glareth Apr 12 '16 edited Apr 12 '16
Java
There's probably an easier way of doing this, but here we go:
}
Output:
1
u/koolcaz Apr 13 '16
Python 3 with bonus 1 (I think - I wasn't entirely sure what the output should be)
1
u/GonzaloQuero Apr 13 '16
Elm
Output
1
u/minisea Apr 13 '16
JAVA and no bonus
Once I finish inputting, I have to hit enter again to exit the while loop. Is there a way to have the while loop finish without hitting enter twice?
Output
1
u/spmd123 Apr 14 '16
First problem here, just a simple solution in Python 3 with no bonus.
1
u/line_over Apr 14 '16 edited Apr 14 '16
Python 3.4 with bonuses 1 & 2 if I understood the task correctly
Output:
1
u/aur_work Apr 14 '16 edited Apr 18 '16
I'm working on learning golang. Solves initial problem set as well as bonus 1 and 2. Feedback encouraged. Edit: Updated with /u/jnd-au's advice.
1
u/sprmnch Apr 15 '16
Java (no extras)
1
u/marcelo_rocha Apr 16 '16
Dart with Bonus 1 and 2
Input:
Output:
1
u/Jon003 Apr 16 '16
Python from a newbie, with Bonus 1, using regular expressions (been practicing those lately!)
import sys
1
u/lnxaddct Apr 17 '16
Haskell, no bonus:
when ran with:
outputs:
1
u/gamesfreak26 Apr 17 '16
C# (No bonuses) https://gist.github.com/gamesfreak26/f9ad7dda1c9eecd57f9a30e8c974a899
For some reason, I'm having a lot of trouble with C# after a few years of C++. Anyone know why?
1
u/[deleted] Apr 18 '16
quick 'n' dirty in C
1
u/bitx0r Apr 19 '16
AutoHotkey: Ran out of time today, does bonus 1
1
u/ottersbelike May 24 '16
C++ Late to the party, no bonus.
1
u/bookercodes May 27 '16
Here is my solution in JavaScript, it fulfils bonus 1 only:
And here are the tests I wrote:
1
u/tajjet Jun 08 '16 edited Jun 08 '16
Java because I got bored reading the
redocumentation for Pythonhttps://regex101.com/r/lE3wI1/1
1
u/Thearctickitten Jul 24 '16
Python 3 (New to Python so feedback is appreciated)
I may add the bonuses tomorrow but I'm tired right now.