最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

go語言之給定英語文章統(tǒng)計單詞數(shù)量(go語言小練習)

 更新時間:2020年01月28日 10:28:49   作者:傳參  
這篇文章給大家分享go語言小練習給定英語文章統(tǒng)計單詞數(shù)量,實現(xiàn)思路大概是利用go語言的map類型,以每個單詞作為關(guān)鍵字存儲數(shù)量信息,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧

給定一篇英語文章,要求統(tǒng)計出所有單詞的個數(shù),并按一定次序輸出。思路是利用go語言的map類型,以每個單詞作為關(guān)鍵字存儲數(shù)量信息,代碼實現(xiàn)如下:

package main

import (
 "fmt"
 "sort"
)

func wordCounterV1(str string) {
 /*定義變量*/
 stringSlice := str[:]
 temp := str[:]
 wordStatistic := make(map[string]int)

 /*把所有出現(xiàn)的單詞放入map中*/
 j := 0
 for i := 0; i < len(stringSlice); i++ {
  if !((stringSlice[i] >= 65 && stringSlice[i] <= 90) || (stringSlice[i] >= 97 && stringSlice[i] <= 122)) {
   temp = str[j:i]
   if len(temp) != 0 {
    wordStatistic[temp]++
   }
   j = i + 1
  }
 }

 /*把首字母為大寫的單詞轉(zhuǎn)換為小寫;去除無效字符*/
 for i := range wordStatistic {
  if len(i) > 1 {
   if (i[0] >= 65 && i[0] <= 90) && (i[1] <= 65 || i[1] >= 90) {
    strTemp := make([]byte, len(i), len(i))
    copy(strTemp, i)
    strTemp[0] += 32
    wordStatistic[string(strTemp)] += wordStatistic[i]
    delete(wordStatistic, i)
   }
  } else {
   if i[0] != 'a' && i[0] != 'A' {
    delete(wordStatistic, i)
   } else if i[0] == 'A' {
    wordStatistic["a"] += wordStatistic[i]
    delete(wordStatistic, i)
   }
  }

 }

 /*把map的關(guān)鍵字映射到string切片進行排序*/
 sortSlice := make([]string, 0, len(wordStatistic))
 for i := range wordStatistic {
  sortSlice = append(sortSlice, i)
 }
 sort.Strings(sortSlice)

 /*輸出結(jié)果*/
 for _, v := range sortSlice {
  fmt.Printf("%s:%d\n", v, wordStatistic[v])
 }
 fmt.Printf("word count:%d\n", len(wordStatistic))
}

主函數(shù)隨便輸入一篇英語文章

func main() {

 str := ` There are moments in life when you miss someone so much 
 that you just want to pick them from your dreams and hug them for 
 real! Dream what you want to dream;go where you want to go;be what 
 you want to be,because you have only one life and one chance to do 
 all the things you want to do.

   May you have enough happiness to make you sweet,enough trials
  to make you strong,enough sorrow to keep you human,enough hope to
  make you happy? Always put yourself in others'shoes.If you feel
  that it hurts you,it probably hurts the other person, too.
 
   The happiest of people don't necessarily have the best of
  everything;they just make the most of everything that comes along
  their way.Happiness lies for those who cry,those who hurt, those
  who have searched,and those who have tried,for only they can
  appreciate the importance of people
 
   who have touched their lives.Love begins with a smile,grows with
  a kiss and ends with a tear.The brightest future will always be based
  on a forgotten past, you can't go on well in life until you let go of
  your past failures and heartaches.
 
   When you were born,you were crying and everyone around you was smiling.
 Live your life so that when you die,you're the one who is smiling and
  everyone around you is crying.
 
   Please send this message to those people who mean something to you,
 to those who have touched your life in one way or another,to those who 
 make you smile when you really need it,to those that make you see the 
 brighter side of things when you are really down,to those who you want
  to let them know that you appreciate their friendship.And if you don't,
  don't worry,nothing bad will happen to you,you will just miss out on 
  the opportunity to brighten someone's day with this message.`
  //調(diào)用功能
 wordCounterV1(str)
}

編譯后終端輸出結(jié)果:

C:\Users\24213\go project>cd src\github.com\go-study\lesson6\practice1

C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>go build

C:\Users\24213\go project\src\github.com\go-study\lesson6\practice1>practice1
a:4
all:1
along:1
always:2
and:8
another:1
appreciate:2
are:2
around:2
bad:1
based:1
be:3
because:1
begins:1
best:1
born:1
brighten:1
brighter:1
brightest:1
can:2
chance:1
comes:1
cry:1
crying:2
day:1
die:1
do:2
don:3
down:1
dream:2
dreams:1
ends:1
enough:4
everyone:2
everything:2
failures:1
feel:1
for:3
forgotten:1
friendship:1
from:1
future:1
go:4
grows:1
happen:1
happiest:1
happiness:2
happy:1
have:7
heartaches:1
hope:1
hug:1
human:1
hurt:1
hurts:2
if:2
importance:1
in:4
is:2
it:3
just:3
keep:1
kiss:1
know:1
let:2
lies:1
life:5
live:1
lives:1
love:1
make:6
may:1
mean:1
message:2
miss:2
moments:1
most:1
much:1
necessarily:1
need:1
nothing:1
of:6
on:3
one:4
only:2
opportunity:1
or:1
other:1
others:1
out:1
past:2
people:3
person:1
pick:1
please:1
probably:1
put:1
re:1
real:1
really:2
searched:1
see:1
send:1
shoes:1
side:1
smile:2
smiling:2
so:2
someone:2
something:1
sorrow:1
strong:1
sweet:1
tear:1
that:6
the:10
their:3
them:3
there:1
they:2
things:2
this:2
those:9
to:19
too:1
touched:2
trials:1
tried:1
until:1
want:6
was:1
way:2
well:1
were:2
what:2
when:5
where:1
who:10
will:3
with:4
worry:1
you:32
your:4
yourself:1
word count:144

總結(jié)

以上所述是小編給大家介紹的go語言之給定英語文章統(tǒng)計單詞數(shù)量(go語言小練習),希望對大家有所幫助!

相關(guān)文章

  • golang如何使用sarama訪問kafka

    golang如何使用sarama訪問kafka

    這篇文章主要介紹了golang如何使用sarama訪問kafka,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Go語言入門13之runtime包案例講解

    Go語言入門13之runtime包案例講解

    這篇文章主要介紹了Go語言入門runtime包相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • Go語言使用protojson庫實現(xiàn)Protocol Buffers與JSON轉(zhuǎn)換

    Go語言使用protojson庫實現(xiàn)Protocol Buffers與JSON轉(zhuǎn)換

    本文主要介紹Google開源的工具庫Protojson庫如何Protocol Buffers與JSON進行轉(zhuǎn)換,以及和標準庫encoding/json的性能對比,需要的朋友可以參考下
    2023-09-09
  • Go-ethereum?解析ethersjs中產(chǎn)生的簽名信息思路詳解

    Go-ethereum?解析ethersjs中產(chǎn)生的簽名信息思路詳解

    這篇文章主要介紹了Go-ethereum?解析ethersjs中產(chǎn)生的簽名信息,我們解析簽名的需要知道,簽名的消息,簽名,和公鑰,按照這個思路,我們可以通過ethers實現(xiàn)消息的簽名,也可以通過go-ethereum實現(xiàn),需要的朋友可以參考下
    2022-08-08
  • 使用Go語言寫一個Http?Server的實現(xiàn)

    使用Go語言寫一個Http?Server的實現(xiàn)

    本文主要介紹了使用Go語言寫一個Http?Server的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Go語言集成開發(fā)環(huán)境之VS Code安裝使用

    Go語言集成開發(fā)環(huán)境之VS Code安裝使用

    VS Code是微軟開源的一款編輯器,插件系統(tǒng)十分的豐富,下面介紹如何用VS Code搭建go語言開發(fā)環(huán)境,需要的朋友可以參考下
    2021-10-10
  • GoLang channel使用介紹

    GoLang channel使用介紹

    Channel 和 goroutine 的結(jié)合是 Go 并發(fā)編程的大殺器。而 Channel 的實際應(yīng)用也經(jīng)常讓人眼前一亮,通過與 select,cancel,timer 等結(jié)合,它能實現(xiàn)各種各樣的功能。接下來,我們就要梳理一下 channel 的應(yīng)用
    2022-10-10
  • Golang守護進程用法示例分析

    Golang守護進程用法示例分析

    這篇文章主要介紹了Golang守護進程用法示例,創(chuàng)建守護進程首先要了解go語言如何實現(xiàn)創(chuàng)建進程,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習吧
    2023-05-05
  • Go語言運算符案例講解

    Go語言運算符案例講解

    這篇文章主要介紹了Go語言運算符案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Go語言中切片使用的注意事項小結(jié)

    Go語言中切片使用的注意事項小結(jié)

    切片是引用類型,相信對大家來說都不陌生,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Go語言中切片使用的一些注意事項,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2018-01-01

最新評論

灌南县| 沅陵县| 濉溪县| 宜君县| 灵台县| 乌鲁木齐县| 彩票| 华宁县| 湘阴县| 淮滨县| 綦江县| 嘉兴市| SHOW| 蓬溪县| 车险| 瓦房店市| 南昌市| 社会| 平泉县| 深水埗区| 文水县| 河间市| 揭东县| 萝北县| 莒南县| 苏尼特左旗| 桓台县| 德阳市| 镇赉县| 阿荣旗| 深泽县| 威远县| 和顺县| 澄江县| 叶城县| 绥芬河市| 乌兰浩特市| 亳州市| 宁南县| 洛宁县| 县级市|