Squares

General FreeBASIC programming questions.
MrSwiss
Posts: 3190
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Squares

Postby MrSwiss » Jul 17, 2019 2:47

albert wrote:There's 12 * 60 rhymes = 720 elements..
Sorry albert, first error (min./day):
    1) 12 hours AM
    2) 12 hours PM
    3) 24 hours/day
Wich means: 1440 min./day (elements)
Before starting with such complex matters, you should get your math skills tuned.
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Squares

Postby fxm » Jul 17, 2019 5:10

For example:

Code: Select all

Const As Integer maxindex = 720
Const As Integer period = 60
Dim As Integer index = (Timer / period) Mod maxindex + 1

Do
  Dim As Integer i = (Timer / period) Mod maxindex + 1
  If i <> index Then
    index = i
    Print index  '' or play the #index element, with index in [1, maxindex]
  End If
  Sleep 10
Loop While Inkey = ""
Imortis
Moderator
Posts: 1625
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Squares

Postby Imortis » Jul 17, 2019 12:49

You don't need that many elements.

12 elements for the hour
6 elements for the 10s place
9 elements for the 1s place
1 element for 00 on the minutes.

28 elements should be all that is needed.

@albert: As to prudish, we want to make sure the forums are suitable for all ages.
I would be glad to help or provide suggestions for your code. This is a variation of a coding problem I had in college. My inclination (as hinted at above) would be to parse the time as a string and build the new string based on the distinct verbal elements of the time instead of trying to make an individual element for every single possible time.

@MrSwiss:
Since is previous time rhymes have no consideration for AM or PM, his initial math is correct.
MrSwiss
Posts: 3190
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Squares

Postby MrSwiss » Jul 17, 2019 13:21

@Imortis, seriously???

The times to take anything albert writes seriously, has gone a very long time ago.

What did take you so long, before eventually stepping in to those "below the waist-
level", stupid, annoying rhymes. I wouldn't have waited half the time (month's).
Imortis
Moderator
Posts: 1625
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Squares

Postby Imortis » Jul 17, 2019 14:20

MrSwiss wrote:@Imortis, seriously???
...snip...
What did take you so long, before eventually stepping in to those "below the waist-
level", stupid, annoying rhymes. I wouldn't have waited half the time (month's).


I was conferring with the other admins. I wanted to make sure we were on the same page. I am here every day. Some of the other admins are not.
MrSwiss
Posts: 3190
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Squares

Postby MrSwiss » Jul 17, 2019 14:50

Imortis wrote:I was conferring with the other admins. I wanted to make sure we were on the same page.
Fair enough (but only until you have the guiding lines).
Imortis wrote: I am here every day. Some of the other admins are not.
Isn't that the very reason to have Mod's?

More detailed:
Stepping in, before things escalate (pro-active, instead of reactive)
This then also means, that annoyed users expect a Mod. to get involved, before they're annoyed
enough, to contact the Admins. straight away.

e.g. like a soccer referee does:
1) admonish
2) yellow card (usually without direct consequence)
3) second yellow, in the same game = red (disqualified from current game, usually with: additional punishment)
3.5) 2 x yellow card (not the same game, punishment: misses next game, or similar)
4) straight red card (disqualified, with additional punishment, usually more than just 2 x yellow [3.5])
5) single yellow cards expire, after a given time/occasion e.t.c.
albert
Posts: 4916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jul 17, 2019 16:11

There's 12 hours on the clock, and 60 minutes per hour , so that's = 720 array( elements )

Some of the minutes have multiple rhymes.

I would be easiest if you could name the vars by the time...

Then you could do:

dim as string 1:01 = "bun so fun"

var t = time , print var t : would print var 1:01 = "bun so fun"

As is: i would need a 720 case , select case.
albert
Posts: 4916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jul 17, 2019 18:14

@Dodicat

Can you help with the de-compressor???

Code: Select all



Declare Function      compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string

screen 19

dim as double time1 , time2 , time3 , time4
do
   
    randomize
   
    dim as string s=""
    For n As Long = 1 To 10
        s+=chr(Int(Rnd*256))'+8)
    Next
   
    time1=timer
    'begin compress
        dim as string comp=s
        for a as longint = 1 to 1 step 1
            comp = compress_loop(comp)
        next
    'end compress
    time2 = timer
   
    time3=timer
    'begin decompress
        dim as string final_out = comp
        for a as longint = 1 to 1 step 1
            final_out = decompress_loop(final_out)
        next
    'end decompress
    time4 = timer
   
   'sleep
   
    'cls
    'draw string( 0,10) , left(s,100)
    'draw string( 0,30) , left(final_out,100)
    print string(99,"=")
    print "inp = " ; (s)
    print string(99,"=")
    print "out = " ; (final_out)
    print
    print "compress time   = "; time2-time1
    print "decompress time = "; time4-time3
    print
   
    if s = final_out then print "Decompressed OK" else print "Decompression failed."
    print string(99,"=")
   
    sleep
   
loop until inkey = chr(27)

sleep
end
'===============================================================================
'===============================================================================
'begin functions
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
   
    dim as string binari=""
    dim as string zeros = "00000000"
    dim as string n1
    for a as longint = 1 to len(chrs) step 1
        n1 = zeros + bin( chrs[a-1] )
        binari+=right(n1,8)
    next
   
    'we need to make binari and even multiple of 3
   
    print "c inp = " ; len(chrs)
    print "c bin = " ; len(binari) , binari
   
    dim as string bin_count
    dim as longint count = 0
    dim as string str1
    dim as longint dec1
    do
        str1 = str(len(binari)/3)
        dec1=instr(1,str1,".")
        if dec1<>0 then binari+="0" : count+=1
    loop until dec1=0
    bin_count = chr(count)
   
    print "c bin = " ; len(binari) , binari
   
    dim as string outs= ""
    dim as string map= ""
    for a as longint = 1 to len(binari) step 3
       
        n1 = mid(binari,a,3)
       
        if n1="000" then outs+="0" : map+="0"
        if n1="001" then outs+="1" : map+="0"
       
        if n1="010" then outs+="0" : map+="10"
        if n1="011" then outs+="1" : map+="10"
       
        if n1="100" then outs+="0" : map+="11"
        if n1="101" then outs+="1" : map+="11"
       
        if n1="110" then outs+="0" : map+="100"
        if n1="111" then outs+="1" : map+="100"
       
    next
   
    print "c out = " ; len(outs) , outs
    print "c map = " ; len(map) , map

    'we need to make outs and map and even multiple of 8
    dim as string out_count
    count = 0
    do
        str1 = str(len(outs)/8)
        dec1=instr(1,str1,".")
        if dec1<>0 then outs+="0" : count+=1
    loop until dec1=0
    out_count = chr(count)
   
    dim as string map_count
    count = 0
    do
        str1 = str(len(map)/8)
        dec1=instr(1,str1,".")
        if dec1<>0 then map+="0" : count+=1
    loop until dec1=0
    map_count = chr(count)

    dim as string final_out = ""
    for a as longint = 1 to len(outs) step 8
        final_out+=chr(valulng("&B"+mid(outs,a,8)))
    next
    final_out+="END"
    for a as longint = 1 to len(map) step 8
        final_out+=chr(valulng("&B"+mid(map,a,8)))
    next
   
    final_out = bin_count + out_count + map_count + final_out
   
    print "c fin  = "  ; len(final_out) ', binari
   
    return final_out
       
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    dim as longint bin_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
    dim as longint out_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
    dim as longint map_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
       
    dim as longint place = instr(1,chrs,"END")
    dim as string outs = left(chrs,place-1)
    dim as string map = mid(chrs,place+3)
   
    dim as string bin_outs=""
    dim as string zeros = "00000000"
    dim as string n1
    for a as longint = 1 to len(outs) step 1
        n1 = zeros + bin( outs[a-1] )
        bin_outs+=right(n1,8)
    next
    bin_outs = left(bin_outs,len(bin_outs)-out_count)
   
    dim as string bin_map=""
    for a as longint = 1 to len(map) step 1
        n1 = zeros + bin( map[a-1] )
        bin_map+=right(n1,8)
    next
    bin_map = left(bin_map,len(bin_map)-map_count)
   
    print "d out = "    ; len(bin_outs) , bin_outs
    print "d map = " ; len(bin_map) , bin_map
   
    dim as string bin_0 = string( len(bin_map) , chr(0) )
    for a as longint = 1 to len(bin_map) step 1
        if mid(bin_map,a,1) = "0" then mid(bin_0,a,1) = "0"
    next
   
    dim as string bin_1 = string( len(bin_map) , chr(0) )
    for a as longint = 1 to len(bin_map) step 1
        if mid(bin_map,a,1) = "1" then mid(bin_1,a,1) = "1"
    next
   
    print
    print "d zer = "    ; len(bin_0) , bin_0
    print "d one = " ; len(bin_1) , bin_1
   
       
    dim as string final_out = ""
   
    return final_out

end function

albert
Posts: 4916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jul 17, 2019 23:07

@fxm

Thanks for the time code....
Last edited by albert on Jul 18, 2019 2:05, edited 1 time in total.
dodicat
Posts: 5891
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Postby dodicat » Jul 18, 2019 0:05

I'll have a look tomprrow Albert.
albert
Posts: 4916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jul 19, 2019 0:20

@Dodicat

I think this formula works best...

n1 = mid(binari,a,3)

if n1="000" then outs+="0" : map+="00"
if n1="001" then outs+="1" : map+="00"

if n1="010" then outs+="0" : map+="01"
if n1="011" then outs+="1" : map+="01"

if n1="100" then outs+="0" : map+="10"
if n1="101" then outs+="1" : map+="10"

if n1="110" then outs+="0" : map+="1"
if n1="111" then outs+="1" : map+="1"

Code: Select all



Declare Function      compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string

screen 19

dim as double time1 , time2 , time3 , time4
do
   
    randomize
   
    dim as string s=""
    For n As Long = 1 To 8
        s+=chr(Int(Rnd*256))'+8)
    Next
   
    time1=timer
    'begin compress
        dim as string comp=s
        for a as longint = 1 to 1 step 1
            comp = compress_loop(comp)
        next
    'end compress
    time2 = timer
   
    time3=timer
    'begin decompress
        dim as string final_out = comp
        for a as longint = 1 to 1 step 1
            final_out = decompress_loop(final_out)
        next
    'end decompress
    time4 = timer
   
   'sleep
   
    'cls
    'draw string( 0,10) , left(s,100)
    'draw string( 0,30) , left(final_out,100)
    print string(99,"=")
    print "inp = " ; (s)
    print string(99,"=")
    print "out = " ; (final_out)
    print
    print "compress time   = "; time2-time1
    print "decompress time = "; time4-time3
    print
   
    if s = final_out then print "Decompressed OK" else print "Decompression failed."
    print string(99,"=")
   
    sleep
   
loop until inkey = chr(27)

sleep
end
'===============================================================================
'===============================================================================
'begin functions
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
   
    dim as string binari=""
    dim as string zeros = "00000000"
    dim as string n1
    for a as longint = 1 to len(chrs) step 1
        n1 = zeros + bin( chrs[a-1] )
        binari+=right(n1,8)
    next
   
    'we need to make binari and even multiple of 3
   
    print "c inp = " ; len(chrs)
    print "c bin = " ; len(binari) , binari
   
    dim as string bin_count
    dim as longint count = 0
    dim as string str1
    dim as longint dec1
    do
        str1 = str(len(binari)/3)
        dec1=instr(1,str1,".")
        if dec1<>0 then binari+="0" : count+=1
    loop until dec1=0
    bin_count = chr(count)
   
    print "c bin = " ; len(binari) , binari
   
    dim as string outs= ""
    dim as string map= ""
    for a as longint = 1 to len(binari) step 3
       
        n1 = mid(binari,a,3)
       
        if n1="000" then outs+="0" : map+="00"
        if n1="001" then outs+="1" : map+="00"
       
        if n1="010" then outs+="0" : map+="01"
        if n1="011" then outs+="1" : map+="01"
       
        if n1="100" then outs+="0" : map+="10"
        if n1="101" then outs+="1" : map+="10"
       
        if n1="110" then outs+="0" : map+="1"
        if n1="111" then outs+="1" : map+="1"
       
    next
   
    print "c out = " ; len(outs) , outs
    print "c map = " ; len(map) , map

    'we need to make outs and map and even multiple of 8
    dim as string out_count
    count = 0
    do
        str1 = str(len(outs)/8)
        dec1=instr(1,str1,".")
        if dec1<>0 then outs+="0" : count+=1
    loop until dec1=0
    out_count = chr(count)
   
    dim as string map_count
    count = 0
    do
        str1 = str(len(map)/8)
        dec1=instr(1,str1,".")
        if dec1<>0 then map+="0" : count+=1
    loop until dec1=0
    map_count = chr(count)

    dim as string final_out = ""
    for a as longint = 1 to len(outs) step 8
        final_out+=chr(valulng("&B"+mid(outs,a,8)))
    next
    final_out+="END"
    for a as longint = 1 to len(map) step 8
        final_out+=chr(valulng("&B"+mid(map,a,8)))
    next
   
    final_out = bin_count + out_count + map_count + final_out
   
    print "c fin  = "  ; len(final_out) ', binari
   
    return final_out
       
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    dim as longint bin_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
    dim as longint out_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
    dim as longint map_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
       
    dim as longint place = instr(1,chrs,"END")
    dim as string outs = left(chrs,place-1)
    dim as string map = mid(chrs,place+3)
   
    dim as string bin_outs=""
    dim as string zeros = "00000000"
    dim as string n1
    for a as longint = 1 to len(outs) step 1
        n1 = zeros + bin( outs[a-1] )
        bin_outs+=right(n1,8)
    next
    bin_outs = left(bin_outs,len(bin_outs)-out_count)
   
    dim as string bin_map=""
    for a as longint = 1 to len(map) step 1
        n1 = zeros + bin( map[a-1] )
        bin_map+=right(n1,8)
    next
    bin_map = left(bin_map,len(bin_map)-map_count)
   
    print "d out = "    ; len(bin_outs) , bin_outs
    print "d map = " ; len(bin_map) , bin_map
   
    dim as string bin_0 = string( len(bin_map) , chr(0) )
    for a as longint = 1 to len(bin_map) step 1
        if mid(bin_map,a,1) = "0" then mid(bin_0,a,1) = "0"
    next
   
    dim as string bin_1 = string( len(bin_map) , chr(0) )
    for a as longint = 1 to len(bin_map) step 1
        if mid(bin_map,a,1) = "1" then mid(bin_1,a,1) = "1"
    next
   
    print
    print "d zer = "   ; len(bin_0) , bin_0
    print "d one = " ; len(bin_1) , bin_1
   
       
    dim as string final_out = ""
   
    return final_out

end function

dodicat
Posts: 5891
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Postby dodicat » Jul 19, 2019 0:39

No luck yet Albert.
It's a bit like figuring out a decryption algorithm.
albert
Posts: 4916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jul 19, 2019 1:06

*Edited for content.*
albert
Posts: 4916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jul 19, 2019 19:16

@Dodicat

I think i got it this time...
Can you help , with the search & replace code , in the decompress_loop()

n1 = mid(binari,a,3)

if n1="000" then outs+="0" : map+="00"
if n1="001" then outs+="1" : map+="00"

if n1="010" then outs+="0" : map+="01"
if n1="011" then outs+="1" : map+="01"

if n1="100" then outs+="0" : map+="10"
if n1="101" then outs+="1" : map+="10"

if n1="110" then outs+="0" : map+="1"
if n1="111" then outs+="1" : map+="1"

Code: Select all



Declare Function      compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string

screen 19

dim as double time1 , time2 , time3 , time4
do
   
    randomize
   
    dim as string s=""
    For n As Long = 1 To 8
        s+=chr(Int(Rnd*256))'+8)
    Next
   
    time1=timer
    'begin compress
        dim as string comp=s
        for a as longint = 1 to 1 step 1
            comp = compress_loop(comp)
        next
    'end compress
    time2 = timer
   
    time3=timer
    'begin decompress
        dim as string final_out = comp
        for a as longint = 1 to 1 step 1
            final_out = decompress_loop(final_out)
        next
    'end decompress
    time4 = timer
   
   'sleep
   
    'cls
    'draw string( 0,10) , left(s,100)
    'draw string( 0,30) , left(final_out,100)
    print string(99,"=")
    print "inp = " ; (s)
    print string(99,"=")
    print "out = " ; (final_out)
    print
    print "compress time   = "; time2-time1
    print "decompress time = "; time4-time3
    print
   
    if s = final_out then print "Decompressed OK" else print "Decompression failed."
    print string(99,"=")
   
    sleep
   
loop until inkey = chr(27)

sleep
end
'===============================================================================
'===============================================================================
'begin functions
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
   
    dim as string binari=""
    dim as string zeros = "00000000"
    dim as string n1
    for a as longint = 1 to len(chrs) step 1
        n1 = zeros + bin( chrs[a-1] )
        binari+=right(n1,8)
    next
   
    'we need to make binari and even multiple of 3
   
    print "c inp = " ; len(chrs)
    print "c bin = " ; len(binari) , binari
   
    dim as string bin_count
    dim as longint count = 0
    dim as string str1
    dim as longint dec1
    do
        str1 = str(len(binari)/3)
        dec1=instr(1,str1,".")
        if dec1<>0 then binari+="0" : count+=1
    loop until dec1=0
    bin_count = chr(count)
   
    print "c bin = " ; len(binari) , binari
   
    dim as string outs= ""
    dim as string map= ""
    for a as longint = 1 to len(binari) step 3
       
        n1 = mid(binari,a,3)
       
        if n1="000" then outs+="0" : map+="00"
        if n1="001" then outs+="1" : map+="00"
       
        if n1="010" then outs+="0" : map+="01"
        if n1="011" then outs+="1" : map+="01"
       
        if n1="100" then outs+="0" : map+="10"
        if n1="101" then outs+="1" : map+="10"
       
        if n1="110" then outs+="0" : map+="1"
        if n1="111" then outs+="1" : map+="1"
       
    next
   
    print "c out = " ; len(outs) , outs
    print "c map = " ; len(map) , map

    'we need to make outs and map and even multiple of 8
    dim as string out_count
    count = 0
    do
        str1 = str(len(outs)/8)
        dec1=instr(1,str1,".")
        if dec1<>0 then outs+="0" : count+=1
    loop until dec1=0
    out_count = chr(count)
   
    dim as string map_count
    count = 0
    do
        str1 = str(len(map)/8)
        dec1=instr(1,str1,".")
        if dec1<>0 then map+="0" : count+=1
    loop until dec1=0
    map_count = chr(count)

    dim as string final_out = ""
    for a as longint = 1 to len(outs) step 8
        final_out+=chr(valulng("&B"+mid(outs,a,8)))
    next
    final_out+="END"
    for a as longint = 1 to len(map) step 8
        final_out+=chr(valulng("&B"+mid(map,a,8)))
    next
   
    final_out = bin_count + out_count + map_count + final_out
   
    print "c fin  = "  ; len(final_out) ', binari
   
    return final_out
       
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    dim as longint bin_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
    dim as longint out_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
    dim as longint map_count = asc(left(chrs,1))
    chrs=mid(chrs,2)
       
    dim as longint place = instr(1,chrs,"END")
    dim as string outs = left(chrs,place-1)
    dim as string map = mid(chrs,place+3)
   
    dim as string bin_outs=""
    dim as string zeros = "00000000"
    dim as string n1
    for a as longint = 1 to len(outs) step 1
        n1 = zeros + bin( outs[a-1] )
        bin_outs+=right(n1,8)
    next
    bin_outs = left(bin_outs,len(bin_outs)-out_count)
   
    dim as string bin_map=""
    for a as longint = 1 to len(map) step 1
        n1 = zeros + bin( map[a-1] )
        bin_map+=right(n1,8)
    next
    bin_map = left(bin_map,len(bin_map)-map_count)
   
    print "d out = "    ; len(bin_outs) , bin_outs
    print "d map = " ; len(bin_map) , bin_map
   
    dim as string bin_0 = string( len(bin_map) , chr(0) )
    for a as longint = 1 to len(bin_map) step 1
        if mid(bin_map,a,1) = "0" then mid(bin_0,a,1) = "0"
    next
   
    dim as string bin_1 = string( len(bin_map) , chr(0) )
    for a as longint = 1 to len(bin_map) step 1
        if mid(bin_map,a,1) = "1" then mid(bin_1,a,1) = "1"
    next
   
    print
    print "d zer = "   ; len(bin_0) , bin_0
    print "d one = " ; len(bin_1) , bin_1
   
    dim as string outputs = string(len(bin_map),chr(0))
   
    place=0
    do
        place = instr(place+1,bin_map,"1001")
        mid(outputs,place,4) = "1001"
        mid(bin_map,place,4) = "    "
    loop until place = 0
   
    place=0
    do
        place = instr(place+1,bin_map,"00")
        mid(outputs,place,2) = "00"
        mid(bin_map,place,2) = "  "
    loop until place = 0
   
    place=0
    do
        place = instr(place+1,bin_map,"01")
        mid(outputs,place,2) = "01"
        mid(bin_map,place,2) = "   "
    loop until place = 0
   
    place=0
    do
        place = instr(place+1,bin_map,"10")
        mid(outputs,place,2) = "10"
        mid(bin_map,place,2) = "   "
    loop until place = 0
   
    print
    print "d map = " ; len(bin_map) , bin_map
    print "d out = " ; len(outputs) , outputs
   
       
    dim as string final_out = ""
   
    return final_out

end function

dodicat
Posts: 5891
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Postby dodicat » Jul 19, 2019 22:29

Hi Albert.
I am not sure what to find and replace in the decompress.
Everything formed is some binary string with ones and zeros, some with spaces, but the answer has to be asci 0 to 255 to agree with input s.
How do I get the jump?

Return to “General”

Who is online

Users browsing this forum: No registered users and 16 guests