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. ❏ Copyright 2009-2010 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. ==================================================================== 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.
dilbar · Jun 27, 2010 11:56 AM · 17,125 views
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; <p class="MsoNormal" style="margin-bottom:0in;margin-bot
colo · Jun 27, 2010 1:09 PM
kharacha pathala ta bachala natra ta bhaka bhaka marala
maalpaani · Jun 27, 2010 9:37 PM
This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.
Start a New Discussion