2015-01-01から1年間の記事一覧

foreach ループをインデックス付きでまわす

using System; using System.Collections.Generic; using System.Linq; class Program { static IEnumerable<string> Three() { yield return "one"; yield return "two"; yield return "three"; } static void Main() { var xs = new[] { 4, 23, 9, -12 }; foreach </string>…

Scala の sliding メソッドを作成してみる

Scala には、sliding メソッドというのがあります。これを C# で実装してみました。 using System; using System.Collections.Generic; using System.Linq; static class Ext { public static IEnumerable<T[]> Sliding<T>(this IEnumerable<T> src, int size, int step</t></t></t[]>…

ネストしたループから抜ける

goto 文の練習。 using System; class Program { static void Main() { Console.WriteLine("BEGIN"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { Console.WriteLine("i:{0} j:{1}", i, j); if (j == 2) { goto END; } } } END: Console.…

UTC とローカル時刻を表示する

using System; class Program { static void Main() { var utc = DateTime.UtcNow; var now = DateTime.Now; Console.WriteLine("utc now : {0}", utc); Console.WriteLine("utc now -> local : {0}", utc.ToLocalTime()); Console.WriteLine(" now : {0}", …

何度も実行される箇所で Linq を使うことによるパフォーマンスの影響

Project Euler 23 を解いていて、Linq 使用の有無で速度差が出たのでメモ。 約数の和を求める SumProperDivisors メソッドで Linq を使う場合と、Linq を使わずに for 文で計算する場合とでどれくらい速度に差が出るか計測してみました。 using System; usin…

ファイル名から拡張子を取り除く

Path.GetFileNameWithoutExtension メソッドを使うと、パス名から拡張子を取り除いたファイル名を取得することが出来ます。 using System; using System.IO; class Program { static void Main() { var ss = new[] { "name.txt", "name", "foo/name.txt", "/…

Linq: Select と Take の組み合わせ

xs.Select(処理).Take(N) と xs.Take(N).Select(処理) に違いがあるか気になりました。 つまり、欲しい値は N 個だけなんだけど、Select(処理) は xs の全ての要素に対して適用されるのだろうか。 using System; using System.Linq; class Program { static …

Unicode メモ

using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { static string Hex<T>(T b) { return string.Format("{0:X}", b); } static void Main() { string s = "𪚲"; // Unicode 2A6B2(D869+DEB2) // UTF8 F0</t>…

文字列の連結の速度比較: string.Join vs StringBuilder

文字列の連結の速度比較をしてみました。 List<string> クラスに文字列を格納し、string.Join() で文字列を連結する。 StringBuilder クラスに文字列を格納し、ToString() メソッドで文字列を生成する。 上記 2 通りの方法で速度を計測してみました。 using System; </string>…

Mono で Rx をはじめてみる

Rx

hotmiyacchi.hatenablog.com 上のブログ記事で紹介されている UniRx をみて、コマンドラインから Reactive extensions の練習がしたくなりました。 Mono で Rx をはじめるまでのインストール手順を書きます。 環境 OS X El Capitan 10.11.1 $ mono --version…

List<int> を List<long> に変換する

List<int> を List<long> に変換するサンプルです。 using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<int> list = new List<int>() { 1, 4, 120, -55 }; List<long> xs = list.Select(e => (long)e).ToList(); Console.</long></int></int></long></int>…

インスタンスが同一か確認する

Object.ReferenceEquals メソッドでインスタンスが同一かどうか確認することができます。 public static bool ReferenceEquals( Object objA, Object objB ) サンプル using System; class C {} struct S {} class Program { static void Same(object a, obj…

BigInteger を int に変換する

BigInteger を int へ変換するサンプルです。 // biginteger-to-int.cs using System; using System.Numerics; class Program { static void Main() { var bigint = new BigInteger(1234567); int n = (int)bigint; Console.WriteLine(n); } } 実行結果です…

リストの反転。List.Reverse と Enumerable.Reverse を呼び分ける

リストを反転するときの List.Reverse と Enumerable.Reverse を呼び分けるサンプルです。 $ csharp Mono C# Shell, type "help;" for help Enter statements below. csharp> var xs = new List<int>() { 1, 2, 3, 4, 5 } csharp> xs.Reverse() // List.Reverse </int>…

Swap メソッド

値を Swap するメソッドです。 using System; class Program { static void Swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; } static void Main() { int a = 1; int b = 2; Console.WriteLine($"{a} {b}"); Swap(ref a, ref b); Console.WriteLine($"{a</t>…

Mono: コンパイラオプションでシンボルを定義する

mcs コマンドの -define オプションでシンボルを定義することが出来ます。 // define.cs using System; class Program { static void Main() { #if DEBUG Console.WriteLine("DEBUG シンボルは、定義されています"); #else Console.WriteLine("DEBUG シンボ…

文字列を置換する。置換回数を指定する

string クラスの Replace メソッドによる文字列の置換は、マッチする全ての文字列が置換されます。 $ csharp Mono C# Shell, type "help;" for help Enter statements below. csharp> var s = "abc abc abc"; csharp> s.Replace("abc", "def"); "def def def…

文字列の反転

文字列を反転させるプログラムです。 using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string s = "hello"; var t = s.Reverse(); // Iterator が返されます Console.WriteLine(t); string s2 = …

2次元配列の回転

2 次元配列を、時計回り、反時計回りに回転させるプログラムです。 実行例です。 時計回りに回転 0 1 2 3 4 5 3 0 4 1 5 2 5 4 3 2 1 0 2 5 1 4 0 3 反時計回りに回転 0 1 2 3 4 5 2 5 1 4 0 3 5 4 3 2 1 0 3 0 4 1 5 2 プログラム using System; using Syst…

Mono で BigInteger を使う

Mono で BigInteger を使う方法です。 以下のコードでは BigInteger を使っています。mcs コマンドでコンパイルすると、コンパイルエラーになります。 // sample.cs using System; class Program { public static void Main() { BigInteger x = 10; Console.…

長さ 1 の文字列の配列を文字の配列に変換する

文字列を要素とする配列があります。配列に格納されている文字列の長さは全て 1 とします。 そのときに、文字列の配列を、文字の配列に変換するプログラムです。 using System; using System.Collections.Generic; using System.Linq; class Program { stati…

Mono: checked キーワードを使わずに、オーバーフローチェックを有効にする

以下の記事で、Visual Studio では checked キーワードを使わなくても、ビルド設定でオーバーフローチェックができるとありました。 競技プログラミングのための C# (4.0 以降) の Tips 詰め合わせ - C♯の勉強 Mono でも checked キーワードを使わずにオー…

0..6 の範囲でぐるぐる回る

mod

0..6 の範囲でぐるぐる回るサンプルです。 以下の、上の段の数値を下の段の数値に変換するサンプルです。 -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ↓に変換する 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, …

Codeforces 1B - Spreadsheets

Problem - 1B - Codeforces using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; class Program { // A, B, C, ..., Y, Z, AA, AB, AC, ... を返す static IEnumerable<string> Seq() { // A = 1, B = 2, Y </string>…

Convert クラスで基数変換

Convert クラスを使用して基数変換を行うサンプルです。 using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { for (int i = 0; i < 256; i++) { var bin = Convert.ToString(i, 2); var oct = Conver…

Python の itertools.repeat を C# で作る。その 2

Python の itertools.repeat を C# で作る の、その 2 です。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<T> Repeat<T>(T obj) { for (;;) { yield return obj; } } public static IEnumerable<T> </t></t></t>…

Python の itertools.count を C# で作る

Python の itertools.count を C# で作ってみます。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<int> Count(int start, int step=1) { int x = start; while (true) { yield return x; x += st</int>…

Python の itertools.cycle を C# で作る

Python の itertools.cycle を C# で作ってみます。 using System; using System.Collections.Generic; using System.Linq; class Iter { public static IEnumerable<T> Cycle<T>(IEnumerable<T> seq) { for (;;) { bool empty = true; foreach (var elt in seq) { yi</t></t></t>…

AOJ で string.Join の第2引数に int[] を渡すとコンパイルエラーになる

AOJ

AOJ で C# コードのコンパイルエラーに関するメモです。 AOJ の mono のバージョンのためか、string.Join の第2に引数に int[] を渡すとコンパイルエラーになるようです。その回避方法です。 // OK static void Display1(int[] xs) { string[] ss = xs.Selec…

Python の itertools.repeat を C# で作る

Python の itertools.repeat を C# で作ってみます。 using System; using System.Collections.Generic; using System.Collections; using System.Linq; class Repeat<T> : IEnumerable<T> { private const int INFINITY = -1; // 無限のときの _times の値 privat</t></t>…