Go语言基础(codewars---6kyu和5kyu)

说明

  • Go语言从0开始的codewars代码记录,6kyu5kyu是一些带有一点算法思想的语法题
  • 主要记录一些语法题中遇到的一些Best Practice,以学习Go语言库函数的使用方法
  • 部分会加注释

一、String相关题

1、Stop gninnipS My sdroW!(6kyu)

Description:

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.

Examples:

spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" 
spinWords( "This is a test") => returns "This is a test" 
spinWords( "This is another test" )=> returns "This is rehtona test"

Best Practice:

package kata

import "strings"

func SpinWords(str string) string {
  runes := strings.Split(str, " ")
  for i, v := range runes{
    if len(v) >= 5{
      runes[i] = reverse(v)
    }
  }
  return strings.Join(runes, " ")
  
}

func reverse(str string) string {
  runes := []rune(str)
  for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1{
    runes[i], runes[j] = runes[j], runes[i]
  }
  return string(runes)
}

Tips: 中文字符占3个字节[]rune是按字符存储,可以存储中文字符,所以一般都直接用[]rune

2、Consecutive strings(6kyu)

Description:

You are given an array(list) strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array.

Examples:
strarr = ["tree", "foling", "trashy", "blue", "abcdef", "uvwxyz"], k = 2

Concatenate the consecutive strings of strarr by 2, we get:

treefoling   (length 10)  concatenation of strarr[0] and strarr[1]
folingtrashy ("      12)  concatenation of strarr[1] and strarr[2]
trashyblue   ("      10)  concatenation of strarr[2] and strarr[3]
blueabcdef   ("      10)  concatenation of strarr[3] and strarr[4]
abcdefuvwxyz ("      12)  concatenation of strarr[4] and strarr[5]

Two strings are the longest: "folingtrashy" and "abcdefuvwxyz".
The first that came is "folingtrashy" so 
longest_consec(strarr, 2) should return "folingtrashy".

In the same way:
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"
n being the length of the string array, if n = 0 or k > n or k <= 0 return "" (return Nothing in Elm, "nothing" in Erlang).

Note
consecutive strings : follow one after another without an interruption

Best Practice:

package kata

import "strings"

func LongestConsec(strarr []string, k int) string {
  longest := ""
  for i := 0; i < len(strarr) - k + 1; i ++{
    joined := strings.Join(strarr[i:i+k], "")
    if len(longest) < len(joined){
      longest = joined
    }
  }
  return longest
}

Tips: strings.Join(strarr[i:i+k], "")"" 连接字符串;strarr[i:i+l] 截取strarr中 i~i+k位置的元素

3、Build Tower(6kyu)

Description:

Build a pyramid-shaped tower given a positive integer number of floors. A tower block is represented with "*" character.

For example, a tower with 3 floors looks like this:

[
  "  *  ",
  " *** ", 
  "*****"
]
And a tower with 6 floors looks like this:

[
  "     *     ", 
  "    ***    ", 
  "   *****   ", 
  "  *******  ", 
  " ********* ", 
  "***********"
]

Best Practice:

package kata

import "strings"

func TowerBuilder(nFloors int) []string {
  res := make([]string, nFloors)
  for i := 1; i <= nFloors; i ++{
    space := nFloors - i
    str := strings.Repeat(" ", space) + strings.Repeat("*", 2*i -1 ) + strings.Repeat(" ", space)
    res[i - 1] = str
  }
  return res
}

Tips: strings.Repeat(str, n)str重复n次,返回string类型

4、Your order, please(6kyu)

Description:

Your task is to sort a given string. Each word in the string will contain a single number. This number is the position the word should have in the result.

Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).

If the input string is empty, return an empty string. The words in the input String will only contain valid consecutive numbers.

Examples
"is2 Thi1s T4est 3a"  -->  "Thi1s is2 3a T4est"
"4of Fo1r pe6ople g3ood th5e the2"  -->  "Fo1r the2 g3ood 4of th5e pe6ople"
""  -->  ""

Best Practice:

package kata

import "strings"
import "strconv"

func Order(sentence string) string {
  parts := strings.Fields(sentence)
  tmp := []string{}

  for i := 1; i <= len(sentence); i ++{
    for _, v := range parts{
      if strings.Contains(v, strconv.Itoa(i)){
        tmp = append(tmp, v)
      }
    }
  }
  sentence = strings.Join(tmp, " ")
  return sentence
}

Tips: strings.Fields(sentence) 以空白符分割sentence

5、Mexican Wave(6kyu)

Description:

Task
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up. 
Rules
 1.  The input string will always be lower case but maybe empty.

 2.  If the character in the string is whitespace then pass over it as if it was an empty seat
Example
wave("hello") => []string{"Hello", "hEllo", "heLlo", "helLo", "hellO"}

Best Practice1:

package kata

func wave(words string) []string {
  res := []string{}
  for i, v := range words{
    if v == ' '{
      continue
    }
    upperV := string(v - 'a' + 'A')
    res = append(res, words[:i] + upperV + words[i+1:])
  }
  return res
}

Best Practice2:

package kata

func wave(words string) []string {
  res := []string{}
  runes := []rune(words)
  for i := 0; i < len(runes); i ++{
    if runes[i] >= 97 && runes[i] <= 122{
      runes[i] -= 32
      res = append(res, string(runes))
      runes[i] += 32
    }
  }
  return res
}

Tips: v - 'a' + 'A'的效果就是小写变大写

二、Map相关题

1、Find the odd int(6kyu)

Description:

Given an array of integers, find the one that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

Examples
[7] should return 7, because it occurs 1 time (which is odd).
[0] should return 0, because it occurs 1 time (which is odd).
[1,1,2] should return 2, because it occurs 1 time (which is odd).
[0,1,0,1,0] should return 0, because it occurs 3 times (which is odd).
[1,2,2,3,3,3,4,3,3,3,2,2,1] should return 4, because it appears 1 time (which is odd).

Best Practice:

package kata

func FindOdd(seq []int) int {
  resMap := make(map[int]int)
  for _, v := range seq{
      resMap[v] ++
  }
  for k, v := range resMap{
    if v%2 == 1{
      return k
    }
  }
  return 0
}

Tips: 一个clever的practice

package kata

func FindOdd(seq []int) int {
    res := 0
    for _, x := range seq {
        res ^= x
    }
    return res
}
2、Counting Duplicates(6kyu)

Description:

Count the number of Duplicates
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

Example
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice

Best Practice:

package kata

import "strings"

func duplicate_count(s1 string) int {
  m := make(map[rune]int)
  res := 0
  for _, v := range strings.ToLower(s1){
    if m[v]++; m[v] == 2{
      res ++
    }
  }
  return res
}

Tips: strings.ToLower() 将字符串全部转换成小写

3、Array.diff(6kyu)

Description:

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

array_diff({1, 2}, 2, {1}, 1, *z) == {2} (z == 1)
If a value is present in b, all of its occurrences must be removed from the other:

array_diff({1, 2, 2, 2, 3}, 5, {2}, 1, *z) == {1, 3} (z == 2)

Best Practice:

package kata

func ArrayDiff(a, b []int) []int {
  m := make(map[int]bool)
  for _, v := range b{
    m[v] = true
  }
  res := []int{}
  for _, v := range a{
    if !m[v]{
      res = append(res, v)
    }
  }
  return res
}

Tips: 利用map 快速判断a[i]是否是b中元素

三、math相关函数

1、Bit Counting(6kyu)

Description:

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case

Best Practice:
1、用math/bits 库中的OnesCount 函数

package kata

import "math/bits"

func CountBits(n uint) int {
  return bits.OnesCount(n)
}

2、自己实现

package kata

func CountBits(n uint) int {
  res := 0
  for n != 0{
    if n % 2 == 1{
      res += 1
    }
    n /= 2
  }
  return res
}

Tips: bits.OnesCount(n) 计算二进制n1的个数

2、Playing with digits(6kyu)

Description:

Some numbers have funny properties. For example:

89 --> 8¹ + 9² = 89 * 1

695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2

46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p

we want to find a positive integer k, if it exists, such that the sum of the digits of n taken to the successive powers of p is equal to k * n.
In other words:

Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k

If it is the case we will return k, if not return -1.

Note: n and p will always be given as strictly positive integers.

digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

Best Practice:

package kata

import (
  "math"
  "strconv"
)

//用字符串转换的方法
func DigPow(n, p int) int {
  sum := 0.0
  for i, s := range strconv.Itoa(n) {
    sum += math.Pow(float64(s-'0'), float64(p+i)) //减去末尾的'0'——int
  }
  intSum := int(sum)
  if intSum%n == 0 {
    return intSum / n
  }
  return -1
}

Tips: math.Pow(a, b float64) float64 参数和返回值都是float64

四、递归相关

1、Bouncing Balls(6kyu)

Description:

A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known.

He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66).

His mother looks out of a window 1.5 meters from the ground.

How many times will the mother see the ball pass in front of her window (including when it's falling and bouncing?

Three conditions must be met for a valid experiment:
Float parameter "h" in meters must be greater than 0
Float parameter "bounce" must be greater than 0 and less than 1
Float parameter "window" must be less than h.
If all three conditions above are fulfilled, return a positive integer, otherwise return -1.

Note:
The ball can only be seen if the height of the rebounding ball is strictly greater than the window parameter.

Examples:
- h = 3, bounce = 0.66, window = 1.5, result is 3

- h = 3, bounce = 1, window = 1.5, result is -1 

(Condition 2) not fulfilled).

Best Practice:
1、递归

package kata

//递归
func BouncingBall(h, bounce, window float64) int {
  res := -1
  if h <= window || bounce <= 0 || bounce >= 1{
    
  }else{
      res = 2 + BouncingBall(h*bounce, bounce, window)
  }
  
  return res
}

2、非递归

package kata

//非递归
func BouncingBall(h, bounce, window float64) int {
  res := -1
  if h <= window || bounce <= 0 || bounce >= 1{
    
  }else{
    for ; h > window; h = h*bounce{
      res += 2
    }
  }
  
  return res
}

Tips: 没什么好说的

2、Product of consecutive Fib numbers(5kyu)

Description:

The Fibonacci numbers are the numbers in the following integer sequence (Fn):

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...

such as

F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.

Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying

F(n) * F(n+1) = prod.

Your function productFib takes an integer (prod) and returns an array:

[F(n), F(n+1), true] or {F(n), F(n+1), 1} or (F(n), F(n+1), True)
depending on the language if F(n) * F(n+1) = prod.

If you don't find two consecutive F(n) verifying F(n) * F(n+1) = prodyou will return

[F(n), F(n+1), false] or {F(n), F(n+1), 0} or (F(n), F(n+1), False)
F(n) being the smallest one such as F(n) * F(n+1) > prod.

Some Examples of Return:
(depend on the language)

productFib(714) # should return (21, 34, true), 
                # since F(8) = 21, F(9) = 34 and 714 = 21 * 34

productFib(800) # should return (34, 55, false), 
                # since F(8) = 21, F(9) = 34, F(10) = 55 and 21 * 34 < 800 < 34 * 55
-----
productFib(714) # should return [21, 34, true], 
productFib(800) # should return [34, 55, false], 
-----
productFib(714) # should return {21, 34, 1}, 
productFib(800) # should return {34, 55, 0},        
-----
productFib(714) # should return {21, 34, true}, 
productFib(800) # should return {34, 55, false}, 
Note:
You can see examples for your language in "Sample Tests".

Best Practice:

package kata

func ProductFib(prod uint64) [3]uint64 {
  // your code
  f1, f2 := uint64(0), uint64(1)
  for f1 * f2 < prod{
    f1, f2 = f2, f1 + f2
  }
  success := uint64(0)
  if f1 * f2 == prod{
    success = 1
  }
  return [3]uint64{f1, f2, success}
}

Tips: 如果用递归实现斐波那契则会TL

五、其它库函数

1、Are they the “same”?(6kyu)

Description:

Given two arrays a and b write a function comp(a, b) (orcompSame(a, b)) that checks whether the two arrays have the "same" elements, with the same multiplicities (the multiplicity of a member is the number of times it appears). "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

Examples
Valid arrays
a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]
comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11] 
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]
Invalid arrays
If, for example, we change the first number to something else, comp is not returning true anymore:

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]
comp(a,b) returns false because in b 132 is not the square of any number of a.

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]
comp(a,b) returns false because in b 36100 is not the square of any number of a.

Remarks
a or b might be [] or {} (all languages except R, Shell).
a or b might be nil or null or None or nothing (except in C++, COBOL, Crystal, D, Dart, Elixir, Fortran, F#, Haskell, Nim, OCaml, Pascal, Perl, PowerShell, Prolog, PureScript, R, Racket, Rust, Shell, Swift).
If a or b are nil (or null or None, depending on the language), the problem doesn't make sense so return false.

Note for C
The two arrays have the same size (> 0) given as parameter in function comp.

Best Practice:

package kata

import (
  "reflect"
  "sort"
)

func Comp(a, b []int) bool {
  if a == nil || b == nil {
    return false
  }
  
  for i, n := range a {
    a[i] = n * n
  }
  
  sort.Ints(a)
  sort.Ints(b)
  return reflect.DeepEqual(a, b)
}

Tips:
1、func Ints(a []int) 函数将a排序为递增顺序。
2、func DeepEqual(a1, a2 interface{}) bool 用来判断两个值是否深度一致:除了类型相同;在可以时(主要是基本类型)会使用==; 但还会比较arrayslice的成员,map的键值对,结构体字段进行深入比对。map的键值对,对键只使用==,但值会继续往深层比对。DeepEqual函数可以正确处理循环的类型。函数类型只有都会nil时才相等;空切片不等于nil切片;还会考虑arrayslice的长度、map键值对数。

2、The Supermarket Queue(6kyu)

Description:

There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!

input
customers: an array of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
n: a positive integer, the number of checkout tills.
output
The function should return an integer, the total time required.

Important
Please look at the examples and clarifications below, to ensure you understand the task correctly :)

Examples
queueTime([5,3,4], 1)
// should return 12
// because when there is 1 till, the total time is just the sum of the times

queueTime([10,2,3,3], 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the 
// queue finish before the 1st person has finished.

queueTime([2,3,10], 2)
// should return 12
Clarifications
There is only ONE queue serving many tills, and
The order of the queue NEVER changes, and
The front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
N.B. You should assume that all the test input will be valid, as specified above.

Best Practice:

package kata

import "sort"

func QueueTime(customers []int, n int) int {
  var out = make([]int, n)
  for _, v:= range customers{
    out[0] += v
    sort.Ints(out)
  }
  return out[n-1]
}

Tips: 依次轮着排队

未完待续…

Description:


Best Practice:


Tips:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值