Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,92 @@
# we define a UNION MODE so that our middle 3 digits PROC can #
# return either an integer on success or a error message if #
# the middle 3 digits couldn't be extracted #
MODE EINT = UNION( INT # success value #, STRING # error message # );
# PROC to return the middle 3 digits of an integer. #
# if this is not possible, an error message is returned #
# instead #
PROC middle 3 digits = ( INT number ) EINT:
BEGIN
# convert the absolute value of the number to a string with the #
# minumum possible number characters #
STRING digits = whole( ABS number, 0 );
INT len = UPB digits;
IF len < 3
THEN
# number has less than 3 digits #
# return an error message #
"number must have at least three digits"
ELIF ( len MOD 2 ) = 0
THEN
# the number has an even number of digits #
# return an error message #
"number must have an odd number of digits"
ELSE
# the number is suitable for extraction of the middle 3 digits #
INT first digit pos = 1 + ( ( len - 3 ) OVER 2 );
# the result is the integer value of the three digits #
( ( ( ABS digits[ first digit pos ] - ABS "0" ) * 100 )
+ ( ( ABS digits[ first digit pos + 1 ] - ABS "0" ) * 10 )
+ ( ( ABS digits[ first digit pos + 2 ] - ABS "0" ) )
)
FI
END; # middle 3 digits #
main: (
# test the middle 3 digits PROC #
[]INT test values = ( 123, 12345, 1234567, 987654321
, 10001, -10001, -123, -100
, 100, -12345
# the following values should fail #
, 1, 2, -1, -10, 2002, -2002, 0
);
FOR test number FROM LWB test values TO UPB test values
DO
CASE middle 3 digits( test values[ test number ] )
IN
( INT success value ): # got the middle 3 digits #
printf( ( $ 11z-d, " : ", 3d $
, test values[ test number ]
, success value
)
)
,
( STRING error message ): # got an error message #
printf( ( $ 11z-d, " : ", n( UPB error message )a $
, test values[ test number ]
, error message
)
)
ESAC;
print( ( newline ) )
OD
)

View file

@ -1,84 +1,27 @@
#include <iostream>
#include <sstream>
#include <string>
// @author Martin Ettl
// @date 2013-02-04
/**
* Convert variables of type T to std::string
*
*
* @param d --> digit of type T
*
* @return <-- the corresponding string value
*/
template <typename T> const std::string toString(const T &d)
std::string middleThreeDigits(int n)
{
std::ostringstream result;
result << d;
return result.str();
}
auto number = std::to_string(std::abs(n));
auto length = number.size();
/**
* Determine the middle n digits of the integer. If it is not possible to determine the
* the middle n digits, an empty string is provided.
*
* @param iDigit --> The digit to test
* @param n --> The number of digits inbetween
*
* @return <-- the middle three digits
*/
std::string strMiddleNDigits(int iDigit, const int &n)
{
// is negative: --> convert to a positive number
if(iDigit<0)
{
iDigit*=-1;
if (length < 3) {
return "less than three digits";
} else if (length % 2 == 0) {
return "even number of digits";
} else {
return number.substr(length / 2 - 1, 3);
}
// convert to string
std::string strNumber (toString(iDigit));
size_t len(strNumber.length());
if( (len < n) || (len % 2 == 0) )
{
return "";
}
size_t mid(len/2);
return strNumber.substr(mid-n/2, n);
}
/**
* Determine the middle three digits of the integer. If it is not possible to determine the
* the middle three digits, an empty string is provided.
*
* @param iDigit --> The digit to test
*
* @return <-- the middle three digits
*/
std::string strMiddleThreeDigits(int iDigit)
{
return strMiddleNDigits(iDigit,3);
}
int main()
{
const int iPassing[] = {123, 12345, 1234567, 987654321, 10001, -10001,
-123, -100, 100, -12345
};
for(unsigned int ui = 0; ui < 10; ++ui)
{
std::cout << "strMiddleThreeDigits("<< iPassing[ui] <<"): "
<< strMiddleThreeDigits(iPassing[ui])<< "\n";
}
auto values {123, 12345, 1234567, 987654321, 10001,
-10001, -123, -100, 100, -12345,
1, 2, -1, -10, 2002, -2002, 0};
const int iFailing[] = {1, 2, -1, -10, 2002, -2002, 0};
for(unsigned int ui = 0; ui < 7; ++ui)
{
std::string strResult = strMiddleThreeDigits(iFailing[ui]);
std::cout << "strMiddleThreeDigits("<< iFailing[ui] <<"): "
<< (strResult.empty()?"Need odd and >= 3 digits":strResult)
<< "\n";
for (auto&& v : values) {
std::cout << "middleThreeDigits(" << v << "): " <<
middleThreeDigits(v) << "\n";
}
return 0;
}

View file

@ -1,7 +1,7 @@
import std.stdio, std.traits, std.conv, std.range;
import std.stdio, std.traits, std.conv;
string middleThreeDigits(T)(in T n) if (isIntegral!T) {
auto s = n < 0 ? n.text().dropOne : n.text();
string middleThreeDigits(T)(in T n) pure nothrow if (isIntegral!T) {
auto s = n < 0 ? n.text[1 .. $] : n.text;
auto len = s.length;
if (len < 3 || len % 2 == 0)
return "Need odd and >= 3 digits";
@ -12,10 +12,10 @@ string middleThreeDigits(T)(in T n) if (isIntegral!T) {
void main() {
immutable passing = [123, 12345, 1234567, 987654321, 10001, -10001,
-123, -100, 100, -12345, long.min, long.max];
foreach (n; passing)
foreach (immutable n; passing)
writefln("middleThreeDigits(%s): %s", n, middleThreeDigits(n));
immutable failing = [1, 2, -1, -10, 2002, -2002, 0, int.min, int.max];
foreach (n; failing)
immutable failing = [1, 2, -1, -10, 2002, -2002, 0,int.min,int.max];
foreach (immutable n; failing)
writefln("middleThreeDigits(%s): %s", n, middleThreeDigits(n));
}

View file

@ -0,0 +1,14 @@
middle_3_digits(Number, [D1,D2,D3]) :-
verify_middle_3_able(Number, Digits),
append(FrontDigits, [D1,D2,D3| BackDigits], Digits),
same_length(FrontDigits, BackDigits).
verify_middle_3_able(Number, Digits) :-
must_be(number, Number),
AbsNumber is abs(Number),
number_chars(AbsNumber, Digits),
length(Digits, NumDigits),
( 3 > NumDigits -> domain_error('at least 3 digits', Number)
; 0 is NumDigits mod 2 -> domain_error('odd number of digits', Number)
; true
).

View file

@ -0,0 +1,6 @@
test_correct :-
TestCases = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345],
foreach( ( member(TestCase, TestCases),
middle_3_digits(TestCase, Result) ),
format('Middle 3 digits of ~w ~30|: ~w~n', [TestCase, Result])
).

View file

@ -0,0 +1,14 @@
def middle_three_digits(num)
# minus sign doesn't factor into digit count,
# and calling #abs acts as a duck-type assertion
num = num.abs
# convert to string and find length
length = (str = num.to_s).length
# check validity
raise ArgumentError, "Number must have at least three digits" if length < 3
raise ArgumentError, "Number must have an odd number of digits" if length.even?
return str[length/2 - 1, 3].to_i
end

View file

@ -0,0 +1,14 @@
samples = [
123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345,
1, 2, -1, -10, 2002, -2002, 0
]
left_column_width = samples.map { |n| n.to_s.length }.max
samples.each do |n|
print "%#{left_column_width}d: " % n
begin
puts "%03d" % middle_three_digits(n)
rescue ArgumentError => e
puts e
end
end

View file

@ -1,30 +0,0 @@
def middle_three_digits(n)
# minus sign doesn't factor into digit count,
# and calling #abs acts as a duck-type assertion
n = n.abs
# convert to string and find length
l = (s = n.to_s).length
# check validity
raise ArgumentError, "Number must have at least three digits" if l < 3
raise ArgumentError, "Number must have an odd number of digits" if l.even?
return s[l/2-1,3].to_i
end
samples = [
123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345,
1, 2, -1, -10, 2002, -2002, 0
]
width = samples.map { |n| n.to_s.length }.max
samples.each do |n|
print "%#{width}d: " % n
begin
puts "%03d" % middle_three_digits(n)
rescue ArgumentError => e
puts e
end
end

View file

@ -0,0 +1,12 @@
;WITH DATA
AS (SELECT CAST(ABS(NUMBER) AS NVARCHAR(MAX)) charNum,
NUMBER,
LEN(CAST(ABS(NUMBER) AS NVARCHAR(MAX))) LcharNum
FROM TABLE1)
SELECT CASE
WHEN ( LCHARNUM >= 3
AND LCHARNUM % 2 = 1 ) THEN SUBSTRING(CHARNUM, LCHARNUM / 2, 3)
ELSE 'Error!'
END Output,
NUMBER Input
FROM DATA