Nairaland Forum

Welcome, Guest: Join Nairaland / Login / Trending / Recent / New
Stats: 1238379 members, 1650714 topics. Date: Thursday, 24 April 2014 at 04:20 AM

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

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

Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) / Please Assist Me In This C Code Challenge! / 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 Nobody: 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, 2013
[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?
Re: Simple Code Challenge: C#, Java, C, C++ by filanto(m): 9:02pm On Jun 13, 2013
Beaf:

Lol, dude, I don't need access to a compiler to write code, I keep wandering why you keep harping on that; after a few years of coding, you really don't need a compiler to be able to analyse code.
i disagree with this statement
Re: Simple Code Challenge: C#, Java, C, C++ by kambo(m): 7:56pm On Jun 16, 2013
Hello OP. Is this contest still on ar u still xpectg mor submissions?
Ur question is confusg. Clarify .
Theres an array, lets call it d universe (Uni) and a pattern to search in d universe lets call it the subset (Sub). Q1: how many Universes are ther? 1,2,3?
2: is d match suppose 2 b a 'sliding' match or 'non-sliding'?
A slidg match will match 'ear' in 'heard' but will fail to match 'era'
though d universe , contains the elements in d substring, 'era'. So,wat kind of matchg dyu require? If it is an 'anyhow/nonsliding' match, then i dont see anyway, nested loopg wont b required. It wud b a cpu intensive operation.

3.) if d query is for a slidg match of a sub within a universe, wats d issue with multiple Universes?
Is the search suppose to b performed on all universes sequentially, the search result returng a list of matching (beginning) indexes. If so, then d end result will b a collection of list of matching indexes from all the Uni's. E.g let U1 , u2,u3 b d different universes to test s on. S is the substrng.
,match(Un,s)--> list<index> .
I.e function match takes a universe and a substring and returns a list of matchg indexes,
if so, then,
THEY'll b n number of list<indexes>. So u1,u2,u3 will generate 3 list<indexes>. If the match is performed on 3 different universes.
Then, it will b case of callg function 'match' 3 times.
Is this wat u want or is it a case of calling match once on all 3 universes and the search perfrmed on all 3 within a single loop without inner loops or recursion.
?
I.e a function
match(u1,u2,u3, ...Un , s) --> list<index>.
Multiple universes, 1 substrng, 1 list of (beginng) indexes.
Pls clarify and dont tell me to go read d strtg pages. Clarify here.
Re: Simple Code Challenge: C#, Java, C, C++ by kambo(m): 3:15am On Jun 18, 2013
for all its worth.
If it this thread is closed ,oh well!.
wrote this yesterday night. posting it this early morn 3am+.
I think this is what the q is all about abt. a sliding search . like find 'ear' in 'beard'.

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


import java.util.Vector;
import java.util.Iterator;
import java.util.Collection;

public class NLquiz
{

public final void printRes(final Collection<Integer> col)
{
if(col==null||col.isEmpty())
return;
else
{
System.out.println("results::"wink;
java.util.Iterator<Integer> iter = col.iterator();
int c=0;
while(iter.hasNext())
{
System.out.print(iter.next());
System.out.print(" "wink;
if(c>0&&(c%10==0))
System.out.println();
}//
}
System.out.println();
}

public int[] col2irr(final Collection<Integer> col)
{
if(col==null||col.isEmpty())
return new int[]{};
else
{
int[] nurr = new int[col.size()];

Integer i=null;
Iterator<Integer> iter = col.iterator();

for(int x=0;x<nurr.length;x++)
{
i = iter.next();
nurr[x]=i.intValue();

}
return nurr;
}
}
public void printArr(final int[] irr)
{
if(irr==null||irr.length==0)return;
else
{
System.out.print("[ "wink;
for(int x=0;x<irr.length;x++){
System.out.print(irr[x]);
System.out.print(" "wink;}
}
System.out.print(" ]"wink;System.out.println();System.out.println();

}
public static void main(String[] args)throws Exception
{
System.out.println("match test: "wink;
int[] uni=new int[]{1,2,4,5,6,7,8,55,7,33,6,0,0,4,4,7,8,55,00,8,4,5,7,8,55,7};
uni =
new int[]{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};
int[] sub = new int[]{8,55,7};
sub = new int[]{1,3,0,0,8};
NLquiz nlq = new NLquiz();
System.out.println("the uni: "wink;
nlq.printArr(uni);
System.out.println("the sub: "wink;
nlq.printArr(sub);
System.out.println(" match test: "wink;
Collection<Integer> res = nlq.match(uni,sub);
System.out.println("match found? "+(!(res==null||res.isEmpty())));
if(res!=null&&!res.isEmpty())
System.out.println("match found .. !! . printing indexes .. "wink;
nlq.printRes(res);
System.out.println("sub.L = "+sub.length);
System.out.println("uni.L = "+uni.length);
int[] irr = nlq.col2irr(res);
if(irr.length>0)
System.out.println(" values at matching indexes : "wink;
for(int x=0;x<irr.length;x++)
System.out.print(uni[irr[x]]+" "wink;
}

//[i] this is the work horse.[/b]



public java.util.Collection<Integer> match(final int[] Uni,final int[] sub)
{
if(Uni==null||sub==null||Uni==null||sub.length>Uni.length||sub.length==0||Uni.length==0)return null;
else
{
int maxIndex=sub.length-1;
Vector<Integer> result = new Vector<Integer>();

int stepper = -1;

for(int x=0;x<=Uni.length;x++)
{
// System.out.println("stepper value: "+stepper);

int checkLimit = x+maxIndex;

if(checkLimit<Uni.length)
{
if(Uni[x]==sub[0]&&Uni[checkLimit]==sub[maxIndex])
{
//System.out.println("beginning And End match found at : x= "+x);
stepper=0;
}
}

if(stepper==-1)
continue;

//System.out.println("stepper value-lower end: "+stepper);

if(Uni[x]!=sub[stepper])
{
stepper=-1;
}
else stepper++;
if(stepper==maxIndex)
{
result.add(x-(sub.length-2));
stepper=-1;
}
}
return result;

}
}

}
----------------------------------------------------------------------------------------------------------------------
Uni = Universe . of strings
sub = sub(set/string) to search out in the universe.

-------------------------------------------------------------------------------------------------------------------------
Re: Simple Code Challenge: C#, Java, C, C++ by akigbemaru: 2:38am On Feb 04
I like this another C++ LINK

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

Java Has Failed! / What Makes A Good Software Developer? / [Project] 2012 Summer Of Code - WORK HAS COMMENCED

(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 literature webmasters programming techmarket

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

Nairaland - Copyright © 2005 - 2014 Oluwaseun Osewa. All rights reserved. See Nairalist and How To Advertise
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.