Posts

Rocks and Diamonds

Image
In this post I want to tell about one simple programming task with solving in Java. Suppose we have two strings with latin symbols out of order. First string may have duplicates and that string we call Rocks. Second string have no duplicates and we call it Diamonds. Our task to find out how many Diamonds exist in Rocks. Every symbol in second string is diamond. If we want to use brute forse it will be not very effective. In that case we need to iterate full Rocks as many times as Diamonds exist in second string and we need to iterate the whole Rocks - from beginning to end. To become more efficient we will to execute two preparations - sort Rocks and Diamonds alphabetically. Now we have two sorted strings. Let begin iterate throught Diamonds and for every Diamond we iterate Rocks, but not the whole - when we found Diamond in Rock we increment our result counter, then take next Rock we check if it more then Diamond alphabetically. If it is then we take next Diamond because previous ...

Usage of flag in New constructor of log package in Golang

Image
Package log in Golang provides simple functionality for logging. In previous post we made several examples of usage New constructor for Logger. In this post will be exposed examples of usage New and meaning of third argument of New constructor. Let's begin with the simplest example. We will make logger that logs messages to standard output: package main import ( "log" "os" ) func main() { logger := log.New(os.Stdout, "", log.Ldate) logger.Println("First log message") } Output: 2020/04/21 First log message Let me remind signature of New: func New(out io.Writer, prefix string, flag int) *Logger In example as out io.Writer we used os.Stdout - standard output. As flag int used const from log package - log.Ldate - it is integer 1 - provides the date in the local time zone: 2020/04/21. Total there are 8 such constants in log package: log.Ldate - integer 1 - the date in the local time zone: 2020/04...

Examples of log package in Golang

Image
Hi, everybody! Today we view at some examples of log package in Golang. Package log has constructor method New . Its signature is following: func New(out io.Writer, prefix string, flag int) *Logger It means we need argument out implementing interface io.Writer : type Writer interface { Write(p []byte) (n int, err error) } Let's create our simple one: import ( "fmt" ) type CustomWriter struct {} func(c *CustomWriter) Write(p []byte) (n int, err error) { fmt.Print(string(p)) return len(p), nil } Now look at example: package main import ( "fmt" "log" ) type CustomWriter struct {} func(c *CustomWriter) Write(p []byte) (n int, err error) { fmt.Print(string(p)) return len(p), nil } func main() { var logger = log.New(&CustomWriter{}, "", log.LstdFlags) logger.Println("First log message") } Output: 2020/04/18 23:59:18 First log message Let's look at ...

C examples: simple input to output

Image
C programming language is one of the most used in the world. Actually by TIOBE index 2018 it is placed at the second stage of the most cited languages. In the series of posts C examples I will introduce different examples of C code. Today I start with first example. It is simple program that accepts input from stdin and returns it to stdout. /* Copy input to output */ #include int main(void) { int c; while ((c=getchar()) != EOF) putchar(c); return 0; }

Differences between abstract class and interface in Java

Image
One of the most frequently asked questions on Java job interview: " What the difference between abstract class and interface in Java? " And in this post I will try to answer that question. The differences are following: The number of extendings . An abstract class can extend only one typical Java class or one abstract class at a time. But an interface can extend any number of interfaces at a time. What can extend . An abstract class can extend concrete (regular) class or abstract class. But interface can extend another interface only. Keyword "abstract" for methods . Keyword "abstract" is mandatory for declaring a method as an abstract in abstract class. But in interface that keyword is optional, because methods abstract here by default. Access modifiers for methods . An abstract class can have protected and public abstract methods. But interface can have public abstract methods only. Variables . An abstract class can have static, final or static f...

Ways of creating threads in Java

Image
In this post I will tell you about how to create threads in Java. So, let's get started! Threads in Java are objects. They are instances of java.lang.Thread class or its subclasses. There are two ways of creating threads in Java: First . We can just create an instance of Thread class . Thread thread = new Thread(); // And start it thread.start(); But this is useless, because our thread has no code to execute. Instead we can extend Thread class and create our subclass of Thread with custom code to execute. public class CustomThread extends Thread { public void run(){ System.out.println("Our custom thread is running"); } } CustomThread customThread = new CustomThread(); customThread.start(); In method run we can put all necessary code to execute in our thread. Also we can create anonymous subclass of Thread class. Thread thread = new Thread(){ public void run(){ System.out.println("This is anonymous subclass of Thread. And it...

Get links to all articles from site with python

Image
Hi, everybody! In this post will tell you about one small python script. Recently I have met task to get all links to articles from my blog. I have made some Google search and find some points to this task. I used python and its package BeautifulSoup for html parsing. Also used re module and urllib3 for http request. Here is it, my script. I will be happy if you find it usefull. from bs4 import BeautifulSoup import urllib3 import re links = [] site = "https://alextech18.blogspot.com" pattern = "://alextech18.blogspot.com" def getLinks(url, pattern, html_only=True): http = urllib3.PoolManager() html_page = http.request('GET', url) soup = BeautifulSoup(html_page.data, features="html.parser") # Use separate pattern, because, blogspot for example, can use links leading with http and https for link in soup.findAll('a', attrs={'href': re.compile(pattern)}): if link.get('href') not in links...