当前位置:网站首页>[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences
[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences
2022-06-28 20:01:00 【Study notes of Zhou】
Go from 0 To the beginning 5
Preface
- This period is for learning Golang:Map Comprehensive review 、 Conditional statements 、 Loop exercises , If you don't understand something, you can comment and discuss it !
- This series of articles adopts the core code mode of Niuke to provide case code , Help you from 0 To the beginning of the learning process of supporting brush questions ~
- I recommend a brush question for you 、 Interview artifact , I also use this artifact to learn !~ Links are as follows : Brush question artifact jump link
- The artifact not only has a beautiful web interface , And the operation is very easy to get started ! It is very suitable for beginners to learn systematically !
- Novice Xiaobai can use this artifact to brush questions everyday 、 Look at the face of Dachang 、 Learn the basics of computer 、 Communicate with Daniel face to face ~ The picture of question brushing has been placed below ~

Q1:Map- String formation
Problem description : Given two strings des and src , Judge des Can it be done by src The characters inside make up ,// If possible , return true ; Otherwise return to false,src Each character in can only be in des Used once in .
Related knowledge :
1、for range Traversal string .
2、cnt[ch-‘a’] Implicit conversion byte To int type .
3、 The index of the array can act as a map Of key, To show the only .
Sample input :“ab”,“aab”
Sample output :true
Case code :
Ideas : Let's play one. map Used to save which elements can be used With a -1 that will do . If there is no direct return failure .
//import "fmt"
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * * @param des string character string * @param src string character string * @return bool Boolean type */
func canConstruct(des string, src string) bool {
// write code here
have := make(map[byte]int)
s := []byte(src)
for _, i := range s {
have[i]++
}
p := []byte(des)
for _, i := range p {
if have[i] == 0 {
return false
}
have[i]--
}
return true
}
Q2:Map- Numbers that don't repeat
Problem description : Given an array , Find all the non repeating numbers in the array , And output in the order from small to large .
Related knowledge :
1、map use make Method to initialize
2、 Slicing can be done with []int{} To initialize
3、for range Traversing slices
4、_,ok :=map[key] How to judge m Medium key Whether there is
5、 For slicing append Method
Sample input :[1,1,2,2,3,3,4,4,5,5]
Sample output :[]
Sample input :[3,3,2,2,5,5,1,2,2]
Sample output :[1]
Case code :
import "sort"
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * Finally, there should be one The process of sorting Use sort The sorting provided is sufficient * @param s int Integer one-dimensional array * @return int Integer one-dimensional array */
func getNoRepeat(s []int) []int {
// write code here
var ans []int
have := make(map[int]int)
for _, i := range s {
if _, ok := have[i]; ok == false {
have[i] = 1
}else{
have[i]++
}
}
for i, j := range have{
if j == 1{
ans = append(ans, i)
}
}
sort.Ints(ans)
return ans
}
Q3: Conditional statements - Age determination
Problem description : The known age segments are as follows , baby ( born 0-1 year )、 Young children (1-4 year ) contain 1 year 、 children (5-11) contain 5 year 、 juvenile (12-18) contain 12 year 、 youth (19-35) contain 19 year 、 middle-aged (36-59) contain 36 year 、 The elderly (60 above ) contain 60 year , Enter a person's age , Return to the appropriate age group .
Related knowledge :
1、if Any number of... Can be followed else if sentence .condition The evaluation of is carried out from top to bottom , Until some if perhaps else if Medium condition by true when , Execute the corresponding code block . If there is no one conditon by true, execute else Code block in .
Sample input :35
Sample output :“ youth ”
Case code :
//import "fmt"
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * @param age int integer Age * @return string character string */
func getAge( age int ) string {
// write code here
var ans string
switch {
case age <1 && age >= 0:
ans = " baby "
case age >=1 && age <= 4:
ans = " Young children "
case age >= 5 && age <= 11:
ans = " children "
case age >= 12 && age <= 18:
ans = " juvenile "
case age >= 19 && age <= 35:
ans = " youth "
case age >=36 && age <=59:
ans = " middle-aged "
case age >= 60:
ans = " The elderly "
}
return ans
}
Q4: Conditional statements - Grade judgment
Problem description : Output the grade according to the score , The decision rules are as follows The score is below 60 Not pass ,60-80 contain 60 It's medium , 80-90 contain 80 For good ,90 Points above include 90 For excellence .
Related knowledge :
1、switch Statements are used to perform different actions based on different conditions , every last case Branches are all unique , Test from top to bottom , Until it matches . Golang switch Branch expressions can be of any type , Not limited to constants . Omission break, Automatically terminate by default .
switch var1 {
case val1:
…
case val2:
…
default:
…
}
Sample input :59
Sample output :“ fail, ”
Case code :
func judgeScore(score int) string {
// write code here
var grade string
switch {
case score < 60:
grade = " fail, "
case score >= 60 && score < 80:
grade = " secondary "
case score >= 80 && score < 90:
grade = " good "
case score >= 90:
grade = " good "
}
return grade
}
Q5: Loop statement - Multiplication table
Problem description : Print 9*9 Multiplication table .
Related knowledge :
1、golang in for One or more... Are nested in the loop for loop , The code format is as follows :
for [condition | ( init; condition; increment ) | Range]
{
for [condition | ( init; condition; increment ) | Range]
{
statement(s)
}
statement(s)
}
init: It's usually an assignment expression , Assign initial values to control variables ;
condition: A relational or logical expression , Cycle control conditions ;
increment: It's usually an assignment expression , Increment or decrement of a control variable .
statement: Loop statement
2、goalng in ,fmt.printf Format printing %d Representation number ,-3d Indicates left alignment , Occupy 3 position \n Represents the next line .
Sample input :
Sample output :
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
Case code :
import "fmt"
func main() {
fmt.Println("1*1=1")
for i := 2; i <= 9; i++ {
for j := 1; j <= i-1; j++ {
str := fmt.Sprintf("%d*%d=%-3d", j, i,j*i)
fmt.Printf("%s", str)
}
str := fmt.Sprintf("%d*%d=%d", i, i, i*i)
fmt.Printf("%s\n", str)
}
}
Q6: Loop statement - Coordinate transformation
Problem description : Known one mn Two dimensional array , Index of elements in a two-dimensional array (x,y) It can be expressed as a two-dimensional coordinate , Now convert this two-dimensional coordinate to one-dimensional coordinate , One dimensional coordinates =xn+y. Return this one-dimensional array .
Related knowledge :
1、 Arrays can be accessed by subscripts , The subscript is from 0 Start , The last element subscript is :len-1
for i := 0; i < len(a); i++ {
}
for index, v := range a {
}
2、 Length is part of the array type , therefore ,var a[5] int and var a[10]int It's a different type .
3、 To define an array :var a [len]int, such as :var a [5]int, Array length must be constant , And it's part of the type . Once defined , The length cannot change .
Sample input :[[1,2,3],[4,5,6],[7,8,9]]
Sample output :[1,2,3,4,5,6,7,8,9]
Case code :
//import "fmt"
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * @param array int Two dimensional arrays of integers * @return int Integer one-dimensional array */
func transform(array [][]int) []int {
// write code here
m := len(array)
n := len(array[0])
var arr []int = make([]int, m*n)
i := 0
for a := 0; a < m; a++ {
for b := 0; b < n; b++ {
arr[i] = array[a][b]
i++
}
}
return arr
}
later : Brush Title artifact
Click the link to jump to registration , Start your nanny level question brushing ! Brush the questions and write strange codes. The way of God
In addition, we can not only brush questions here , There will be everything you want , It is very suitable for Xiaobai and beginners ~
1、 Algorithm (398 topic ): Interview must brush 100 topic 、 Introduction to algorithm 、 Interview frequency list
2、 Data structure (300 topic ): Are very classic linked lists 、 Trees 、 Pile up 、 Stack 、 queue 、 Dynamic planning, etc
3、 Language (500 topic ):C/C++、java、python Introductory algorithm exercises
4、SQL piece (82 topic ): Quick start 、SQL Will know 、SQL Advanced challenges 、 The real question of the interview
5、 The real question of the written test of Dachang : Bytes to beat 、 Meituan 、 Baidu 、 tencent … Master the experience and don't be afraid of the interview !

边栏推荐
猜你喜欢

Day88.七牛云: 房源图片、用户头像上传

bluecmsv1.6代码审计

如何通过W3school学习JS/如何使用W3school的JS参考手册
![[324. swing sequence II]](/img/4f/dbbc28c7c13ff94bd0956f2ccf9603.png)
[324. swing sequence II]

阿里开源(EasyExcel)

Parallax JS special effect JS carousel map plug-in

Markdown Mermaid planting grass (1)_ Introduction to Mermaid

I. The HR system is put on the enterprise wechat ISV to enhance the in-depth application of enterprise wechat in service chain retail and other industries
![return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};](/img/6a/45a4494276deba72ef9833818229f5.png)
return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
随机推荐
颜色渐变的FontAwesome图标
Tcwind mode setting
Digital collection, ten thousand words long text, most of the questions you want to know have been clearly explained, which must be seen by practitioners
C language - function knowledge points
Variational graph auto-encoders (VGAE)
论文阅读:Duplex Contextual Relation Network for Polyp Segmentation
2022茶艺师(中级)考试模拟100题及模拟考试
Concours de segmentation des images gastro - intestinales de kaggle Baseline
Risc-v instruction set
internship:术语了解及着手写接口
视差js特效js轮播图插件
Double contextual relationship network for polyp segmentation
2342
Installation and configuration of CGAL in PCL environment 5.4.1
April 10, 2022 -- take the first step with C -- use C from Net class library call method (not understood)
wc命令的使用
Intelligent computing system 1 environment construction
Server configuration estimation of core IOT Bluetooth AOA positioning system
F (x) construct the equation, calculate the partial derivative by gradient descent, determine the partial derivative adjustment by loss function, and deal with nonlinear problems by activation functio
如何通过W3school学习JS/如何使用W3school的JS参考手册