FreeBASIC syntax challenge games

For other topics related to the FreeBASIC project or its community.
fxm
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 09, 2017 8:44

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

Re: FreeBASIC syntax challenge games

Postby dodicat » Jan 09, 2017 11:58

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/viewtopic.php?f=17&t=25257&p=227584#p227584
fxm
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 09, 2017 13:07

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
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 09, 2017 13:13

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: 3774
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Postby dodicat » Jan 09, 2017 13:16

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

Re: FreeBASIC syntax challenge games

Postby Tourist Trap » Jan 09, 2017 14:47

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
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 10, 2017 12:17

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: 3774
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Postby dodicat » Jan 10, 2017 15:36

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
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 10, 2017 15:43

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

Re: FreeBASIC syntax challenge games

Postby dodicat » Jan 10, 2017 20:38

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
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 10, 2017 21:09

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: 1957
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Postby Tourist Trap » Jan 10, 2017 21:21

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
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 10, 2017 21:41

More simply:

Code: Select all

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

Re: FreeBASIC syntax challenge games

Postby Tourist Trap » Jan 10, 2017 22:01

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
Posts: 6711
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: FreeBASIC syntax challenge games

Postby fxm » Jan 10, 2017 22:55

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

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest