Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b650571

Browse files
committed
Basic HackerRank algorithms
1 parent 3a824bf commit b650571

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func stringsToInts(strs []string) []int {
12+
var ints []int
13+
ints = make([]int, len(strs))
14+
for i := 0; i < len(strs); i++ {
15+
ints[i], _ = strconv.Atoi(strs[i])
16+
}
17+
return ints
18+
}
19+
20+
func main() {
21+
s := bufio.NewScanner(os.Stdin)
22+
s.Scan()
23+
N, _ := strconv.Atoi(s.Text())
24+
25+
mx := make([][]int, N)
26+
for i := 0; i < N; i++ {
27+
s.Scan()
28+
mx[i] = stringsToInts(strings.Split(s.Text(), " "))
29+
}
30+
31+
firstD := 0
32+
secD := 0
33+
for j := 0; j < N; j++ {
34+
firstD += mx[j][j]
35+
secD += mx[j][(N-1)-j]
36+
}
37+
38+
res := firstD - secD
39+
if res < 0 {
40+
res *= -1
41+
}
42+
43+
fmt.Println(res)
44+
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func stringsToInts(strs []string) []int {
12+
var ints []int
13+
ints = make([]int, len(strs))
14+
for i := 0; i < len(strs); i++ {
15+
ints[i], _ = strconv.Atoi(strs[i])
16+
}
17+
return ints
18+
}
19+
20+
func filter(ints []int, filter func(int) bool) []int {
21+
var intsf []int
22+
for _, v := range ints {
23+
if filter(v) {
24+
intsf = append(intsf, v)
25+
}
26+
}
27+
return intsf
28+
}
29+
30+
func main() {
31+
s := bufio.NewScanner(os.Stdin)
32+
s.Scan()
33+
N, _ := strconv.Atoi(s.Text())
34+
35+
s.Scan()
36+
ints := stringsToInts(strings.Split(s.Text(), " "))
37+
positives := filter(ints, func(v int) bool {
38+
return v > 0
39+
})
40+
41+
negatives := filter(ints, func(v int) bool {
42+
return v < 0
43+
})
44+
45+
zeros := filter(ints, func(v int) bool {
46+
return v == 0
47+
})
48+
49+
fmt.Println(float64(len(positives)) / float64(N))
50+
fmt.Println(float64(len(negatives)) / float64(N))
51+
fmt.Println(float64(len(zeros)) / float64(N))
52+
53+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"io/ioutil"
7+
"strings"
8+
"strconv"
9+
)
10+
11+
func main() {
12+
var N string
13+
fmt.Scanln(&N)
14+
bytes, _:= ioutil.ReadAll(os.Stdin)
15+
16+
var sum int64 = 0
17+
for _, value := range strings.Split(string(bytes), " ") {
18+
parse, _ := strconv.ParseInt(value, 10, 64)
19+
sum += parse
20+
}
21+
22+
23+
fmt.Println(sum)
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
import "fmt"
3+
4+
func solveMeFirst(a uint32,b uint32) uint32{
5+
// Hint: Type return (a+b) below
6+
7+
}
8+
9+
func main() {
10+
var a, b, res uint32
11+
fmt.Scanf("%v %v", &a,&b)
12+
res = solveMeFirst(a,b)
13+
fmt.Println(res)
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Vpackage main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func main() {
12+
var N string
13+
fmt.Scanln(&N)
14+
bytes, _ := ioutil.ReadAll(os.Stdin)
15+
16+
var sum int64 = 0
17+
for _, value := range strings.Split(string(bytes), " ") {
18+
parse, _ := strconv.ParseInt(value, 10, 64)
19+
sum += parse
20+
}
21+
22+
fmt.Println(sum)
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /