Reflect on myself to refocus on important things when writing a blog post


Background

Two months after writing a retrospective last year, I came to write a new retrospective. The reason is that blog posts were not written in what I intended! Of course there are some reasons like busy life, lower interests in major than before, works and etc. However I found myself that when writing a short blog post, I too much focus on little things.


What is problem?

Too much focus on little things

Still there are problems when writing a blog post. Too much focus on little things. Below are important things when writing a blog post except content.

  • It should be understandable when read again in the future.
  • It should be understandable to other readers.

I need to focus on above two things but I’m not. Below problems are still exist to me.

  • Cares too much about spelling and grammar.
  • Cares too much about form and layout.

Of course in writing, in order not to be embarrassed… spelling and grammar is important. Also, layout and form are important for readability. However! the problem is that if the content is written in 30 minutes, it takes an hour to check the above things.

For example, when mentioning language in post, I considered a simple question like “Which one do I have to use Python or python?” in an hour. Too much focus on it even though it is little things. In English, I found that language is a proper noun so it should be written in capital letter… but there are already many words written in lower case in my posts. Do I have to change all in now?… Hmm I’m not sure. After now, I decided to use capital letter except in special case like URL or snake case naming convention. If problems were found in existing documents, fix when problems were found. Don’t try to fix such problems in all documents. There are over 200 posts so it is not worthy to do that.

Another example is that I too much focus on space between paragraph or header. For example, number of using “<br>” tags or “How many #s are needed in this header?”

Above things are rules so it is important. I know. However if it takes twice times than writing a content, there seems to be a problem. Is it just a growing procedure of blogger? Whatever, from now I made my own rule so I don’t like to spend too much time on above problems.


Wrap Up

Keep in mind below things when writing a blog post.

  • Focus on contents
  • Use capital letter when mentioning computer language if it does not have to written in lower case.
  • If problems were found in existing documents, fix when problems were found. Don’t try to fix such problems in all documents
  • Contents are important than layout and format

List slicing examples in Python


Environment and Prerequisite

  • Python(3.X)


What is Python slicing?

  • Slicing or Slice: refers the method and notation of importing objects by specifying a range of sequential objects (such as lists, tuples, strings).
  • Slicing makes new objects. Easily say, copy some portion of objects.


Basic Usage and Form

Basic Form

  • Consider there is sequential objects data structure(example: list, tuple, string) which name is a. Its basic form of slicing is like below.

a[start : end : step]

  • Each start, end, step can have both positive or negative number.
  • start: Start point of slicing.
  • end: End point of slicing. Note that it does not include end point!
  • step: Also known as stride, determine moving steps of import and direction. It is an option. Check below examples for your understanding.


Position of Indices Values

  • Like explained above, it can have Positive or Negative number.
  • Positive Number: It starts number 0 from the front of data structure and goes forward by increasing the index.
  • Negative Number: It starts number -1 from back of data structure and goes backward by decreasing the index.
  • Below shows example of indices values in list ['a', 'b', 'c', 'd', 'e'].
a = ['a', 'b', 'c', 'd', 'e']

// Index References
-------------------------------
|  a  |  b  |  c  |  d  |  e  |
-------------------------------
|  0  |  1  |  2  |  3  |  4  |          // In case of positive number
-------------------------------
| -5  | -4  | -3  | -2  | -1  |          // In case of negative number
-------------------------------


Example

  • All examples are base on list a = ['a', 'b', 'c', 'd', 'e'].


Import from specific start point to end

  • a[ start : ]

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ 1 :  ]
['b', 'c', 'd', 'e']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ -3 :  ]
['c', 'd', 'e']


Import from start point to specific end point

  • a[ : end ]

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[  : 2 ]
['a', 'b']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[  : -1 ]
['a', 'b', 'c', 'd']


Import from a specific point to a specific point

  • a[ start : end ]

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ 2 : 4 ]
['c', 'd']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ -4 : -2 ]
['b', 'c']


>>> a = ['a', 'b', 'c', 'd', 'e']
# get values of index from 1 to 3 in reverse
>>> a[ 3 : 0 : -1]
['d', 'c', 'b']


Example of step

  • a[ start : end : step ]
  • If step is positive: Get value per size of ‘step’ while moving to right.
  • If step is negative: Get value per size of ‘step’ while moving to left.

>>> a = ['a', 'b', 'c', 'd', 'e']
# get value while moving 2 steps.
>>> a[ : : 2 ]
['a', 'c', 'e']


>>> a = ['a', 'b', 'c', 'd', 'e']
# get value while moving 3 steps.
>>> a[ -5 : : 3 ]
['a', 'd']


>>> a = ['a', 'b', 'c', 'd', 'e']
# get all in reverse.
>>> a[ : : -1 ]
['e', 'd', 'c', 'b', 'a']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ 3 : : -1 ]
['d', 'c', 'b', 'a']


Reference

Write code of checking whether given string is palindrome


Environment and Prerequisite

  • Python
  • C++


What is Palindrome?

  • Palindrome is a string which reads the same backward as forward.
  • Example: 토마토, abdba, 토마토맛토마토, 1234567654321


Code

  • Time Complexity: O(n)


C


#include <stdio.h>
#include <string.h>

int is_palindrome(char * s){

	int len = strlen(s);

	int left = 0;
	int right = len-1;


	// move left one and right one to middle one by one
	// (right - left) > 1
	// [In case of odd number] left only middle element
	// [In case of even number] go until "left + 1==right"
	while ( (right - left) > 1 ) {

		if( s[right] != s[left] ){
			return 0;
		}
		left += 1;
		right -= 1;
	}

	return 1;

}


int main (){


	char * palindrome = "abcdcba";
	char * non_palindrome = "abcdefg";

	printf("%d\n", is_palindrome(palindrome));
	printf("%d\n", is_palindrome(non_palindrome));

	return 0;

}


C++


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool is_palindrome(string s){

	string s_reverse = s;
	reverse(s_reverse.begin(), s_reverse.end());
	return s == s_reverse ? true : false;

}


int main (){

	string s1 = "abcde1edcba";
	string s2 = "asdlkfjaw;oefjao;iwefja;ow";

	cout << is_palindrome(s1) << '\n';
	cout << is_palindrome(s2) << '\n';

	return 0;

}


Python


def is_palindrome(s):
	return s == s[::-1]


s1 = "abcde1edcba"
s2 = "fjaw;"

print(is_palindrome(s1))
print(is_palindrome(s2))


Reference

Go언어에서 구조체(struct)와 메소드(method)의 기본적인 사용법을 정리해보자


환경

  • Go(1.15.8)
  • 다른 프로그래밍 언어 사용 경험


구조체(struct)와 메소드(method)

Go에서의 구조체(struct)

  • Go에서는 자기만의 방식으로 OOP를 지원한다. 여기서 신기하게도 기존에 흔하게 사용하는 class가 없다고 한다. class가 없기에 상속도 없다.
  • 그래서 구조체(struct)와 메소드(method)를 사용한다.
  • 아래에서는 이제 간단하게 구조체(struct)의 사용법과 그에 사용되는 메소드(method)에 대해 알아본다.


구조체(struct) 예시

  • 아래처럼 4가지 방법이 사용가능하다.
  • 기존 예약어인 new를 사용할수도 있고 생성자 함수를 만들수도 있다.
package main

import (
    "fmt"
)

// define struct
type Rectangle struct {
    width int
    height int
}

// define constructor
func NewRectangle(width int, height int) *Rectangle{
    return &Rectangle{width, height}
}

func main() {

    // create instances
    var rec1 = Rectangle{}
    rec1.width = 2
    rec1.height = 3

    rec2 := Rectangle{4, 5}

    var rec3 = new(Rectangle)
    rec3.width = 6
    rec3.height = 7

    var rec4 = NewRectangle(8, 9)

    // check instances
    fmt.Println(rec1.width, rec1.height)
    fmt.Println(rec2.width, rec2.height)
    fmt.Println(rec3.width, rec3.height)
    fmt.Println(rec4.width, rec4.height)
}
2 3
4 5
6 7
8 9


메소드(method) 예시

  • 리시버(Receiver)를 통해 어떤 구조체(struct)의 메소드인지 명시가 가능하다.
  • Value 리시버와 Pointer 리시버가 있다.
  • 아래 예제에서는 (r Rectangle)(r *Rectangle)가 리시버(Receiver)다.
package main

import (
    "fmt"
)

// define struct
type Rectangle struct {
    width int
    height int
}

// define constructor
func NewRectangle(width int, height int) *Rectangle{
    return &Rectangle{width, height}
}

// define methods
func (r Rectangle) area() int{
    return r.width * r.height
}

// pointer receiver
func (r *Rectangle) setWidth(width int){
    r.width = width
}

// value receiver
func (r Rectangle) setHeight(height int){
    r.height = height
}

func main() {

    // create instances
    var rec = NewRectangle(8, 9)

    // check instances
    fmt.Println(rec.width, rec.height)
    fmt.Println()

    // check methods
    fmt.Println(rec.area())
    fmt.Println()

    rec.setWidth(10)
    // rec.height would not changed because its method's receiver is value receiver
    rec.setHeight(10)
    fmt.Println(rec.width, rec.height)
}
8 9

72

10 9


참고자료

Post about basic usage of struct and method in Go language


Environment and Prerequisite

  • Go(1.15.8)
  • Experience with other programming languages


Struct and Method

Struct in Go

  • Go language supports its own way of OOP. There is no class in Go. Also no class so no inheritance.
  • So it use struct and method.
  • Briefly write about struct and method in below.


Struct Example

  • There are four ways like below example.
  • We can use keyword new or we can make constructor function.
package main

import (
    "fmt"
)

// define struct
type Rectangle struct {
    width int
    height int
}

// define constructor
func NewRectangle(width int, height int) *Rectangle{
    return &Rectangle{width, height}
}

func main() {

    // create instances
    var rec1 = Rectangle{}
    rec1.width = 2
    rec1.height = 3

    rec2 := Rectangle{4, 5}

    var rec3 = new(Rectangle)
    rec3.width = 6
    rec3.height = 7

    var rec4 = NewRectangle(8, 9)

    // check instances
    fmt.Println(rec1.width, rec1.height)
    fmt.Println(rec2.width, rec2.height)
    fmt.Println(rec3.width, rec3.height)
    fmt.Println(rec4.width, rec4.height)
}
2 3
4 5
6 7
8 9


Method Example

  • By using receiver, we can define method for specific class.
  • There are value receiver and pointer receiver.
  • In below example, (r Rectangle) and (r *Rectangle) are receivers.
package main

import (
    "fmt"
)

// define struct
type Rectangle struct {
    width int
    height int
}

// define constructor
func NewRectangle(width int, height int) *Rectangle{
    return &Rectangle{width, height}
}

// define methods
func (r Rectangle) area() int{
    return r.width * r.height
}

// pointer receiver
func (r *Rectangle) setWidth(width int){
    r.width = width
}

// value receiver
func (r Rectangle) setHeight(height int){
    r.height = height
}

func main() {

    // create instances
    var rec = NewRectangle(8, 9)

    // check instances
    fmt.Println(rec.width, rec.height)
    fmt.Println()

    // check methods
    fmt.Println(rec.area())
    fmt.Println()

    rec.setWidth(10)
    // rec.height would not be changed because its method's receiver is value receiver
    rec.setHeight(10)
    fmt.Println(rec.width, rec.height)
}
8 9

72

10 9


Reference