Built-in class Extension's v1.9.5 [Updated]

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Built-in class Extension's v1.9.5
Author Sarlecc



 
Introduction
This script adds four new methods to the String class and three to the Array class more may be added in the future as I think of them.


Updates:


- v1.5 added three methods to the Array class and changed the name to Built-in class Extension's. (as it now handles more than just Strings)


- v1.6 added mix_up method to String class.


- v1.8 added search method to array class, added prime? method to Math module. Also due to the length of the script putting it up on  mediafire.
- v1.9 added two new methods to the Array class. Also was having problems updating the script so the download link changed...


-v1.9.5 added new prime number checking algorithm. now check if a prime number is prime much faster (slower though on composite numbers)


Features

Spoiler



- Can remove every other character
- Can add characters every other character
- Can convert a string to random characters.


- Can mix the characters in a string.


- Can find the mean.


- Can find the median.


- Can find the range.


- Can search for certain numbers in an array


- Can determine if a number is a prime number


- Can find the id in an array with the lowest value


- Can find the id in an array with the highest value



Screenshots
None needed
 
How to Use
Place script below materials and above main

Code:
#==============================================================================
# String Extension by Sarlecc
# 
# Strings
# "".remove_every(n, d) n = number, d = divsion (optional)
# "Example".remove_every(0, 3)# "".add_every(chars, n, d) chars = characters
# "Example".add_every("u", 2, 4)
# "".rand_chars
# "Example".rand_chars
# "".mix_up
#
# Arrays
# [].mean
# [].median
# [].range
# [].search(num1, num2) num2 is optional
#
# The following two methods return the id with the lowest or highest value in the given range
# both num1 and num2 are optional if omitted or values are to high then it will use 0 and the size 
# of the array
# [].lowest(num1, num2)
# [].highest(num1, num2)
#
# Math
# Math.prime?(num)
# Math.prime2?(num)
# Math.prime2? is on average 2.5 times faster than Math.prime? if the number is prime; otherwise it is around 16-353 times slower
# its recommended that you use Math.prime? for checking thousands of numbers at a time as Math.prime2? will give an error (I assume
# that it has to do with the multithreading and attempting to do the next number while still doing the previous number).
# use Math.prime2? for checking larger primes
# Math.prime? time for number 18987964267331664557:
# 4521.762485 seconds
# Math.prime2? time for number 18987964267331664557:
# 1922.888493 seconds
# Math.prime? time for 106573388391:
# 0.000027-0.000028 seconds
# Math.prime2? time for 106573388391:
# 0.000452-0.009907 seconds
#==============================================================================
 
Demo
Yea none needed.
 
Script
 Script



GitHub

FAQ
None yet.
 
Credit
- Sarlecc

Terms


Author's Notes:


The difference between rand_chars and mix_up is mix_up only returns a string containing characters in that string. The rand_chars method returns random characters that do not necessarily show up in the string. I'm guessing that mix_up might be good for anagrams/scrabble style games?
 
Last edited by a moderator:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Updated script to version 1.6: One more method added to the String class: mix_up

Still thinking of an effective way to do mode (for the Array class) that allows ties.

Edit: Didn't realize this but mix_up can use the same character twice.... working on a fix.

Edit2: Fixed it.

Edit3: Benchmark results of the string methods little surprised at how long mix_up takes in comparison to the others:

#using string Sarlecc with a loop of 500,000 user system total real 7.750000 0.000000 7.750000 ( 7.767275) #mix_up 1.953000 0.000000 1.953000 ( 1.960044) #remove_every(0) 2.578000 0.000000 2.578000 ( 2.572987) #add_every(" ", 0) 1.953000 0.000000 1.953000 ( 1.952794) #rand_chars
Edit4 yea I know lots of edits on this one :p : Managed to shave off a whooping 3.3 seconds from the mix_up benchmark same string and same amount of loops. Get the slightly updated script.
 
Last edited by a moderator:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Updated script to v1.8

Two new methods:

[].search(num1, num2)  example:

a = [1, 5, 6, 1, 6]a.search(1, 6)returns #> [1, 6, 1, 6, 4] #The last number is the how many matches were made.

Math.prime?(num) example:

a = [1, 3, 4, 5, 7, 8, 9, 11, 50, 234, 321, 938, 997]a2 = []a.each {|i| if Math.prime?(i) a2.push i end }#returns > [3, 5, 7, 11, 997]

Math.prime?(num) will only return true/false

Edit: Found some errors with the Math.prime? function it will now find the correct prime numbers up to 93693190 - 1 (to prevent long processing times). However it is much slower than it used to be will be working on a way to make it faster.

Edit2: removed limit.

Edit3: Ugh realized it was the true part that is the slow part (found out my test number was really a false thus the fast speeds) the limit is back in then....

Edit4: Thanks to bgillisp was able to make the prime? method much faster and increased the limit to 263882790666230.

Edit5: Realized I forgot to put the even check in again lol. Where this doesn't help for odd numbers it will speed up looping through large number sets a ton. (2 is the only prime even number so the rest are automatically false).
 
Last edited by a moderator:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Updated to v1.90: Ever wanted to find the id in an array within a certain range of id's with the highest or lowest value? Well now you can using the [].highest(num1, num2) or [].lowest(num1, num2) methods! (num1 and num2 can be omitted if they are to high then it uses 0 and the size of the array).
 

Adon

ADONKADONK
Veteran
Joined
Mar 24, 2012
Messages
93
Reaction score
15
First Language
English
Primarily Uses
I love this script extension and have found it useful in a system for my game. Thank you for the excellent work! However, I recommend using a service like Pastebin to host your scripts as it is the optimal service for such things and is very easy to use!
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
:D Glad you found it useful!

I will look into Pastebin (been using mediafire since IGMC 2014 but with my slow connection something else might work better).
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Realized that my github link was no longer working for this script it is now fixed.


Also updated the script with a new prime number checking algorithm which is on average 2.5 times faster when checking a prime number. However it is 16-353 times slower for composites. Note that for each digit you add (for prime numbers only) the amount of time for the algorithm to complete gets multiplied by around 8. For example:


Math.prime2?(19 digit prime) will take my computer around 4 minutes to complete whereas:


Math.prime2?(20 digit prime) will take my computer around 32 minutes to complete.


Untested: Math.prime2?(30 digit prime) should take my computer around 65,372.4 years to complete.


The original algorithm which is still in the script by the way would take the following time for the same numbers:


Math.prime(19 digit prime) will take my computer around 11-13 minutes to complete


Math.prime(20 digit prime) will take my computer around 90 minutes to complete


Untested: Math.prime?(30 digit prime) should take my computer around 163,431 years to complete.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Don't mind me, just installing a fog machine. :LZSwink:


I can't stop looping this BGM while at work since I began to play PSO2. It just too good for mood booster.
How to post a picture there in the status feed?
So I was at the grocery store trying to reach for some small garbage bags on the top shelf way in the back. Instead of going all the way downstairs to find someone to help me I grabbed a broom and pulled it down. Putting the broom back I said, "Thank you for your service".
S.O.S.--10:16pm......two toddlers still awake....send help...

Forum statistics

Threads
100,422
Messages
975,684
Members
132,080
Latest member
Garycomehome
Top