# Challenge of the Month - December 2012

### General discussion

• Welcome to the monthly SmallBasic Challenge!

These challenges are intended for people who are learning to program for the first time or for those returning to programming who want to start using SmallBasic.  Some will be easy, some will be hard - but they will all make you think, and more importantly be GREAT FUN!

Please post your solutions / partial solutions / questions / feedback etc. into this thread that will remain 'sticky' for the month.  The only rule is that your solution must use standard SmallBasic methods (no extensions).

It would be good if people could post their problems with these challenges so that a discussion can start so that everyone can learn from each other.

Also post feedback on the kind of challenges that you want to see more of in the future.

Text Challenge 1

Write a program to format a list of words so they are displayed in 5 vertical columns, with each column having the same indentation.

Text Challenge 2

Write a program to reverse all the words in a sentence, For example "this is my sentence" would be "sentence my is this"

Physics Challenge

Model the radioactive decay of some atoms.  They should decay into 2 parts with an average time of say 10 seconds.  The life of the atom before decay should be random with an average (half-life) of 10 seconds.

If you fancy making the challenge harder, then create a graphics visualization of the decaying atoms or read up about radioactive decay and try to use the decay formulae based on a half-life.  See Wikipedia.

Community suggestions

By Nonki

1] Draw a Christmas tree - how about with flashing lights.

2] Calculate happy numbers below 1000.  See here for details.

By Zock77

3] Write a program to calculate a random number without using Math.GetRandomNumber.

Do you have an idea for a future challenge? Please post it here!

Tuesday, December 04, 2012 7:26 PM

### All replies

• My Text Challenge 1: (My bad, it's the Text Challenge 2)

GraphicsWindow.BackgroundColor = "Cyan"
GraphicsWindow.Width = 400
GraphicsWindow.Height = 200
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 30
Controls.SetSize(tbInput,380,40)
Controls.SetSize(btnReverse,200,50)
Shapes.Move(txtOutput,10,120)
Controls.ButtonClicked = Click
Sub Click
Sentence = Controls.GetTextBoxText(tbInput)
BackwardSentence = ""
index = 1
word = ""
For i = 1 To Text.GetLength(Sentence)
letter = Text.GetSubText(Sentence,i,1)
If letter = " " Then
index = index+1
Else
word[index] = Text.Append(word[index],letter)
EndIf
EndFor
For i = index To 1 Step -1
BackwardSentence = BackwardSentence+word[i]+" "
EndFor
Shapes.SetText(txtOutput,BackwardSentence)
EndSub

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

• Edited by Friday, December 07, 2012 10:43 PM
Wednesday, December 05, 2012 4:09 AM
• @ Math Man - good answer - very short and clear code - perfect!
Wednesday, December 05, 2012 9:57 PM
• My community challenge 2 for checking happy numbers (very well commented, I might add). Import: DPH994

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

• Edited by Wednesday, December 05, 2012 11:48 PM
Wednesday, December 05, 2012 11:46 PM
• Here is my solution for Text Challenfge 1:

''=========================================
''   show words in 5 columns      textchallenge 1  december 2012
''   2012-12-07    WhTurner ---------------------------------------
''=========================================
alf="abcdefghijklmnopqrstuvwxyz"
spc="                                        "
la=text.GetLength(alf)
Nwrd=100     ''number of words
'' The random word part can be exchanged e.g. for input from file
For iw=1 To Nwrd   ''generate random words
wrd=""
code=0
nchar=4+math.GetRandomNumber(6)    '' 5 to 10 characters
For ic=1 To nchar    ''length
nc=math.GetRandomNumber(la)
cc=Text.GetSubText(alf,nc,1)
wrd=wrd+cc
If ic<=nchar Then
code=code*la+nc
EndIf
EndFor
For ic=nchar+1 To 10
code=code*la
EndFor
wrds[iw]=wrd
codes[iw]=code   ''needed for sorting
EndFor  ''iw        end of word generation
''======  sorting ==========
For i=1 To Nwrd-1
For j=i+1 To Nwrd
If codes[i]>codes[j] Then
wrd=wrds[i]
wrds[i]=wrds[j]
wrds[j]=wrd
code=codes[i]
codes[i]=codes[j]
codes[j]=code
EndIf
EndFor
EndFor
'' ========= output ============
MaxLin=Nwrd/5
For line=1 To MaxLin
For w=line To Nwrd Step MaxLin
TextWindow.Write(wrds[w])
TextWindow.Write(Text.GetSubText(spc,1,15-text.GetLength(wrds[w])))
EndFor
TextWindow.WriteLine("")
EndFor

Jan [ WhTurner ] The Netherlands

Friday, December 07, 2012 12:23 PM
• Nice!

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Friday, December 07, 2012 3:09 PM
• Text Challenge II

Import Code : MPV596

They say working hard is good but i say working smart is best...

Friday, December 07, 2012 3:10 PM
• My Text Challenge 1: (My bad, it's the Text Challenge 2)

GraphicsWindow.BackgroundColor = "Cyan"
GraphicsWindow.Width = 400
GraphicsWindow.Height = 200
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 30
Controls.SetSize(tbInput,380,40)
Controls.SetSize(btnReverse,200,50)
Shapes.Move(txtOutput,10,120)
Controls.ButtonClicked = Click
Sub Click
Sentence = Controls.GetTextBoxText(tbInput)
BackwardSentence = ""
index = 1
word = ""
For i = 1 To Text.GetLength(Sentence)
letter = Text.GetSubText(Sentence,i,1)
If letter = " " Then
index = index+1
Else
word[index] = word[index]+letter
EndIf
EndFor
For i = index To 1 Step -1
BackwardSentence = BackwardSentence+word[i]+" "
EndFor
Shapes.SetText(txtOutput,BackwardSentence)
EndSub

Hello MathMan,

As you can see in the picture there is a problem with your code possibly in the following line of code

word[index] + letter

this is a mistake often made by people including me when writing codes in Small Basic. Small Basic automatically identify the type of character and react accordingly. we can avoid this when dealing with strings by using a Library Function Text.Append(string_1,string_2).

Thanks! :)

They say working hard is good but i say working smart is best...

Friday, December 07, 2012 3:55 PM
• @ Amir CPS

I fixed that. Thanks for informing me. You said that you would be on my team for the team challenge. Maybe you could help us for the last few days?

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

• Edited by Friday, December 07, 2012 10:45 PM
Friday, December 07, 2012 10:44 PM
• Community suggestions     1] Draw a Christmas tree - how about with flashing lights.

QFB498  flashing  5patterns.

• Edited by Saturday, December 08, 2012 7:00 AM
Saturday, December 08, 2012 6:58 AM
• Pretty! :)

-Noah J. Buscher Tappisoft.com & Tappisoft.net

Saturday, December 08, 2012 7:02 AM
• @MathMan

Actually my exams are going on i really don't have much time to spend on computer...

They say working hard is good but i say working smart is best...

Saturday, December 08, 2012 2:24 PM
• @ Amir

That's too bad it had to coincide with the Team Challenge. :(

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Saturday, December 08, 2012 4:35 PM
• This is my challenge for community suggestion 1] : HMX882.

Please run this program in local.

Nonki Takahashi

Tuesday, December 11, 2012 8:36 AM
• Nice!

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Tuesday, December 11, 2012 3:05 PM
• I have an idea for the next challenge. How about programming a smart program? A kind of KI.

Greetings Timo

Tuesday, December 11, 2012 3:17 PM
• This is my challenge for community suggestion 2] : DGL589.

Nonki Takahashi

Wednesday, December 12, 2012 2:30 AM

@Timo - What sort of thing are you thinking for the 'smart program' - do you have a specific idea in mind?

Here is a starter for the physics challenge (some of the maths - Small Basic has no exponent function so have to use Power) with limited graphics, Import LJF644.

An ideas on Zock77's challenge (Random number generation) - how about using the last digits of the Clock.ElapsedMilliseconds after a call to get a Flicker image?  Are the results random? - how do you test it?

Tuesday, December 18, 2012 10:44 PM
• I have an idea for the next challenge. How about programming a smart program? A kind of KI.

Greetings Timo

Timo, I think by "KI" you meant "AI" as in Artificial Intelligence. If you really meant "KI" could you explain to us what "KI" is?

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Tuesday, December 18, 2012 11:30 PM
• Ah I am sorry. I used the german abbreviation for AI (KI is for "Künstliche Intelligenz"). I mean the AI.

@LitDev

For example in a game. Like a Breaker game. The player must fight with an AI for the victory.

Greetings Timo

Wednesday, December 19, 2012 3:53 PM
• I have some AI in this program here.

A spark to start a fire is necessary. But mainly you need dry kindling.

Wednesday, December 19, 2012 5:14 PM
• This is my challenge for community suggestion 1] : HMX882.

Please run this program in local.

Nonki Takahashi

If you keep watching Nonki's Christmas tree, something interesting rolls by. =^)

The Christmas Trees are blogged here: http://blogs.msdn.com/b/smallbasic/archive/2012/12/19/christmas-trees-small-basic-featured-programs.aspx

And Math Man's Happy Number Checker is blogged here: http://blogs.msdn.com/b/smallbasic/archive/2012/12/17/small-basic-the-happy-number-challenge.aspx

Thanks!

Ed Price (a.k.a User Ed), SQL Server Customer Program Manager (Blog, Twitter, Wiki)

Thursday, December 20, 2012 2:23 AM
• Displays a random number from 1 to 10

QLB611

A spark to start a fire is necessary. But mainly you need dry kindling.

Saturday, December 22, 2012 2:48 AM

@Timo - What sort of thing are you thinking for the 'smart program' - do you have a specific idea in mind?

Here is a starter for the physics challenge (some of the maths - Small Basic has no exponent function so have to use Power) with limited graphics, Import LJF644.

An ideas on Zock77's challenge (Random number generation) - how about using the last digits of the Clock.ElapsedMilliseconds after a call to get a Flicker image?  Are the results random? - how do you test it?

WOW that is what I just did!! I didn't notice that!

A spark to start a fire is necessary. But mainly you need dry kindling.

Saturday, December 22, 2012 2:50 AM
• Community suggestions

By Zock77

3] Write a program to calculate a random number without using Math.GetRandomNumber

• Edited by Saturday, December 22, 2012 9:24 AM a code removed
Saturday, December 22, 2012 9:08 AM
• When I run it I always get a sum of 2000 - this cannot be random if the sum is always the same.  In other word if I know the first 9, then the 10'th isn't random it must be 2000-sum(first 9).

It seems you are using EllaspsedMilliseconds as a random number and using it to select digits then find which of 10 random numbers you are going add 1 to.

Hence they sum to 2000.

The assumption is that EllaspsedMilliseconds is random.  Are the statements below true?

• If it is random, then additional processessing to get rnd is unnecessary.
• If it is not random, then additional processessing to get rnd will not make it random.
• Therefore all additional processing of EllaspsedMilliseconds to get rnd is unnecessary.

Saturday, December 22, 2012 10:51 AM
• I have tested 2000 times  if there occurs a random number from 0 to 9 on average.   So, the sum is 2000.
If random, 0 ,1 ,2,... 9   each number occurs near 200 times.

(Upper photo is the  1000 times test result.  so, each occurence number is near 100)

Look at the following code.
Clock.EllaspsedMilliseconds  returns the same value in a row.
(24 times the same value appears in my PC as described below.)
So, I thought I can not only use Clock.EllaspsedMilliseconds value  for random number.
I thought of this method(MZC192-0 ) as a way to continue serving a different value using the clock.EllaspsedMilliseconds value.

While N<30
N=N+1
rnd[N]=Clock.ElapsedMilliseconds
EndWhile
For i=1 To 30
textwindow.WriteLine(i+" : "+rnd[i])
EndFor

• Edited by Saturday, December 22, 2012 12:49 PM comment added
Saturday, December 22, 2012 12:38 PM
• So ElaspsedMilliseconds isn't random when called close together, therefore you are using a previous value to generate a random number from the non-random EllapsedMilliseconds.

m[N]=math.SquareRoot(m[N-1]*t)

So the random generation is the squareroot bit.

My suggestion was to get a Flickr between calls to ElaspsedMilliseconds. The time spent getting the Flikr should be long enough and not constant to generate random ElaspsedMilliseconds.

Slow, but may be random(ish) for the last few digits

While N<30
N=N+1
url = Flickr.GetPictureOfMoment()
rnd[N]=Clock.ElapsedMilliseconds
EndWhile
For i=1 To 30
textwindow.WriteLine(i+" : "+rnd[i])
EndFor
There are other better ways see http://en.wikipedia.org/wiki/Random_number_generation

Saturday, December 22, 2012 12:50 PM
• Thanks Litdev.

I know Random_number_generation

( for example Linear Congruential Method .)

But this time , I used above easy way.  Sorry, I could't wait Flickr return time .XD)

'r[n+1] =（a*r[n]  +  b）mod C       (Linear Congruential Method)

Saturday, December 22, 2012 1:01 PM
• Yep LCG works well.
Saturday, December 22, 2012 1:09 PM
• I have lots of ideas for challenge i will post em in the month's end.

Merry Xmas!

Saturday, December 22, 2012 7:42 PM
• Please do - post them anytime in this thread - I always have look through before posting new challenges - if you want to wait till near the time that's great too.  If possible put them all in one post so I don't miss them - also remember we are looking for a range of easy to hard challenges.
Saturday, December 22, 2012 8:06 PM
• NaochanON

The program MZC192-0 gives an unrealistic high number of repeating digits. In one run of 2000 digits I found 32 consecutive 8's This is not right.

Jan [ WhTurner ] The Netherlands

Sunday, December 23, 2012 6:00 PM
• NaochanON

The program MZC192-0 gives an unrealistic high number of repeating digits. In one run of 2000 digits I found 32 consecutive 8's This is not right.

Jan [ WhTurner ] The Netherlands

As I mentioned above, this is an easy method. (not exact mathmatical method)

Maybe your PC's speed is very high. This  method uses Clock.ellapsedmillisecond.

So, if PC's speed is higher and higer , Clock.ellapsedmillisecond value doesn't change.

The same number will be obtained.   (You'd better use more slower PC!   XD)

BTW, I tried this comparison. (2000times * 10times)

*  Math.GetRandomNumber is not a good function??????

1) Math.GetRandomNumber using method       LBX755

Min:1928 Max:2128 (2128-2000)/2000*100=6.4%

2) Modified    Clock.ellapsedmillisecond using method  MZC192-1

Min:1952  Max:2062  (2062-2000)/2000=3.1%

Monday, December 24, 2012 2:01 AM
• My ideas for a future challenge:

1. Show a calendar.
2. Draw a picture of any zodiac (star) sign.

Nonki Takahashi

Friday, December 28, 2012 5:09 PM
• Ideas for Challenge

Challenge 1 (Cryptography):

Decode a secret message that has been encoded as follows:
1) all letters are upper case
2) a space is indicated by an underscore (_)
3) a number is preceded by the number symbol (#)
4) all other non-letter characters are unchanged
5) each letter was converted to a number that is the sum of its base number and its position in the word
6) for the base number, A=1, B=2, C=3, etc.

Sample Input:
14 21 13 2 5 18 _ # 2 _ 16 5 14 3 9 12
1 16 18 9 12 _ # 28 , _ # 2001
16 18 15 2 12 5 13 _ # 9 . # 1

Sample Output:
NUMBER 2 PENCIL
APRIL 28, 2001
PROBLEM 9.1

Challenge 2 (Math):

Convert a number in decimal form to the equivalent mixed number with the fraction portion reduced to lowest terms.

Input: any number such as 1.5, 2.65 etc.

Output: Output the integer part of the number, followed by the word AND where the decimal point was, and then the reduced fraction. There is to be a division symbol (/) between the numerator and the denominator of the fraction.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: The numbers are in the range 1..500. There are no repeating decimals.

Sample Input:
2.95

14.2

5.625

Sample Output:
2 AND 19/20
14 AND 1/5
5 AND 5/8

Challenge 3 (Math) Hard :

Write a program to convert Roman numerals into Arabic numerals and vice versa.

Challenge 3 (Text Processing ??) :

Read a string and determine whether each left parenthesis ‘(‘ has a matching right parenthesis ‘)’.

Output: Output either MATCH or NO MATCH.
The output is to be formatted exactly like that for the sample output given below.

Assumptions:
( and ) are the only enclosure symbols in the string.

The left parenthesis must come before the right parenthesis in order to count as a matched set of parentheses.

Sample Input:
(3 + (7 * 2) – 6)
HELLO AND (WELCOME (TO THE) SB (FORUM)
TODAY) IS ((SATURDAY())

Sample Output:
MATCH
NO MATCH
NO MATCH

Challenge 4 (Spell Checker) :

Given a pair of words (the first is the correct spelling and the second is the contestant’s spelling of the word) determine if the word is spelled correctly.

The degree of correctness is as follows:
CORRECT if it is an exact match
ALMOST CORRECT if no more than 2 letters are wrong
WRONG if 3 or more letters are wrong.

The output is to be formatted exactly like that for the sample output given below.

Sample Input:
SAMPLE
SIMPLE
THEIR
THEIR
WINDMILL
WINDOWS

Sample Output:
SIMPLE IS ALMOST CORRECT
THEIR IS CORRECT
WINDOWS IS WRONG

Challenge 5 (Math) Hard :

The 8-Queens problem in chess is to place 8 queens on a chess board such that none of the queens is threatening any of the others. A chess board is an 8 x 8 matrix of squares and queens may move any direction along a row, column or diagonal. The problem is to input the 8 columns of the queens on the rows of a chess board, with 1 being the first column and 8 being the last, e.g. 1 2 3 4 5 6 7 8 means the queens are along the diagonal, which would not be a valid solution.

Example 1:

Enter board configuration: 3 5 2 8 1 7 4 6
This is a valid configuration.

Example 2:
Enter board configuration: 1 8 2 5 3 7 4 6
This is NOT a valid configuration.

You can draw a chess board and show the result on graphic window if you want some more complexity.

Merry Xmas!

Saturday, December 29, 2012 7:50 PM
• My another idea for a future challenge: Write a quine.

Nonki Takahashi

Tuesday, January 01, 2013 2:13 PM
• I couldn't resist doing the quine. Import: HRC429

It does need to be saved with the name "Quine.sb" before it will work, though. It creates a duplicate "Quine.sb" in the sub-folder "Copy".

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

• Edited by Tuesday, January 01, 2013 10:49 PM
Tuesday, January 01, 2013 9:23 PM