Golang. Fisher-Yates algorithm

The user enters any number, letter, word, or whatever he wants, separated by a space. Let’s say he even enters card values (poker hand), such as A 3 J Q 10. Our job is to shuffle these cards.

To do this, it’s cool (and easy) to use the Fisher-Yates Algorithm. This algorithm works by iterating the array starting with the last element, and replacing the current element with a randomly chosen element. The process is repeated until the first element is reached. The result is a randomly shuffled array.

Here is my fiddly code for this “shuffle.”

Continue reading

Posted in Go (en) | Leave a comment

Golang. Console calculator

Let’s write a simple console calculator that performs basic arithmetic operations. First, we give it the numbers with which we want to perform operations using the add <number> command (we can give several numbers). Then we can use the commands inc, acc, sub, mul, div, mod; to do calculations: we enter the index of the number to work with and then the operator arguments. Show can be used to display the numbers entered earlier; exit to exit the program.

Example output:
add 42 13
Operands: 42 13
inc 0 1
[43 13]
mod 1 3
[43 1]
exit

Continue reading

Posted in Go (en) | Leave a comment

Golang. Interfaces

The most cumbersome for newcomers to golang are the interfaces. Here are some examples of code I took apart to “smoke” them, I hope they will help you too. The idea is to look at the code, then close it and from memory try to write from scratch according to this plan:

  • write the code through the usual functions
  • turn functions into methods
  • add an interface

Example number one:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Closures and Fibonacci Numbers

Assignment number 26 in the Go Tour.

Implement a Fibonacci function that returns a function (closure) that returns consecutive Fibonacci numbers (0, 1, 1, 1, 2, 3, 5, …).

Solution:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Exercise: Maps

So, there we have exercise 23 from Go Tour.

Implement the WordCount function. It should return a map (data type map) of the counts of each “word” in the string s. The function wc.Test runs a set of tests against the provided function and outputs success or failure. It makes sense to use the strings.Fields function to perform the task.

Solution: Continue reading

Posted in Go (en) | Leave a comment

Golang. Two-dimensional slices (2D)

From chapter 18 of Go tour.. In general, the tasks in this ‘tour’ are quite itchy B) So…

Implement the Pic function. It should return a fragment of length dy, each element of which is a fragment of dx 8-bit unsigned integers. When you run the program, it will display your picture by interpreting the integers as grayscale (or blue) values. It will draw something like the picture on the left..
Continue reading

Posted in Go (en) | Leave a comment

Golang. Point belong to the circle?

The last example on Pythagoras’ theorem is quite simple. Let’s take a problem from the same topic, but more complicated. Suppose we have a circle in the center of x-axis and y-axis (i.e. center at coordinates 0,0). Let’s write a test for finding an arbitrary point inside the circle.

Here again Pythagoras will help us. In the general case, the formula is as follows:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Pythagoras Theorem

Continue practicing Go and remembering the school curriculum “Pythagoras pants are equal on all sides!” Or to paraphrase in a more boring version: the square of the length of the hypotenuse of a right triangle is equal to the sum of the squares of the cathetuses: c^2 = a^2 + b^2

Our titanic task is this: find the third side of a right triangle. Here’s the code:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Character string into a numeric slice

Let’s write a simple program in Go that will take a string, break it down into slice elements, and convert each element into a number.

First, all the code, and I will break it down in detail below:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Number into words

Let’s write a program that prints numbers as text. Let’s limit it to a range of 1 to 1000.

Here is the first example of code, a fuzzy one:

Continue reading

Posted in Go (en) | Leave a comment

Golang: Newton’s algorithm

Task number 8 in golang tour…

Cycles and functions

To play with functions and loops, let’s implement the square root function: Given a number x, we want to find the number z for which z² is closest to x.

Computers usually calculate the square root of x using a loop. Starting with some assumed z, we can adjust z based on how close z² is to x, getting a better guess:

z -= (z * z - x) / (2 * z)

igroglaz’ note: Continue reading

Posted in Go (en) | Leave a comment

Golang. The average of the numbers entered in the string

Let’s write a program that reads N numbers separated by a space (in one line) and calculates their average value.

The task seems to be simple, but it can be solved in different ways. Here is my version:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Median

Today let’s remember what the median is and how to find it. The median is a number exactly in the middle of an ordered list. It is a very useful thing in statistics and it helps to see the real picture, not the average temperature in the hospital 🙂 For example, we have a list of 55, 1, 22, 7, 92… to find the median:

  1. we order it. We get: 1, 7, 22, 55, 92
  2. look for the one in the middle… that’s 22

If we have an even number of elements in the list, then the median is the average of the sum of two neighboring values. Anyway, here is the code of the simplest variant:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Number of letters in a sentence

Calculate the number of characters in the text – how many which letter occurs how many times. You can do this with maps, but there is another, C-style way: character arithmetic:

Continue reading

Posted in Go (en) | Leave a comment

Golang. Funny basic example of `if/else`

There is a resource for newcomers to the Go language – “A Tour of Go“. There’s a funny example in the beginning by number 7.. Continue reading

Posted in Go (en) | Leave a comment

Golang. Simple programs for beginners

The Go language is very similar to C. Here are some simple programs.

Let’s begin… Calculate 2 to the 12th power.

Continue reading

Posted in Go (en) | Leave a comment

Run notepad++ from the Windows console

Sometimes in Windows, you have to use cmd console, create files in it, etc. So it is convenient to bind file opening in npp (notepad++) to alias npp <file name>. There are different ways to do it, but the easiest is this:

1. create a .bat file named npp.bat and write there

@echo off start "" "C:\Program Files\Notepad++\notepad++.exe" %*

2. we just put it into the folder C:\Windows

Hallelujah! Now you can open files in your favorite npp from the console 🙂

Posted in Notepad++ (en) | Leave a comment