Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,208,804 members, 8,003,836 topics. Date: Friday, 15 November 2024 at 08:22 PM

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

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

Please Help With This Simple Code / Programming FUN With Code Snippets!! -C++,C#, Java,php,python Or Any Language!! / Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) (2) (3) (4)

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

Simple Code Challenge: C#, Java, C, C++ by Beaf: 1:46am On May 26, 2011
Test your ability.

. . .Just a really simple challenge to post a piece of code with a single for loop that will find the following pattern (as a single unit):

1,3,0,0,8

in the following arrays:

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
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
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

The same algorithm must work in all cases and ALL operations must be to do with integers; no chars, strings, bytes, bits etc.

Enjoy! grin grin grin



--Edit--
I need to clarify the open requirement sentence; ". . .Just a really simple challenge to post a piece of code with a single for loop that will find the following pattern (as a single unit):"
This means that the only looping mechanism available to you is what is provided by the for loop. There must be no pseudo looping mechanisms like recursion or the employment of ugly tricks like labels and gotos.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 5:13am On May 26, 2011
C++ Solution:

// 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 [pattern].
*
* Return     : -1 if [pattern] is not in [array]
*              else beaf returns the least non-negative index i,
*              (0 <=  i < ARRAY_LENGTH) where array[i + m] = pattern[m]
*              for all 0 <= m < PATTERN_LENGTH.
*/
int
beaf(const int* const array,
     const int* const pattern,
     const int ARRAY_LENGTH,
     const int PATTERN_LENGTH)
{
   // size of pattern must not be greater than size of array
   if(PATTERN_LENGTH > ARRAY_LENGTH)
      return -1;

   // the current index of [pattern] that we are trying to match
   int next = 0;

   for(int i = 0; i < ARRAY_LENGTH; ++i)
   {
      // if all has been matched, return index of first
      // element that was matched
      if(next == PATTERN_LENGTH)
         return i - PATTERN_LENGTH;

      // move to next element in [pattern] if matching occurs
      if(array[i] == pattern[next])
         ++next;
      // matching does not occur
      else
         // start from the beginning of [pattern] if current element
         // does not match first element of [pattern]
         next = array[i] == pattern[0] ? 1 : 0;
   }

   return -1;
}

#endif


So, to solve the problem, all we need do is to use the following code snippet:

// File: beaf.cpp
#include <iostream>
#include "beaf.h"

int
main()
{   
   int pattern[] = {1, 3, 0, 0, 8};
   int beaf_array[3][36] =
   {
      {
         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
      },

      {
         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
      },

      {
         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
      }

   };

   for(int i = 0; i < 3; ++i)
   {
      int index = beaf(beaf_array[i], pattern, 36, 5);

      std::cout << "For array " << i << ":\n";
      if(index != -1)
         std::cout << "   Pattern was found at index "
                   << index << std::endl << std::endl;
      else
         std::cout << "   Pattern was not found.\n\n";
   }

   return 0;
}
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 6:27am On May 26, 2011
^
Nice try, but nah! It fails.

Why do you have this piece of code?

   if(PATTERN_LENGTH > ARRAY_LENGTH)
      return -1;


I ask because, both PATTERN_LENGTH and ARRAY_LENGTH are consts that will never change, so your condition will always return false.

This bit here is just looking to make trouble with itself and index out of bound type lil horrors  grin. It also fails at its task;

     if(array[i] == pattern[next])
         ++next;


There are a couple of other gotcha's, but I'm curious about new entries.

1 Like

Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 6:57am On May 26, 2011
LOL @ Beaf. I thought you understood C++.  (Could you please attach a picture of your terminal or environment where it says my code didn't compile or failed to produce correct results, if that will not take too much of your time? Let the compiler be the decider. You can see mine below)

Beaf:

Why do you have this piece of code?

   if(PATTERN_LENGTH > ARRAY_LENGTH)
      return -1;


PATTERN_LENGTH is the length of [pattern] array and [ARRAY_LENGTH] is the length of [array]. I have this in case someone calls the beaf function with invalid code like:

int pattern[] = {1, 2, 3, 4, 4};
int array[] ={4, 7};

int index = beaf(array, pattern, 2, 5). It simply makes certain that the latter is not greater than the former. How will the value always be false if someone sends in wrong values.

Compile and run the code as is (save and copy the first as "beaf.h" and the second as "beaf.cpp"wink:

This is my output on  Ubuntu 11.04 x_86_64 GNU/Linux with g++

Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 7:40am On May 26, 2011
^
Nah, if you check carefully, you'll find that the pattern is present in all 3 arrays, not just two. So, good try, but still fail.
Your code can't find the pattern in the first array or all the bad things I pointed out earlier will happen.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 7:55am On May 26, 2011
You said, "as a single unit". Ergo, consecutively. The pattern 1,3,0,0,8 is a subsequence of the first array, but it does not exist as a single unit like you required. Rephrase your question. And also, did you compile it? I hope you are not one of those NLers who just diagnose but have nothing to back it up with. At least, I have shown you my output.

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 [none] Pray tell, where does 1, 3, 0, 0, 8 exist as a single unit.
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
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.

If all the bad things you pointed out will happen, then show us, with proof, not words. Maybe you can't compile the code? It shouldn't be too hard, I suppose.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 8:02am On May 26, 2011
omo_to_dun:

You said, "as a unit". Ergo, consecutively. The pattern 1,3,0,0,8 is a subsequence of the first array, but it does not exist as a single unit like you required. Rephrase your question. And also, did you compile it? I hope you are not one of those NLers who just diagnose but have nothing to back it up with. At least, I have shown you my output.

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 [none]
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
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.

If all the bad things you pointed out will happen, then show us, with proof, not words. Maybe you can't compile the code? It shouldn't be too hard, I suppose.


Nah, i won't give the answer until a few others have tried, that will kill the fun. You have done well, but are still off mark.
My question does not need rephrasing either, not at all. It requires the pattern array to be found as one unit as you've pointed out.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 8:25am On May 26, 2011
Your question was slightly flawed. If you explicitly stated that wrapping was permitted, then, of course, the pattern occurs at the end of the first array. When you are looking for substrings in a string, for example  "beaf" in "eafhb", do you say that "beaf" exists in "eafhb" as a unit? No, you don't. Anyway, check my new function description, I have enabled wrapping.


#ifndef BEAF_PROBLEM_I
#define BEAF_PROBLEM_I

/*
* Description: Function beaf searches [array](with wrapping) 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 [pattern].
*
* Return     : -1 if [pattern] is not in [array]
*              else beaf returns the least non-negative index i,
*              (0 <=  i < ARRAY_LENGTH) where array[i + m] = pattern[m]
*              for all 0 <= m < PATTERN_LENGTH.
*/
int
beaf(const int* const array,
     const int* const pattern,
     const int ARRAY_LENGTH,
     const int PATTERN_LENGTH)
{
   // size of pattern must not be greater than size of array
   if(PATTERN_LENGTH > ARRAY_LENGTH)
      return -1;

   // the current index of [pattern] that we are trying to match
   int next = 0;
   bool wrap = false;

   for(int i = 0; ; ++i)
   {
      // wrap at end
      if(i == ARRAY_LENGTH && !wrap)
      {
         wrap = true;
         i = 0;
      }
      else if(i == ARRAY_LENGTH && wrap)
         break;

      // if all has been matched, return index of first
      // element that was matched
      if(next == PATTERN_LENGTH && !wrap)
         return i - PATTERN_LENGTH;
      else if(next == PATTERN_LENGTH && wrap)
         return i - PATTERN_LENGTH + ARRAY_LENGTH;

      // move to next element in [pattern] if matching occurs
      if(array[i] == pattern[next])
         ++next;
      // matching does not occur
      else
         // start from the beginning of [pattern] if current element
         // does not match first element of [pattern]
         next = array[i] == pattern[0] ? 1 : 0;
   }

   return -1;
}

#endif


New output:

Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 8:49am On May 26, 2011
^
Nothing is flawed about the way I set out the problem. You made wrong assumptions about what wasn't stated. This time as well, you've made a fresh assumption, your code works, but only by accident. Lol!
If you change the order in which you search the arrays, it will fail; no specific order was specified, but it is stated that the algorithm must work for all three arrays. So, you are close, but still far. Lol!

Surely "beaf" exists in "eafhb" as a unit in an array, it only depends on how you look at it.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 9:04am On May 26, 2011
I have been in tons of programming contests and brainstorming coding sessions; one of the requirements is that problems are not to be unambiguously stated. If a programmer has to make assumptions, then your problem is not fully defined. I am happy how you are not compiling the program but simply going by your intuition. The code works only by accident? LOL. When you questioned me about this code snippet:


if(PATTERN_LENGTH > ARRAY_LENGTH)
   return -1;


It was then I knew that you didn't understand my simple code; how can a programmer say that it will always return false, given that different values could be passed to the function? Or maybe you didn't know that the variables were not declared const in function "beaf" but that they were passed in as argument. I expect more from you.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 9:24am On May 26, 2011
omo_to_dun:

I have been in tons of programming contests and brainstorming coding sessions; one of the requirements is that problems are not to be unambiguously stated. If a programmer has to make assumptions, then your problem is not fully defined. I am happy how you are not compiling the program but simply going by your intuition. The code works only by accident? LOL. When you questioned me about this code snippet:


if(PATTERN_LENGTH > ARRAY_LENGTH)
   return -1;


It was then I knew that you didn't understand my simple code; how can a programmer say that it will always return false, given that different values could be passed to the function? Or maybe you didn't know that the variables were not declared const in function "beaf" but that they were passed in as argument. I expect more from you.

Abeg, let other people try jare! You failed the task.
Don't be too worried about whether I am attempting to compile or not when I can look at the logic and tell if the code will fail.

You are still griping about this?


if(PATTERN_LENGTH > ARRAY_LENGTH)
   return -1;


How in the World can the values of the two arrays I gave change? Why would you be more concerned with parameters a hypothetical "anybody" is passing in while you are the one attempting the problem? It was just a piece of junk code, in fact if you were bothered about the hypothetical "anybody," there are foolproof ways to calculate array length. No?
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 9:35am On May 26, 2011
Oh, I see where the problem is. My function solves it for the general case. Not just yours. I was intrigued by the question, so I decided to solve it for the general case. If you look at my main code, you can set the arrays to be anything. Why did you think that I made a function? If I wanted to solve just yours, I wouldn't need a function; I'll just write a simple loop. No need to get angry. And I am not stopping anyone from submitting answers. This is a public forum. It is just not about getting answers, the learning process is very important. If you understood my code(and read my function description, you'll see that it was for the general case). And to just say that , "nah, you failed" is simply not enough. That is not how you prove that someone's code doesn't work. You have to show it! It is okay if you do not have immediate access to a compiler.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 10:08am On May 26, 2011
omo_to_dun:

Oh, I see where the problem is. My function solves it for the general case. Not just yours. I was intrigued by the question, so I decided to solve it for the general case. If you look at my main code, you can set the arrays to be anything. Why did you think that I made a function? If I wanted to solve just yours, I wouldn't need a function; I'll just write a simple loop. No need to get angry. And I am not stopping anyone from submitting answers. This is a public forum. It is just not about getting answers, the learning process is very important. If you understood my code(and read my function description, you'll see that it was for the general case). And to just say that , "nah, you failed" is simply not enough. That is not how you prove that someone's code doesn't work. You have to show it! It is okay if you do not have immediate access to a compiler.

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. In all honesty, I didn't read your comments as well, cos I didn't need to.

If your function had been for the general case as you claim, you would have made no assumptions and your code would have been robust for all situations. Don't get me wrong, I don't say you aren't smart, you are, but you surely make a heap of assumptions and allow your mind and ego to trap you.
An example of your assumptions is that you must loop an array like it is a straight line, from beginning to end, thats why the issue of "beaf" existing in "eafhb" as a unit came up. IMHO, an array is a circle (which is why we loop it), except you specifically wish it to be something else; a circle has no beginning or end. So why should an array be visualised as having a beginning and an end, why can we not begin looping "eafhb" from the 5th element and end at the 4th? Lol, the mind is its own trap!

I'm not angry, ok maybe I got a bit upset at your challenging my knowledge because of the bad code below (and the potentially dangerous method signature it enables). You allow users enter arbitrary content at your own peril anyway (hackers delight):

if(PATTERN_LENGTH > ARRAY_LENGTH)
return -1;
Re: Simple Code Challenge: C#, Java, C, C++ by naijaswag1: 10:37am On May 26, 2011
saw this late but will soon post a java version.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 10:42am On May 26, 2011
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. In all honesty, I didn't read your comments as well, cos I didn't need to.
It is ironical that you talk about my ego. Even my Professors would never say something like: "I don't need access to a compiler to write code ".  I am not here to show off anything, I am only here to learn. I guess it is my fault, I should have kept it simple. You didn't even read my comments; dude, even geniuses have less egos.  grin

Beaf:

An example of your assumptions is that you must loop an array like it is a straight line, from beginning to end, thats why the issue of "beaf" existing in "eafhb" as a unit came up. IMHO, an array is a circle (which is why we loop it), except you specifically wish it to be something else; a circle has no beginning or end. So why should an array be visualised as having a beginning and an end, why can we not begin looping "eafhb" from the 5th element and end at the 4th? Lol, the mind is its own trap!
Unless otherwise stated, an array is a "contiguous portion of memory." I am glad that you said, "In My Humble Opinion". I simply felt you should have stated that the array should be treated like a "circle", not a straight line that programmers are used to. If someone asks you to print an array, I am sure you wouldn't treat it as a circle, or else you'd have an infinite loop.

Beaf:

I'm not angry, ok maybe I got a bit upset at your challenging my knowledge because of the bad code below (and the potentially dangerous method signature it enables). You allow users enter arbitrary content at your own peril anyway (hackers delight):

if(PATTERN_LENGTH > ARRAY_LENGTH)
   return -1;


Actually, since the code does not write to either of the arrays, it does not matter if the user sends invalid input. An hacker cannot use buffer overflow or any other tricks to manipulate the address space; if the arrays are dynamically allocated, then the heap will generate a page fault, send a SEMENTATION FAULT signal and the application will terminate if the signal is not processed. Similarly if it was generated on the stack, a segmentation fault might also occur. I assume you don't write much code in C/C++, a lot of the functions in the standard library where you send in a buffer also requires you to send in the size.

For what it's worth, this is one of the best conversations I have ever had on Nairaland. I have wasted too much time with Mrs.Chima in the romance section.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 11:13am On May 26, 2011
omo_to_dun:

It is ironical that you talk about my ego. Even my Professors would never say something like: "I don't need access to a compiler to write code ".  I am not here to show off anything, I am only here to learn. I guess it is my fault, I should have kept it simple. You didn't even read my comments; dude, even geniuses have less egos.  grin

Damn! cheesy

omo_to_dun:

Unless otherwise stated, an array is a "contiguous portion of memory." I am glad that you said, "In My Humble Opinion". I simply felt you should have stated that the array should be treated like a "circle", not a straight line that programmers are used to. If someone asks you to print an array, I am sure you wouldn't treat it as a circle, or else you'd have an infinite loop.

I disagree. The data of even the most intricate photo is held in a "contiguous portion of memory," but of course, no photo is a straight line.
The computer is just limited in how it represents things.

omo_to_dun:

Actually, since the code does not write to either of the arrays, it does not matter if the user sends invalid input. An hacker cannot use buffer overflow or any other tricks to manipulate the address space; if the arrays are dynamically allocated, then the heap will generate a page fault, send a SEMENTATION FAULT signal and the application will terminate if the signal is not processed. Similarly if it was generated on the stack, a segmentation fault might also occur. I assume you don't write much code in C/C++, a lot of the functions in the standard library where you send in a buffer also requires you to send in the size.

The fact that the code doesn't write to either array doesn't make creating such method signatures correct (in the context of your input from users defence). As for the standard library, it is built for performance, so it is excused. However, it is as unsafe as your calling code, so thats no vindication.

omo_to_dun:


For what it's worth, this is one of the best conversations I have ever had on Nairaland. I have wasted too much time with Mrs.Chima in the romance section.


Yeah, I went and checked the profile of "that gruff guy" stirring up my embers this morning, and I couldn't stop staring. If that is Mrs.Chima, kindly convey to the beautiful soul that you have dropped your interests forthwith. . . Tell her that a sinewed stallion, springing with zest is straining to larvish his potence on her and caress her silky smooth curves. grin grin grin grin
. . .Lovely pix you got there!
Re: Simple Code Challenge: C#, Java, C, C++ by dellnet: 11:30am On May 26, 2011
This code challenge is not valid in computer science. There is no how to use linear method to solve this problem as it states.
Re: Simple Code Challenge: C#, Java, C, C++ by logica(m): 11:42am On May 26, 2011
If I can't use a REGEXP, I won't even bother solving the problem. grin
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 11:45am On May 26, 2011
^
No RegExp. Lol!

dell_net:

This code challenge is not valid in computer science. There is no how to use linear method to solve this problem as it states.

omo_to_dun already has the kernel (so sad to say embarassed). The challenge is valid, but tricky.
Re: Simple Code Challenge: C#, Java, C, C++ by dellnet: 11:53am On May 26, 2011
Beaf:

omo_to_dun already has the kernel (so sad to say embarassed). The challenge is valid, but tricky.
yeah but not with linear method.
Re: Simple Code Challenge: C#, Java, C, C++ by usisky(m): 2:04pm On May 26, 2011
@Beaf,

oga Beaf, maybe d problem has to do with your question. cos when i read it, i thot exactly in the same line as the first
person who replied. so u need to rephrase it better. which ever way, it looks so so easy. but why d no use of char, only int. say i must use char for a reason, why not. int takes more memory space where char would have been enough. pls rephrase. make it complete
Re: Simple Code Challenge: C#, Java, C, C++ by whoelse(m): 4:16pm On May 26, 2011
My answer, in C#


class ArraySearch
{
static void Main(string[] args)
{
int[] searchArray = new int[] { 1, 3, 0, 0, 8 };
int[] fullArray = new int[]{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,
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,
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 result = ArraySearch.Check(searchArray, fullArray);
Console.WriteLine("Your answer is {0}", result);
Console.Read();
}

/// <summary>
/// Searches an array for another array.
/// </summary>
/// <param name="searchArray">The array to be searched for.</param>
/// <param name="fullArray">The array to search in.</param>
/// <returns>If -1, then the array doesn't exist, else returns the start index of the first item.</returns>
public static int Check(int[] searchArray, int[] fullArray)
{
int invalid = -1;
//Check for empty arrays.
if(fullArray == null || fullArray.Length == 0)
{
return invalid;
}

//And here too.
if (searchArray == null || searchArray.Length == 0)
{
return invalid;
}

//Check that the length of the full array can contain the searchArray.
if (searchArray.Length > fullArray.Length)
{
return invalid;
}

int currentIndex = 0;
for (int i = 0; i < fullArray.Length; i++, currentIndex++)
{
if (fullArray[i] == searchArray[currentIndex])
{
//Check if all the items in the array have been searched for.
if (searchArray.Length == currentIndex +1)
{
return i + (currentIndex * -1);
}
}
else
{
//Rewind back to the last known stop.
i += (currentIndex * -1);
currentIndex = -1;
}
}

return invalid;
}
}
Re: Simple Code Challenge: C#, Java, C, C++ by Seun(m): 2:13am On May 27, 2011
Why can't I use Python?
Re: Simple Code Challenge: C#, Java, C, C++ by mikkytrio(m): 3:59am On May 27, 2011
package nairaland;

/**
*
* @author mikkytrionze
*/
public class Nairaland
{
private final int[] pattern = {1,3,0,0,8};
private final int[] a = {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};
private final int[] b = {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};
private final int[] c = {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};

Nairaland()
{
this.search(pattern, a);
this.search(pattern, b);
this.search(pattern, c);
}

private void check(int ans)
{
if(ans == 0)
{
System.out.println("No pattern recognized!"wink;
}
else
{
System.out.println("Pattern recognized at position "+ans);
}
}
/**
* @param parttern the argument representing the pattern to search for
* @param array the array to search from
* @return the position of the first int in the array
*/
private int search(int[] parttern, int[] array)
{
int pos = 0, count =-1, temp =0;
for(int i=0; i<array.length; i++)
{
if(array[i] == parttern[pos])
{
if(count > -1)
count = -1;
else
count = 0;
temp = i;//position it is in the array
}

if(count > -1 && count <parttern.length)
{
if(array[i] == parttern[count])
{
count++;
if(count == parttern.length-1)
break;
}
else
{count=-1;}
}

else
{count=-1;temp=0;}
}
this.check(temp);
return 0;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Nairaland n = new Nairaland();
}

}
Re: Simple Code Challenge: C#, Java, C, C++ by mikkytrio(m): 4:15am On May 27, 2011
boring morning so had to make my php pips know am on. I hope this does it for you.

<?php

/**
*
* @author mikkytrionze
*/
class Nairaland
{
private $pattern = array(1,3,0,0,cool;
private $a = array(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);
private $b = array(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);
private $c = array(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 __construct()
{
$this->search($this->pattern, $this->a);
$this->search($this->pattern, $this->b);
$this->search($this->pattern, $this->c);
}

private function check($ans)
{
if($ans == 0)
{
print("No pattern recognized!\n<br>"wink;
}
else
{
echo("Pattern recognized at position ".$ans."<br>"wink;
}
}

/**
* @param parttern the argument representing the pattern to search for
* @param array the array to search from
* @return the position of the first int in the array
*/
private function search($parttern, $array)
{
$pos = 0;
$count =-1;
$temp =0;
for($i=0; $i<count($array); $i++)
{
if($array[$i] == $parttern[$pos])
{
if($count > -1)
$count = -1;
else
$count = 0;
$temp = $i;//position it is in the array
}

if($count > -1 && $count <count($parttern))
{
if($array[$i] == $parttern[$count])
{
$count++;
if($count == count($parttern)-1)
break;
}
else
{$count=-1;}
}

else
{$count=-1;$temp=0;}
}
$this->check($temp);
return 0;
}

/**
* @param args the command line arguments
*/
}

$n = new Nairaland();
?>
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 5:13am On May 27, 2011
Seun:

Why can't I use Python?

Someone already attempted in PHP, so lets see your python solution.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 5:14am On May 27, 2011
@whoelse
You cannot merge the three arrays into one. Dat na ojoro! Nice try, but no dice.

usisky:

@Beaf,

oga Beaf, maybe d problem has to do with your question. cos when i read it, i thot exactly in the same line as the first
person who replied. so u need to rephrase it better. which ever way, it looks so so easy. but why d no use of char, only int. say i must use char for a reason, why not. int takes more memory space where char would have been enough. pls rephrase. make it complete

Nah, my question is fine. In my replies so far, I've given out some pretty hefty hints to the solution. omo_to_dun had the kernel, but didn't try hard enough.
The reason I ruled out chars and other data types is to force us to create original solutions.
I've just finished coding up the answer, but I wanna see how others approach it (I have two solutions, one is slightly dubious grin).
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 5:21am On May 27, 2011
@mikkytrio

I'm quite tired from a hard slug all day, so I've not been able to look at your code without seeing double. lol!
However, I can tell that it fails to find the pattern in the first array.
Re: Simple Code Challenge: C#, Java, C, C++ by mikkytrio(m): 5:40am On May 27, 2011
@beaf lets see this. I guess I am getting your question. I didn't go through the whole thread initially.



package nairaland;

/**
*
* @author mikkytrionze
*/
public class Nairaland
{
private final int[] pattern = {1,3,0,0,8};

private int position;

private final int[] a = {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};
private final int[] b = {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};
private final int[] c = {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};

Nairaland()
{
this.search(pattern, a);
this.search(pattern, b);
this.search(pattern, c);
}


/**
* @param parttern the argument representing the pattern to search for
* @param array the array to search from
*/
private void search(int[] parttern, int[] array)
{
int count = 0;
String corr = "";
//go through the whole pattern and search if they are available in the array
for(int i=0; i<parttern.length; i++)
{
boolean search = this.in(parttern[i], array, array.length-1);
corr += Integer.toString(parttern[i])+" Found at position = "+Integer.toString(this.position)+"\n";
if(search == true)
{
count+=1;
}
}

if(count == parttern.length)
{
System.out.println("Pattern recognized!\n "+corr);
}
else
{
System.out.println("No pattern recognized!"wink;
}
}

/**
* @param searchValue the value to search for from the pattern in the array
* @param array the array to search for a pattern from
* @param lenght the length of the array
* @return a boolean value stating true if the value was found or not
*/
private boolean in(int searchValue, int[] array, int lenght)
{
if(searchValue == array[lenght])
{
this.position = lenght;
return true;
}
else
{
if(lenght == 0)
{
return false;
}
else
{
--lenght;
}
return in(searchValue,array, lenght);
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Nairaland n = new Nairaland();
}

}
Re: Simple Code Challenge: C#, Java, C, C++ by mikkytrio(m): 5:51am On May 27, 2011
@beaf waiting ,
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 6:01am On May 27, 2011
^
Lol! I should have said no recursion!
I really can't look at your code properly now, tho. I'm just chilling after a day of real donkey loads, bros no vex, I go look ya latest code later. It looks promising tho.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 6:18am On May 27, 2011
@mikkytrio
Allow me to offer my humble opinion. I compiled and ran your code. As you can see below, the indices for some elements appear to be wrong; I suppose we interpreted the questioned differently. Nice though, I haven't read a Java code in a long time!

(1) (2) (3) (4) (5) (Reply)

ALX Virtual Assistant Program / Programming Challenge For Beginners N20000 / Nigerian Ethical Hackers In Here --->

(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: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

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