![]() |
Programming Exercise #3
In a string of N characters, calculate the frequencies of repeated patterns, e.g., given "AUGCCCGTAUACGTA", one would get CGTA:2, CGT:2, GTA:2, CG:2, GT:2, TA:2, AU:2, CC:2. You do not need to display the information, only get the data in a format from which it can be extracted (hopefully in a pattern that is of order less than the algorithm). You may assume that only the letters A-Z will be used to make the given string.
It may help to review Programming Exercises #1 and #2. Remember, the goal is to make this as efficient as possible with graal script. Your algorithm will be ranked first by big-O notation, then by runtime speed. |
Do we need to find every pattern possible, or just ones specified?
For example, findAllPatterns( n ) or something like findNumber( n, pattern ) |
Assuming that the algorithm should find the number of occurrences of a string in another string. Which it shouldn't.
PHP Code:
PHP Code:
PHP Code:
|
Okay, I have mine finished. It takes about 5 seconds to run (go figure!) but it gets the right answer, at least from what I have seen.
PHP Code:
The script is very inefficient and slow, but it works ... NOTE: You will need to change the [] in the format to a percent sign, it won't let me post without giving me a bad request error. Look for this line: echo( format( "String: []s, Occurances: []s", rc[0], rc[1] ) ); PHP Code:
Also, if a mod wants to delete my last post in this thread they can, it won't let me edit it or delete it anymore for some reason, gives a Bad Request error. |
Quote:
Quote:
|
PHP Code:
|
I have updated the guidelines slightly.
|
Quote:
|
Quote:
|
No comment on what I posted?
You've irritated me. |
How about making Programming Exercise #4 something that we normal mortals stand a chance at? :whatever:
|
Not much to comment. The code and output dump should speak for themselves.
PHP Code:
PHP Code:
|
Quote:
|
Quote:
e: The new standard set by DrakilorP2P, O(n²) @Ziro: Will keep that in mind. ;) |
Quote:
Increased speed of script: PHP Code:
|
Dunno but it seems to show up in every single one of these exercises :/
|
Quote:
hehe |
Quote:
For example, the quicksort algorithm is O(n log n) [for every item to sort, it takes log base 2 of n loops to find its place]. |
Quote:
EDIT: I fixed mine, it wins now. It goes something like this: PHP Code:
|
Well, after having this explained to me I'm kinda ticked that this wasn't even pointed out from the start.
If I were to optimize my script I would have used Drakilor's method which for me to do now would just be copying. |
PHP Code:
Example1: PHP Code:
PHP Code:
|
Quote:
|
Quote:
@Chris, you started your algorithm 355 years into the future? Also you fail at subtracting. :p e: Here are exact (well, kinda) loop count functions for the algorithms (n=str.length()): Inverness: http://img258.imageshack.us/img258/2...b9bf3c1nt8.png Where epsilon (the E) is a number representative of the other loops that lead to 'continue;'. The script could be adjusted slightly to eliminate this term. DrakilorP2P: http://img87.imageshack.us/img87/353...66d612ekl4.png It should be pretty obvious where I get the O(n^2) from... |
Quote:
|
Quote:
You also have yet to point out to others what functions they should be avoiding and why. Quote:
You need to come down to the level of us mortals. |
Quote:
Quote:
Quote:
Quote:
I don't see what you're so upset about, you made a good contribution. |
I bet I could beat my previous time of 12 seconds by triggering my web server which would use awesome fast PHP to find patterns, then return them to Graal.
Slower than some, but surely faster than 12 seconds? |
I'm upset because you're evaluating based on this big-O thing but didn't state that in the first place and explain it.
Anyhow, if the goal is "to make this as efficient as possible with graal script" then you should be measuring based on time to complete instead of your big-O thing since the hard-coded functions do have a notable speed difference. |
If it's going to be based on time they would all have to be done on one computer instead of us each executing them as our computer speeds can change the time.
|
Quote:
And could you "pfa" get yourself a new account or put the proper way to address you in your signature or something? I can't go calling you Tolnaftate2004 or a shortened version of it and I'm not sure if I should be calling you "pfa" either and didn't even know about that until the announcement that you became GST. |
Quote:
If I pass a large enough string, an O(n^2) algorithm will always be faster than an O(n^3). Here are some hints: linear search: searching by iteration. obj.index, obj.pos, obj.positions are most likely using linear search. What this means is that there is a 'hidden' for loop within script that use these functions. If N is obj.length() or obj.size(), obj.positions takes about N loops to find all occurences of your substring. obj.pos and obj.index on average take N/2 loops. The 'in' operator when used with arrays is also using linear search (most likely). This again takes on average N/2 loops. Other uses of it are likely O(1). obj.remove uses linear search as well. Maybe more later... ;x |
Quote:
|
Quote:
|
Quote:
e: Just made the realization that obj.substring() also contains a hidden for loop. Which makes all big-O calculations I did wrong (you're all 1 order higher). :3 |
PHP Code:
|
Quote:
|
Quote:
|
PHP Code:
|
Quote:
|
Quote:
|
| All times are GMT +2. The time now is 11:46 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.