move: arrays retro clock and slices

This commit is contained in:
Inanc Gumus
2019-01-25 14:28:36 +03:00
parent 5d9bf4195d
commit 54b402c610
51 changed files with 15 additions and 15 deletions

View File

@ -0,0 +1,43 @@
# GOAL 1: Printing the Digits
## Challenge Steps
1. Define a new placeholder type
2. Create the digits from "zero" to "nine"
You can use these characters for the clock:
```
Digit character : █
Separator character : ░
```
1. Put them into the "digits" array
2. Print the digits side-by-side
1. Loop for all the lines in a digit
2. Print each digit line by line
3. Don't forget printing a space after each digit
4. Don't forget printing a newline after each line
EXAMPLE: Let's say you want to print 10.
```
██ ███ <--- Print a new line after printing a single line from
█ █ █ all the digits.
█ █ █
█ █ █
███ ███
^^
||
++----> Add space between the digits
```
## Solution
You can find the solution in the solution folder.

View File

@ -0,0 +1,11 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
func main() {
}

View File

@ -0,0 +1,126 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
)
func main() {
// for keeping things easy to read and type-safe
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
// This array's type is "like": [10][5]string
//
// However:
// + "placeholder" is not equal to [5]string in type-wise.
// + Because: "placeholder" is a defined type, which is different
// from [5]string type.
// + [5]string is an unnamed type.
// + placeholder is a named type.
// + The underlying type of [5]string and placeholder is the same:
// [5]string
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
// Explanation: digits[0]
// + Each element of clock has the same length.
// + So: Getting the length of only one element is OK.
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
//
// The range clause below is ~equal to the following code:
// line := 0; line < 5; line++
for line := range digits[0] {
// Print a line for each placeholder in digits
for digit := range digits {
fmt.Print(digits[digit][line], " ")
}
fmt.Println()
}
}

View File

@ -0,0 +1,20 @@
# GOAL 2: Printing the Clock
## Notes
* Note main.go file contains the solution of the previous step.
* "solution" folder contains the solution for this step.
## Challenge Steps
1. Get the current time
2. Get the current hour, minute and second from the current time
3. Create the clock array by getting the digits from the digits array
4. Print the clock by using the clock array
5. Create a separator array (it's also a placeholder type)
6. Add the separators into the correct positions of the clock array

View File

@ -0,0 +1,107 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
for line := range digits[0] {
for digit := range digits {
fmt.Print(digits[digit][line], " ")
}
fmt.Println()
}
}

View File

@ -0,0 +1,131 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
fmt.Printf("hour: %d, min: %d, sec: %d\n", hour, min, sec)
// [8][5]string
clock := [...]placeholder{
// extract the digits: 17 becomes, 1 and 7 respectively
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
}

View File

@ -0,0 +1,56 @@
# GOAL 3: Animate the Clock
## Notes
* Note main.go file contains the solution of the previous step.
* "solution" folder contains the solution for this step.
## Challenge Steps
1. Create an infinite loop to update the clock
2. Update the clock every second
[time.Sleep(time.Second)](https://golang.org/pkg/time/#Sleep) will stop the world for 1 second
3. Clear the screen before the infinite loop
1. Get my library for clearing the screen:
`go get -u github.com/inancgumus/screen`
2. Then, import it and call it in your code like this:
`screen.Clear()`
3. If you're using Go Playground instead, do this:
`fmt.Println("\f")`
4. Move the cursor to the top-left corner of the screen, before each step
of the infinite loop
* Call this in your code like this:
`screen.MoveTopLeft()`
* If you're using Go Playground instead, do this again:
`fmt.Println("\f")`
---
## SIDE NOTE FOR THE CURIOUS
If you're curious about how my screen clearing package works, read on.
**On bash**, it uses special commands, if you open the code, you can see that.
* `\033` is a special control code:
* `[2J` clears the screen and the cursor
* `[H` moves the cursor to 0, 0 screen position
* [See for more information](https://bluesock.org/~willkg/dev/ansi.html).
**On Windows**, I'm directly calling the Windows API functions. This is way advanced at this stage of the course, however, I'll probably explain it afterward.
So, my package automatically adjusts itself depending on where it is compiled. On Windows, it uses the special Windows API calls; On other operating systems, it uses the bash special commands that I've explained above.

View File

@ -0,0 +1,131 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
fmt.Printf("hour: %d, min: %d, sec: %d\n", hour, min, sec)
// [8][5]string
clock := [...]placeholder{
// extract the digits: 17 becomes, 1 and 7 respectively
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
}

View File

@ -0,0 +1,144 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
// For Go Playground, do not use this.
screen.Clear()
// Go Playground will not run an infinite loop.
// So, instead, you may loop for 1000 times:
// for i := 0; i < 1000; i++ {
for {
// For Go Playground, use this instead:
// fmt.Print("\f")
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
// pause for 1 second
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,25 @@
# GOAL 4: Blinking the Separators
## Notes
* Note main.go file contains the solution of the previous step.
* "solution" folder contains the solution for this step.
## Challenge Steps
Separators should be visible once in every two seconds.
### Example:
* 1st second: They're invisible
* 2nd second: visible
* 3rd second: invisible
* 4th second: visible
### HINT
There are two ways to do this:
1. Manipulating the clock array directly (by adding/removing the separators)
2. Or: Deciding what placeholders to print when printing the clock

View File

@ -0,0 +1,137 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,142 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,178 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
// for keeping things easy to read and type-safe
type placeholder [5]string
// put the digits (placeholders) into variables
// using the placeholder array type above
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
// This array's type is "like": [10][5]string
//
// However:
// + "placeholder" is not equal to [5]string in type-wise.
// + Because: "placeholder" is a defined type, which is different
// from [5]string type.
// + [5]string is an unnamed type.
// + placeholder is a named type.
// + The underlying type of [5]string and placeholder is the same:
// [5]string
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
// For Go Playground, do not use this.
screen.Clear()
// Go Playground will not run an infinite loop.
// Loop for example 1000 times instead, like this:
// for i := 0; i < 1000; i++ { ... }
for {
// For Go Playground, use this instead:
// fmt.Print("\f")
screen.MoveTopLeft()
// get the current hour, minute and second
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
// extract the digits: 17 becomes, 1 and 7 respectively
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
// Explanation: clock[0]
// + Each element of clock has the same length.
// + So: Getting the length of only one element is OK.
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
//
// The range clause below is ~equal to the following code:
// line := 0; line < len(clock[0]); line++
for line := range clock[0] {
// Print a line for each placeholder in clock
for index, digit := range clock {
// Colon blink on every two seconds.
// + On each sec divisible by two, prints an empty line
// + Otherwise: prints the current pixel
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
// Print the next line and,
// give it enough space for the next placeholder
fmt.Print(next, " ")
}
// After each line of a placeholder, print a newline
fmt.Println()
}
// pause for 1 second
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,36 @@
## NOTES
- I've explained the challenges and the solutions step by step
- So, if you get stuck, you can check out the next step without having to look at the entire final solution
- You can find the explanations and solutions by clicking on the header links below
## GET SOCIAL
- Click the link below to tweet your solution when you complete: http://bit.ly/GOTWEET-CLOCK
- Discuss your solution with other gophers here: http://bit.ly/LEARNGOSLACK
---
## GOALS:
### 👉 [STEP #1 — Print the Digits](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/01-printing-the-digits/)
- [ ] Define a new placeholder type
- [ ] Create the digit arrays from 0 to 9
- [ ] Put them into the "digits" array
- [ ] Print them side-by-side
### 👉 [STEP #2 — Print the Clock](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/02-printing-the-clock/)
- [ ] Get the current time
- [ ] Create the clock array
- [ ] Print the clock
- [ ] Add the separators
### 👉 [STEP #3 — Animate the Clock](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/03-animating-the-clock/)
- [ ] Create an infinite loop to update the clock
- [ ] Update the clock every second
- [ ] Clear the screen before the infinite loop
- [ ] Move the cursor to the top-left corner of the screen before each
step of the infinite loop
### 👉 [BONUS: STEP #4 — Blink the Clock](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/04-blinking-the-separators/)
- [ ] Blink the separators
### 👉 [FULL ANNOTATED SOLUTION](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/05-full-annotated-solution/main.go)

View File

@ -0,0 +1,173 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// ---------------------------------------------------------
// EXERCISE: Refactor
//
// Goal is refactoring the clock project by moving some of its parts to
// a new file in the same package.
//
// 1. Create a new file: placeholders.go
// 2. Move the placeholder type to placeholder.go
// 3. Move all the placeholders (zero to nine and the colon) to placeholder.go
// 4. Move the digits array to placeholders.go
//
// HINT
// + placeholders.go file should belong to main package as well
//
// To remember how to do so: Rewatch the "PART I — Packages, Scopes and Importing"
// section.
//
// + Short declaration won't work in the package scope.
// Remember why by watching: "Short Declaration: Package Scope" lecture
//
// + If you receive the following error and you don't know what to do watch:
// "Packages - Learn how to run multiple files" lecture.
//
// # command-line-arguments
// undefined: placeholder
// undefined: colon
//
// EXPECTED OUTPUT
// The same output before the refactoring.
// ---------------------------------------------------------
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,48 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,95 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,87 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// ---------------------------------------------------------
// EXERCISE: Set an Alarm
//
// Goal is printing " ALARM! " every 10 seconds.
//
// EXPECTED OUTPUT
//
// ██ ███ ███ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █
// █ ███ ███ █ ███ ███
// █ █ ░ █ █ ░ █ █
// ███ ███ ███ ███ ███ ███
//
// ███ █ ███ ██ █ █ █
// █ █ █ █ █ █ █ ███ █
// ███ █ ███ ██ █ █ █
// █ █ █ █ █ █ █ █ █
// █ █ ███ █ █ █ █ █ █ █
//
// ██ ███ ███ ██ █ █ ██
// █ █ ░ █ █ ░ █ █ █
// █ ███ ███ █ ███ █
// █ █ ░ █ █ ░ █ █
// ███ ███ ███ ███ █ ███
//
// HINTS
//
// <<< BEWARE THE SPOILER! >>>
//
// I recommend you to first solve the exercise yourself before reading the
// following hint.
//
//
// + You can create a new array named alarm with the same length of the
// clocks array, so you can copy the alarm array to the clocks array
// every 10 seconds.
//
// ---------------------------------------------------------
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,95 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,53 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
alarmed := sec%10 == 0
for line := range clock[0] {
if alarmed {
clock = alarm
}
for index, digit := range clock {
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,154 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var alarm = [...]placeholder{
{
" ",
" ",
" ",
" ",
" ",
},
{
"███",
"█ █",
"███",
"█ █",
"█ █",
},
{
"█ ",
"█ ",
"█ ",
"█ ",
"███",
},
{
"███",
"█ █",
"███",
"█ █",
"█ █",
},
{
"██ ",
"█ █",
"██ ",
"█ █",
"█ █",
},
{
"█ █",
"███",
"█ █",
"█ █",
"█ █",
},
{
" █ ",
" █ ",
" █ ",
" ",
" █ ",
},
{
" ",
" ",
" ",
" ",
" ",
},
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,147 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// ---------------------------------------------------------
// EXERCISE: Add Split Seconds
//
// Your goal is adding the split second to the clock. A split second is
// 1/10th of a second.
//
// 1. Find the current split second
// 2. Add dot character to the clock (as in the expected output)
// 3. Add the split second digit to the clock
// 4. Blink the dot every two seconds (just like the separators)
// 5. Update the clock every 1/10th of a second, instead of every second.
// (Update the clock every 100 millliseconds)
//
// HINTS
// + You can find the split second using Nanosecond method of the Time type.
// https://golang.org/pkg/time/#Time.Nanosecond
//
// + A split second is the first digit of the Nanosecond.
//
// + Remember: time.Second is an integer constant, so it can be divided
// with a number.
//
// https://golang.org/pkg/time/#Time.Second
//
// EXPECTED OUTPUT
// Note that, clock is updated every split second instead of a second.
//
// Separators are displayed (second is an odd number):
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █ █
// █ █ ███ █ █ █ █ █
// █ █ ░ █ █ ░ █ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ ██
// █ █ ░ █ █ ░ █ █ █
// █ █ ███ █ █ █ █
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ █ █
// █ █ ░ █ █ ░ █ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ █
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █
// █ █ ███ █ █ █ █
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ █
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ ░ █ █ ░ █ █ █ █
// █ █ ███ █ █ █ ███
// █ █ ░ █ █ ░ █ █ █
// ███ ███ ███ ███ ███ █ ░ ███
//
// Separators are not displayed (second is an even number):
//
// ██ ██ ███ ██ ██ ███ ███
// █ █ █ █ █ █ █ █ █
// █ █ ███ █ █ ███ █ █
// █ █ █ █ █ █ █ █ █
// ███ ███ ███ ███ ███ ███ ███
//
// ---------------------------------------------------------
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,95 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,51 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
ssec := now.Nanosecond() / 1e8
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
dot,
digits[ssec],
}
for line := range clock[0] {
for index, digit := range clock {
next := clock[index][line]
if (digit == colon || digit == dot) && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
const splitSecond = time.Second / 10
time.Sleep(splitSecond)
}
}

View File

@ -0,0 +1,103 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var dot = placeholder{
" ",
" ",
" ",
" ",
" ░ ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,113 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// ---------------------------------------------------------
// EXERCISE: Ticker: Slide the Clock
//
// Your goal is slide the placeholders every second.
// Please run the solution to see it in action.
//
//
// THIS IS A HARD EXERCISE:
// + It will take you days but it will worth it.
// + For experienced developers, this can take an hour or so.
//
//
// 1. You need to determine the starting and the ending digits to create
// the sliding effect.
//
//
// 2. Each second, start from the next placeholder, skip the previous one.
// This means: Only draw the next placeholders.
//
// Like this:
//
// 12:40:31
// 2:40:31
// 40:31
// 0:31
// :31
// 31
// 1
//
//
// 3. After the last placeholder is displayed, fill the lines for the missing
// placeholders, and then start from the first placeholder. Draw it to the
// right part of the screen.
//
// Like this:
//
// 12:40:31
// 2:40:31
// 40:31
// 0:31
// :31
// 31
// 1
// 1
// 12
// 12:
// 12:4
// 12:40
// 12:40:
// 12:40:3
// 12:40:31
//
// As you can see, you need to draw the clock from the right part of the
// screen, beginning from the first placeholder.
//
//
// HINTS
// + You would need to clear the screen inside the loop instead of once.
// Otherwise the previous placeholders will be left on the screen.
//
//
// EXPECTED OUTPUT
// Please run the solution to see it in action. Do not look at the source-code
// though.
// ---------------------------------------------------------
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,95 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,86 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
for shift := 0; ; shift++ {
// we need to clear the screen here.
// or the previous character will be left on the screen
//
// alternative: you can fill the rest of the missing placeholders
// with empty lines
screen.Clear()
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
l := len(clock)
// this sets the beginning and the ending placeholder positions (indexes).
// shift%l prevents the indexing error.
s, e := shift%l, l
// to slide the placeholders from the right part of the screen.
//
// here, we assume that as if the clock's length is double of its length.
// this makes things easy to manage: that's why: l*2 is there.
//
// shift is always increasing, for it's to go beyond the clock's length,
// it should be equal or greater than l*2, right (after the remainder of course)?
//
// so, if the clock goes beyond its length; this code detects that,
// and resets the starting position to the first placeholder's index,
// and it keeps doing so until the clock is fully displayed again.
if shift%(l*2) >= l {
s, e = 0, s
}
// print empty lines for the missing place holders.
// this creates the effect of moving placeholders from right to left.
//
// l-e can only be non-zero when the above if statement runs.
// otherwise, l-e is always zero, because l == e.
//
// this is one of the other benefits of assuming the length of the
// clock as the double of its length. otherwise, l-e would always be 0.
for j := 0; j < l-e; j++ {
fmt.Print(" ")
}
// draw the digits starting from 's' to 'e'
for i := s; i < e; i++ {
next := clock[i][line]
if clock[i] == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,95 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}

View File

@ -0,0 +1,21 @@
# Exercises
These exercises will reinforce your knowledge of manipulating arrays. You will prove yourself that you can write easy to maintain Go programs.
1. **[Refactor](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/exercises/01-refactor)**
In this exercise, you will refactor the project to multiple files by moving
all the placeholders. This will make the project easy to maintain down the road.
2. **[Set an Alarm](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/exercises/02-alarm)**
You will print " ALARM! " every 10 seconds.
3. **[Split Second](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/exercises/03-split-second)**
You will display the split second in the clock, you will need to update the
clock every 1/10th of a second instead of every second.
4. **[Ticker](https://github.com/inancgumus/learngo/tree/master/14-arrays/13-project-clock/exercises/04-ticker)**
This is an HARD EXERCISE. You will slide the clock animation from right-to-left. After this exercise, you will truly feel that you've mastered everything you've learned so far.