Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,745 members, 7,809,842 topics. Date: Friday, 26 April 2024 at 03:52 PM

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

Nairaland Forum / Science/Technology / Programming / Simple Code Challenge: C#, Java, C, C++ (19035 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)

Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 2:14am On May 28, 2011
^
Beaf, that is the problem; you should have stated that you wanted all occurrences; you are just confusing everyone. Completely state your goals next time, and don't keep people guessing. Anyway, I dash you one GBOSA for letting folks use their brains, rather than complaining about the dullness of the programming section.
Re: Simple Code Challenge: C#, Java, C, C++ by ektbear: 2:15am On May 28, 2011
omo_to_dun:

If you peruse the code carefully, you'll see that the loop does not necessarily run N times, since i is not incremented in every iteration. If the pattern in the array occurs in a wrap around fashion, as in the first array that beaf posted, then the loop will necessarily iterate more than N-times. Matter of fact, time analysis for the loop is O(N + M - 1)) = O(N + M) for the worst case;

The 2N I stated was just a crude worst case estimate. I guess you are right, O(N+M) is actually a sharper estimate.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 2:18am On May 28, 2011
^
Oh Ok. You are one smart guy. I barely hear folks on this section mention time complexity using the O-notations.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 2:21am On May 28, 2011
omo_to_dun:

^
Beaf, that is the problem; you should have stated that you wanted all occurrences; you are just confusing everyone. Completely state your goals next time, and don't keep people guessing. Anyway, I dash you one GBOSA for letting folks use their brains, rather than complaining about the dullness of the programming section.


No, the real test is over, we are now in jara stage. Tomorrow, I'm going to test for the most perfomant solution as well, lets spread the goodies! grin
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 2:26am On May 28, 2011
omo_to_dun:

@Beaf
On your next problem, can you, please, include a benchmark program to test for the time analysis of different input? You don't have to do it for all programs; I can help out with C, C++, or Java. [/font][/size]

I think this is quite a good idea. Maybe we should actually open a thread to dump such programmes. What d'you think?
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 2:28am On May 28, 2011
^
Not bad, not bad at all.  cool
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 2:31am On May 28, 2011
^
Take the honours then and open it. wink
Re: Simple Code Challenge: C#, Java, C, C++ by ektbear: 2:35am On May 28, 2011
@omo_to_dun: Thanks. . . I liked programming a lot as a kid, but didn't understand the importance of complexity analysis until much later. This problem that Beaf posted is a pretty good example of why it is important though.

You can imagine a crappy double loop algorithm that'd take the full O(NM) iterations, versus this one that @whoelse posted that is only O(N).

If M is comparable to N, you've gone from quadratic time O(N^2)  to linear time O(N). So if N is like 1 billion or 1 trillion, this really matters a lot.

Anyway, complexity analysis is important  smiley
Re: Simple Code Challenge: C#, Java, C, C++ by Otuabaroku: 2:47am On May 28, 2011
Beaf:

^
Damn! grin
Dude, there is no nested loop involved in that solution. I simply decided to loop through the results after they'd been returned, which is not nesting.

I actually ran it on my PC and it worked.

Being the killjoy that I am tho, I went and upped the test to include several instances of the search pattern in each test array (two in each), at random positions. embarassed
The new arrays are:
0, 8, 1, 0, 0, 0, 8, 6, 7, 1, 3, 0, 0, 8, 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, 1, 3, 0, 0, 8, 5, 9, 1
1, 0, 5, 2, 6, 3, 0, 7, 4, 1, 1, 0, 0, 7, 0, 0, 8, 1, 3, 0, 0, 8, 0, 0, 8, 6, 7, 8, 9, 1, 3, 0, 0, 8, 9, 1

All entries failed the test. . . Except the one from here; https://www.nairaland.com/nigeria/topic-675768.32.html#msg8406195
It very correctly reported:
Found at 9
Found at 33
Found at 9
Found at 28
Found at 17
Found at 29




LOL!! I therefore shamelessly award the babe below to my humble self (strictly for the cure of a manly "itch"wink!



^^^ are you kidding me or is it the more I look the less I see. Please can one tell me how this function:


public static int Check(int[] searchArray, int[] fullArray)
{
int j = 0;
int result = -1;
for (int i = 0; i < fullArray.Length; )
{
int x = i + j;
if (x >= fullArray.Length) x -= fullArray.Length;
if (fullArray[x] == searchArray[j])
{
j++;
}
else
{
j = 0;
i++;
}
if (j >= searchArray.Length)
{
result = i;
break;
}
}

return result;
}



will return the individual index of occurrences of a pattern in array that has assuming 5 patterns in it without looping through it to get your answer.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 3:04am On May 28, 2011
@ Beaf

Thanks for bestowing the honor upon thy humble fellow programmer. Here goes the thread.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 3:09am On May 28, 2011
^
Thanks dude. Its great that we can now have some agreed standards. Nice one.

Otuabaroku:

^^^ are you kidding me or is it the more I look the less I see. Please can one tell me how this function:


public static int Check(int[] searchArray, int[] fullArray)
{
int j = 0;
int result = -1;
for (int i = 0; i < fullArray.Length; )
{
int x = i + j;
if (x >= fullArray.Length) x -= fullArray.Length;
if (fullArray[x] == searchArray[j])
{
j++;
}
else
{
j = 0;
i++;
}
if (j >= searchArray.Length)
{
result = i;
break;
}
}

return result;
}



will return the individual index of occurrences of a pattern in array that has assuming 5 patterns in it without looping through it to get your answer.

It does work. I think you are missing the way the loop is declared, the bits marked "//@@@@@" in the code below are very important to its functioning (I think thats where your confusion lies).
While loops are usually declared in this manner, for (int i = 0; i < fullArray.Length; i++), it is also perfectly legal to do the following:

for (int i = 0; i < fullArray.Length; ). Note that i++ is missing. The winning code increments i elswhere under tight conditions.

Constructs like for ( ;  ; ) are also legal, in this case its an infinite loop equivalent to while(true).

Check the code below with this new understanding, it is incredibly beautiful. Test it also and you'll find it works nicely.


public static int Check(int[] searchArray, int[] fullArray)
       {
           int j = 0;
           int result = -1;
           for (int i = 0; i < fullArray.Length; )//@@@@@
           {
               int x = i + j;
               if (x >= fullArray.Length) x -= fullArray.Length;
               if (fullArray[x] == searchArray[j])
               {
                   j++;
               }
               else
               {
                   j = 0;
                   i++;//@@@@@
               }
               if (j >= searchArray.Length)
               {
                   result = i;
                   break;
               }
           }

           return result;
       }
Re: Simple Code Challenge: C#, Java, C, C++ by Otuabaroku: 3:17am On May 28, 2011
@ beaf loop declaration is not my confusion at all. what I am talking about is that the function returning an int which in this case stands for the index of the pattern in the array. Assuming we have 5 patterns in the array, the function will only return the index of the last pattern in the array. If the function is to return all the index of each pattern that occurred in the array, the means, it will need to return a list of index not just an int. do you get what i am arriving at.
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 3:25am On May 28, 2011
Otuabaroku:

Assuming we have 5 patterns in the array, the function will only return the index of the last pattern in the array.
Wrong. It will return the index of the first pattern in the array. I have tested this!


Otuabaroku:

If the function is to return all the index of each pattern that occurred in the array, the means, it will need to return a list of index not just  an int. do you get what i am arriving at.

You are absolutely correct. That's why beaf declared himself the winner since whoelse's code only returns the first occurrence of the pattern, not all the patterns. whoelse assumed, like most of us did, that only the first occurrence was to be returned; that's why all or codes returned an int instead of a list/array.
Re: Simple Code Challenge: C#, Java, C, C++ by Otuabaroku: 3:53am On May 28, 2011
omo_to_dun:

Wrong. It will return the index of the first pattern in the array. I have tested this!


You are absolutely correct. That's why beaf declared himself the winner since whoelse's code only returns the first occurrence of the pattern, not all the patterns. whoelse assumed, like most of us did, that only the first occurrence was to be returned; that's why all or codes returned an int instead of a list/array.
[/quote
^^^^ Brilliant! If you agree with me that the function needs to return a list/array, that means in order to get the result printed out, you need to iterate the list /array there by using for(or any other ) loop to accomplish the task;hence using 2 loops indirectly. The same can be said about beaf code.




Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 4:19am On May 28, 2011
Otuabaroku:

^^^^ Brilliant! If you agree with me that the function needs to return a list/array, that means in order to get the result printed out, you need to iterate the list /array there by using for(or any other ) loop to accomplish the task;hence using 2 loops indirectly. The same can be said about beaf code.

You are right; however, you are supposed to separate the printing of the list from the body of your function, which was exactly what Beaf did. Remember, the problem asked for the indices of the pattern; printing them is just for completeness sake.
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 6:41am On May 28, 2011
^
Thanks.

Otuabaroku: ^^^^ Brilliant! If you agree with me that the function needs to return a list/array, that means in order to get the result printed out, you need to iterate the list /array there by using for(or any other ) loop to accomplish the task;hence using 2 loops indirectly. The same can be said about beaf code.

The loop was simply for convenience, printing out the output is a bonus. That said, I could have done:

Console.WriteLine(foundPosCollection.ToString(","wink);

or

Response.Write(String.Join(foundPosCollection, "
"wink); //web

or

Console.WriteLine(String.Join(foundPosCollection, Environment.NewLine)); //console

Look ma, no loop!! . . .Guy, E be like say you de jealous my babe trophy! grin
Re: Simple Code Challenge: C#, Java, C, C++ by Otuabaroku: 8:35am On May 28, 2011
Beaf:

^
Thanks.

The loop was simply for convenience, printing out the output is a bonus. That said, I could have done:

Console.WriteLine(foundPosCollection.ToString(","wink);

or

Response.Write(String.Join(foundPosCollection, "
"wink); //web

or

Console.WriteLine(String.Join(foundPosCollection, Environment.NewLine)); //console

Look ma, no loop!! . . .Guy, E be like say you de jealous my babe trophy! grin
^^^Lol! I'm just imagining what you are going to enjoy, that is why I'm going crazy. Mean while enjoy why your champion last as I am coming to challenge it really hard.
Re: Simple Code Challenge: C#, Java, C, C++ by whoelse(m): 4:32pm On May 28, 2011
^^^I'm so envying Beaf too. At least i could have enjoyed that prize while it lasted, Dammnnn.

Beaf, that is the problem; you should have stated that you wanted all occurrences; you are just confusing everyone. Completely state your goals next time, and don't keep people guessing. Anyway, I dash you one GBOSA for letting folks use their brains, rather than complaining about the dullness of the programming section.

@omo_to_dun
The funny thing is i actually considered that but as Beaf stated in his question, he just requested for the array as a single unit so its safe to say my assumptions are correct. I still consider myself as part-winner.

@ekt_bear
Seen your posts in other sections. Didnt think programming was your tin. Thumbs up on ur analysis.

@Beaf
Where's my share of the prize cool
Re: Simple Code Challenge: C#, Java, C, C++ by ektbear: 4:40pm On May 28, 2011
^-- thanks. kind of a dabbler in lots of things, expert in almost none of them smiley
Re: Simple Code Challenge: C#, Java, C, C++ by Beaf: 5:22pm On May 28, 2011
whoelse:

^^^I'm so envying Beaf too. At least i could have enjoyed that prize while it lasted, Dammnnn.

@omo_to_dun
The funny thing is i actually considered that but as Beaf stated in his question, he just requested for the array as a single unit so its safe to say my assumptions are correct. I still consider myself as part-winner.

@ekt_bear
Seen your posts in other sections. Didnt think programming was your tin. Thumbs up on your analysis.

@Beaf
Where's my share of the prize cool

shocked Kai!!
I've woken up rather spent this morning, after a night of deep mystery, packed with intense gymnastic pleasures. . . Sensual thrills. . . Ecstasy. wink lol! But bruv, you are the overall winner na!
Your code is so sexy I keep going back to stare; its some high IQ stuff that gave you choice fireworks and champagne. Congrats!
No vex me o! Wetin you wan tek woman do wen you get champagne? grin grin grin

@topic
Please can somebody help out with a test to see which of the best entries is most performant, and award a worthy trophy? I'm damn tired after waking too early to watch F1 quallies. The three to test are ekt_bear's, whoelse's and Beaf's. If you take up the task, please dump your test code/methodology on omo_to_dun's specially created thread here: https://www.nairaland.com/nigeria/topic-677281.0.html
I would have cheated with a hyper performant entry based on a total rejig of ekt_bear's entry, but I can't find the strength to boot up a PC with a dev environment. I desperately need sleep. embarassed Ekt_bear, can you do it, we already discussed switching the looped array briefly (to the one with just 5 elements)?
Re: Simple Code Challenge: C#, Java, C, C++ by dellnet: 6:03pm On May 28, 2011
Got to be kidding me.
Re: Simple Code Challenge: C#, Java, C, C++ by freepeople: 5:22pm On Jun 08, 2011
I love programming. But I'm illiterate in programming language. I desperately needs to learn it. Can somebody advise me on how to achieve my aim. I would also love to know the books to read, sites to visit, or people to contact. Get in touch with me by writing to montymyguy@ yahoo .com
Re: Simple Code Challenge: C#, Java, C, C++ by Tolulop001(f): 6:23pm On Jun 08, 2011
This sis an absolute deviation, but i need help! Im trying to write a macro in VBA(I hope im in the right place)

Im stuck at work, trying to write a macro that copies and pastes a certain piece of information.

the next step is that the next time the macro runs, i need it to move to the next column  before repeating the action again.

here goes '

Range("F5:F10"wink.Select
    Selection.Copy
    Range("G5"wink.Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("F5:F10"wink.Select
    Selection.Copy
    Range("F5"wink.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

Im totally amateur at it, i know the code sucks embarassed

what i need it to do, is to change from the first F5, to G5(the newly pasted column) , as in to step forward one column and apply the macro on there. does this make sense? pls HELP! im in the office and cant leave till i get this  sad
Re: Simple Code Challenge: C#, Java, C, C++ by nich(m): 6:25pm On Jun 08, 2011
cheesy   'was really smiling like a kid that just got a kiss from the finest babe on the block.   wink

  smiley  i loved the grammar you guyz were yarning about the codes; knew & still know nothing  grin  about the codes y'all typed and compiled (tis like trying to read arabic   tongue  ); but loved the fact that there are fellaz - NL fellaz for that matter - who can do stuff like this. meeeeeeeeeehn! i'm over-impressed. the future indeed is bright! Naija may be rough & chaotic & frustrating right now, but daaaaaaaamn i see 'light beams' @ the end of the tunnel!

great job fellaz! OP, i hail! sharp sharp i don go find bedspace for this hostel. need to hang around this common room alot more.

singing: " .  .  . the storm is over .  .  ."
Re: Simple Code Challenge: C#, Java, C, C++ by marcus1234: 7:20pm On Jun 08, 2011
Re: Simple Code Challenge: C#, Java, C, C++ by cameronese(m): 9:24pm On Jun 08, 2011
Cheesy 'was really smiling like a kid that just got a kiss from the finest babe on the block. Wink

Smiley i loved the grammar you guyz were yarning about the codes; knew & still know nothing Grin about the codes y'all typed and compiled (tis like trying to read arabic Tongue ); but loved the fact that there are fellaz - NL fellaz for that matter - who can do stuff like this. meeeeeeeeeehn! i'm over-impressed. the future indeed is bright! Naija may be rough & chaotic & frustrating right now, but daaaaaaaamn i see 'light beams' @ the end of the tunnel!

great job fellaz! OP, i hail! sharp sharp i don go find bedspace for this hostel. need to hang around this common room alot more.

singing: " . . . the storm is over . . ."


I like your spirit, you'll be a good learner wink
Re: Simple Code Challenge: C#, Java, C, C++ by Lordrama: 7:51am On Jun 09, 2011
i am lost
Re: Simple Code Challenge: C#, Java, C, C++ by kheme(m): 8:34am On Jun 09, 2011
ok, i was really really interested from page on, but on getting this far, i've totally lost interest!
Re: Simple Code Challenge: C#, Java, C, C++ by Nobody: 9:48am On Jun 09, 2011
saw this late

but all programmers BIG UPS
Re: Simple Code Challenge: C#, Java, C, C++ by mrofficial(m): 9:53am On Jun 09, 2011
Wetin you come dey find for politics before, Beaf?
Re: Simple Code Challenge: C#, Java, C, C++ by Slyr0x: 12:17pm On Jun 09, 2011
nich:

 cheesy   'was really smiling like a kid that just got a kiss from the finest babe on the block.   wink

  smiley  i loved the grammar you guyz were yarning about the codes; knew & still know nothing  grin  about the codes y'all typed and compiled (tis like trying to read arabic   tongue  ); but loved the fact that there are fellaz - NL fellaz for that matter - who can do stuff like this. meeeeeeeeeehn! i'm over-impressed. the future indeed is bright! Naija may be rough & chaotic & frustrating right now, but daaaaaaaamn i see 'light beams' @ the end of the tunnel!

great job fellaz! OP, i hail! sharp sharp i don go find bedspace for this hostel. need to hang around this common room alot more.

singing: " .  .  . the storm is over .  .  ."

+100. grin . I had to give like 10 of my friends this thread's link.

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

Web Based Software Vs Standalone Solution / Nigerian Ethical Hackers In Here ---> / Why People Always Think Those That Make Use Of Laptop Are Into Fraud?

(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. 76
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.