[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 12062 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 60 days
Recommended Popular Threads Controvertial Threads
Conservative discussions
Homeland Security revokes temporary status for 532,000 Cubans, Haitians, Nicaraguans and Venezuelans
Venezuela TPS lawuit
रबि लामिछानेको दाहिने हात ICE को हिरासतमा
ICE kidnapping people off the streets over op eds
US citizen Petitioning my wife who was out of status when she was in H1B. What to do ?
#MAGA#FAFO is delicious
What is the purpose of an F1 student visa in the U.S.?
Measles vax deaths vs measles death in USA
Nepal TPS decision
Don’t Just Read—Join the Conversation
Please ask KRISTI NOEM in her facebook and other social media to renew TPS
महँगो अण्डाको पिकल्प : कुखुरा र खोर भाडामा लिने
Nepal TPS
MLS AND LIVE SOCCER MATCH FREE SITE THAHA CHA?
Freedom of speech is dead in USA
Whatever happening in airport are illegal and should stop ? What are these immigration lawyers doing ?
Travelling to Nepal with expiration less than 6 months
Homeowner Charged renting to illegal migrants
USCIS to Check Social Media Posts of Green Card Applicants Legally Residing in US
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