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:
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:
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
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
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 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:
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:
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:
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
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:
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:
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:
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:
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
The Go language is very similar to C. Here are some simple programs.
Let’s begin… Calculate 2 to the 12th power.
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 🙂
I love CodeBlocks IDE for my C language projects… But biggest blunder of CodeBlocks IDE is that you must manually add to ‘Watch’ global variables every @#$% time when debugging. Eg in C++ Builder and other IDEs there is no such problem… Why, why they did it?..
Ok, ok.. “One should avoid to use global variables”, – many will say. But using globals for tiny programs it’s actually the most convenient way to do fast tasks. And AFAIK most of the projects which use CodeBlocks is actually enthusiasts projects.. So it’s really out of scope feature there.
Got the update for Windows today – KB5019509 and the terminal’s arm was torn off. That is, it stopped displaying console programs like my Roguelike game. The update that broke it, adds tabs to the console, while breaking everything else… well, not the point (by the way, to see the latest Windows updates, type wmic qfe list into cmd).
To fix it:
Click on the header of the terminal window -> Properties
In Default Terminal Application put Windows Console Host
Restart the console and hallelujah! We got our oldschool console back. Old Billy’s nose lost the game again ;P
Add “New” .txt in Windows 11 to the context menu
Windows Registry Editor Version 5.00 [-HKEY_CLASSES_ROOT\.txt\ShellNew] [HKEY_CLASSES_ROOT\.txt\ShellNew] "NullFile"="" [-HKEY_CLASSES_ROOT\txtfilelegacy] [HKEY_CLASSES_ROOT\txtfilelegacy] @="Text File" [HKEY_CLASSES_ROOT\txtfilelegacy\shell] [HKEY_CLASSES_ROOT\txtfilelegacy\shell\printto] "NeverDefault"="" [HKEY_CLASSES_ROOT\txtfilelegacy\shell\printto\command] @=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,\ 00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,6e,00,6f,00,\ 74,00,65,00,70,00,61,00,64,00,2e,00,65,00,78,00,65,00,20,00,2f,00,70,00,74,\ 00,20,00,22,00,25,00,31,00,22,00,20,00,22,00,25,00,32,00,22,00,20,00,22,00,\ 25,00,33,00,22,00,20,00,22,00,25,00,34,00,22,00,00,00
Run it, kill the explorer and enjoy the ability to create .txt files 😉