Squares

General FreeBASIC programming questions.
Locked
dodicat
Posts: 8269
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
Here's some code to catch and locate substrings in a string, in this case a comma.

Code: Select all

 

Function FindAndReplace(instring As String,ReplaceThis As String,WithThis As String) As String
    var lens1=Len(ReplaceThis),lens2=Len(WithThis)
    If lens1=lens2 Then lens1=0
   dim as string s=instring 
    Dim As Integer position=Instr(s,ReplaceThis)
    While position>0
        If lens1 Then   
            s=Left(s,position-1) & WithThis & Mid(s,position+Lens1)
        Else
            Mid(s,position) = WithThis
        End If
        position=Instr(position+Lens2,s,ReplaceThis)
    Wend
    Function=s
End Function

Function COUNTER(instring As String,PartString As String,c() as integer) As integer
    redim c(0)
    dim count as integer
    var lens2=len(PartString)
   dim as string s=instring 
    Dim As Integer position=Instr(s,PartString)
    redim preserve c(1 to ubound(c)+1):c(ubound(c))=position
    While position>0
        count=count+1
        position=Instr(position+Lens2,s,PartString)
        redim preserve c(1 to ubound(c)+1)
        c(ubound(c))=position
    Wend
    redim preserve c(1 to ubound(c)-1)
    Function=count
End Function

'============== MAKE A STRING =====================
dim as string g
#macro make(s)
#define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
    s=string(intrange(1000,3000)," ")
for n as integer=0 to len(s)-1
 var num=IntRange(0,128)
 if num=7  or num=10 then num=48 'dont want beeps or spaces
    s[n]=num
next n
s=","+s  'just add a comma at beginning for a check
#endmacro



make(g) 'make the string
'==================================================

dim as double t1,t2

dim as string substring=","

redim as integer p()
print "Length of string ";len(g)
print
print "A substring = ";substring
print
print "Number of occurencies  of "& substring & "  =  ";
t1=timer
print counter(g,substring,p())
t2=timer

print
print "Time taken to find them   ";t2-t1

print "Positions of these in the string"
print
'show the positions
for n as integer=1 to ubound(p)
    print n,p(n)
next n

print "Now replace the substring with (----->A COMMA WAS HERE<-----)"
print
dim as string substring2="(----->A COMMA WAS HERE<-----)"
t1=timer
dim as string g2=FindAndReplace(g,substring,substring2)
print
print "Length of new string ";len(g2)
print
print "Number of occurencies of (----->A COMMA WAS HERE<-----) = ";

print counter(g2,substring2,p())

t2=timer
print "Time taken to replace them   ";t2-t1

print "Start positions of these in the string"
print
'show the positions
for n as integer=1 to ubound(p)
    print n, p(n)
next n
print
print "press a key to check the replacements"
sleep
print g2


sleep
  
And an altered compressor.
press space to refresh, esc to end.

Code: Select all

 #define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
Function FindAndReplace(instring As String,ReplaceThis As String,WithThis As String,flag As Integer=0) As String
    If flag=1 Then Swap replacethis,withthis
    Var lens1=Len(ReplaceThis),lens2=Len(WithThis)
    If lens1=lens2 Then lens1=0
    Dim As String s=instring 
    Dim As Integer position=Instr(s,ReplaceThis)
    While position>0
        If lens1 Then   
            s=Left(s,position-1) & WithThis & Mid(s,position+Lens1)
        Else
            Mid(s,position) = WithThis
        End If
        position=Instr(position+Lens2,s,ReplaceThis)
    Wend
    Function=s
End Function


Dim As String s1

#macro make()
s1=""
randomize timer
For z As Integer=1 To 1500
    s1=s1+Chr(IntRange(48,49))
Next z
#endmacro

Print
Dim As String s2
Dim As Integer flag=0,length

screen 20
do
start:

if flag=0 then
make()
print "original"
print s1
end if

print
s2=FindAndReplace(s1,"000","a",flag)
s2=FindAndReplace(s2,"001","b",flag)
s2=FindAndReplace(s2,"010","c",flag)
s2=FindAndReplace(s2,"011","d",flag)

s2=FindAndReplace(s2,"100","e",flag)
s2=FindAndReplace(s2,"101","f",flag)
s2=FindAndReplace(s2,"110","g",flag)
s2=FindAndReplace(s2,"111","h",flag)


s2=FindAndReplace(s2,"00","j",flag)
s2=FindAndReplace(s2,"01","k",flag)
s2=FindAndReplace(s2,"10","l",flag)
s2=FindAndReplace(s2,"11","m",flag)

s2=FindAndReplace(s2,"aa","n",flag)
s2=FindAndReplace(s2,"bb","o",flag)
s2=FindAndReplace(s2,"cc","p",flag)
s2=FindAndReplace(s2,"dd","q",flag)
s2=FindAndReplace(s2,"ee","r",flag)
s2=FindAndReplace(s2,"ff","s",flag)
s2=FindAndReplace(s2,"gg","t",flag)
s2=FindAndReplace(s2,"hh","u",flag)
s2=FindAndReplace(s2,"jj","v",flag)
s2=FindAndReplace(s2,"kk","w",flag)
s2=FindAndReplace(s2,"ll","x",flag)
s2=FindAndReplace(s2,"mm","y",flag)

If flag=0 Then
    Print "compress"
    Print s2
    Print
    length=len(s2)
    Print "length ";Length
    Print
    'REVERSE THE PROCESS:
    flag=1:goto start
Else
    Print "Original again"
    Print s2
    Print
    Print "length            ",Len(s2)
    print "compressed length ",length
    print "Ratio             ",length/len(s2)
    If s2=s1 Then Print "ok"
    print
    print "___________________________________________"
    sleep
    cls
    goto _loop
End If


_loop:
s2="":flag=0

if inkey=chr(27) then end
loop   
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I was referring to a file with comma separated spaces. Print #1 , space( 32 ) , space(65) , space(255)
You can't tell where the commas are in the file, its all spaces.

If you do Print #1 , space( 32 ) ; "_" ; space(65) ; "_" ; space(255) then it compresses to about twice the original filesize..

Just another failure...
dodicat
Posts: 8269
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
Have you tried out the FreeBASIC libraries for compression.
For example Zlib:
http://www.freebasic.net/wiki/wikka.php ... ExtLibzlib

It seems ok for compressing pure numbers (which I have here), but not so hot at other characters.
Also it uses chr(10) and chr(7) for compressed characters, i.e. a new line and a beep.
I dont get any beeps printing the compressed 1500 characters.

Code: Select all

#include once "zlib.bi"


#define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
screen 20

dim as integer stringlength=1500

Dim As Integer destinationlength = compressBound(stringlength)


Dim As UByte Ptr source = Allocate(stringlength)
Dim As UByte Ptr destination = Allocate(destinationlength)

dim as string original
'MAKE A STRING
For i As Integer = 1 To stringlength 
    original=original+chr(IntRange(48,57)) 'only numbers
Next
print "original, ";len(original);"  characters"
print original

source=@original[0]

print
'COMPRESS
 compress(destination, @destinationlength, source, stringlength)
print
Print "Compressed to "& destinationlength & " characters."
print

dim as string compressed
for n as integer=0 to destinationlength-1
    compressed=compressed+ chr(destination[n])
next n

print
print compressed
print

'clear the string pointer 
For i As Integer = 0 To stringlength - 1
    source[i] = 0
Next

'NOW UNCOMPRESS

 uncompress(source, @stringlength, destination, destinationlength)
 
dim as string uncompressed
'Build the uncompressed string

For i As Integer = 0 To stringlength - 1
    uncompressed=uncompressed+ chr(source[i])
Next

print
print "Back to original"
print uncompressed
print
print "length original ";len(original)
print "length compressed ";len(compressed)
print "Ratio ";len(compressed)/len(original)
if original=uncompressed then print "ok" else print "ERROR"
Deallocate(source)
Deallocate(destination)
sleep

 
BasicCoder2
Posts: 3955
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Squares

Post by BasicCoder2 »

dodicat wrote:Here's some code to catch and locate substrings in a string, in this case a comma.
I wonder how many code snippets are buried in the Squares thread never to be found again?
Here is another find/replace function.

I had to modify it in one project, replacing german words for english words, to only replace whole words not parts of words.
So in the example below,

IS THIS A IS TEST STRING IS","

The IS in THIS was not replaced and the word THIS remained intact to be replaced by something else as a whole word.

Code: Select all

function FindReplace(text as string, find as string, replace as string) as string
    dim as string outString
    for p as integer = 1 to len(text)
        if mid(text,p,len(find)) = find then
            outString = outString + replace
            p = p + len(find)-1  'skip found string
        else
            outString = outString + mid(text,p,1) 'insert replacement 
        end if
    next p
    return outString
end function
    
print FindReplace("IS THIS A IS TEST STRING IS","IS","XXXX")

sleep
BasicCoder2
Posts: 3955
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Squares

Post by BasicCoder2 »

albert wrote:Just another failure...
There is a science behind what is possible with data compression.
http://www.data-compression.com/theory.html
and so on ...
dodicat
Posts: 8269
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

BasicCoder2
That's a neat function.
It's true, there's many a long forgotten snatch and snippet buried in the circle and the square.

Code: Select all

dim as string s
print
s="Arbeit macht das Leben süß."

Function FindAndReplace(instring As String,ReplaceThis As String,WithThis As String,flag As Integer=0) As String
    If flag=1 Then Swap replacethis,withthis
    Var lens1=Len(ReplaceThis),lens2=Len(WithThis)
    If lens1=lens2 Then lens1=0
    Dim As String s=instring 
    Dim As Integer position=Instr(s,ReplaceThis)
    While position>0
        If lens1 Then   
            s=Left(s,position-1) & WithThis & Mid(s,position+Lens1)
        Else
            Mid(s,position) = WithThis
        End If
        position=Instr(position+Lens2,s,ReplaceThis)
    Wend
    Function=s
End Function
print FindAndReplace(FindAndReplace(FindAndReplace(FindAndReplace(s,"Arbeit","Work"),"macht","makes"),"das Leben","life"),"süß.","sweet.")
sleep
 
BasicCoder2
Posts: 3955
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Squares

Post by BasicCoder2 »

@dodicat,
my version :)

Code: Select all

dim shared as string german(5),english(5)

dim shared as string text1

for i as integer = 0 to 4
    read german(i),english(i)
next i

function FindReplace(text as string, find as string, replace as string) as string
    dim as string outString
    for p as integer = 1 to len(text)
        if mid(text,p,len(find)) = find then
            outString = outString + replace
            p = p + len(find)-1  'skip found string
        else
            outString = outString + mid(text,p,1) 'insert replacement 
        end if
    next p
    return outString
end function

text1 = "Arbeit macht das Leben süß"
for i as integer = 0 to 4
    text1 = FindReplace(text1,german(i),english(i))
next i
print text1

sleep

data "Arbeit","work"
data "macht","makes"
data "das","the"
data "Leben","life"
data "süß","sweet"
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Who ever creates a compression technique that beats the current ones, could potentially become a multi-millionaire or possibly a even a billionaire..

Especially if the technique can be built into hardware for on-the-fly inline zip and unzip.
dodicat
Posts: 8269
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Yea, I do notice, Albert, that your projects have a hidden agenda. Perpetual motion , extreme compression, anti-gravity machines, prime number formulae, your new language, uncrackable encryption, and these, it seems probable, are only a few of your enterprises.
Even one of the above, if successful, would guarantee an extremely big wad.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I've changed the language , so that the Consonant Vowel combinations can be swapped with the Vowel Consonant combinations.
So there will only be 352 words at the base of the language, instead of 704..

I've been thinking about how to make it sound good , and if each word can be spoken C/V or V/C then that would possibly lead to a better sounding language. So now its just picking out the 352 core words.

=================================================================================================================
On the anti-gravity, I think creating a magnetic field in space and then repelling it might be the answer..
You create a field and then turn it off and then you got a few micro seconds to repel it before it collapses. ???
Maybe a horseshoe magnet at the back of the ship with ions blasting against it might work for forward motion?
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I've read just about every book on aliens, and theres all different looking aliens..
Supposedly when an alien ship lands it leaves radioactive circles where it lands, but not as radioactive as if a nuke had gone off and without burning the grass it landed on. So they might be using high frequency sonic levitation to fly??
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

http://news.yahoo.com/largest-prime-num ... 57465.html


I can't find my prime number code, I got so much failures in my directories, I cant find it.
I was wondering about primes plotted around a sin cos circle, I wonder if they would line up and overlap.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat
@Richard

I'm not sure if my program gets only prime numbers, but it seems that all primes fall on certain degrees around the circle.
I plotted the first 1,000 primes in blue and and then the next of 10,000 in red and they overlap..

Code: Select all

dim as ulongint low = 1    
dim as ulongint high = 10000
dim as ulongint one
dim as ulongint ones(low to high)
redim as ulongint primes(1),prime(1)
dim as string test
dim as ulongint count,prime_count

one   = 1
count = low
prime_count = 1
for a as ulongint = low to high step 1

    ones(count) = one
    
    test = str(ones(count)/2) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    test = str(ones(count)/3) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    test = str(ones(count)/5) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    test = str(ones(count)/7) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
        
    if ones(count) > 1 then
        redim preserve primes(prime_count+1)
        primes(prime_count) = ones(count)
        prime_count += 1
    end if

    for a as ulongint = 1 to ubound(primes(0))
        test = str(ones(count)  /primes(a)) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    next
    if ones(count) <> 0  then
        redim preserve prime(prime_count+1)
        prime(prime_count) = ones(count)
        print prime(prime_count)
        prime_count += 1
    end if
    
    one   = one   + 1
    count = count + 1
next
'===============================================================================
'===============================================================================
'plot primes on the circle
'===============================================================================
'===============================================================================
dim as integer xres,yres
screen 19
screeninfo xres,yres
screenres xres,yres
dim as double rad= atn(1)/45
dim as double c1,s1
dim as double x1,y1
dim as integer xctr=xres/2
dim as integer yctr=yres/2
dim as double radius = 275
    for count = 1 to 1000'ubound(primes)
        if primes(count)<>0 then    
            c1= cos(primes(count)*rad)
            s1= sin(primes(count)*rad)
            
            x1=radius*c1
            y1=radius*s1
            
            line(xctr,yctr)-(xctr+x1,yctr+y1),1
        end if
    next
    for count = 1001 to ubound(primes)
        if primes(count)<>0 then    
            c1= cos(primes(count)*rad)
            s1= sin(primes(count)*rad)
            
            x1=radius*c1
            y1=radius*s1
            
            line(xctr,yctr)-(xctr+x1,yctr+y1),4
        end if
    next
sleep
END

All 4 quarters of the plotting are symmetrical except the 2,3,5 oddballs at the beginning..
dodicat
Posts: 8269
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
They are all primes alright, I've tested with isprime.

Code: Select all

dim as ulongint low = 1    
dim as ulongint high = 10000
dim as ulongint one
dim as ulongint ones(low to high)
redim as ulongint primes(1),prime(1)
dim as string test
dim as ulongint count,prime_count

'==========================================
function isprime(n as ulongint) as integer
    if (n=2) or (n=3) then return -1
    if n mod 2=0 then exit function
    if n mod 3=0 then exit function
    dim as ulongint limit=sqr(N)+1
    For I as ulongint=6 to limit step 6
        if N mod (i-1)=0 then exit function
        if N mod (i+1)=0 then exit function
    Next I
return -1
end function
'===========================================
one   = 1
count = low
prime_count = 1
for a as ulongint = low to high step 1

    ones(count) = one
    
    test = str(ones(count)/2) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    test = str(ones(count)/3) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    test = str(ones(count)/5) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    test = str(ones(count)/7) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
        
    if ones(count) > 1 then
        redim preserve primes(prime_count+1)
        primes(prime_count) = ones(count)
        prime_count += 1
    end if

    for a as ulongint = 1 to ubound(primes(0))
        test = str(ones(count)  /primes(a)) : if instr(1,test,".") = 0 then if test <> "1" then ones(count) = 0
    next
    if ones(count) <> 0  then
        redim preserve prime(prime_count+1)
        prime(prime_count) = ones(count)
        '=================================================
        if isprime (prime(prime_count))=0 then print prime(prime_count): sleep
        '==================================================
        print prime(prime_count)
        prime_count += 1
    end if
    
    one   = one   + 1
    count = count + 1
next
'===============================================================================
'===============================================================================
'plot primes on the circle
'===============================================================================
'===============================================================================
dim as integer xres,yres
screen 19
screeninfo xres,yres
screenres xres,yres
dim as double rad= atn(1)/45
dim as double c1,s1
dim as double x1,y1
dim as integer xctr=xres/2
dim as integer yctr=yres/2
dim as double radius = 275
    for count = 1 to 1000'ubound(primes)
        if primes(count)<>0 then    
            c1= cos(primes(count)*rad)
            s1= sin(primes(count)*rad)
            
            x1=radius*c1
            y1=radius*s1
            
            line(xctr,yctr)-(xctr+x1,yctr+y1),1
        end if
    next
    for count = 1001 to ubound(primes)
        if primes(count)<>0 then    
            c1= cos(primes(count)*rad)
            s1= sin(primes(count)*rad)
            
            x1=radius*c1
            y1=radius*s1
            
            line(xctr,yctr)-(xctr+x1,yctr+y1),4
        end if
    next
sleep
END
 
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I guess I should have checked to see in any non-primes, fall on the same degrees as primes ???

I forgot how to turn sin / cos back into degrees?
Locked