Skip to content

Commit 872f333

Browse files
committed
neverlish: week01
1 parent 8901b1d commit 872f333

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

contains-duplicate/neverlish.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test(t *testing.T) {
6+
result1 := containsDuplicate([]int{1, 2, 3, 1})
7+
8+
if result1 != true {
9+
t.Fatal("failed test1")
10+
}
11+
12+
result2 := containsDuplicate([]int{1, 2, 3, 4})
13+
14+
if result2 != false {
15+
t.Fatal("failed test2")
16+
}
17+
}
18+
19+
func containsDuplicate(nums []int) bool {
20+
data := make(map[int]bool)
21+
22+
for _, num := range nums {
23+
if data[num] {
24+
return true
25+
} else {
26+
data[num] = true
27+
}
28+
}
29+
return false
30+
}

house-robber/neverlish.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test(t *testing.T) {
8+
result1 := rob([]int{1, 2, 3, 1})
9+
10+
if result1 != 4 {
11+
t.Fatal("failed test1")
12+
}
13+
14+
result2 := rob([]int{2, 7, 9, 3, 1})
15+
16+
if result2 != 12 {
17+
t.Fatal("failed test2")
18+
}
19+
}
20+
21+
func rob(nums []int) int {
22+
length := len(nums)
23+
24+
if length == 0 {
25+
return 0
26+
}
27+
if length == 1 {
28+
return nums[0]
29+
}
30+
31+
moneys := make([]int, length)
32+
33+
moneys[0] = nums[0]
34+
moneys[1] = max(nums[0], nums[1])
35+
36+
for position := 2; position < length; position++ {
37+
moneys[position] = max(moneys[position-1], moneys[position-2]+nums[position])
38+
}
39+
40+
return moneys[length-1]
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test(t *testing.T) {
6+
result := longestConsecutive([]int{100, 4, 200, 1, 3, 2})
7+
8+
if result != 4 {
9+
t.Fatal("failed test")
10+
}
11+
12+
result2 := longestConsecutive([]int{0, 3, 7, 2, 5, 8, 4, 6, 0, 1})
13+
14+
if result2 != 9 {
15+
t.Fatal("failed test2")
16+
}
17+
}
18+
19+
func longestConsecutive(nums []int) int {
20+
numsSet := make(map[int]bool)
21+
22+
for _, num := range nums {
23+
numsSet[num] = true
24+
}
25+
26+
longest := 0
27+
28+
for num := range numsSet {
29+
if !numsSet[num-1] {
30+
currentNum := num
31+
currentLength := 1
32+
33+
for numsSet[currentNum+1] {
34+
currentNum++
35+
currentLength++
36+
}
37+
38+
if currentLength > longest {
39+
longest = currentLength
40+
}
41+
}
42+
}
43+
44+
return longest
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test(t *testing.T) {
6+
result := topKFrequent([]int{1, 1, 1, 2, 2, 3}, 2)
7+
8+
if result[0] != 1 || result[1] != 2 {
9+
t.Fatal("failed test")
10+
}
11+
}
12+
13+
func topKFrequent(nums []int, k int) []int {
14+
freq := make(map[int]int)
15+
16+
for _, num := range nums {
17+
freq[num]++
18+
}
19+
20+
freq_by_counts := make(map[int][]int)
21+
22+
for num, count := range freq {
23+
if _, ok := freq_by_counts[count]; !ok {
24+
freq_by_counts[count] = []int{}
25+
}
26+
freq_by_counts[count] = append(freq_by_counts[count], num)
27+
}
28+
29+
result := []int{}
30+
31+
for count := len(nums); count > 0; count-- {
32+
if nums, ok := freq_by_counts[count]; ok {
33+
if len(result) >= k {
34+
break
35+
}
36+
result = append(result, nums...)
37+
}
38+
}
39+
return result
40+
}

valid-palindrome/neverlish.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func Test(t *testing.T) {
9+
result1 := isPalindrome("A man, a plan, a canal: Panama")
10+
if result1 != true {
11+
t.Fatal("failed test1")
12+
}
13+
14+
result2 := isPalindrome("race a car")
15+
if result2 != false {
16+
t.Fatal("failed test2")
17+
}
18+
19+
result3 := isPalindrome("")
20+
21+
if result3 != true {
22+
t.Fatal("failed test3")
23+
}
24+
}
25+
26+
func isPalindrome(s string) bool {
27+
s = strings.ToLower(s)
28+
29+
var filtered []rune
30+
for _, r := range s {
31+
if ('a' <= r && r <= 'z') || ('0' <= r && r <= '9') {
32+
filtered = append(filtered, r)
33+
}
34+
}
35+
36+
for index, r := range filtered[:len(filtered)/2] {
37+
if r != filtered[len(filtered)-index-1] {
38+
return false
39+
}
40+
}
41+
return true
42+
}

0 commit comments

Comments
 (0)