Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Learn Programming While Assembling an On-Screen Christmas Tree
Programming

Learn Programming While Assembling an On-Screen Christmas Tree

Last Updated on January 6, 2023 by Editorial Team

Author(s): Sumudu Tennakoon

Programming

A programming exercise for holiday learning and fun using Python and Julia with a practical comparison between the two languages.

This article is to share some holiday joy with an educational value to computer programming enthusiasts and young learners.

The Output

The expected output of the Holiday Greeting Message β€œMerry Christmas and Happy HolidaysΒ !”
The expected output of the Holiday Greeting Message. (Image byΒ author)

Goal of the Excercise

To create a Holiday Greeting Card to display in the console/terminal with

  • An illustration of a decorated Christmas Tree using characters.
  • Randomly distributed Ornaments on the treeΒ body.
  • a star at the top of the tree as the tree-topper.
  • A border ribbon at the bottom of the treeΒ body.
  • A base is attached to theΒ tree.
  • Greeting message β€œMERRY CHRISTMAS AND HAPPY HOLIDAYS”
  • Colors: red star green tree, a blue border at the bottom, magenta base, and the message inΒ red.

Concept andΒ Logic

  • Use screen print function to output characters to the screen systematically offsetting the placement on the printΒ line.
  • To get the triangular shape of the tree body, the number of characters at the top should be one(1). To keep one character at the center, subsequent lines should have an odd number of characters such asΒ 3,5,7…..
  • The printed characters should be center aligned to the treewidth. Blank (space: β€˜ β€˜)characters should be used as padding before the first printed character. How many padding characters need to add to each line should be calculated.
  • One character will be chosen as tree leaves (e.g. ^). Three different shapes (characters) of ornaments will be added to the tree (e.g. o, @, +). The position of each ornament is decided based on a random number between 1 and the size of the printed characters of that line. If the location of the character is the same as the random number associated with each ornament the tree leaves character should be replaced by the ornament character. This random number is re-generated every time before adding a new character to theΒ line.
  • Characters of each line (string) should be printed before assembling the nextΒ line.
  • The last line of the tree body (end of the leaves part) should be a ribbon represented by # character in theΒ output.
  • The lines for the tree base should be assembled using a pair of character IΒ (II).
  • A ground line (string) with the length of treewidth should be added after theΒ base.
  • A string containing the greeting message β€œMERRY CHRISTMAS AND HAPPY HOLIDAYSΒ !” should be printed as threeΒ lines.
  • Another string similar to the ground line with the length of treewidth will be added after theΒ message.

When applying the colors, designated character colors should be assigned to each print statement above.

What you need to know/What you willΒ learn

By working through this exercise, you will learn or practice your skillsΒ on,

  1. How to print screen output with different options.
  2. Variables and Value Assignment
  3. Strings and String Concatenation
  4. Generate and use a sequence ofΒ numbers.
  5. Operators
  6. Loops (for…)
  7. Conditional Statements (if…else)
  8. Use of built-in functions (print, randomΒ number)
  9. Adding colors to the screen outputΒ text
  10. How to use external libraries/packages

In the next section, we will construct a code step by step to generate the desired output using two different programming languages, Python and Julia. As a Programming enthusiast/learner you are encouraged to focus more on the concept, logic, and algorithm than the programming language-specific syntax orΒ style.

Let’s Start Building the Christmas Tree

Let’s learn how to build the tree using both Python and Julia side by side. Please go to the end of the article to see the fullΒ code.

Task 1: Setup Tree Dimensions

Create variables to store width, body height, and full height with the base. These variables will be used later to decide the character placements.

Note that in Julia we used semicolon (;) at the end of the lines to suppress the output being printed. Although, it is optional to haveΒ ;.

(Image byΒ author)

Task 2: Print Basic TreeΒ Body

the variables center and padding is used to center align the string beingΒ printed.

Looping and Sequence Generator

Python range(start, end, step) is used to seed the values of x and y to iterate through the loop. When the step value is not provided python assumes it asΒ 1.

Similarly in Julia start:step:end used in the for aΒ loop.

Here, the value of x varies from 1 to the width of the tree. The step/increment of 2 is used to get only the oddΒ numbers.

String Concatenation and multiplication

In Python, two strings can concatenate using + and multiple copies of the same string can be concatenated other using * with an integer value as a multiplication factor ("a" + "b" yields "ab". whereas "a"*3 yieldsΒ "aaa").

In Julia, two strings can concatenate using * and multiple copies of the same string can be concatenated other using ^ with an integer value as a multiplication factor ("a" * "b" yields "ab". whereas "a"^3 yieldsΒ "aaa").

Comparisons

Both python and Julia share the same syntax for comparison operators where x < 25 returns true if x is less than 25 and x > 25 returns true if x is greater than 25. Similarly x == 25 is true only if the value of x isΒ 25.

In Python boolean values are True and False. In Julia it is true and false Note the small case letter inΒ Julia.

The output of the task 2. (Image byΒ author)

Task 3: Add bottom border ribbon, base, andΒ Ground

Logical operators

Python has logical operators and and or, whereas Julia use && and || as logical operators. The expression a and b in python is equivalent to a && b in Julia and returns the value true only if both the a and b get the valueΒ true.

The output of the task 3. (Image byΒ author)

Task 4: Add Ornaments

To generate a random number between given integer values, you have to import random to the Python code. The function randint(start,end) will generate a random integer between the given integer values start andΒ end.

Julia has the function rand(start,end) works similar to the randint function inΒ Python.

The output of the task 4. (Image byΒ author)

Task 5: Add GreetingΒ Message

Add three print lines "MERRY CHRISTMAS"Β , "AND", and "HAPPY HOLIDAYSΒ !" to complete the Greeting.

The output of the task 5. (Image byΒ author)

Task 6:

Add colors to strings. In python, coloram a library can be used to set the color of the string being printed on the console. Julia has Crayons a package that can use to generate colored and styledΒ strings.

The output of the task 6. (Image byΒ author)

What’s Next?

If you completed all 6 tasks above, Congratulations! You have completed building a programmatically Generated Holiday Greeting using both Python andΒ Julia.

You can also add your own creativity to the output e.g. Changing the colors and characters, Adding more Ornaments, Assigning different colors for ornaments, varying the color of the leaves, adding some animation, etc.

Adding colors to each character

In Python, you can use the end parameter in the print() function. print(Fore.RED + β€˜*’, end=’’). This will eliminate adding a new line ('n')character to each string beingΒ printed.

In Julia, you can use print() function instead of println(). Julia functionprint() does not add a new line ('n')character to each string beingΒ printed.

Christmas tree after adding colors to the ornaments. (Image byΒ author)

The completeΒ code

Python

Julia

The notebooks can also be downloaded from GitHub using the linksΒ below.

Even this exercise is completed using Python and Julia, you can try it using your favorite programming language utilizing the same concept andΒ logic.

Disclaimer

The views and opinions expressed in this paper are those of the author and do not represent those of the employer or other institutions related to the author. This article is part of a broader publication aimed at addressing data literacy in the community. The author has put a good amount of effort into researching the topics discussed, simplifying the technical jargon to increase the understanding of the content, finding relevant references to ensure the validity of the facts presented. Discussion, criticism, alternative thoughts, and suggestions areΒ welcome.

References


Learn Programming While Assembling an On-Screen Christmas Tree was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓