r/dailyprogrammer • u/nint22 1 2 • Oct 30 '12
[10/30/2012] Challenge #109 [Easy] Digits Check
Description:
Write a function, where given a string, return true if it only contains the digits from 0 (zero) to 9 (nine). Else, return false.
Formal Inputs & Outputs:
Input Description:
string data - a given string that may or may not contains digits; will never be empty
Output Description:
Return True or False - true if the given string only contains digits, false otherwise
Sample Inputs & Outputs:
"123" should return true. "123.123" should return a false. "abc" should return a false.
Notes:
This is a trivial programming exercise, but a real challenge would be to optimize this function for your language and/or environment. As a recommended reading, look into how fast string-searching works.
28
u/andkerosine Oct 30 '12
Trivial programming exercises call for esoteric languages, so I did this one in Whitespace. It simply outputs 0 for False and 1 for True. It could be a little bit more compact, but I like to ensure the stack is clean at the end of execution.
2
u/beginners-mind 0 0 Jan 05 '13
A little late to the party but here is anoter clojure solution.
10
u/shandelman Oct 30 '12 edited Oct 30 '12
Recursive Python answer:
But I know you guys like short code, so here's a functional, more Pythonic way to do it:
11
u/5outh 1 0 Oct 30 '12 edited Oct 30 '12
Haskell:
5
u/theOnliest Oct 30 '12
Perl:
4
u/[deleted] Oct 30 '12
J:
4
u/DouglasMeyer Oct 31 '12
RegExp / JS
Because everything needs to be solved using regexps :-)
7
u/pivotallever Oct 31 '12 edited Oct 31 '12
Here's 7 different methods in python. Each method is timed, and the time it takes is printed. The number it tests is either 5000 digits long, or the pathological case of 5000 digits plus 1 letter at the end. Each function is run 1000 times. I am bad at recursion, so if someone has a recursive python solution which doesn't overflow the stack, let me know and I will add it in.
Sample output:
Pure digits:
Pathological case:
1
u/[deleted] Oct 31 '12 edited Oct 31 '12
C:
I haven't tested this, it may contain bugs.
EDIT: Replaced string.h with a macro for isdigit
EDIT: Replaced isdigit with a function.
1
u/ImOnALampshade Nov 02 '12
Late to the part, I know, but I'm new here. In C/C++:
(Need to cast the character to an int on my compiler, where isdigit is a macro define w/ array lookup)
3
u/SpontaneousHam 0 0 Oct 30 '12 edited Oct 30 '12
Done in Haskell:
If anyone out there could show me how to do this pointfree style, I'd be interested in the implementation.Using andkerosine's advice:
3
u/[deleted] Oct 30 '12
Ruby:
3
u/[deleted] Oct 31 '12 edited Oct 31 '12
C++, probably not supposed to do this:
3
u/[deleted] Oct 31 '12
I'm not quite sure how you could use knuth-morris-pratt that you linked to for this? Isn't that for searching for a specific substring? Whereas here we are searching for any single character that is not a digit.
I've been doing a lot of Haskell with data structures recently, so
Technically speaking this would work for any foldable structure of characters, not just strings.
EDIT: Formatting
3
u/[deleted] Nov 09 '12
Haskell:
3
u/[deleted] Nov 14 '12
Python:
4
u/jesussqueegee Oct 30 '12
C#, just the method:
I'm pretty new to programming as a whole, so I'm sure there's an easier way to do it, but that was what came to mind.
2
u/yentup 0 0 Oct 30 '12
in Python:
2
u/ixid 0 0 Oct 30 '12 edited Oct 31 '12
In the D language:
2
u/skeeto -9 8 Oct 30 '12
Common Lisp,
2
u/the_mighty_skeetadon Oct 30 '12
Ruby. Pretty sure the best way here is Regexp, since the number of options (10) is pretty limited. Otherwise, I'd go with a hash checker.
Here are the tests from the examples.
2
u/skeeto -9 8 Oct 30 '12
In ANSI C,
2
u/SlimTim10 Oct 30 '12
Emacs Lisp:
2
u/[deleted] Oct 30 '12
Perl 6:
Not as trivial as it sounds.
2
u/skibo_ 0 0 Oct 31 '12
2
u/joeyGibson Oct 31 '12
Nobody has done a Scala version yet (at least that I could find, so here it is in one line.
2
u/andystevens91 Nov 02 '12
Hi /r/learnprogramming! I discovered this wonderful subreddit only some days ago and here is my first attempt in C (I'm just starting to learn, probably there are billions of ways of doing this better than me). Also, I'm sorry but English is not my primary language :)
Here it is:
2
u/[deleted] Nov 03 '12 edited Nov 03 '12
I am very new to programming and trying to learn groovy any critiques would be great.
I think this works too but I am not entirely sure if I need the println's
2
u/anidev Nov 05 '12
does sh count?
2
u/[deleted] Nov 14 '12
Python:
Still a noob, this was my first try at using regex for anything. Seems to work.
2
u/chazmizta Nov 15 '12 edited Nov 16 '12
Here's a shitty python answer i made:
Edit: Formatting + i'm an idiot
2
u/ahlk 0 0 Nov 24 '12 edited Nov 24 '12
perl. feels like cheating
2
u/no1warlord Jan 15 '13 edited Jan 15 '13
VB.Net (noob):
2
u/jaydottee Jan 17 '13
Here's my code in Java, I'm just starting out pretty much, so any tips would be much appreciated!
2
u/radiataGhost Jan 20 '13 edited Mar 25 '13
My Python 3 solution. Still kind of new to Python, any comments are appreciated
edit: derp
3
u/Unh0ly_Tigg 0 0 Oct 30 '12
Java regular expressions:
Using regular expressions can make some things really easy.
2
u/thevideoclown Oct 30 '12 edited Oct 30 '12
Java. Really quick and ugly code. I need more practice but it works :/
2
u/moxmith Oct 30 '12
My solution in Java. Would love any tips or feedback you have.
1
u/nagasgura 0 0 Oct 30 '12
Python:
1
u/error1f1f 0 0 Oct 31 '12
Looks like the method i used was already posted. My solution in C++ anyways:
1
u/ckjazz Oct 31 '12
How'd I do? First go at something in java really.
1
u/[deleted] Oct 31 '12
Smalltalk:
1
u/waftedfart Oct 31 '12
In C:
1
u/Rapptz 0 0 Oct 31 '12
1
u/oureux Oct 31 '12
I never ran it but it should work :)
1
u/rowenlemming Oct 31 '12
Javascript
1
u/takac00 Oct 31 '12
Clojure, nice 'lazy' implementation, doesn't matter how long the string is it will return on the first non number.
1
u/[deleted] Oct 31 '12
I took what I thought was a slightly different approach. After I finished I looked at everyone else's code and realized everyone used char array. I guess char array is a smarter approach but I didn't think of it. For C#:
1
u/dbh937 0 0 Oct 31 '12
Python:
1
u/Zamarok Oct 31 '12 edited Nov 09 '12
JavaScript:
1
u/robin-gvx 0 2 Oct 31 '12
1
u/d3v3l0p3r Oct 31 '12
Powershell ( works in others using .NET 2.0 and up )
1
u/TuringFeel Oct 31 '12
coffeescript
1
u/swarage 0 0 Oct 31 '12
using ruby and regex
1
u/[deleted] Oct 31 '12
Trying to work on my C++:
1
u/davit-khaburdzania Nov 01 '12
in ruby
1
u/thebugfinder Nov 01 '12
Ruby
1
u/aksealman 0 0 Nov 02 '12
This solution is done in a c++ function. Also this is my first post on Reddit so my formatting might be a little weird
1
u/tgkokk 0 0 Nov 02 '12
Java:
1
u/[deleted] Nov 05 '12
SML (Mosml):
Pretty straightforward. Could properly be optimized.
1
u/MrPlow442 Nov 13 '12 edited Nov 13 '12
Looks like I've still got ways to go before i can call myself a programmer
C++11:
1
u/ahoy1 Nov 17 '12
Python
1
u/duydosomething Dec 05 '12
Here's my solution in Java. Would love feedback
}
1
u/dankki 0 0 Dec 10 '12
1
u/marekkpie Jan 08 '13
Lua. For some odd reason both '%d' and '[0-9]' both count the decimal as a digit, so I had to make this one a little lame.
1
u/shubhamVerma Mar 23 '13
Python
1
u/flightcrank 0 0 Mar 28 '13
pretty easy one
using C:
1
u/dont_have_soap Apr 04 '13
Java, using try-catch with Integer.parseInt():
0
u/[deleted] Nov 07 '12
Sort of late to the party but I am new here.
Python Answer
yup.....
0
u/[deleted] Oct 30 '12
[deleted] — view deleted comment
0
u/[deleted] Nov 04 '12
[deleted] — view deleted comment