[Show all top banners]

Dilbar
Replies to this thread:

More by Dilbar
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Could anyone please help me with these two problems.
[VIEWED 10199 TIMES]
SAVE! for ease of future access.
Posted on 06-27-10 11:56 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Can any body help me in solving these two questions. Would really appreciate the help thanks...


Question1.




Every element in an array of integers points to a relative location from the current element. Precisely, if A[k] = m, the jump from k should land at k+A[k]=k+m.


Write a function



int arrayJmp(int[] A);



that returns the number of jumps until the pointer jumps out of the array when starting from the firstelement.


For example:





A[0]=2, A[1]=3, A[2]=1, A[3]=1, A[4]=3



The pointer's 1st jump is from 0 to 2, 2nd jump from 2 to 3, 3rd jump from 3 to 4, 4th jump from 4 to 7, but 7 is out of the array. The number of jumps until the pointer jumps out of the array is 4.


Return -1 if the sequence of jumps never ends.





====================================================================



Question 2:




Write a function



int nesting(string S);



which given a string S made of characters ( and ) returns 1if S is properly nested, 0 otherwise.


In precise terms, a string is propetly nested if it has one of the following forms:



  • empty,

  • (S), where S is properly nested,

  • ST, where both S and T are properly nested.

For example, given the string





(()(())())



your function should return 1, and for the string





())



your function should return 0.





 
Posted on 06-27-10 1:09 PM     [Snapshot: 84]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Here is the solution , all the best !!

===========================================


using System;

using System.Collections.Generic;



using System.Linq;



using System.Text;



 namespace ClassLibrary1



{



    public class Class1



    {



        public static void Main(string[]
args)



        {



            string s = "((())())";



            Console.WriteLine(s+"
Should return 1:"
+ nesting(s));



 



            string s1 = "(())())";



            Console.WriteLine(s1 + "
Should return 0:"
+ nesting(s1));



 



            string s2 = string.Empty;



            Console.WriteLine(s2 + "
Should return 1:"
+ nesting(s2));



           



            string s3 = "s(sddsfs(sdasdf)()()
))"
;



            Console.WriteLine(s3 + "
Should return 0:"
+ nesting(s3));



            Console.WriteLine("Test
Number o jump "
);



            int[] arr  =
{2,3,1,1,3};



           int[] arrNeverEnds = { 2, 3, 1, -1, 2, 3 };



            Console.WriteLine("Number
of jump for {2,3,1,1,3} :"
+ arrayJmp(arr));



            Console.WriteLine("Jump
never ends { 2, 3, 1, -1, 2, 3 } :"
+ arrayJmp(arrNeverEnds));



            Console.ReadLine();



       }



 



       public static  int arrayJmp(int[] A)



        {



            if (A.Length == 0)



            {



               
throw new  ArgumentException("empty array");



            }



 



            int numberOfJump = 0;



            int nextJumpIndex 
=0 ;



 



            for (int i =0; i <
A.Length ; i ++)



            {



               
nextJumpIndex = nextJumpIndex+A[nextJumpIndex];



               
numberOfJump++;



               
if (nextJumpIndex > A.Length)



               
{



                    return
numberOfJump;



               
}



 



            }



 



            return -1;



        }



        public static int nesting(string s)



        {



            if (string.IsNullOrEmpty(s))



            {



               
return 1;



          



            }



             char[] arrS = s.ToCharArray();



            Stack<char>
stack = new Stack<char>();



             foreach (char c in arrS)



            {



               
if (c == '(')



               
{



                   
stack.Push(c);



                }



 



               
if (c == ')')



               
{



                   
if (stack.Count > 0)



                   
{



                        stack.Pop();



                   
}



                   
else



                   
{



                        return 0;



 



                   
}



               
}



 



               



            }



 



            if (stack.Count > 0)



            {



               
return 0;



            }



            else



            {



               
return 1;



            }



        }



 



    }



}



 



 


 
Posted on 06-27-10 9:37 PM     [Snapshot: 271]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

kharacha pathala ta bachala natra ta bhaka bhaka marala
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
मन भित्र को पत्रै पत्र!
TPS Work Permit/How long your took?
Does the 180 day auto extension apply for TPS?
Travelling to Nepal - TPS AP- PASSPORT
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters