challenge 仲間はずれの判定

リストxsが渡されたときに
  • 全部の要素が同じ値である(例:[1, 1, 1, 1])、
  • 一つだけ仲間はずれがある(例:[1, 2, 1, 1, 1])、
  • その他
を識別する関数を作ってください。 また判定後に「全部の要素が同じ値」の場合にはその値、 「一つだけ仲間はずれがある」の場合にはその仲間はずれの値と多数派の値を複雑な処理なしに取得できるようにしてください。

型にうるさい言語のために:リストの中身は非負の整数だと仮定して負の値を未定義値代わりに使っても構いません。

追記:リストの長さは3以上であると仮定して構いません。(2個で異なる値の時にどちらが仲間はずれか決まらないので。) nobsunさん、noriさん、ご指摘ありがとうございます。

もっとスリム化できそうですがとりあえず。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
public class NakamaHazure {
    public static void main(String[] args) {
        check(Arrays.asList(1,1,1,1));    // => [1]
        check(Arrays.asList(1,2,1,1,1));    // => [2,1]
        check(Arrays.asList(1,2,2,1,1));    // => []
        check(Arrays.asList(1,2,2,1,3));    // => []
    }
    private static void check(List<Integer> list) {
        HashSet<Integer> set = new HashSet<Integer>();
        HashSet<Integer> minor = new HashSet<Integer>();;
        Integer major = null;
        for (Integer i : list) {
            if (!set.add(i)) {
                if (major != null && major != i) {
                    System.out.println("[]");
                    return;
                }
                minor.remove(i);
                major = i;
            } else {
                minor.add(i);
            }
        }
        if (set.size() == 1) {
            System.out.println("[" + set.iterator().next() + "]");
        } else if (set.size() == 2) {
            System.out.println("[" + minor.iterator().next() + "," + major + "]");
        } else {
            System.out.println("[]");
        }
    }
}

array_count_values便利ですね。あえて使わないで作ってみる事で便利さを際実感

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$a[]= array(1,1,1,1);#1
$a[]= array(1,2,1,1,1);#2
$a[]= array(2,1,2,2);#その他
$a[]= array(1,2,2,2);#その他
$a[]= array(1,1,2,2);#その他
$a[]= array(1,1,2,3);#その他
function check($_array){
    $uniques = sizeof(array_unique($_array));
    switch($uniques){
        case(1):
            return array("仲間"=>array_pop($_array));
        case(2):
            sort($_array);
            #仲間はずれ
            if( $_array[0]!=$_array[1] ){
                return array( "仲間はずれ"=>array_shift($_array),"仲間"=>array_pop($_array) );
            }else if($_array[0]==$_array[1] && $_array[sizeof($_array)-1] != $_array[sizeof($_array)-2] ){
                return array( "仲間はずれ"=>array_pop($_array),"仲間"=>array_shift($_array) );
            }
        default:
            return "その他";
    }


}

foreach( $a as $b ){
    print_r( check($b) ).PHP_EOL;
}

Posted feedbacks - C#

入力は配列、出力は長さが2以下の配列。出力の先頭が多数派、2番目が仲間はずれ。
if文の羅列なので美しくない。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public T[] Classify<T>(T[] array) where T : struct
{
    const int MAJORITY = 0;
    const int MINORITY = 1;
    T?[] values = new T?[2];
    int[] counters = new int[2];
    foreach (T value in array)
    {
        if (values[MAJORITY] == null)
        {
            values[MAJORITY] = value;
            counters[MAJORITY]++;
        }
        else if (values[MAJORITY].Value.Equals(value))
        {
            counters[MAJORITY]++;
        }
        else if (values[MINORITY] == null)
        {
            values[MINORITY] = value;
            counters[MINORITY]++;
        }
        else if (values[MINORITY].Value.Equals(value) && counters[MAJORITY] == counters[MINORITY])
        {
            values[MINORITY] = values[MAJORITY];
            values[MAJORITY] = value;
            counters[MAJORITY]++;
        }
        else
        {
            return new T[0];
        }
    }
    T[] result = new T[values[MINORITY] == null ? 1 : 2];
    result[MAJORITY] = values[MAJORITY].Value;
    if (values[MINORITY] != null)
        result[MINORITY] = values[MINORITY].Value;
    return result;
}

入力は配列、出力は文字列
判定部分が思ったより短くならなかった
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
               public string Classify(int[] ary)
               {
                       System.Collections.Hashtable ht= new System.Collections.Hashtable();
                       for(int i= 0; i< ary.Length; i++)
                               ht[ary[i]]= (ht[ary[i]] == null? 1 :(int)ht[ary[i]]+1);

                       int[] keys= new int[ht.Keys.Count];
                       int[] vals= new int[ht.Values.Count];
                       ht.Keys.CopyTo(keys, 0);
                       ht.Values.CopyTo(vals, 0);
                       Array.Sort(vals,keys);
                       switch(ht.Keys.Count)
                       {
                               case 1:
                                       return "みんな同じ :"+keys[0];
                               case 2:
                                       return "仲間はずれ Major:"+keys[1]+" Minor:"+ keys[0];
                               default:
                                       return "その他";
                       }
               }

LINQ版
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
static void NakamaHazure(params int[] a)
{
    var x = from y in a
            group y by y into z
            orderby z.Count() ascending
            select new { count = z.Count(), value = z.Key };

    if (a.Length == x.ElementAt(0).count)
    {
        Console.WriteLine(x.ElementAt(0).value );
    }
    else if (x.Count() == 2 && x.ElementAt(0).count == 1 )
    {
        Console.WriteLine(x.ElementAt(0).value + "," + x.ElementAt(1).value);
    }
}

Index

Feed

Other

Link

Pathtraq

loading...