September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,7 @@
INPUT "Your string: ", s$
IF VAL(s$) = 0 AND s$ <> "0" THEN
PRINT "Not a number"
ELSE
PRINT "This is a number"
END IF

View file

@ -0,0 +1 @@
<cfoutput>#isNumeric(42)#</cfoutput>

View file

@ -0,0 +1,15 @@
PROGRAM NUMERIC
PROCEDURE IS_NUMERIC(S$->ANS%)
LOCAL T1,T1$
T1=VAL(S$)
T1$=STR$(T1)
ANS%=(T1$=S$) OR T1$=" "+S$
END PROCEDURE
BEGIN
PRINT(CHR$(12);)
INPUT("Enter a string",S$)
IS_NUMERIC(S$->ANS%)
IF ANS% THEN PRINT("is num") ELSE PRINT("not num")
END PROGRAM

View file

@ -2,13 +2,9 @@ defmodule RC do
def is_numeric(str) do
case Float.parse(str) do
{_num, ""} -> true
{_num, _r} -> false # _r : remainder_of_bianry
:error -> false
_ -> false
end
end
end
strs = ["123", "-12.3", "123.", ".05", "-12e5", "+123", " 123", "abc", "123a", "12.3e", "1 2"]
Enum.each(strs, fn str ->
IO.puts "#{inspect str}\t=> #{RC.is_numeric(str)}"
end)
["123", "-12.3", "123.", ".05", "-12e5", "+123", " 123", "abc", "123a", "12.3e", "1 2"] |> Enum.filter(&RC.is_numeric/1)

View file

@ -0,0 +1,9 @@
Public Sub Form_Open()
Dim sAnswer, sString As String
sString = Trim(InputBox("Enter as string", "String or Numeric"))
If IsNumber(sString) Then sAnswer = "'" & sString & "' is numeric" Else sAnswer = "'" & sString & "' is a string"
Print sAnswer
End

View file

@ -0,0 +1,11 @@
static function isNumeric(n:String):Bool
{
if (Std.parseInt(n) != null) //Std.parseInt converts a string to an int
{
return true; //as long as it results in an integer, the function will return true
}
else
{
return false;
}
}

View file

@ -0,0 +1,14 @@
// version 1.1
fun isNumeric(input: String): Boolean =
try {
input.toDouble()
true
} catch(e: NumberFormatException) {
false
}
fun main(args: Array<String>) {
val inputs = arrayOf("152", "-3.1415926", "Foo123", "-0", "456bar", "1.0E10")
for (input in inputs) println("$input is ${if (isNumeric(input)) "numeric" else "not numeric"}")
}

View file

@ -0,0 +1,4 @@
var %value = 3
if (%value isnum) {
echo -s %value is numeric.
}

View file

@ -1,5 +1,5 @@
sub is-number( $term --> Bool ) {
?($term ~~ /\d/) and +$term ~~ Numeric;
?($term ~~ /\d/) and $term ~~ Numeric;
}
printf "%10s %s\n", "<$_>", is-number( $_ ) for flat

View file

@ -1,5 +1,8 @@
s = '123'
try:
i = float(s)
except (ValueError, TypeError):
print 'not numeric'
def is_numeric(s):
try:
float(s)
return True
except (ValueError, TypeError):
return False
is_numeric('123.0')

View file

@ -1,3 +1 @@
s = '123'
if s.isdigit():
print int(s)
'123'.isdigit()

View file

@ -1,28 +1,13 @@
def is_numeric(lit):
'Return value of numeric literal string or ValueError exception'
# Handle '0'
if lit == '0': return 0
# Hex/Binary
litneg = lit[1:] if lit[0] == '-' else lit
if litneg[0] == '0':
if litneg[1] in 'xX':
return int(lit,16)
elif litneg[1] in 'bB':
return int(lit,2)
else:
try:
return int(lit,8)
except ValueError:
pass
# Int/Float/Complex
try:
return int(lit)
except ValueError:
pass
try:
return float(lit)
except ValueError:
pass
return complex(lit)
def is_numeric(literal):
"""Return whether a literal can be parsed as a numeric value"""
castings = [int, float, complex,
lambda s: int(s,2), #binary
lambda s: int(s,8), #octal
lambda s: int(s,16)] #hex
for cast in castings:
try:
cast(literal)
return True
except ValueError:
pass
return False

View file

@ -1,16 +1,23 @@
>>> for s in ['0', '00', '123', '-123.', '-123e-4', '0123', '0x1a1', '-123+4.5j', '0b0101', '0.123', '-0xabc', '-0b101']:
print "%14s -> %-14s %s" % ('"'+s+'"', is_numeric(s), type(is_numeric(s)))
def numeric(literal):
"""Return value of numeric literal or None if can't parse a value"""
castings = [int, float, complex,
lambda s: int(s,2), #binary
lambda s: int(s,8), #octal
lambda s: int(s,16)] #hex
for cast in castings:
try:
return cast(literal)
except ValueError:
pass
return None
"0" -> 0 <type 'int'>
"00" -> 0 <type 'int'>
"123" -> 123 <type 'int'>
"-123." -> -123.0 <type 'float'>
"-123e-4" -> -0.0123 <type 'float'>
"0123" -> 83 <type 'int'>
"0x1a1" -> 417 <type 'int'>
"-123+4.5j" -> (-123+4.5j) <type 'complex'>
"0b0101" -> 5 <type 'int'>
"0.123" -> 0.123 <type 'float'>
"-0xabc" -> -2748 <type 'int'>
"-0b101" -> -5 <type 'int'>
>>>
tests = [
'0', '0.', '00', '123', '0123', '+123', '-123', '-123.', '-123e-4', '-.8E-04',
'0.123', '(5)', '-123+4.5j', '0b0101', ' +0B101 ', '0o123', '-0xABC', '0x1a1',
'12.5%', '1/2', '½', '', 'π', '', '1,000,000', '1 000', '- 001.20e+02',
'NaN', 'inf', '-Infinity']
for s in tests:
print("%14s -> %-14s %-20s is_numeric: %-5s str.isnumeric: %s" % (
'"'+s+'"', numeric(s), type(numeric(s)), is_numeric(s), s.isnumeric() ))

View file

@ -0,0 +1,4 @@
// This function is not limited to just numeric types but rather anything that implements the FromStr trait.
fn parsable<T: FromStr>(s: &str) -> bool {
s.parse::<T>().is_ok()
}

View file

@ -0,0 +1,74 @@
BEGIN
BOOLEAN PROCEDURE ISNUMERIC(W); TEXT W;
BEGIN
BOOLEAN PROCEDURE MORE;
MORE := W.MORE;
CHARACTER PROCEDURE NEXT;
NEXT := IF MORE THEN W.GETCHAR ELSE CHAR(0);
CHARACTER PROCEDURE LAST;
LAST := IF W.LENGTH = 0
THEN CHAR(0)
ELSE W.SUB(W.LENGTH,1).GETCHAR;
CHARACTER CH;
W.SETPOS(1);
IF MORE THEN
BEGIN
CH := NEXT;
IF CH = '-' OR CH = '+' THEN CH := NEXT;
WHILE DIGIT(CH) DO CH := NEXT;
IF CH = '.' THEN
BEGIN
CH := NEXT;
IF NOT DIGIT(CH) THEN GOTO L;
WHILE DIGIT(CH) DO CH := NEXT;
END;
IF CH = '&' THEN
BEGIN
CH := NEXT;
IF CH = '-' OR CH = '+' THEN CH := NEXT;
WHILE DIGIT(CH) DO CH := NEXT;
END;
END;
L: ISNUMERIC := (W.LENGTH > 0) AND THEN (NOT MORE) AND THEN DIGIT(LAST);
END;
REAL X;
TEXT T;
FOR X := 0, -3.1415, 2.768&+31, 5&10, .5, 5.&10 DO
BEGIN
OUTREAL(X, 10, 20);
OUTIMAGE;
END;
OUTIMAGE;
FOR T :- "0", "-3.1415", "2.768&+31", ".5", "5&22" DO
BEGIN
OUTTEXT(IF ISNUMERIC(T) THEN " NUMERIC " ELSE "NOT NUMERIC ");
OUTCHAR('"');
OUTTEXT(T);
OUTCHAR('"');
IF T = "0" THEN OUTCHAR(CHAR(9)); OUTCHAR(CHAR(9));
COMMENT PROBE ;
X := T.GETREAL;
OUTREAL(X, 10, 20);
OUTIMAGE;
END;
OUTIMAGE;
X := 5.&10;
!X := 5&;
!X := 5.;
X := .5;
FOR T :- "", "5.", "5&", "5&+", "5.&", "5.&-", "5.&10" DO
BEGIN
OUTTEXT(IF ISNUMERIC(T) THEN " NUMERIC " ELSE "NOT NUMERIC ");
OUTCHAR('"');
OUTTEXT(T);
OUTCHAR('"');
OUTIMAGE;
END;
END

View file

@ -0,0 +1,6 @@
fcn isNum(text){
try{ text.toInt(); True }
catch{ try{ text.toFloat(); True }
catch{ False }
}
}