[Assorted-commits] SF.net SVN: assorted: [246] problems/topcoder
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-01-20 00:23:49
|
Revision: 246 http://assorted.svn.sourceforge.net/assorted/?rev=246&view=rev Author: yangzhang Date: 2008-01-19 16:23:46 -0800 (Sat, 19 Jan 2008) Log Message: ----------- left out some old files on laptop Added Paths: ----------- problems/topcoder/BinaryCode/BinaryCode.cs problems/topcoder/Lottery/Lottery.cs problems/topcoder/PenLift/PenLift.jnt Added: problems/topcoder/BinaryCode/BinaryCode.cs =================================================================== --- problems/topcoder/BinaryCode/BinaryCode.cs (rev 0) +++ problems/topcoder/BinaryCode/BinaryCode.cs 2008-01-20 00:23:46 UTC (rev 246) @@ -0,0 +1,129 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +public class BinaryCode { + public string[] decode(string message) { + //List<int> xs = new List<int>(); + //foreach(char c in message) xs.Add(c - '0'); + //List<string> ss = new List<string>(); + //for (int i = 0; i < 2; i++) { + // List<int> ys = new List<int>(); + // ys.Add(i); + // for (int j = 1; j < xs.Count; j++) + // ys.Add(xs[j - 1] - ys[j - 1] - (j > 1 ? ys[j - 2] : 0)); + // string s = ""; + // foreach (int j in ys) s += j; + // bool bad = false; + // foreach (int j in ys) if (j != 0 && j != 1) bad = true; + // { + // int j = xs.Count; + // if (xs[j - 1] - ys[j - 1] - (j > 1 ? ys[j - 2] : 0) != 0) bad = true; + // } + // if (bad) s = "NONE"; + // ss.Add(s); + //} + //return ss.ToArray(); + + string m = message; + string[] ss = new string[2]; + for (int i = 0; i < 2; i++) { + string s = "" + i; + for (int j = 1; j < m.Length; j++) + s += (m[j - 1] - s[j - 1] - (j > 1 ? s[j - 2] : '0') + '0'); + bool bad = false; + for (int j = 0; j < m.Length; j++) + if (s[j] != '0' && s[j] != '1') bad = true; + if (s[s.Length - 1] + (s.Length > 1 ? s[s.Length - 2] : '0') != '0' + m[m.Length - 1]) bad = true; + if (bad) s = "NONE"; + ss[i] = s; + } + return ss; + } + + + + + + + public void pp<T>(T x) { Console.WriteLine(x); } + + + + + + + + + // BEGIN CUT HERE + public static void Main(string[] args) { + eq(0,(new BinaryCode()).decode("123210122"),new string[] { "011100011", "NONE" }); + eq(1,(new BinaryCode()).decode("11"),new string[] { "01", "10" }); + eq(2,(new BinaryCode()).decode("22111"),new string[] { "NONE", "11001" }); + eq(3,(new BinaryCode()).decode("123210120"),new string[] { "NONE", "NONE" }); + eq(4,(new BinaryCode()).decode("3"),new string[] { "NONE", "NONE" }); + eq(5,(new BinaryCode()).decode("12221112222221112221111111112221111"),new string[] { "01101001101101001101001001001101001", "10110010110110010110010010010110010" }); + Console.WriteLine("done!"); + Console.Read(); + } + private static void eq( int n, object have, object need) { + if( eq( have, need ) ) { + Console.WriteLine( "Case "+n+" passed." ); + } else { + Console.Write( "Case "+n+" failed: expected " ); + print( need ); + Console.Write( ", received " ); + print( have ); + Console.WriteLine(); + } + } + private static void eq( int n, Array have, Array need) { + if( have == null || have.Length != need.Length ) { + Console.WriteLine("Case "+n+" failed: returned "+have.Length+" elements; expected "+need.Length+" elements."); + print( have ); + print( need ); + return; + } + for( int i= 0; i < have.Length; i++ ) { + if( ! eq( have.GetValue(i), need.GetValue(i) ) ) { + Console.WriteLine( "Case "+n+" failed. Expected and returned array differ in position "+i ); + print( have ); + print( need ); + return; + } + } + Console.WriteLine("Case "+n+" passed."); + } + private static bool eq( object a, object b ) { + if ( a is double && b is double ) { + return Math.Abs((double)a-(double)b) < 1E-9; + } else { + return a!=null && b!=null && a.Equals(b); + } + } + private static void print( object a ) { + if ( a is string ) { + Console.Write("\"{0}\"", a); + } else if ( a is long ) { + Console.Write("{0}L", a); + } else { + Console.Write(a); + } + } + private static void print( Array a ) { + if ( a == null) { + Console.WriteLine("<NULL>"); + } + Console.Write('{'); + for ( int i= 0; i < a.Length; i++ ) { + print( a.GetValue(i) ); + if( i != a.Length-1 ) { + Console.Write(", "); + } + } + Console.WriteLine( '}' ); + } + // END CUT HERE +} Added: problems/topcoder/Lottery/Lottery.cs =================================================================== --- problems/topcoder/Lottery/Lottery.cs (rev 0) +++ problems/topcoder/Lottery/Lottery.cs 2008-01-20 00:23:46 UTC (rev 246) @@ -0,0 +1,131 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +using P = System.Collections.Generic.KeyValuePair<double, string>; + +public class Lottery { + public string[] sortByOdds(string[] rules) { + List<P> list = new List<P>(); + foreach(string rule in rules) { + string[] parts = rule.Split(':'); + string name = parts[0]; + parts = parts[1].Trim().Split(); + int choi = int.Parse(parts[0]); + int blan = int.Parse(parts[1]); + bool sort = (parts[2] == "T"); + bool uniq = (parts[3] == "T"); + double z = 999; + if (!sort && !uniq) z = Math.Pow(choi, blan); + if (!sort && uniq) z = fact(choi) / fact(choi - blan); + if (sort && !uniq) z = fact(choi+blan-1) / fact(choi-1) / fact(blan); + if (sort && uniq) z = fact(choi) / fact(choi - blan) / fact(blan); + //pp(name + " " + choi + " " + blan + " " + " " + sort + " " + uniq + " " + z); + list.Add(new P(z, name)); + } + list.Sort(delegate(P x, P y) { return x.Key.CompareTo(y.Key) != 0 ? x.Key.CompareTo(y.Key) : x.Value.CompareTo(y.Value); }); + List<String> ys = new List<String>(); + foreach (P p in list) ys.Add(p.Value); + return ys.ToArray(); + } + double fact(int x) { + double y = 1; + for (int i = 0; i < x; i++) { + y *= i+1; + } + return y; + } + + + + + + + public void pp<T>(T x) { Console.WriteLine(x); } + + + + + + + + + // BEGIN CUT HERE + public static void Main(string[] args) { + eq(0,(new Lottery()).sortByOdds(new string[] {"PICK ANY TWO: 10 2 F F" + ,"PICK TWO IN ORDER: 10 2 T F" + ,"PICK TWO DIFFERENT: 10 2 F T" + ,"PICK TWO LIMITED: 10 2 T T"}),new string[] { "PICK TWO LIMITED", "PICK TWO IN ORDER", "PICK TWO DIFFERENT", "PICK ANY TWO" }); + eq(1,(new Lottery()).sortByOdds(new string[] {"INDIGO: 93 8 T F", + "ORANGE: 29 8 F T", + "VIOLET: 76 6 F F", + "BLUE: 100 8 T T", + "RED: 99 8 T T", + "GREEN: 78 6 F T", + "YELLOW: 75 6 F F"} + ),new string[] { "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET" }); + eq(2,(new Lottery()).sortByOdds(new string[] {}),new string[] { }); + Console.WriteLine("done!"); + Console.Read(); + } + private static void eq( int n, object have, object need) { + if( eq( have, need ) ) { + Console.WriteLine( "Case "+n+" passed." ); + } else { + Console.Write( "Case "+n+" failed: expected " ); + print( need ); + Console.Write( ", received " ); + print( have ); + Console.WriteLine(); + } + } + private static void eq( int n, Array have, Array need) { + if( have == null || have.Length != need.Length ) { + Console.WriteLine("Case "+n+" failed: returned "+have.Length+" elements; expected "+need.Length+" elements."); + print( have ); + print( need ); + return; + } + for( int i= 0; i < have.Length; i++ ) { + if( ! eq( have.GetValue(i), need.GetValue(i) ) ) { + Console.WriteLine( "Case "+n+" failed. Expected and returned array differ in position "+i ); + print( have ); + print( need ); + return; + } + } + Console.WriteLine("Case "+n+" passed."); + } + private static bool eq( object a, object b ) { + if ( a is double && b is double ) { + return Math.Abs((double)a-(double)b) < 1E-9; + } else { + return a!=null && b!=null && a.Equals(b); + } + } + private static void print( object a ) { + if ( a is string ) { + Console.Write("\"{0}\"", a); + } else if ( a is long ) { + Console.Write("{0}L", a); + } else { + Console.Write(a); + } + } + private static void print( Array a ) { + if ( a == null) { + Console.WriteLine("<NULL>"); + } + Console.Write('{'); + for ( int i= 0; i < a.Length; i++ ) { + print( a.GetValue(i) ); + if( i != a.Length-1 ) { + Console.Write(", "); + } + } + Console.WriteLine( '}' ); + } + // END CUT HERE +} Added: problems/topcoder/PenLift/PenLift.jnt =================================================================== (Binary files differ) Property changes on: problems/topcoder/PenLift/PenLift.jnt ___________________________________________________________________ Name: svn:mime-type + application/octet-stream This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |