C#|どう書く?org あみだくじ3

 問題文に

>”このあみだくじをたどって”(中略)結果を表示させる

>プログラムを作ってください。

とあるのだから、ちゃんとあみだくじを辿るコードを書くべきだと気付きました。

//http://ja.doukaku.org/103/ 投稿用
using System;
using System.Collections.Generic;
class Program2 {
  static void Main(string[] args) {
    string amidaStr = @"A B C D E

-
- -
- -
- -

|-| | | |";
    List<char> amida = new List<char>();
    //あみだをPlayerが解釈できる形式に変換
    foreach(string line in amidaStr.Split(new char[] { '\n' })) {
      amida.Add(line.ToCharArray());
    }
    amida.Add(new char[amida[0].Length]);//Playlerが回答を記入する欄
    //Player生成
    for(int i = 0; i < amida[0].Length; i = i + 2) {
      new Player(i, amida);
    }
    //出力
    foreach(char[] line in amida) {
      Console.WriteLine(line);
    }
    Console.ReadLine();
  }
}
class Player {
  public Player(int start,List<char[]> amida) {
    Play(start, 1, amida, amida[0][start]);   
  }
  private void Play(int x,int y, List<char[]> amida, char name){
    if(amida[y][x] == '|') { //あみだが終わっていなければ
      if(x >= 2 && amida[y][x - 1] == '-') {
        Play(x - 2, y + 1, amida, name); //左に移動して進む
      } else if(x <= amida[0].Length - 3 && amida[y][x + 1] == '-') {
        Play(x + 2, y + 1, amida, name); //右に移動して進む
      } else {
        Play(x, y + 1, amida, name); //移動せずに下に進む
      }
    } else {
      amida[y][x] = name; //終わったら結果を記入
    }
  }
}

C#|どう書く?org あみだくじ2

 ざっくりと最適化しました。ちょっと自信あります。我ながらエレガントに書けたと思います。

//http://ja.doukaku.org/103/ 投稿用
using System;
using System.Collections.Generic;
class Program {
  static void Main(string[] args) {
    Amida(@"A B C D E
-
- -
- -
- -
|-| | | |");     Console.ReadLine();   }   static void Amida(string amidaText) {     List<string> temp = new List<string>(amidaText.Split(new char[] { '\n' }));     List<char> lastLine = new List<char>(temp[0].ToCharArray());     foreach(string line in temp) {       Console.WriteLine(line);       for(int i = 1; i < line.Length; i = i + 2) {         if(line[i] == '-'){           lastLine.Reverse(i - 1, 3);         }       }     }     Console.WriteLine(lastLine.ToArray());   } }

C#|どう書く?org あみだくじ

 どう書く?orgのお題、あみだくじへの投稿用に書きました。もっとエレガントに書ける気がしますが、これで行きます。

//http://ja.doukaku.org/103/ 投稿用
using System;
using System.Collections.Generic;
class Program {
  static void Main(string[] args) {
    Amida(@"A B C D E
-
- -
- -
- -
|-| | | |");     Console.ReadLine();   }   static void Amida(string amidaText) {     string amidaTemp = amidaText.Split(new char{'\n'});     List<char> amidaRows = new List<char>();     amidaRows.Add(amidaTemp[0].ToCharArray());     for(int i = 1; i < amidaTemp.Length; i++) {       amidaRows.Add(amidaTemp[i].ToCharArray());       for(int j = 0; j < amidaTemp[i].Length; j = j + 2) {         if(j > 0 && amidaTemp[i][j - 1] == '-') {           char tmp;           tmp = amidaRows[0][j];           amidaRows[0][j] = amidaRows[0][j - 2];           amidaRows[0][j - 2] = tmp;         }       }     }     amidaRows.Add(amidaRows[0]);     amidaRows.RemoveAt(0);     amidaRows.Insert(0, amidaTemp[0].ToCharArray());     foreach(char[] line in amidaRows) {       Console.WriteLine(line);     }   } }

青い道路は自転車専用、東京・世田谷でレーン実験

 たまには自転車ネタでも書こうと思います。

青い道路は自転車専用、東京・世田谷でレーン実験

車道に自転車レーンを設置する社会実験だそうです。これが定着すれば、日本での交通手段としての自転車の可能性はぐんと広がるでしょう。

 ただ、レーンの幅が45センチというのは狭いです。歩道を走ることが出来る自転車の基準のひとつである普通自転車の車幅は、60センチ以下です。45センチだと大半の自転車はハミ出ます。きっと頭が弱い人が決めたのでしょう。普段から全く自転車に乗らない人でも簡単に分かることだと思います。

 しかもこの自転車レーンが、道交法で言う自転車道と解釈されれば、幅45センチ以上60センチ以下の自転車は走るところが無くなります。

 どちらにしろ、いくら道路に小細工しても人が変わらなきゃ意味がありません。一番大切で効果的なのは教育だと思うのですが、行政にその気は無いようです。

C#|どう書く?org正整数のゲーデル数化?

 どう書く?orgのお題正整数のゲーデル数化?への投稿用に書きました。

 既に投稿されているSiroKuroさんのコードのc – ‘0’ってところに萌えつつ、こういうときにIEnumeratorを使うと効率がいいのね~と勉強になりつつ、こんな感じで書いてみました。

 分かりやすく書いたつもりがかえって意図が伝わりにくくなってる気がします。

 参考としてリンクされいるWikiを見ても、ゲーデル数というものがが何なのかさっぱり理解出来ませんが、お題に示されているように書いてみました。

//http://ja.doukaku.org/100/ 投稿用
using System;
using System.Collections.Generic;
class Program {
  static void Main(string[] args) {
    Console.WriteLine(goedel(9));
    Console.WriteLine(goedel(81));
    Console.WriteLine(goedel(230));
    Console.ReadLine();
  }
  static double goedel(int n) {
    string tmpStr = n.ToString();
    double r = 1;
    List<double> prime = new List<double>(new double[] { 2 });
    for(int i = 3; ; i += 2) {
      bool isPrime = true;
      for(int j = 3; j < i; j++) {
        if(i % j == 0) {
          isPrime = false;
          break;
        }
      }
      if(isPrime) prime.Add(i);
      if(prime.Count >= tmpStr.Length) break;
    }
    for(int i = 0; i < tmpStr.Length; i++) {
      r *= Math.Pow(prime[i], (double.Parse(tmpStr[i].ToString())));
    }
    return r;
  }
}

C#|TextBoxにもマウスジェスチャー機能を簡単に追加

 C#|WebBrowserにマウスジェスチャー機能を簡単に追加のTextBox版を書きました。コンテキストメニューの表示設定の切り替えのタイミング以外は元のコードと殆ど同じです。このコンテキストメニューの表示設定の切り替えタイミングの試行錯誤に作業時間の殆どを費やしました。

 これをしないと、マウスジェスチャが終わった後にメニューが開いちゃうんです。で、開かないようにしたら、二度と開かなかったりします。この微妙なタイミングが難しかったです。

 なんでTextBox用にも書いたかと言うと、VB.NETで書いた電八用のビューアにマウスジェスチャ機能を付けたかったからです。↓で閉じる、これだけの為にです。

 ここでVisualStudioだとVBのプロジェクトを参照設定してスタートアッププロジェクトに設定すれば楽なのでしょうが、僕が使っているのはC#、VBそれぞれのExpress Editionです。複数の言語を同時には扱えないので、C#でビルド→VBでテストという繰り返しでした。VSが欲しいです。MSはアカデミックパックに加えてNEETパックとかフリーターパックでも販売するべきです。

 C#とVB.NETを交互に書くのは不思議な感じでした。

C#|どう書く?org 文字列の八方向検索2

 他の皆のコードよりかなり長かったので、短くしてみました。半分になりました。処理時間は倍になった気がします。

 他の皆の他言語のコードも読めれば参考に出来るのですが、難しいです。

//http://ja.doukaku.org/
//http://ja.doukaku.org/99/投稿用
using System;
using System.Collections.Generic;
namespace どう書く_org文字列の八方向検索 {
  class Program {
    static string search;
    static List<string> list;
    static void Main(string[] args) {
      string sample = "リオウウリウ\nウオリウオリ\nオリリオリウ\nリリオオウオ";
      search = "ウオリ";
      char sp = new char { '\n' };
      int width = sample.Split(sp)[0].Length;
      int height = sample.Split(sp).Length;
      list = new List<string>(sample.Split(sp));
      for(int y = 0; y < list.Count; y++) { //縦ループ
        for(int x = 0; x < list[y].Length; x++) { //横ループ
          for(int dx = -1; dx <= 1; dx++) { //左右方向ループ
            for(int dy = -1; dy <= 1; dy++) { //上下方向ループ -1は上、左 1は下、右を表す
              try {
                string strb = "";
                for(int i = 0; i < search.Length; i++) { //iは移動量
                  strb += list[y + i * dy][x + i * dx]; //移動方向をdy,dxで乗算することで反転
                }
                if(search == strb) {
                  string directionStr = "";
                  if(dx < 0) {
                    directionStr += "左";
                  } else if(dx > 0) {
                    directionStr += "右";
                  }
                  if(dy < 0) {
                    directionStr += "上";
                  } else if(dy > 0) {
                    directionStr += "下";
                  }
                  Console.WriteLine("(" + x + "," + y + ")" + "," + directionStr);
                }
              } catch(ArgumentOutOfRangeException) { } catch(IndexOutOfRangeException) { }
            }
          }
        }
      }
      Console.ReadLine();
    }
  }
}

C#|論理エラーでロンリー

 お手製アドレス帳のCSVインポート機能のコードを書いていたのですが、

if(hoge.Text.Contains(“ZONU”)){

  hoge.Text = hoge.Text.Replace(“ZONU”, “DQN”);

  huga.Test = hoge.Text;

}

みたいなコードを書いて3行目のコードは動いて、2行目では置換されないってどういうことですか…?二日三日悩んでどうにもならなくてどうでも良くなりました。

 Vectorでも漁ってればそのうち良いアドレス帳が見付かるでしょう。

C#|どう書く?org 文字列の八方向検索

 どう書く?orgのお題、文字列の八方向検索投稿用に書きました。

using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;
namespace どう書く_org文字列の八方向検索 {
  class Program {
    static string search;
    static List<string> list;
    static void Main(string[] args) {
      string sample =
@"リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ";
      search = "ウオリ";
      char sp = new char { '\n' };
      int width = sample.Split(sp)[0].Length;
      int height = sample.Split(sp).Length;
      list = new List<string>(sample.Split(sp));
      for(int y = 0; y < list.Count; y++) {
        for(int x = 0; x < list[y].Length; x++) {
          if(list[y][x] == search[0]) {
            if(E(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右");
            }
            if(W(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左");
            }
            if(S(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "下");
            }
            if(N(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "上");
            }
            if(NE(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右上");
            }
            if(SE(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右下");
            }
            if(NW(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左上");
            }
            if(SW(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左下");
            }
          }
        }
      }
      Console.ReadLine();
    }
    static string E(int x, int y) {
      try {
        return list[y].Substring(x, search.Length);
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string W(int x, int y) {
      try {
        return Strings.StrReverse(E(x - search.Length + 1, y));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string S(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y + i][x]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string N(int x, int y) {
      try {
        return Strings.StrReverse(S(x, y - search.Length + 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string SE(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y + i][x + i]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string NW(int x, int y) {
      try {
        return Strings.StrReverse(SE(x - search.Length + 1, y - search.Length + 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string NE(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y - i][x + i]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string SW(int x, int y) {
      try {
        return Strings.StrReverse(NE(x - search.Length + 1, y + search.Length - 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
  }
}