Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

What is the most interesting algorithm you know?

OpenStudy (jagatuba):

Can you define what you mean by interesting?

OpenStudy (anonymous):

No :(

OpenStudy (jagatuba):

Ok then the most interesting algorithm I know is one that I wrote. ;)

OpenStudy (anonymous):

\begin{array}l\color{#FF0000}{\text{Nice}}\text{ }\color{#FF7F00}{\text{what}}\text{ }\color{#FFE600}{\text{does}}\text{ }\color{#00FF00}{\text{it}}\text{ }\color{#0000FF}{\text{do?}}\text{ }\color{#6600FF}{\text{When}}\text{ }\color{#8B00FF}{\text{did}}\text{ }\color{#FF0000}{\text{you}}\text{ }\color{#FF7F00}{\text{write}}\text{ }\color{#FFE600}{\text{it?}}\end{array}

OpenStudy (jagatuba):

Which one lol.

OpenStudy (anonymous):

\begin{array}l\color{#FF0000}{\text{Y}}\color{#FF0000}{\text{ }}\color{#FF7F00}{\text{o}}\color{#FF7F00}{\text{ }}\color{#FFE600}{\text{u}}\color{#FFE600}{\text{ }}\color{#00FF00}{\text{r}}\color{#00FF00}{\text{ }}\color{#0000FF}{\text{ }}\color{#0000FF}{\text{ }}\color{#0000FF}{\text{a}}\color{#0000FF}{\text{ }}\color{#6600FF}{\text{l}}\color{#6600FF}{\text{ }}\color{#8B00FF}{\text{g}}\color{#8B00FF}{\text{ }}\color{#FF0000}{\text{o}}\color{#FF0000}{\text{ }}\color{#FF7F00}{\text{r}}\color{#FF7F00}{\text{ }}\color{#FFE600}{\text{i}}\color{#FFE600}{\text{ }}\color{#00FF00}{\text{t}}\color{#00FF00}{\text{ }}\color{#0000FF}{\text{h}}\color{#0000FF}{\text{ }}\color{#6600FF}{\text{m}}\color{#6600FF}{\text{ }}\color{#8B00FF}{\text{ }}\color{#8B00FF}{\text{ }}\color{#FF0000}{\text{:}}\color{#FF0000}{\text{ }}\color{#FF7F00}{\text{-}}\color{#FF7F00}{\text{ }}\color{#FFE600}{\text{D}}\color{#FFE600}{\text{ }}\end{array}

OpenStudy (jagatuba):

Yeah which one? lol

OpenStudy (anonymous):

\begin{array}l\color{#FF0000}{\text{Y}}\color{#FF7F00}{\text{o}}\color{#FFE600}{\text{u}}\color{#00FF00}{\text{r}}\color{#0000FF}{\text{ }}\color{#0000FF}{\text{f}}\color{#6600FF}{\text{a}}\color{#8B00FF}{\text{v}}\color{#FF0000}{\text{o}}\color{#FF7F00}{\text{r}}\color{#FFE600}{\text{i}}\color{#00FF00}{\text{t}}\color{#0000FF}{\text{e}}\color{#6600FF}{\text{ }}\color{#6600FF}{\text{o}}\color{#8B00FF}{\text{n}}\color{#FF0000}{\text{e}}\color{#FF7F00}{\text{!}}\end{array}

OpenStudy (jagatuba):

They are all interesting to me.

OpenStudy (anonymous):

\begin{array}l\color{#FF0000}{\text{The}}\text{ }\color{#FF7F00}{\text{only}}\text{ }\color{#FFE600}{\text{algorithm}}\text{ }\color{#00FF00}{\text{I've}}\text{ }\color{#0000FF}{\text{ever}}\text{ }\color{#6600FF}{\text{written}}\text{ }\color{#8B00FF}{\text{is}}\text{ }\color{#FF0000}{\text{this}}\text{ }\color{#FF7F00}{\text{one}}\text{ }\color{#FFE600}{\text{I'm}}\text{ }\color{#00FF00}{\text{using}}\text{ }\color{#0000FF}{\text{to}}\text{ }\color{#6600FF}{\text{color}}\text{ }\color{#8B00FF}{\text{my}}\text{ }\color{#FF0000}{\text{words}}\text{ }\color{#FF7F00}{\text{:(}}\end{array}

OpenStudy (jagatuba):

Okay, in 1980 I designed and coded a game that was subsequently ripped off by a major arcade game producer. I was writing it one a Vic-20. It was a side-scroller with a vehicle that could jump. I had to come up with an algorithm that kept all of the sprites in sync while on the ground, but followed each other like a train when jumping. Later I added an algorithm to add a somewhat similar effect while it was on the ground to simulate bumpy terrain.

OpenStudy (anonymous):

compared to my text-coloring algorithm, that's much cooler. Is that major arcade game producer still in business today?

OpenStudy (jagatuba):

yes

OpenStudy (jagatuba):

the algorithm was actually really simple, but I was 12 at the time so I thought it was pretty genius.

OpenStudy (anonymous):

when I was 12, I didn't even know what an algorithm was :-P

OpenStudy (jagatuba):

lol - neither did I.

OpenStudy (jagatuba):

More recently, on my first foray into Java, I wrote one that put the appropriate appendix to a number (1st, 2nd, 3rd, 4th, etc.). Before the haters start flaming me, I know that is really easy, trivial, and NO BIG DEAL, but since it was not a requirement of the assignment and I was the only one in the class that even thought to do it, I was pretty proud of myself.

OpenStudy (anonymous):

so if I type in "42" it appends "nd" to the end?

OpenStudy (anonymous):

hmm... let's try doing it in C!

OpenStudy (jagatuba):

Yes.

OpenStudy (jagatuba):

The trick is doing the check during your parse routine.

OpenStudy (anonymous):

http://ideone.com/aJRon So in C, this will be a parsing loop that will include a step checking the last character in the string

OpenStudy (jagatuba):

Yes.

OpenStudy (anonymous):

i'm not too good with parsing though :(

OpenStudy (jagatuba):

I don't know enough C to help you there.

OpenStudy (anonymous):

Right; although C syntax is very similar to Java, string handling in both languages is very very different anyway :(

OpenStudy (jagatuba):

Yeah I figured. Seems that way with most languages that are direct derivatives of each other.

OpenStudy (anonymous):

that appendix algorithm isn't so trivial in C :(

OpenStudy (jagatuba):

Well it didn't seem trivial when I first came up with it, but looking back it really wasn't that hard, but isn't that always the way it is?

OpenStudy (anonymous):

http://ideone.com/FIUgO so that's it in C++. Time to try it in C :-D

OpenStudy (jagatuba):

I think you can actually do it without parsing. It just happened that the numbers that I wanted to append were parsed strings. Let me look at my old code and see what I can come up with.

OpenStudy (anonymous):

This is an interesting exercise in C :)

OpenStudy (jagatuba):

What you have looks about right. Does it work?

OpenStudy (anonymous):

it seems to work

OpenStudy (jagatuba):

Yup.

OpenStudy (jagatuba):

So now see if you can do it without parsing.

OpenStudy (jagatuba):

Using int variables.

OpenStudy (jagatuba):

Should be able to do it with math. I'm trying to figure out the formula now.

OpenStudy (anonymous):

lol

OpenStudy (jagatuba):

What's so funny. I wait. I know. I'm a geek. Tell me something I don't already know.

OpenStudy (anonymous):

I think we might be able to use the bitwise operations and stuff to do it... or maybe the modulo operator.

OpenStudy (anonymous):

mod 10!

OpenStudy (anonymous):

http://ideone.com/IuM5n there we go

OpenStudy (jagatuba):

Yeah that's what I was thinking. If Number % 10 = 1 then sNumber = Number + "st" and so forth.

OpenStudy (jagatuba):

Yep did you test?

OpenStudy (anonymous):

I don't like '11st' or '12nd' or '13rd' though... how do we fix those cases?

OpenStudy (jagatuba):

Yes I believe i have a check in there for those numbers.

OpenStudy (anonymous):

http://ideone.com/0enJt fixed

OpenStudy (jagatuba):

Yep. Exactly.

OpenStudy (anonymous):

I think the C version is easy to do now :-D

OpenStudy (jagatuba):

See. Told you it was trivial. ;)

OpenStudy (anonymous):

hmm.... but my code won't do it properly if you give it something like 111 or 112 or 113 :(

OpenStudy (jagatuba):

Actually my java version is all on one line except for the check. resultsArea.append(horRule + yearCount + (yearCount == 1 ? "st" : (yearCount == 2 ? "nd" : (yearCount == 3 ? "rd" : "th"))) + " year\n");

OpenStudy (jagatuba):

because you are only checking 11-13.

OpenStudy (anonymous):

right.... how do I fix it?

OpenStudy (jagatuba):

Mod 100

OpenStudy (anonymous):

but it will break if I give it a bigger number like 1111 or 1112 :-D

OpenStudy (jagatuba):

hmm. good point.

OpenStudy (jagatuba):

See I didn't even have to think that far because we were dealing with a program where all of my parsed numbers were < 100

OpenStudy (anonymous):

so in order for me to make it work for arbitrary numbers, I must use strings?

OpenStudy (jagatuba):

No shouldn't. I know this is calculable.

OpenStudy (anonymous):

http://ideone.com/G1Ydb so the string version is now working correctly.

OpenStudy (jagatuba):

Nice. That's one solution. I still think it's calculable. But I'm going to have to ponder it when I'm in math mode. I've been in writing mode all morning.

OpenStudy (anonymous):

I shall ask this one in the Math section :-D I'm sure someone will answer it

OpenStudy (jagatuba):

I'm going to try to figure it out, but post your solution on ideone when you have it.

OpenStudy (jagatuba):

No I think mod 100 will work. Show me how it breaks.

OpenStudy (anonymous):

http://ideone.com/lYxkE

OpenStudy (anonymous):

you're right... it works!

OpenStudy (jagatuba):

112 mod 100 = 12 1112 mod 100 = 12 11112 mod 100 = 12 ad infinitum

OpenStudy (jagatuba):

Yea!

OpenStudy (jagatuba):

Who says you can't learn anything from trivial things?

OpenStudy (jagatuba):

See no that we know this if we EVER encounter a similar situation we will already be a step toward the solution.

OpenStudy (anonymous):

This is why I believe there is no such thing as a 'stupid question'

OpenStudy (anonymous):

nice conversation guys! most interesting algorithm? Hmmm I think my favorite one will be written in 2012 by myself :)

OpenStudy (jagatuba):

Yeah this was one of the more enlightening conversations that I've had lately. It's nice that i was able to walk away with something from it. I nominated agdgdgdgwngo "Best Question Asker" at the OS party because he always asks good questions. Okay well not always (what is a computer?).

OpenStudy (anonymous):

I've thinking of asking a question like this for a couple of days, i'm more interested in interesting data structures though =) some algorithms i've found interesting are A* Bellman-Ford max flow min cost max flow they have quite a few uses

OpenStudy (anonymous):

A* is interesting because it's relatively old algorithm (first described in 1968, but a simpler form of it was proposed by Dijkstra in 1959), but very long lived - it's still the gold standard for path finding. Pretty much any video game uses A* or a variant of it for AI pathfinding to this day!

OpenStudy (anonymous):

i read only beginning but java is more similar to c++ than c :P thats why its hard to play with strings in c

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!