I started programming when I was 10 years old by reading and modifying simple programs that my older friends from high school wrote as homework. They were written in Pascal, a simple programming language designed in the end of the `60s, intended to teach students structured programming. After a couple of days of tinkering, I talked to my friend Sergiu about programming. He gave me a book for beginners called “The little programmer”. The book explained basic concepts like variables, control flow, loops and arrays. It was filled with exercises that helped me a lot. My favourite ones where drawing with text.
The goal of these exercises is to practice and understand loops in a more visual way. The exercises vary in difficulty from easy to hard (like the circle). For each exercise there will be a playground with a solution. When opening a solution remember to show the Assistant Editor in Xcode in order to see the console output.
Feel free to post your solutions in the comments or share other similar exercises.
Square
Given an integer N
draw a square of N x N
asterisks, like the ones below:
N = 1
*
N = 2
**
**
N = 3
***
***
***
Twists
- draw an empty square
N = 3
***
* *
***
N = 4
****
* *
* *
****
- draw an empty square, but instead of asterisks, use
+
for corners,-
for the top and bottom sides and|
for the left and right ones
N = 2
++
++
N = 4
+--+
| |
| |
+--+
Rectangle
Given two integers N
and M
draw a rectangle of N x M
asterisks, like the ones below:
N = 1 M = 3
***
N = 2 M = 2
**
**
N = 3 M = 7
*******
*******
*******
Twists
- draw an empty rectangle
N = 3 M = 4
****
* *
****
N = 4 M = 7
*******
* *
* *
*******
- draw an empty rectangle, but instead of asterisks, use
+
for corners,-
for the top and bottom sides and|
for the left and right ones
N = 2 M = 3
+-+
+-+
N = 4 M = 3
+-+
| |
| |
+-+
Triangle
Given an integer N
draw a triangle of asterisks. The triangle should have N
lines, the i-th line should have i asterisks on it.
N = 1
*
N = 3
*
**
***
N = 4
*
**
***
****
Twists
- draw an empty triangle
N = 3
*
**
***
N = 4
*
**
* *
****
N = 5
*
**
* *
* *
*****
- draw an empty triangle, but instead of asterisks, use
+
for corners,-
for the bottom side,|
for the left side and\
for the right side
N = 2
+
++
N = 4
+
|\
| \
+--+
N = 5
+
|\
| \
| \
+---+
Pyramid
Given an integer N
draw a pyramid of asterisks, like the ones below:
N = 1
*
N = 2
*
***
N = 3
*
***
*****
N = 4
*
***
*****
*******
Rhombus
Given an integer N
draw a rhombus of asterisks, like the ones below:
N = 1
*
N = 2
*
***
*
N = 3
*
***
*****
***
*
N = 4
*
***
*****
*******
*****
***
*
Aztec Pyramid
Given an integer N
draw a Aztec pyramid of asterisks, like the ones below:
N = 1
**
**
N = 2
**
**
******
******
N = 3
**
**
******
******
**********
**********
Chess Board
Given an integer N
draw a chess board of size N x N
. Each line of the chess board should have spaces and number signs(#) alternating. A space represents a white cell and the number sign a black one. The chess board should be bordered using +
, -
and |
like in the examples below:
N = 1
+-+
|#|
+-+
N = 3
+---+
|# #|
| # |
|# #|
+---+
N = 5
+-----+
|# # #|
| # # |
|# # #|
| # # |
|# # #|
+-----+
N = 8
+--------+
|# # # # |
| # # # #|
|# # # # |
| # # # #|
|# # # # |
| # # # #|
|# # # # |
| # # # #|
+--------+
Circle
Given an integer R
draw a circle of asterisks that has the radius equal to R
units. For simplicity reasons assume that a character(space or asterisk) has the height 1 unit and width 0.5 units, in other words a rectangle of size N x 2N
looks like a square.
Radius = 2
*
*******
*********
*******
*
Radius = 5
*
*************
*****************
*******************
*******************
*********************
*******************
*******************
*****************
*************
*
Radius = 10
*
*****************
*************************
*****************************
*********************************
***********************************
*************************************
***************************************
***************************************
***************************************
*****************************************
***************************************
***************************************
***************************************
*************************************
***********************************
*********************************
*****************************
*************************
*****************
*
Sine
Plot the sine function
Your output should look like something like this:
****** *****
* * * **
* * * *
* *
* *
* * * *
* *
*
* *
*
* * *
* * * *
* * * *
*
* * *
* * *
*
* * * *
* *
* *
* * * *
* * * *
****** ******
If you found this useful please take a moment and share it with your friends!
Great article but could you please post the solutions as pastebin code links instead of complete playground files? Makes it tedious to download and open a separate file for each solution.
Thanks!
https://github.com/WeHeartSwift/DrawingWithText/blob/master/all_solutions.swift
Thanks Andrei!
I did something very similar when I started programming in c, I made sine, but left to right, up to down lol
Very Nice Tutorial!
Wow!
That make sense. In the beginning I thought that looks easy. But than i started.
Quite tricky and makes fun. A good reason to spent little Dollars for more.