Posts

Showing posts from June, 2018

Printing Patterns with the For Loop - Part One

Image
(A)              1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 (B) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 (C) 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 (D)   1  2 2 3 3 3  2 2   1 (E) 1  2  3 4  5  6 7  8  9     Printing patterns is often part of the school exercises you should do when you are doing your introductory programming course. It helps you learn how to apply your brains as a programmer. Whilst many examples of how to print patterns exist online and in books, not many of them explain the step by step thinking process involved to come up with such examples. It is the aim of this post to explain how to approach problems. It is often easy for an experienced programmer to mentally figure out the whole algorithm  needed to solve a given a solution even before they start writing the code. But it is usually not so with beginners. So how...

Printing a Calendar

I was impressed by how you can easily print a calendar in python and it looks pretty. I decided to write my own implementation in Groovy. Below is the code. def getMonthStart (date){ new Date (date. year , date. month , 1 ) } def getMonthEnd (date){ getMonthStart( new Date(date. year , date. month , 27 ) + 6 ) - 1 } def getCalendar (year, month){ getCalendar(year, month, 1 ) } def getCalendar (year, month, day){ getCalendar( new Date(year - 1900 , month - 1 , day)) } def getCalendar (d){ def l = [] def today = new Date(). date def header = d. format ( 'MMMM, Y' ) l << header. center ( 28 ) << '\n' [ 'Su' , 'Mo' , 'Tu' , 'We' , 'Th' , 'Fr' , 'Sa' ]. each { l << " $it " } def start = getMonthStart(d) if (start. day != 0 ) l << ...

Ba Be Bi Bo Bu

One day I decided to start teaching my daughter how to read. So I decided to prepare a sheet of the phonetics based on the English alphabet. Here is the Groovy code I wrote: vowels = 'aeiou' . toList () ( 'A' .. 'Z' ). each {letter -> if (!vowels*. toUpperCase (). contains (letter)){ vowels. each { vowel -> print "$letter$vowel\t" } println "" } } And here is the output: Ba Be Bi Bo Bu Ca Ce Ci Co Cu Da De Di Do Du Fa Fe Fi Fo Fu Ga Ge Gi Go Gu Ha He Hi Ho Hu Ja Je Ji Jo Ju Ka Ke Ki Ko Ku La Le Li Lo Lu Ma Me Mi Mo Mu Na Ne Ni No Nu Pa Pe Pi Po Pu Qa Qe Qi Qo Qu Ra Re Ri Ro Ru Sa Se Si So Su Ta Te Ti To Tu Va Ve Vi Vo Vu Wa We Wi Wo Wu Xa Xe Xi Xo Xu Ya Ye Yi Yo Yu Za Ze Zi Zo Zu Let me get you feedback. :)

99 Bottles of Beer

I don't drink beer but the song "99 Bottles of Beer" is an interesting one to program. Here is the source code in Groovy: def bottles (n){ if (n == 1 ) "1 bottle" else if (n == 0 ) "no more bottles" else "${n} bottles" ; } for (x in ( 99 .. 0 )) { println "${bottles(x).replaceFirst(" n ", " N ")} of beer on the wall, ${bottles(x)} of beer." if (x!= 0 ) println "Take one down and pass it around, ${bottles(x-1)} of beer on the wall." else println "Go to the store and buy some more, 99 bottles of beer on the wall." } And here is the output showing the song:          99 bottles of beer on the wall, 99 bottles of beer.             Take one down and pass it around, 98 bottles of beer on the wall.             98 bottles of beer on the wall, 98 bottles of beer.           ...