code_junkie

Find the Minimum Positive Value

Nov 13th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. value1Temp := MaxInt;
  2. value2Temp := MaxInt;
  3. value3Temp := MaxInt;
  4.  
  5. if ( value1T > 0) then
  6. value1Temp := value1;
  7. if ( value2 > 0) then
  8. value2Temp := value2;
  9. if ( value3 > 0) then
  10. value3Temp := value3;
  11.  
  12. Result := Min(value1Temp, Min(value2Temp, value3Temp));
  13. if Result = MaxInt then
  14. Result := 0;
  15.  
  16. function LowestPositiveInt(IntArr : array of Integer):Integer;
  17. var
  18. iX : integer;
  19. bValid : boolean;
  20. begin
  21. Result := MaxInt;
  22. bValid := False;
  23. for ix := 0 to High(IntArr) do
  24. if (IntArr[ix] > 0) then
  25. begin
  26. bValid := true;
  27. if (IntArr[iX] < Result) then
  28. Result := IntArr[ix];
  29. end;
  30. if not bValid then
  31. Result := 0;
  32. end;
  33.  
  34. ShowMessage(IntToStr( LowestPositiveInt([5,2,3,-1,12]) ));
  35.  
  36. Result := LowestPositiveInt( [ Value1, Value2, Value3 ] );
  37.  
  38. int maxPositiveValue(int *number, int listSize)
  39. {
  40. int i, result = 0;
  41.  
  42. for(i = 0; i < listSize; i++)
  43. {
  44. if(number[i] > 0 && (number[i] < result || result == 0))
  45. result = number[i];
  46. }
  47.  
  48. return result;
  49. }
  50.  
  51. function cmMinInt( XX, YY, ZZ : longint ) : longint;
  52. begin
  53. result := max(0,longint(
  54. min(longint((XX-1) xor $80000000),
  55. min(longint((YY-1) xor $80000000),
  56. longint((ZZ-1) xor $80000000)
  57. )) xor $80000000)+1);
  58. end;
  59.  
  60. function cmMinInt( XX, YY, ZZ : longint ) : longint;
  61. begin
  62. // swap ordinal coding for range MININT..0 with range 1..MAXINT
  63. XX := XX-1; // move region division to between 0 and 1
  64. XX := XX xor $80000000; // swap regions, preserving ordering
  65. XX := longint(XX); // cram back into signed 32-bit
  66. // similarly with YY and ZZ
  67. YY := longint((YY-1) xor $80000000);
  68. ZZ := longint((ZZ-1) xor $80000000);
  69. // find min of three recoded values
  70. result := min(XX,min(YY,ZZ));
  71. // swap ordering back again
  72. result := result xor $80000000; // swap regions, preserving ordering
  73. result := result+1; // move region division back home
  74. result := longint(result); // cram back into signed 32-bit
  75. // if all three were neg, result at this point is neg -- clip to zero
  76. result := max(0,result);
  77. end;
  78.  
  79. #include <stdio.h>
  80. #include <limits.h>
  81.  
  82. int smallest_v1(int a, int b, int c)
  83. {
  84. int min = INT_MAX;
  85.  
  86. min = a>0 && a<min ? a : min;
  87. min = b>0 && b<min ? b : min;
  88. min = c>0 && c<min ? c : min;
  89. }
  90.  
  91. // See Hacker's Delight, chapter 4.
  92. int smallest_v2(int a, int b, int c)
  93. {
  94. int min = INT_MAX;
  95.  
  96. if ( (unsigned) a < min ) min = a;
  97. if ( (unsigned) b < min ) min = b;
  98. if ( (unsigned) c < min ) min = c;
  99.  
  100. return min;
  101. }
  102.  
  103. int main()
  104. {
  105. printf("min v1: %dn", smallest_v1(-10, 7, 3));
  106. printf("min v2: %dn", smallest_v1(-10, 7, 3));
  107. }
  108.  
  109. 1 <= i <= 10
  110.  
  111. (unsigned)(i - 1) <= 9
  112.  
  113. [1,2,42,-12].delete_if{|n| n <= 0 }.min || 0
  114.  
  115. Result := Min(IfThen(Value1 > 0, Value1, MAXINT),
  116. Min(IfThen(Value2 > 0, Value2, MAXINT),
  117. IfThen(Value3 > 0, Value3, MAXINT)));
  118.  
  119. public int GetMinimumPositiveValue(IEnumerable<int> values)
  120. {
  121. int result = int.MaxValue;
  122. bool hasPositiveValue = false;
  123.  
  124. foreach (int value in values)
  125. {
  126. if(value == 1) { return value; }
  127.  
  128. if(value > 0)
  129. {
  130. hasPositiveValue = true;
  131.  
  132. if(value < result)
  133. {
  134. result = value;
  135. }
  136. }
  137. }
  138.  
  139. return hasPositiveValue ? result : 0;
  140. }
  141.  
  142. //return the smallest non-zero positive number, or null.
  143. //relying on Min or Max is considered cheating.
  144. public static int? smallestNonZeroPositiveNumberOfThree(
  145. int val1, int val2, int val3)
  146. {
  147. //we have no guarantee that any of the 3 inputs will be positive
  148. int? result = null;
  149. if (val1 > 0)
  150. {
  151. result = val1;
  152. if (val2 > 0 && val2 < result) { result = val2; }
  153. if (val3 > 0 && val3 < result) { result = val3; }
  154. }
  155. else if (val2 > 0)
  156. {
  157. result = val2;
  158. if (val3 > 0 && val3 < result) { result = val3; }
  159. }
  160. else if (val3 > 0)
  161. {
  162. result = val3;
  163. }
  164. return result;
  165. }
  166.  
  167. Result := 0;
  168. if value1 > 0 then
  169. Result := value1;
  170. if (value2 > 0) and ((Result = 0) or (value2 < Result)) then
  171. Result := value2;
  172. if (value3 > 0) and ((Result = 0) or (value3 < Result)) then
  173. Result := value3;
  174.  
  175. values.Min(r => r > 0 ? r : (int?)null) ?? 0
  176.  
  177. public int GetSmallestPositive(IList<int> pValues)
  178. {
  179. if(pValues == null)
  180. {
  181. throw new ArgumentNullException("pValues");
  182. }
  183. int _negNumCount = 0;
  184. int _smallest = int.MaxValue;
  185. for(int i = 0; i < pValues.Count; ++i)
  186. {
  187. if(pValues[i] < _smallest)
  188. {
  189. if(pValues[i] <= 0)
  190. {
  191. ++_negNumCount;
  192. continue;
  193. }
  194. _smallest = pValues[i];
  195. if(_smallest == 1)
  196. {
  197. return 1;
  198. }
  199. }
  200. }
  201. return (_negNumCount == pValues.Count) ? 0 : _smallest;
  202. }
  203.  
  204. template <typename T>
  205. T min_pos(T* a, int n)
  206. {
  207. int i /* = 0 */ /* Edit: commented this out */;
  208.  
  209. // Find the first positive element in the array
  210. for (i = 0; i < n; ++i)
  211. if (a[i] > 0)
  212. break;
  213.  
  214. // If no positive element was found, return 0
  215. if (i == n)
  216. return 0;
  217.  
  218. T res = a[i];
  219.  
  220. // Search the rest of the array for an element
  221. // that is positive yet smaller than res
  222. for (++i; i < n; ++i)
  223. {
  224. if ((a[i] > 0) && (a[i] < res))
  225. res = a[i];
  226. }
  227.  
  228. return res;
  229. }
  230.  
  231. int foo(int value1, int value2, int value3)
  232. {
  233. int value1Temp, value2Temp, value3Temp, tempMax;
  234. value1Temp = max(value1, 0);
  235. value2Temp = max(value2, 0);
  236. value3Temp = max(value3, 0);
  237. tempMax = value1Temp | value2Temp | value3Temp;
  238. if (value1Temp == 0) { value1Temp = tempMax; }
  239. if (value2Temp == 0) { value2Temp = tempMax; }
  240. if (value3Temp == 0) { value3Temp = tempMax; }
  241. return min(value1Temp, min(value2Temp, value3Temp));
  242. }
  243.  
  244. int min(int x, int y)
  245. {
  246. return y + ((x - y) & -(x < y));
  247. }
  248.  
  249. int max(int x, int y)
  250. {
  251. return x - ((x - y) & -(x < y));
  252. }
  253.  
  254. int foo(int value1, int value2, int value3)
  255. {
  256. int value1Temp, value2Temp, value3Temp, tempMax, mask;
  257. value1Temp = max(value1, 0);
  258. value2Temp = max(value2, 0);
  259. value3Temp = max(value3, 0);
  260. tempMax = value1Temp | value2Temp | value3Temp;
  261. mask = -(value1Temp > 0);
  262. value1Temp = (value1Temp & mask) | (tempMax & ~mask);
  263. mask = -(value2Temp > 0);
  264. value2Temp = (value2Temp & mask) | (tempMax & ~mask);
  265. mask = -(value3Temp > 0);
  266. value3Temp = (value3Temp & mask) | (tempMax & ~mask);
  267. return min(value1Temp, min(value2Temp, value3Temp));
  268. }
  269.  
  270. foo xs = let xs1 = filter (>0) xs in if null xs1 then 0 else minimum xs1
  271.  
  272. foo3 x1 x2 x3 = foo [x1, x2, x3]
  273.  
  274. values.Min(r => r > 0 ? r : Int32.MaxValue)
Add Comment
Please, Sign In to add comment