Guest User

Bad Code Coding Challenge #40 (C# .NET Core 3.1)

a guest
Jul 6th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. /*
  5. |-----------------------|
  6. |   1   |   2   |   3   |
  7. |  .,?! |  abc  |  def  |
  8. |-----------------------|
  9. |   4   |   5   |   6   |
  10. |  ghi  |  jkl  |  mno  |
  11. |-----------------------|
  12. |   7   |   8   |   9   |
  13. |  pqrs |  tuv  |  wxyz |
  14. |-----------------------|
  15. |   *   |   0   |   #   |
  16. |  '-+= |   ⎵  |  case |
  17. |-----------------------|
  18. */
  19.  
  20. public class Program
  21. {
  22.     public static string strjoin( char[] x )
  23.     {
  24.         string j = "";
  25.        
  26.         foreach ( var o in x )
  27.         {
  28.             j += o.ToString();
  29.         }
  30.        
  31.         return j + '\0';
  32.     }
  33.    
  34.     public static int is_all_caps( string x )
  35.     {
  36.         var upper = strjoin(
  37.             ( from z in x.Trim()
  38.               where Char.IsUpper( z )
  39.               select z )
  40.             .ToList()
  41.             .ToArray() );
  42.        
  43.         return upper.Length ^ x.Length;
  44.     }
  45.    
  46.     public static string to_t9( string val )
  47.     {
  48.         var asdf = new( string, string )[]
  49.                    { ( "1", "./,/?/!" ),
  50.                      ( "2", "a/b/c"   ),
  51.                      ( "3", "d/e/f"   ),
  52.                      ( "4", "g/h/i"   ),
  53.                      ( "5", "j/k/l"   ),
  54.                      ( "6", "m/n/o"   ),
  55.                      ( "7", "p/q/r/s" ),
  56.                      ( "8", "t/u/v"   ),
  57.                      ( "9", "w/x/y/z" ),
  58.                      ( "*", "'/-/+/=" ) };
  59.        
  60.         int upper = is_all_caps( val );
  61.        
  62.         if ( val.Length < 1 )
  63.         {
  64.             return val;
  65.         }
  66.  
  67.         if ( val.Length == 1 )
  68.         {
  69.             return Char.IsUpper( val[0] ) ? "# " + val : val;
  70.         }
  71.        
  72.         if ( upper != 0 )
  73.         {
  74.             string v = "";
  75.            
  76.             foreach ( char x in val )
  77.             {
  78.                 if ( x == ' ' )
  79.                 {
  80.                     v += "0 ";
  81.                     continue;
  82.                 }
  83.  
  84.                 if ( Char.IsUpper( x ) )
  85.                 {
  86.                     v += "# ";
  87.                 }
  88.                 foreach ( ( string k, string p ) in asdf )
  89.                 {
  90.                     if ( p.Contains( Char.ToLower( x ) ) )
  91.                     {
  92.                         string[] qwerty = p.Split( '/' ).Where( e => e != "/" ).ToArray();
  93.                         int      times  = 1;
  94.                        
  95.                         foreach ( string q in qwerty )
  96.                         {
  97.                             if ( q == Char.ToLower( x ).ToString() )
  98.                             {
  99.                                 break;
  100.                             }
  101.                             else
  102.                             {
  103.                                 times += 1;
  104.                             }
  105.                         }
  106.                        
  107.                         v += new String( k[0], times );
  108.                         v += " ";
  109.                     }
  110.                 }
  111.             }
  112.            
  113.             return v;
  114.         }
  115.         else
  116.         {
  117.             string v = "## ";
  118.  
  119.             foreach ( char x in val )
  120.             {
  121.                 if ( x == ' ' )
  122.                 {
  123.                     v += "0 ";
  124.                     continue;
  125.                 }
  126.  
  127.                 foreach ( ( string k, string p ) in asdf )
  128.                 {
  129.                     if ( p.Contains( Char.ToLower( x ) ) )
  130.                     {
  131.                         string[] qwerty = p.Split( '/' ).Where( e => e != "/" ).ToArray();
  132.                         int      times  = 1;
  133.  
  134.                         foreach ( string q in qwerty )
  135.                         {
  136.                             if ( q == Char.ToLower( x ).ToString() )
  137.                             {
  138.                                 break;
  139.                             }
  140.                             else
  141.                             {
  142.                                 times += 1;
  143.                             }
  144.                         }
  145.  
  146.                         v += new String( k[0], times );
  147.                         v += " ";
  148.                     }
  149.                 }
  150.             }
  151.            
  152.             return v;
  153.         }
  154.     }
  155.    
  156.     public static void Main()
  157.     {
  158.         var asdf = new string[]
  159.         {
  160.             "GENERATING TEST CASES",
  161.             "=====================",
  162.             "  Hello World       = " + to_t9( "Hello World"       ),
  163.             "  HELLO WORLD       = " + to_t9( "HELLO WORLD"       ),
  164.             "  abba feed high    = " + to_t9( "abba feed high"    ),
  165.             "  I love PHP        = " + to_t9( "I love PHP"        ),
  166.             "  there r 4 lights! = " + to_t9( "there r 4 lights!" )
  167.         };
  168.        
  169.         foreach ( string line in asdf )
  170.         {
  171.             Console.WriteLine( line );
  172.         }  
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment