FreeBASIC syntax challenge games

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Where are these unresolved enigmas?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

The one line solution for:

Code: Select all

Type UDT
  Public:
    Dim As Integer array(20) = {15,12,8,5,3,19,18,6,10,2,13,9,1,20,0,14,7,17,4,11,16}
  Private:
    Declare Operator Let (Byref As UDT)
End Type

Dim As UDT u

For I As Integer = 0 To 20
  Print u.array(I),
Next I
Print
Print

For I As Integer = 0 To 20
  u.array(I) = 0
  Print u.array(I),
Next I
Print
Print

'------------------------------------------------------------------------------------
'u = UDT()  '' error 201: Illegal member access, UDT.operator.let
'u.Constructor()
'swap u,*(new udt) 'My attempt
'some other way

'------------------------------------------------------------------------------------
For I As Integer = 0 To 20
  Print u.array(I),
Next I
Print

Sleep 
and tourist trap's :

http://www.freebasic.net/forum/viewtopi ... 84#p227584
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

dodicat wrote:The one line solution for:

Code: Select all

Type UDT
  Public:
    Dim As Integer array(20) = {15,12,8,5,3,19,18,6,10,2,13,9,1,20,0,14,7,17,4,11,16}
  Private:
    Declare Operator Let (Byref As UDT)
End Type

Dim As UDT u

For I As Integer = 0 To 20
  Print u.array(I),
Next I
Print
Print

For I As Integer = 0 To 20
  u.array(I) = 0
  Print u.array(I),
Next I
Print
Print

'------------------------------------------------------------------------------------
'u = UDT()  '' error 201: Illegal member access, UDT.operator.let
'u.Constructor()
'swap u,*(new udt) 'My attempt
'some other way

'------------------------------------------------------------------------------------
For I As Integer = 0 To 20
  Print u.array(I),
Next I
Print

Sleep 
I don't understand:
- My one line solution:

Code: Select all

'------------------------------------------------------------------------------------
'u = UDT()  '' error 201: Illegal member access, UDT.operator.let
u.Constructor()

'------------------------------------------------------------------------------------
- Your one line solution:

Code: Select all

'------------------------------------------------------------------------------------
'u = UDT()  '' error 201: Illegal member access, UDT.operator.let
swap u,*(new udt) 'My attempt

'------------------------------------------------------------------------------------
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Tourist Trap wrote:Alternatively, for the puppies I've one enigma if you allow me to post it. It may be recreational.

Puppy Enigma
Find a way to print the low cased version of the first letter of a uppercased sentence in one single instruction (the whole program is one single instruction, but adding a pause with SLEEP is allowed).
The sentence could be:

Code: Select all

HAPPY NEW YEAR PUPPIES!
Program output:

Code: Select all

h
Solution in one single instruction or one single keyword ?
(anyway, I'm offside for this enigma !)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

regarding the udt array, maybe I am mistaken, I thought there was another way not using new.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote: Solution in one single instruction or one single keyword ?
(anyway, I'm offside for this enigma !)
Single instruction! You can use LCASE of course, even if not necessary.
This is quite easy indeed, just working from the static string directly (no dim required then) and extracting the material from it. Only parenthesis are delicate to handle, in both of the 2 solutions I have.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Could the above challenge be to find the shorter one code line:
  • Each gives only his score (length of the line in characters).
    (string being referenced by 's')
But for that, we should give a precise rule:
The upper-cased sentence can start with spaces and / or non-alphabetic characters, and the goal is to process the first alphabetic character of the sentence!
For example:

Code: Select all

Dim As String s = "     *** HAPPY NEW YEAR PUPPIES! ***     "
Output must be "h".

My own score = 57 !


As for me, to let you rest a little, I would still propose some enigmas but much easier than my lasts.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

I am not sure about the rules but:

Code: Select all

 ?chr(asc("HAPPY NEW YEAR PUPPIES!")+32)
 sleep 
So it is the string + 14 characters.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

With your lower-casing method, I improve my score (with my rules) to 54 (including the string name 's')!
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

For the string by fxm (not a general string, but the one given):

Code: Select all


?chr(("     *** HAPPY NEW YEAR PUPPIES! ***     ")[9]+32)
sleep

 
If a general string is given, is it strictly Uppercase or Mixed case with random padding at each end (no alphabet characters in the padding)?
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

My rules:
- No alphabetic characters in the padding.
- Any character in the useful string, but always starting with an alphabetic character.

Mixed case in the useful string: my score = 57
Only upper case in the useful string: my score = 54
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

Yes those solutions are very excellent! I didn't know that ASC would retain the first element of a string, or that a string with parenthesis would be evaluated in order to allow deferencing!

That's why I thought of a little more tricky, but equivalent:

Code: Select all

 ?  lCase ( chr( ((@"HELLO PUPPIES!")[0]) [0]) )  
?  lCase ( chr((cast(zstring ptr, @"HELLO PUPPIES!")[0])[0]) )
Sorry I missed the new rules, what is the sentence so ? "happy new year puppies" ?
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

More simply:

Code: Select all

? lCase ( chr( s[0] ) ) 
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote:More simply:

Code: Select all

? lCase ( chr( s[0] ) ) 
In a macro, the parenthesis version makes apparently more sense. It's important because it doesn't work otherwise.

Code: Select all

#define H(e)   lcase( chr( (e)[0] ) )
 
? H("Hello Puppies!")
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Code: Select all

' Rules for the string 's':
' - In the padding : no alphabetic characters
' - In the useful string part : any character but always starting with an alphabetic character

Dim As String s = "     *** HAPPY NEW YEAR PUPPIES! ***     "

?Lcase(Mid(s,Instr(s,Any"ABCDEFGHIJKLMNOPQRSTUVWXYZ"),1))  '' works with mixed case characters in the useful string part
?Chr(s[Instr(s,Any"ABCDEFGHIJKLMNOPQRSTUVWXYZ")-1]+32)     '' works only with upper case characters in the useful string part

Sleep
Post Reply