Wednesday, 5 July 2017

c# coding OOPs


Code on using Char

using System;
using System.Text;

              public class TextInput {

                     protected string _value;

                     public virtual void Add(char c)
                     {
                           _value += c.ToString();
                     }

                     public virtual string GetValue()
                     {

                           return _value;
                     }
              }

              public class NumericInput : TextInput
              {
                     public override void Add(char n)
                     {
                           if(Char.IsNumber(n))
                                  _value += n.ToString();
                     }

              }

              public class UserInput
              {
                     public static void Main(string[] args)
                     {
                           TextInput input = new NumericInput();
                           input.Add('1');
                           input.Add('a');
                           input.Add('0');
                           Console.WriteLine(input.GetValue());
                           Console.ReadLine();
                     }


              }

RESULT: 10


Code 2 : IndexOf and Substring

using System;

public class Path
{
    public string CurrentPath { get; private set; }

    public Path(string path)
    {
        this.CurrentPath = path;
    }

    public void Cd(string newPath)
    {
        string[] folders = CurrentPath.Split('/');
        if(newPath.IndexOf("..")<0)
            CurrentPath += "/" + newPath;
        else
        {
            CurrentPath = CurrentPath.Substring(0, CurrentPath.LastIndexOf("/"));
       
            CurrentPath += newPath.Substring(2, newPath.Length -2);
        }
     
   
    }

    public static void Main(string[] args)
    {
        Path path = new Path("/a/b/c/d");
        path.Cd("../x");
        Console.WriteLine(path.CurrentPath);
    }
}

RESULTS : /a/b/c/x 


Unresolved - Binary Tree Search



using System;

public class Node
{
       public int Value { get; set; }

       public Node Left { get; set; }

       public Node Right { get; set; }

       public Node(int value, Node left, Node right)
       {
              Value = value;
              Left = left;
              Right = right;
       }
}

public class BinarySearchTree
{
       public static bool Contains(Node root, int value)
       {

              if (root == null) return false;

              if (root.Value == value)
                     return true;
              else if (root.Value < value)
                     return Contains(root.Right, value);
              else
                     return Contains(root.Left, value);

       }

       public static void Main(string[] args)
       {
              Node n1 = new Node(1, null, null);
              Node n3 = new Node(3, null, null);
              Node n2 = new Node(2, n1, n3);

              Console.WriteLine(Contains(n2, 3));
              Console.ReadLine();
       }

}


Palindrome Test


public class Palindrome
{
public static bool IsPalindrome(string word)
{

if (word.ToLower() == ReverseString(word.ToLower()))
return true;
return false;
}

public static void Main(string[] args)
{
Console.WriteLine(Palindrome.IsPalindrome("Deleveled"));
Console.ReadLine();
}

public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}

}


-----------------------------

Another String Manipulation

-- imcomplete
using System;

public class Path
{
       public string CurrentPath { get; private set; }

       public Path(string path)
       {
              this.CurrentPath = path;
       }

       public void Cd(string newPath)
       {
              if (newPath == "/")  CurrentPath = "/";
              string[] cmd = newPath.Split('/');
             
              foreach (string cd in cmd)
                     switch (cd)
                     {
                           case "..":
                                  {
                                         CurrentPath = "/" + CurrentPath.Substring(1, CurrentPath.LastIndexOf('/') - 1);
                                         break;
                                  }
                           case "":
                                  {
                                         CurrentPath = ""; //to root this can only happen some cd(/x);
                                         break;
                                  }
                           default:
                                  {
                                         CurrentPath = CurrentPath + "/" + cd;
                                         break;
                                  }
                     }
       }

       public static void Main(string[] args)
       {
              Path path = new Path("/a/b/c/d");
              path.Cd("/x");
              Console.WriteLine(path.CurrentPath);
       Console.ReadLine();
       }

}

-------------
-- Smallest number left to be used ..
input = { 1, 3, 4, 5, 7 }; the answer = 2
input = { 1, 2, 3, 4, 7,9 }; the answer = 5

    class Program
    {
        static void Main(string[] args)
        {
   int[] input = { 1, 3, 4, 5, 7 };
   int result = solution(input);
   Console.WriteLine("small number to be used is {0}", result);
   Console.ReadLine();
        }

  public static int solution(int[] A)
  {
   int smallGuy = 1;
   // write your code in Java SE 8
   if (A.GetUpperBound(0) == 0) return smallGuy;
   if (A == null) return smallGuy;
   var aList = A.ToList();
 
   foreach (var number in A)
   {
    if (aList.IndexOf(smallGuy) >= 0)
     ++smallGuy;
   }
   return smallGuy;
  }
 }


No comments:

Post a Comment