Nairaland Forum

Welcome, Guest: Join Nairaland / Login / Trending / Recent / New
Stats: 1063485 members, 1237439 topics. Date: Saturday, 25 May 2013 at 04:00 PM

Simple Code Challenge: C#, Java, C, C++ - Programming (4) - Nairaland

Nairaland Forum / Science/Technology / Programming / Simple Code Challenge: C#, Java, C, C++ (9195 Views)

Pls Help, Website For C/c++ Compiler Free Download Windows 7 / What Do I Need In My Computer To Start Programming In Php,java,c++ Sql / Is C# Better Than C/C++? (1) (2) (3) (4)

(0) (1) (2) (3) (4) (Reply) (Go Down)

Re: Simple Code Challenge: C#, Java, C, C++ by Grey Beard: 1:55pm On Feb 05, 2012
Beaf:


Which of the examples are you pointing out? The two winning ones don't have that problem (except I'm mistaken).

OK, here's what I mean. In this solution:

PatternSearch(patternArray, searchArray3);
PatternSearch(patternArray, searchArray1);
PatternSearch(patternArray, searchArray2);

private void PatternSearch(int[] patternArray, int[] searchArray)
    {
        int foundPos = -1;
        int foundPosExtendedMode = -1;
        int currSearchElementPos = 0;//current index of searchArray being tested
        int currExtendedModeSearchElementPos = 0;//current index of searchArray being tested in extended mode
        int maxExtendedCycles = 0;
        int patternArrayLength = patternArray.Length;
        bool isLoopInExtendedMode = false;

        for (int i = 0; i < searchArray.Length; i++)
        { ,  }
  }


the method containing the for loop - PatternSearch - is called 3 times. So the same for loop is essentially called 3 times. My question was, can this still count as valid? - Or am I missing something?
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 4:12pm On Feb 05, 2012
candylips:

Beaf am still trying out a functional solution to this problem. Am new to functional programming

Why not not, bruv? Lets see a different approach, which language are you gonna write in?

I've not been able to take up functional programming despite promising to learn Elang and F# since it became public years ago. embarassed
How is you functional language journey?
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 4:17pm On Feb 05, 2012
Grey Beard:

OK, here's what I mean. In this solution:

PatternSearch(patternArray, searchArray3);
PatternSearch(patternArray, searchArray1);
PatternSearch(patternArray, searchArray2);

private void PatternSearch(int[] patternArray, int[] searchArray)
    {
        int foundPos = -1;
        int foundPosExtendedMode = -1;
        int currSearchElementPos = 0;//current index of searchArray being tested
        int currExtendedModeSearchElementPos = 0;//current index of searchArray being tested in extended mode
        int maxExtendedCycles = 0;
        int patternArrayLength = patternArray.Length;
        bool isLoopInExtendedMode = false;

        for (int i = 0; i < searchArray.Length; i++)
        { ,  }
  }


the method containing the for loop - PatternSearch - is called 3 times. So the same for loop is essentially called 3 times. My question was, can this still count as valid? - Or am I missing something?

Lolzzz!
You've made the same mistake as many others. Those 3 calls are the function being tested 3 separate times; there are three distinct arrays to test against, each puts the pattern in a different place to catch any mago-mago coding! grin
All code must get the 3 answers correct.
Re: Simple Code Challenge: C#, Java, C, C++ by delomos: 12:40pm On Feb 06, 2012
Since we're on the subject of functional programming, some, attacking this with JavaScript ,

var array1 = [0,8,1,0,0,0,8,6,7,8,9,5,2,6,3,0,7,4,1,1,0,0,7,0,0,8,6,7,8,9,5,9,1,1,3,0];
var array2 = [1,0,0,0,8,6,7,8,9,1,3,0,0,8,5,2,6,3,0,7,4,1,1,0,0,7,0,0,8,6,7,8,9,5,9,1];
var array3 = [1,0,5,2,6,3,0,7,4,1,1,0,0,7,0,0,8,6,7,8,9,5,0,0,8,6,7,8,9,1,3,0,0,8,9,1];

function findPattern(array, array1, array2){
var result=0;

for(i=0; i<array.length; i++){
if( array[i] == 1 &&
array[i+1] == 3 &&
array[i+2] === 0 &&
array[i+3] === 0 &&
array[i+4] == 8||
array1[i] == 1 &&
array1[i+1] == 3 &&
array1[i+2] === 0 &&
array1[i+3] === 0 &&
array1[i+4] == 8||
array2[i] == 1 &&
array2[i+1] == 3 &&
array2[i+2] === 0 &&
array2[i+3] === 0 &&
array2[i+4] == cool{
result += 1;

}
}
return result;
}


alert(findPattern(array3, array2, array1) + " Pattern found!"wink;

see is in action here: http://jsbin.com/edopas/edit#source (just click render)
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 2:54am On Feb 09, 2012
^
That is wrong, sir. You should read the first page thoroughly and go through the winning submissions.
The 3 arrays are meant to be tested individually, and you should get 3 positives not 2.
Re: Simple Code Challenge: C#, Java, C, C++ by delomos: 9:59am On Feb 09, 2012
Beaf:

, The 3 arrays are meant to be tested individually, and you should get 3 positives not 2.
", a single for loop that will find the following pattern (as a single unit): 1,3,0,0,8"

1,3,0,0,8 should be consecutive ,  ? Yes, I did read the previous solutions/post before posting.

Beaf:

, The 3 arrays are meant to be tested individually, and you should get 3 positives not 2 ,

it is, findPattern(array, array1, array2) through a single for loop , look carefully again @ what the code is doing.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 5:35pm On Feb 09, 2012
^
You are wrong, bruv. Everything about your code is totally wrong; from the approach to the method signature to the result.
I guess getting the wrong result tells it all (even with the hardcoding); you should have 3 positives, not 2.
Re: Simple Code Challenge: C#, Java, C, C++ by csharpjava(m): 12:14pm On Feb 10, 2012
I have made some improvements to the code given by Whoelse and I feel this is still the best code so far. See the full Java code below:


public class whoelseNumberPetternRun {
   

       
     public static String Check(int[] searchArrayIn, int[] fullArrayIn)
     {   
       
       
    int j = 0;
    String result = "";
   
    for (int i=0; i<fullArrayIn.length - 4wink       
    { 

           int x = i + j;
           
    if (j < 5 && fullArrayIn[x] == searchArrayIn[j])
               {           
                    j++;                   
               }                   
                   else
               {
                                         
                       j = 0;
                       i++;
               }
               
                       
               
               if (j == searchArrayIn.length - 1)
               {
                    System.out.println("Found at Location: " + i);
                    result += Integer.toString(fullArrayIn[i]) + ",";
                    result += Integer.toString(fullArrayIn[i+1]) + ",";
                    result += Integer.toString(fullArrayIn[i+2]) + ",";
                    result += Integer.toString(fullArrayIn[i+3]) + ",";
                    result += Integer.toString(fullArrayIn[i+4]) + ",";
               }             
                                     
     }
            return result;
         
        }


    public static void main(String[] args) {
       
      int[] searchArray ={1, 3, 0, 0, 8};
      int[] fullArray =  {1,3,0,0,8,0,8,1,0,0,0,8,6,7,8,9,5,2,6,3,0,7,4,1,1,0,0,7,0,0,8,6,7,8,9,5,9,1,1,3,1,3,0,0,8};   
      int[] fullArray1 = {0,8,1,0,0,1,3,0,0,8,0,8,6,7,8,9,5,2,6,3,0,7,4,1,3,0,0,8,1,1,0,0,7,0,0,8,6,7,8,9,5,9,1,1,3};
      int[] fullArray2 = {0,8,1,0,0,0,8,1,3,0,0,8,6,7,8,9,5,2,6,3,1,3,0,0,8,0,7,4,1,1,0,0,7,0,0,8,6,7,8,9,5,9,1,1,3};   
       
    System.out.println("fullArray " + Check(searchArray, fullArray));
    System.out.println();
    System.out.println("fullArray1 " + Check(searchArray, fullArray1));   
    System.out.println();
    System.out.println("fullArray2 " + Check(searchArray, fullArray2));   
    }
}
Re: Simple Code Challenge: C#, Java, C, C++ by ridott02: 9:27am On Apr 23
[quote author=omo_to_dun]C++ Solution:
[code]
// File: beaf.h

#ifndef BEAF_PROBLEM_I
#define BEAF_PROBLEM_I

/*
* Description: Function beaf searches [array] for the first occurence
*              of the sequence in [pattern].
*
* Param      : [array]          array to be searched.
*            : [pattern]        array containing sequence to search for.
*            : [ARRAY_LENGTH]   size of [array].
*            : [PATTERN_LENGTH] size of

Brother,I love these but no main it wont run or how do you run it?Is it not java code?

(0) (1) (2) (3) (4) (Reply)

Dr. Chinedu Emeka Invents Computer Software To Track Criminals / Can A Nigeria Programmer Afford A Range Rover Sport? / World’s Youngest Microsoft Certified Professional Arfa Karim Dies, Aged 16

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio writing webmasters programming techmarket

Links: (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)

Nairaland - Copyright © 2005 - 2013 Oluwaseun Osewa. All rights reserved. See Privacy Policy & Nairalist. 107.22.156.205