Calculate degrees celcius from farenheit in Go.
Following program shows you how to convert fahrenheit to celsius.
The function FToC below encapsu- lates the temperature conversion logic so that it is defined only once but may be used from multiple p...
An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...
In Golang, there are 2 ways to get the difference in hours between two dates.
Using time.Since Function
The time.Since() function returns the time elapsed since t. It is shorthand for time.Now().Sub(t). For example,
package main
import (
"fm...
In Python, using the timedelta object is the easiest way to subtract a day from a date.
Using timedelta Object
A timedelta object represents a duration, the difference between two dates or times.
See the following example:
#!/usr/bin/python3
#...
In Python, there are 3 ways to remove duplicates in list.
Using Built-in set Function
The built-in set() function returns a new set object, optionally with elements taken from iterable. If you later need a real list again, you can similarly pass the...
In Python, there are 3 ways to count the occurrences of a list item.
Using count Method
The str.count() method returns the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpr...
In Python, there are 2 ways to get the current time.
Using datetime Module
The datetime module supplies classes for manipulating dates and times. See the following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# Import module
import datet...
In Python, there are 2 ways to convert string to int.
Using int Built-in Function
Return an integer object constructed from a number or string x, or return 0 if no arguments are given.
#!/usr/bin/python3
# -*- coding: utf8 -*-
s = "100"
# clas...
In Python, there are 4 ways to find the intersection between two lists.
Using Set Intersection
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also sup...
In Python, there are 5 ways to find the difference between two lists.
Using Set Difference
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support...
Convert Slice to String in Golang.Using strings.Join Function
Converting a slice of strings to a single string is pretty easy to do in Go. The strings.Join() method is present in the standard library for this purpose, and its usage is shown below:
p...
Secure hashes and message digests.Hash algorithms
The hashlib module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (def...
Comparing two slices in Go.Using map cache
Creates a slice of slice values not included in the other given slices. An implementation is shown in the section below.
package main
import "fmt"
// difference returns the elements in `a` that aren't in ...
Compare FILES line by line.
You can use the diff command to show differences between two files, or each corresponding file in two directories. diff outputs differences between files line by line in any of several formats, selectable by command line o...