Daily Go Language Bible--complex number, Boolean, string exercises

Source: Internet
Author: User
Tags sprintf

Go Language Bible-plural

1. We refer to the number of shapes such as A+bi (A, b are real numbers) as complex numbers, where a is called the real part, B is the imaginary part, and I is called the imaginary unit. Complex types of two precision: complex64 and complex128, respectively, corresponding to float32 and float64 two floating-point precision

The 2.complex function is used to construct complex numbers, and the real and IMAG functions return the real and imaginary parts of complex numbers, respectively

Go Language Bible-Boolean

1. Boolean values can be used with && (and) and | | (OR) operator, with short-circuit behavior

Priority ratio of 2.&& | | High

Go Language Bible-strings

1. A string is an immutable sequence of bytes, and the text string is often interpreted as a sequence of Unicode code points (rune) in UTF8 encoding

2. The built-in Len function returns the number of bytes in a string, not the number of characters, and the UTF8 encoding for non-ASCII characters takes two or more bytes

The 3.+ operator constructs a new string of two string links

4. String values can also be written in string literals, as long as a series of byte sequences are enclosed in double quotes

5. Native string literals are ' ... ', use anti-quotes instead of double quotation marks for writing regular expressions, HTML templates, JSON values, command-line prompt information will be convenient

The 6.utf8 is a variable length encoding that encodes Unicode code points into byte sequences, the Go language source file is UTF8 encoded, and the Go Language processing UTF8 encoded text is also excellent, and the range loop of the go language automatically decodes the UTF8 string when processing a string

7. Each symbol is assigned a unique Unicode code point, and the Unicode code point corresponds to the rune integer type in the Go language (Rune is the Int32 equivalent type). A value less than 256 yards can be written in a hexadecimal escape byte, such as \x41 corresponding to the character ' A ', and a larger code point must be escaped in the form of \u or \u

7.utf8. Runecountinstring (s) function statistics number of characters

8. Four packages are particularly important for string handling: bytes, strings, StrConv, and Unicode packages

9. Convert an integer to a string, one by using FMT. sprintf returns a formatted string, and another method is to use StrConv. Itoa ("Integer to ASCII"):

10. The string is converted to an integer StrConv. Parseint StrConv. Parsefloat

Exercise 3.10: Write a non-recursive version of the comma function, using bytes. Buffer instead of a string link operation.

Exercise 3.11: Refine the comma function to support floating-point processing and an optional sign-on processing.

Exercise 3.12: Write a function that determines whether two strings are scrambled, meaning that they have the same characters but correspond to different orders.

Package Mainimport ("FMT" "Strings" "bytes") Func main () {FMT. Println (comma ( -5123456.23)) fmt. Println (Compare ("Abec", "Ecab"))}/* exercise 3.10: Write a non-recursive version of the comma function, using bytes. Buffer instead of a string link operation. Exercise 3.11: Refine the comma function to support floating-point processing and an optional sign-on processing. */func comma (str float64) string{//integer converted to string s: = Fmt. Sprintf ("%.2f", str)//Remove the decimal part after the var end string if dot: = Strings. LastIndex (S, "."); Dot >= 0 {end = S[dot:] s = s[:d ot]} num: = Len (s) var buf bytes.b Uffer J: = 1 for i:=num-1;i>=0;i--{buf. WriteByte (S[i]) if j%3==0 && i!=0{buf. WriteString (",")} J + +} res:=buf. String () var r bytes. Buffer//Invert string for I:=len (res) -1;i>=0;i--{R.writebyte (Res[i])} R.writestri Ng (end) return r.string ()}//Exercise 3.12: Write a function that determines whether two strings are conflicting,That means they have the same characters, but they correspond to different orders. Func Compare (str1 string,str2 string) bool{//compares the length of two strings, and the outer loop is a longer num1:=strings. Count (STR1, "") num2:=strings. Count (STR2, "") if num2 > num1{str1,str2=str2,str1} var res bool for _,v: =                                Range str1{res = False for _,SV: =range str2{if v== sv{ Res =true}} if!res{bre AK}} return res}

  

Daily Go Language Bible--complex number, Boolean, string exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.