Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- value1Temp := MaxInt;
- value2Temp := MaxInt;
- value3Temp := MaxInt;
- if ( value1T > 0) then
- value1Temp := value1;
- if ( value2 > 0) then
- value2Temp := value2;
- if ( value3 > 0) then
- value3Temp := value3;
- Result := Min(value1Temp, Min(value2Temp, value3Temp));
- if Result = MaxInt then
- Result := 0;
- function LowestPositiveInt(IntArr : array of Integer):Integer;
- var
- iX : integer;
- bValid : boolean;
- begin
- Result := MaxInt;
- bValid := False;
- for ix := 0 to High(IntArr) do
- if (IntArr[ix] > 0) then
- begin
- bValid := true;
- if (IntArr[iX] < Result) then
- Result := IntArr[ix];
- end;
- if not bValid then
- Result := 0;
- end;
- ShowMessage(IntToStr( LowestPositiveInt([5,2,3,-1,12]) ));
- Result := LowestPositiveInt( [ Value1, Value2, Value3 ] );
- int maxPositiveValue(int *number, int listSize)
- {
- int i, result = 0;
- for(i = 0; i < listSize; i++)
- {
- if(number[i] > 0 && (number[i] < result || result == 0))
- result = number[i];
- }
- return result;
- }
- function cmMinInt( XX, YY, ZZ : longint ) : longint;
- begin
- result := max(0,longint(
- min(longint((XX-1) xor $80000000),
- min(longint((YY-1) xor $80000000),
- longint((ZZ-1) xor $80000000)
- )) xor $80000000)+1);
- end;
- function cmMinInt( XX, YY, ZZ : longint ) : longint;
- begin
- // swap ordinal coding for range MININT..0 with range 1..MAXINT
- XX := XX-1; // move region division to between 0 and 1
- XX := XX xor $80000000; // swap regions, preserving ordering
- XX := longint(XX); // cram back into signed 32-bit
- // similarly with YY and ZZ
- YY := longint((YY-1) xor $80000000);
- ZZ := longint((ZZ-1) xor $80000000);
- // find min of three recoded values
- result := min(XX,min(YY,ZZ));
- // swap ordering back again
- result := result xor $80000000; // swap regions, preserving ordering
- result := result+1; // move region division back home
- result := longint(result); // cram back into signed 32-bit
- // if all three were neg, result at this point is neg -- clip to zero
- result := max(0,result);
- end;
- #include <stdio.h>
- #include <limits.h>
- int smallest_v1(int a, int b, int c)
- {
- int min = INT_MAX;
- min = a>0 && a<min ? a : min;
- min = b>0 && b<min ? b : min;
- min = c>0 && c<min ? c : min;
- }
- // See Hacker's Delight, chapter 4.
- int smallest_v2(int a, int b, int c)
- {
- int min = INT_MAX;
- if ( (unsigned) a < min ) min = a;
- if ( (unsigned) b < min ) min = b;
- if ( (unsigned) c < min ) min = c;
- return min;
- }
- int main()
- {
- printf("min v1: %dn", smallest_v1(-10, 7, 3));
- printf("min v2: %dn", smallest_v1(-10, 7, 3));
- }
- 1 <= i <= 10
- (unsigned)(i - 1) <= 9
- [1,2,42,-12].delete_if{|n| n <= 0 }.min || 0
- Result := Min(IfThen(Value1 > 0, Value1, MAXINT),
- Min(IfThen(Value2 > 0, Value2, MAXINT),
- IfThen(Value3 > 0, Value3, MAXINT)));
- public int GetMinimumPositiveValue(IEnumerable<int> values)
- {
- int result = int.MaxValue;
- bool hasPositiveValue = false;
- foreach (int value in values)
- {
- if(value == 1) { return value; }
- if(value > 0)
- {
- hasPositiveValue = true;
- if(value < result)
- {
- result = value;
- }
- }
- }
- return hasPositiveValue ? result : 0;
- }
- //return the smallest non-zero positive number, or null.
- //relying on Min or Max is considered cheating.
- public static int? smallestNonZeroPositiveNumberOfThree(
- int val1, int val2, int val3)
- {
- //we have no guarantee that any of the 3 inputs will be positive
- int? result = null;
- if (val1 > 0)
- {
- result = val1;
- if (val2 > 0 && val2 < result) { result = val2; }
- if (val3 > 0 && val3 < result) { result = val3; }
- }
- else if (val2 > 0)
- {
- result = val2;
- if (val3 > 0 && val3 < result) { result = val3; }
- }
- else if (val3 > 0)
- {
- result = val3;
- }
- return result;
- }
- Result := 0;
- if value1 > 0 then
- Result := value1;
- if (value2 > 0) and ((Result = 0) or (value2 < Result)) then
- Result := value2;
- if (value3 > 0) and ((Result = 0) or (value3 < Result)) then
- Result := value3;
- values.Min(r => r > 0 ? r : (int?)null) ?? 0
- public int GetSmallestPositive(IList<int> pValues)
- {
- if(pValues == null)
- {
- throw new ArgumentNullException("pValues");
- }
- int _negNumCount = 0;
- int _smallest = int.MaxValue;
- for(int i = 0; i < pValues.Count; ++i)
- {
- if(pValues[i] < _smallest)
- {
- if(pValues[i] <= 0)
- {
- ++_negNumCount;
- continue;
- }
- _smallest = pValues[i];
- if(_smallest == 1)
- {
- return 1;
- }
- }
- }
- return (_negNumCount == pValues.Count) ? 0 : _smallest;
- }
- template <typename T>
- T min_pos(T* a, int n)
- {
- int i /* = 0 */ /* Edit: commented this out */;
- // Find the first positive element in the array
- for (i = 0; i < n; ++i)
- if (a[i] > 0)
- break;
- // If no positive element was found, return 0
- if (i == n)
- return 0;
- T res = a[i];
- // Search the rest of the array for an element
- // that is positive yet smaller than res
- for (++i; i < n; ++i)
- {
- if ((a[i] > 0) && (a[i] < res))
- res = a[i];
- }
- return res;
- }
- int foo(int value1, int value2, int value3)
- {
- int value1Temp, value2Temp, value3Temp, tempMax;
- value1Temp = max(value1, 0);
- value2Temp = max(value2, 0);
- value3Temp = max(value3, 0);
- tempMax = value1Temp | value2Temp | value3Temp;
- if (value1Temp == 0) { value1Temp = tempMax; }
- if (value2Temp == 0) { value2Temp = tempMax; }
- if (value3Temp == 0) { value3Temp = tempMax; }
- return min(value1Temp, min(value2Temp, value3Temp));
- }
- int min(int x, int y)
- {
- return y + ((x - y) & -(x < y));
- }
- int max(int x, int y)
- {
- return x - ((x - y) & -(x < y));
- }
- int foo(int value1, int value2, int value3)
- {
- int value1Temp, value2Temp, value3Temp, tempMax, mask;
- value1Temp = max(value1, 0);
- value2Temp = max(value2, 0);
- value3Temp = max(value3, 0);
- tempMax = value1Temp | value2Temp | value3Temp;
- mask = -(value1Temp > 0);
- value1Temp = (value1Temp & mask) | (tempMax & ~mask);
- mask = -(value2Temp > 0);
- value2Temp = (value2Temp & mask) | (tempMax & ~mask);
- mask = -(value3Temp > 0);
- value3Temp = (value3Temp & mask) | (tempMax & ~mask);
- return min(value1Temp, min(value2Temp, value3Temp));
- }
- foo xs = let xs1 = filter (>0) xs in if null xs1 then 0 else minimum xs1
- foo3 x1 x2 x3 = foo [x1, x2, x3]
- values.Min(r => r > 0 ? r : Int32.MaxValue)
Add Comment
Please, Sign In to add comment