-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24-01-16.go
More file actions
50 lines (44 loc) · 1.82 KB
/
24-01-16.go
File metadata and controls
50 lines (44 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"math"
)
//The colors used by the printer are recorded in a control string. For example a "good" control string would be aaabbbbhaijjjm meaning that the printer used three times color a, four times color b, one time color h then one time color a...
// Sometimes there are problems: lack of colors, technical malfunction and a "bad" control string is produced e.g. aaaxbbbbyyhwawiwjjjwwm with letters not from a to m.
// You have to write a function printer_error which given a string will return the error rate of the printer as a string representing a rational whose numerator is the number of errors and the denominator the length of the control string. Don't reduce this fraction to a simpler expression.
func PrinterError(s string) string {
denom := len(s)
numer := 0
for _, v := range s {
if v > 'm' || v < 'a' {
numer++
}
}
return fmt.Sprintf("%v/%v", numer, denom)
}
// Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n ( inclusive ).
func PowersOfTwo(n int) []uint64 {
res := make([]uint64, n + 1)
for i := 0; i <= n; i++ {
res[i] = uint64(math.Pow(2, float64(i)))
}
return res
}
//Create a combat function that takes the player's current health and the amount of damage recieved, and returns the player's new health. Health can't be less than 0.
func combat(health, damage float64) float64 {
result := health - damage
if damage > health {
result = 0
}
return result
//return max(health - damage, 0)
//return math.Max(health - damage, 0)
}
//Given a set of numbers, return the additive inverse of each. Each positive becomes negatives, and the negatives become positives.
func Invert(arr []int) []int {
res := make([]int, len(arr))
for i, v := range arr {
res[i] = v * -1
}
return res
}