csharp basic exercises: the closest zero [difficulty: Level 1] - view the C # programming classic exam, exercises of varying difficulty C #, C # novice suitable for self-study of advanced training

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45444821/article/details/102716716

csharp basic exercises: the closest zero [difficulty: Level 1]:

Just find the closest value to zero from the list. Please note that there is a list of negatives.

The list is not always empty and contain only integers. Return

If it can not only define such a value. Of course, we look forward to 0 as the closest value to zero.

example:

代码
[2,4,-1,-3] => -1
[5,2,-2] =>无
[5,2,2] => 2
[13,0,-6] => 0

Programming objectives:

public class Kata
{
  public static int? Closest(int[] arr)
  {
    // return null if it is not possible to return 1 unique closest value
    return 0;
  }
}


Test sample:

namespace Solution {
  using NUnit.Framework;
  using System;
  using System.Collections.Generic;
  [TestFixture]
  public class SolutionTest
  {
      get
      {
        yield return new TestCaseData(new int[] {10, 3, 9, 1}).Returns(1).SetDescription("Simple test");
        yield return new TestCaseData(new int[] {2, 4, -1, -3}).Returns(-1).SetDescription("Simple test");
        yield return new TestCaseData(new int[] {13, 0, -6}).Returns(0).SetDescription("Simple test");
        yield return new TestCaseData(new int[] {13, 0, 0, -6}).Returns(0).SetDescription("Simple test");
      }


Best answer (multiple solutions):

Click to view answer

More related topics:

csharp basic exercises: Counting like a child [of difficulty: Level 1] - view the C # programming classic exam, exercises of varying difficulty C #, C # novice suitable for self-study of advanced training

Disclaimer

This blog All programming topics and answers were collected from the Internet, users are mainly used for study and reference, if any violation of your rights, please contact the administrator to delete, thank
entitled to collect https://www.codewars.com/
HTTPS: / /www.codewars.com/kata/closest-to-zero

Guess you like

Origin blog.csdn.net/weixin_45444821/article/details/102716716